Skip to content

Commit

Permalink
Make sure that dropdowns close no matter where else we click
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Dec 17, 2012
1 parent 944427a commit 313b1f1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
10 changes: 10 additions & 0 deletions screenlib/UIElementDropdown.cpp
Expand Up @@ -19,6 +19,7 @@
3. This notice may not be removed or altered from any source distribution.
*/

#include "UIManager.h"
#include "UIElementDropdown.h"

UIElementType UIElementDropdown::s_elementType;
Expand All @@ -30,6 +31,11 @@ UIElementDropdown::UIElementDropdown(UIBaseElement *parent, const char *name, UI
m_showingElements = true;
}

UIElementDropdown::~UIElementDropdown()
{
GetUI()->ReleaseEvents(this);
}

bool
UIElementDropdown::FinishLoading()
{
Expand Down Expand Up @@ -76,6 +82,8 @@ UIElementDropdown::OnClick()
void
UIElementDropdown::ShowElements()
{
GetUI()->CaptureEvents(this);

for (unsigned int i = 0; i < m_elements.length(); ++i) {
m_elements[i]->Show();
}
Expand All @@ -85,6 +93,8 @@ UIElementDropdown::ShowElements()
void
UIElementDropdown::HideElements()
{
GetUI()->ReleaseEvents(this);

for (unsigned int i = 0; i < m_elements.length(); ++i) {
m_elements[i]->Hide();
}
Expand Down
2 changes: 1 addition & 1 deletion screenlib/UIElementDropdown.h
Expand Up @@ -30,7 +30,7 @@ class UIElementDropdown : public UIElementButton
DECLARE_TYPESAFE_CLASS(UIElementButton)
public:
UIElementDropdown(UIBaseElement *parent, const char *name, UIDrawEngine *drawEngine);
virtual ~UIElementDropdown() { }
virtual ~UIElementDropdown();

override bool FinishLoading();

Expand Down
34 changes: 33 additions & 1 deletion screenlib/UIManager.cpp
Expand Up @@ -305,6 +305,26 @@ UIManager::DeletePanel(UIPanel *panel)
}
}

void
UIManager::CaptureEvents(UIBaseElement *element)
{
if (!IsCapturingEvents(element)) {
m_eventCapture.add(element);
}
}

void
UIManager::ReleaseEvents(UIBaseElement *element)
{
m_eventCapture.remove(element);
}

bool
UIManager::IsCapturingEvents(UIBaseElement *element)
{
return m_eventCapture.find(element);
}

void
UIManager::HideDialogs()
{
Expand Down Expand Up @@ -394,6 +414,8 @@ UIManager::Draw(bool fullUpdate)
bool
UIManager::HandleEvent(const SDL_Event &event)
{
unsigned int i;

if (event.type == SDL_WINDOWEVENT &&
event.window.event == SDL_WINDOWEVENT_RESIZED &&
m_screen->Resizable()) {
Expand All @@ -413,7 +435,17 @@ UIManager::HandleEvent(const SDL_Event &event)
// In case it's not called any other time...
Poll();

for (unsigned i = m_visible.length(); i--; ) {
for (i = m_eventCapture.length(); i--; ) {
UIBaseElement *element = m_eventCapture[i];

for (int drawLevel = NUM_DRAWLEVELS; drawLevel--; ) {
if (element->DispatchEvent(event, (DRAWLEVEL)drawLevel)) {
return true;
}
}
}

for (i = m_visible.length(); i--; ) {
UIPanel *panel = m_visible[i];

for (int drawLevel = NUM_DRAWLEVELS; drawLevel--; ) {
Expand Down
5 changes: 5 additions & 0 deletions screenlib/UIManager.h
Expand Up @@ -111,6 +111,10 @@ class UIManager : public UIArea, public UIFontInterface, public UIImageInterface
return m_panelTransition;
}

void CaptureEvents(UIBaseElement *element);
void ReleaseEvents(UIBaseElement *element);
bool IsCapturingEvents(UIBaseElement *element);

void HideDialogs();

void SetCondition(const char *token, bool isTrue = true);
Expand Down Expand Up @@ -149,6 +153,7 @@ class UIManager : public UIArea, public UIFontInterface, public UIImageInterface
array<UIPanel *> m_panels;
array<UIPanel *> m_visible;
array<UIPanel *> m_delete;
array<UIBaseElement *> m_eventCapture;
PANEL_TRANSITION_TYPE m_panelTransition;
HashTable *m_conditions;
};
Expand Down

0 comments on commit 313b1f1

Please sign in to comment.