From 4120faf83167890981824b73b3d2d6a639b15e90 Mon Sep 17 00:00:00 2001 From: daid Date: Tue, 5 Apr 2016 07:14:43 +0200 Subject: [PATCH] Fix the keyboard input related crash, that mostly happened on comms. --- src/gui/gui2_canvas.cpp | 8 ++++++++ src/gui/gui2_canvas.h | 3 +++ src/gui/gui2_element.cpp | 17 +++++++++++++++++ src/gui/gui2_element.h | 1 + src/gui/gui2_selector.cpp | 5 +---- 5 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/gui/gui2_canvas.cpp b/src/gui/gui2_canvas.cpp index 7722cb6135..8848c47e5f 100644 --- a/src/gui/gui2_canvas.cpp +++ b/src/gui/gui2_canvas.cpp @@ -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; +} diff --git a/src/gui/gui2_canvas.h b/src/gui/gui2_canvas.h index fdee7c058f..12237bdee7 100644 --- a/src/gui/gui2_canvas.h +++ b/src/gui/gui2_canvas.h @@ -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 diff --git a/src/gui/gui2_element.cpp b/src/gui/gui2_element.cpp index 6302f9617d..1a6993037c 100644 --- a/src/gui/gui2_element.cpp +++ b/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) @@ -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(getTopLevelContainer()); + if (canvas) + { + canvas->unfocusElement(this); + } + } } bool GuiElement::onMouseDown(sf::Vector2f position) @@ -174,6 +183,14 @@ GuiContainer* GuiElement::getOwner() return owner; } +GuiContainer* GuiElement::getTopLevelContainer() +{ + GuiContainer* top_level = owner; + while(dynamic_cast(top_level) != nullptr) + top_level = dynamic_cast(top_level)->getOwner(); + return top_level; +} + void GuiElement::updateRect(sf::FloatRect window_rect) { sf::Vector2f local_size = size; diff --git a/src/gui/gui2_element.h b/src/gui/gui2_element.h index 3efd53283e..7e70bbc896 100644 --- a/src/gui/gui2_element.h +++ b/src/gui/gui2_element.h @@ -77,6 +77,7 @@ class GuiElement : public GuiContainer sf::Vector2f getCenterPoint(); GuiContainer* getOwner(); + GuiContainer* getTopLevelContainer(); friend class GuiContainer; friend class GuiCanvas; diff --git a/src/gui/gui2_selector.cpp b/src/gui/gui2_selector.cpp index eb602e27d7..bd28aa70fb 100644 --- a/src/gui/gui2_selector.cpp +++ b/src/gui/gui2_selector.cpp @@ -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(top_level) != nullptr) - top_level = dynamic_cast(top_level)->getOwner(); - popup = new GuiPanel(top_level, ""); + popup = new GuiPanel(getTopLevelContainer(), ""); popup->hide(); }