Fix issue where unborn children were slaughtered when removing a component from an event

This commit is contained in:
Simon Robertshaw
2012-04-09 12:40:30 +01:00
parent da9cec2c3b
commit b2907798f2
5 changed files with 66 additions and 16 deletions

View File

@@ -32,7 +32,7 @@ ConfirmPrompt::ConfirmPrompt(std::string title, std::string message, ConfirmDial
{ {
ui::Engine::Ref().CloseWindow(); ui::Engine::Ref().CloseWindow();
prompt->callback->ConfirmCallback(result); prompt->callback->ConfirmCallback(result);
//delete prompt; TODO: Fix component disposal prompt->SelfDestruct(); //TODO: Fix component disposal
} }
}; };

View File

@@ -29,7 +29,7 @@ ErrorMessage::ErrorMessage(std::string title, std::string message):
void ActionCallback(ui::Button * sender) void ActionCallback(ui::Button * sender)
{ {
ui::Engine::Ref().CloseWindow(); ui::Engine::Ref().CloseWindow();
//delete message; TODO: Fix component disposal message->SelfDestruct(); //TODO: Fix component disposal
} }
}; };

View File

@@ -148,6 +148,5 @@ void Component::OnMouseWheelInside(int localx, int localy, int d)
Component::~Component() Component::~Component()
{ {
if(GetParentWindow()->IsFocused(this))
GetParentWindow()->FocusComponent(NULL);
} }

View File

