Skip to content

Commit

Permalink
Fix the keyboard input related crash, that mostly happened on comms.
Browse files Browse the repository at this point in the history
  • Loading branch information
daid committed Apr 5, 2016
1 parent 60fe45e commit 4120faf
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/gui/gui2_canvas.cpp
Expand Up @@ -88,3 +88,11 @@ void GuiCanvas::onClick(sf::Vector2f mouse_position)
void GuiCanvas::onKey(sf::Keyboard::Key key, int unicode)
{
}

void GuiCanvas::unfocusElement(GuiElement* element)
{
if (focus_element == element)
focus_element = nullptr;
if (click_element == element)
click_element = nullptr;
}
3 changes: 3 additions & 0 deletions src/gui/gui2_canvas.h
Expand Up @@ -23,6 +23,9 @@ class GuiCanvas : public Renderable, public GuiContainer, public InputEventHandl

virtual void onClick(sf::Vector2f mouse_position);
virtual void onKey(sf::Keyboard::Key key, int unicode);

//Called when an element is destroyed in this tree.
void unfocusElement(GuiElement* element);
};

#endif//GUI2_CANVAS_H
17 changes: 17 additions & 0 deletions src/gui/gui2_element.cpp
@@ -1,4 +1,5 @@
#include "gui2_element.h"
#include "gui2_canvas.h"
#include "main.h"

GuiElement::GuiElement(GuiContainer* owner, string id)
Expand All @@ -10,7 +11,15 @@ GuiElement::GuiElement(GuiContainer* owner, string id)
GuiElement::~GuiElement()
{
if (owner)
{
owner->elements.remove(this);
//Find the owning cancas, as we need to remove ourselves if we are the focus or click element.
GuiCanvas* canvas = dynamic_cast<GuiCanvas*>(getTopLevelContainer());
if (canvas)
{
canvas->unfocusElement(this);
}
}
}

bool GuiElement::onMouseDown(sf::Vector2f position)
Expand Down Expand Up @@ -174,6 +183,14 @@ GuiContainer* GuiElement::getOwner()
return owner;
}

GuiContainer* GuiElement::getTopLevelContainer()
{
GuiContainer* top_level = owner;
while(dynamic_cast<GuiElement*>(top_level) != nullptr)
top_level = dynamic_cast<GuiElement*>(top_level)->getOwner();
return top_level;
}

void GuiElement::updateRect(sf::FloatRect window_rect)
{
sf::Vector2f local_size = size;
Expand Down
1 change: 1 addition & 0 deletions src/gui/gui2_element.h
Expand Up @@ -77,6 +77,7 @@ class GuiElement : public GuiContainer
sf::Vector2f getCenterPoint();

GuiContainer* getOwner();
GuiContainer* getTopLevelContainer();

friend class GuiContainer;
friend class GuiCanvas;
Expand Down
5 changes: 1 addition & 4 deletions src/gui/gui2_selector.cpp
Expand Up @@ -26,10 +26,7 @@ GuiSelector::GuiSelector(GuiContainer* owner, string id, func_t func)
});
right->setPosition(0, 0, ATopRight)->setSize(GuiSizeMatchHeight, GuiSizeMax);

GuiContainer* top_level = owner;
while(dynamic_cast<GuiElement*>(top_level) != nullptr)
top_level = dynamic_cast<GuiElement*>(top_level)->getOwner();
popup = new GuiPanel(top_level, "");
popup = new GuiPanel(getTopLevelContainer(), "");
popup->hide();
}

Expand Down

0 comments on commit 4120faf

Please sign in to comment.