@@ -8,7 +8,9 @@ Window::Window(Point _position, Point _size):
Position(_position), Position(_position),
Size(_size), Size(_size),
focusedComponent_(NULL), focusedComponent_(NULL),
AllowExclusiveDrawing(true) AllowExclusiveDrawing(true),
halt(false),
destruct(false)
{ {
} }
@@ -16,7 +18,11 @@ Window::~Window()
{ {
for(unsigned i = 0, sz = Components.size(); i < sz; ++i) for(unsigned i = 0, sz = Components.size(); i < sz; ++i)
if( Components[i] ) if( Components[i] )
{
delete Components[i]; delete Components[i];
if(Components[i]==focusedComponent_)
focusedComponent_ = NULL;
}
} }
void Window::AddComponent(Component* c) void Window::AddComponent(Component* c)
@@ -51,6 +57,11 @@ void Window::RemoveComponent(Component* c)
// find the appropriate component index // find the appropriate component index
if(Components[i] == c) if(Components[i] == c)
{ {
//Make sure any events don't continue
halt = true;
if(Components[i]==focusedComponent_)
focusedComponent_ = NULL;
Components.erase(Components.begin() + i); Components.erase(Components.begin() + i);
// we're done // we're done
@@ -61,7 +72,10 @@ void Window::RemoveComponent(Component* c)
void Window::RemoveComponent(unsigned idx) void Window::RemoveComponent(unsigned idx)
{ {
halt = true;
// free component and remove it. // free component and remove it.
if(Components[idx]==focusedComponent_)
focusedComponent_ = NULL;
delete Components[idx]; delete Components[idx];
Components.erase(Components.begin() + idx); Components.erase(Components.begin() + idx);
} }
@@ -118,7 +132,7 @@ void Window::DoDraw()
void Window::DoTick(float dt) void Window::DoTick(float dt)
{ {
//on mouse hover //on mouse hover
for(int i = Components.size() - 1; i >= 0; --i) for(int i = Components.size() - 1; i >= 0 && !halt; --i)
{ {
if(!Components[i]->Locked && if(!Components[i]->Locked &&
ui::Engine::Ref().GetMouseX() >= Components[i]->Position.X+Position.X && ui::Engine::Ref().GetMouseX() >= Components[i]->Position.X+Position.X &&
@@ -132,12 +146,17 @@ void Window::DoTick(float dt)
} }
//tick //tick
for(int i = 0, sz = Components.size(); i < sz; ++i) for(int i = 0, sz = Components.size(); i < sz && !halt; ++i)
{ {
Components[i]->Tick(dt); Components[i]->Tick(dt);
} }
halt = false;
OnTick(dt); OnTick(dt);
if(destruct)
finalise();
} }
void Window::DoKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt) void Window::DoKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt)
@@ -150,6 +169,8 @@ void Window::DoKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool a
} }
OnKeyPress(key, character, shift, ctrl, alt); OnKeyPress(key, character, shift, ctrl, alt);
if(destruct)
finalise();
} }
void Window::DoKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt) void Window::DoKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt)
@@ -162,6 +183,8 @@ void Window::DoKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool
} }
OnKeyRelease(key, character, shift, ctrl, alt); OnKeyRelease(key, character, shift, ctrl, alt);
if(destruct)
finalise();
} }
void Window::DoMouseDown(int x_, int y_, unsigned button) void Window::DoMouseDown(int x_, int y_, unsigned button)
@@ -170,7 +193,7 @@ void Window::DoMouseDown(int x_, int y_, unsigned button)
int x = x_ - Position.X; int x = x_ - Position.X;
int y = y_ - Position.Y; int y = y_ - Position.Y;
bool clickState = false; bool clickState = false;
for(int i = Components.size() - 1; i > -1 ; --i) for(int i = Components.size() - 1; i > -1 && !halt; --i)
{ {
if(!Components[i]->Locked) if(!Components[i]->Locked)
{ {
@@ -188,13 +211,15 @@ void Window::DoMouseDown(int x_, int y_, unsigned button)
FocusComponent(NULL); FocusComponent(NULL);
//on mouse down //on mouse down
for(int i = Components.size() - 1; i > -1 ; --i) for(int i = Components.size() - 1; i > -1 && !halt; --i)
{ {
if(!Components[i]->Locked) if(!Components[i]->Locked)
Components[i]->OnMouseDown(x, y, button); Components[i]->OnMouseDown(x, y, button);
} }
OnMouseDown(x_, y_, button); OnMouseDown(x_, y_, button);
if(destruct)
finalise();
} }
void Window::DoMouseMove(int x_, int y_, int dx, int dy) void Window::DoMouseMove(int x_, int y_, int dx, int dy)
@@ -202,7 +227,7 @@ void Window::DoMouseMove(int x_, int y_, int dx, int dy)
//on mouse move (if true, and inside) //on mouse move (if true, and inside)
int x = x_ - Position.X; int x = x_ - Position.X;
int y = y_ - Position.Y; int y = y_ - Position.Y;
for(int i = Components.size() - 1; i > -1 ; --i) for(int i = Components.size() - 1; i > -1 && !halt; --i)
{ {
if(!Components[i]->Locked) if(!Components[i]->Locked)
{ {
@@ -214,7 +239,7 @@ void Window::DoMouseMove(int x_, int y_, int dx, int dy)
if(local.X >= 0 && if(local.X >= 0 &&
local.Y >= 0 && local.Y >= 0 &&
local.X < Components[i]->Size.X && local.X < Components[i]->Size.X &&
local.Y < Components[i]->Size.Y ) local.Y < Components[i]->Size.Y && !halt)
{ {
Components[i]->OnMouseMovedInside(local.X, local.Y, dx, dy); Components[i]->OnMouseMovedInside(local.X, local.Y, dx, dy);
@@ -228,7 +253,7 @@ void Window::DoMouseMove(int x_, int y_, int dx, int dy)
Components[i]->OnMouseEnter(local.X, local.Y); Components[i]->OnMouseEnter(local.X, local.Y);
} }
} }
else else if(!halt)
{ {
// leaving? // leaving?
if( a.X >= 0 && if( a.X >= 0 &&
@@ -244,6 +269,8 @@ void Window::DoMouseMove(int x_, int y_, int dx, int dy)
} }
OnMouseMove(x_, y_, dx, dy); OnMouseMove(x_, y_, dx, dy);
if(destruct)
finalise();
} }
void Window::DoMouseUp(int x_, int y_, unsigned button) void Window::DoMouseUp(int x_, int y_, unsigned button)
@@ -251,7 +278,7 @@ void Window::DoMouseUp(int x_, int y_, unsigned button)
int x = x_ - Position.X; int x = x_ - Position.X;
int y = y_ - Position.Y; int y = y_ - Position.Y;
//on mouse unclick //on mouse unclick
for(int i = Components.size() - 1; i >= 0 ; --i) for(int i = Components.size() - 1; i >= 0 && !halt; --i)
{ {
if(!Components[i]->Locked) if(!Components[i]->Locked)
{ {
@@ -264,13 +291,15 @@ void Window::DoMouseUp(int x_, int y_, unsigned button)
} }
//on mouse up //on mouse up
for(int i = Components.size() - 1; i >= 0 ; --i) for(int i = Components.size() - 1; i >= 0 && !halt; --i)
{ {
if(!Components[i]->Locked) if(!Components[i]->Locked)
Components[i]->OnMouseUp(x, y, button); Components[i]->OnMouseUp(x, y, button);
} }
OnMouseUp(x_, y_, button); OnMouseUp(x_, y_, button);
if(destruct)
finalise();
} }
void Window::DoMouseWheel(int x_, int y_, int d) void Window::DoMouseWheel(int x_, int y_, int d)
@@ -278,7 +307,7 @@ void Window::DoMouseWheel(int x_, int y_, int d)
int x = x_ - Position.X; int x = x_ - Position.X;
int y = y_ - Position.Y; int y = y_ - Position.Y;
//on mouse wheel focused //on mouse wheel focused
for(int i = Components.size() - 1; i >= 0 ; --i) for(int i = Components.size() - 1; i >= 0 && !halt; --i)
{ {
if(x >= Components[i]->Position.X && y >= Components[i]->Position.Y && x < Components[i]->Position.X + Components[i]->Size.X && y < Components[i]->Position.Y + Components[i]->Size.Y) if(x >= Components[i]->Position.X && y >= Components[i]->Position.Y && x < Components[i]->Position.X + Components[i]->Size.X && y < Components[i]->Position.Y + Components[i]->Size.Y)
{ {
@@ -289,12 +318,26 @@ void Window::DoMouseWheel(int x_, int y_, int d)
} }
//on mouse wheel //on mouse wheel
for(int i = Components.size() - 1; i >= 0 ; --i) for(int i = Components.size() - 1; i >= 0 && !halt; --i)
{ {
if(!Components[i]->Locked) if(!Components[i]->Locked)
Components[i]->OnMouseWheel(x - Components[i]->Position.X, y - Components[i]->Position.Y, d); Components[i]->OnMouseWheel(x - Components[i]->Position.X, y - Components[i]->Position.Y, d);
} }
OnMouseWheel(x_, y_, d); OnMouseWheel(x_, y_, d);
if(destruct)
finalise();
}
void Window::finalise()
{
delete this;
}
void Window::SelfDestruct()
{
destruct = true;
halt = true;
} }

View File

@@ -58,6 +58,9 @@ enum ChromeStyle
virtual void DoKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt); virtual void DoKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);
virtual void DoKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt); virtual void DoKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt);
//Sets halt and destroy, this causes the Windows to stop sending events and remove itself.
void SelfDestruct();
bool IsFocused(const Component* c) const; bool IsFocused(const Component* c) const;
void FocusComponent(Component* c); void FocusComponent(Component* c);
@@ -79,6 +82,11 @@ enum ChromeStyle
Component* focusedComponent_; Component* focusedComponent_;
ChromeStyle chrome; ChromeStyle chrome;
//These controls allow a component to call the destruction of the Window inside an event (called by the Window)
void finalise();
bool halt;
bool destruct;
}; };