From 2deac598ebb834abf4257fa1bf5b5b4928e62d43 Mon Sep 17 00:00:00 2001 From: Ykkrosh Date: Fri, 13 Aug 2010 13:50:03 +0000 Subject: [PATCH] Remove focus from chat box after sending message This was SVN commit r7931. --- .../mods/public/gui/session_new/messages.js | 3 ++ source/gui/CGUI.cpp | 31 ++++++++++--------- source/gui/CGUI.h | 9 ++++++ source/gui/IGUIObject.h | 6 ++-- .../gui/scripting/JSInterface_IGUIObject.cpp | 17 +++++++--- source/gui/scripting/JSInterface_IGUIObject.h | 1 + 6 files changed, 45 insertions(+), 22 deletions(-) diff --git a/binaries/data/mods/public/gui/session_new/messages.js b/binaries/data/mods/public/gui/session_new/messages.js index da6772b0d7..a930420d0b 100644 --- a/binaries/data/mods/public/gui/session_new/messages.js +++ b/binaries/data/mods/public/gui/session_new/messages.js @@ -68,6 +68,9 @@ function submitChatInput() addChatMessage({ "type": "message", "username": g_Players[1].name, "text": text }); input.caption = ""; + + // Remove focus + input.blur(); } } diff --git a/source/gui/CGUI.cpp b/source/gui/CGUI.cpp index 062bb5433a..1d0f09972a 100644 --- a/source/gui/CGUI.cpp +++ b/source/gui/CGUI.cpp @@ -149,29 +149,17 @@ InReaction CGUI::HandleEvent(const SDL_Event_* ev) switch (ev->ev.button.button) { case SDL_BUTTON_LEFT: + // Focus the clicked object (or focus none if nothing clicked on) + SetFocusedObject(pNearest); + if (pNearest) { - if (pNearest != m_FocusedObject) - { - // Update focused object - if (m_FocusedObject) - m_FocusedObject->HandleMessage(SGUIMessage(GUIM_LOST_FOCUS)); - m_FocusedObject = pNearest; - m_FocusedObject->HandleMessage(SGUIMessage(GUIM_GOT_FOCUS)); - } - pNearest->HandleMessage(SGUIMessage(GUIM_MOUSE_PRESS_LEFT)); pNearest->ScriptEvent("mouseleftpress"); // Block event, so things on the map (behind the GUI) won't be pressed ret = IN_HANDLED; } - else if (m_FocusedObject) - { - m_FocusedObject->HandleMessage(SGUIMessage(GUIM_LOST_FOCUS)); - //if (m_FocusedObject-> TODO SelfishFocus? - m_FocusedObject = 0; - } break; case SDL_BUTTON_WHEELDOWN: // wheel down @@ -535,6 +523,19 @@ IGUIObject* CGUI::FindObjectByName(const CStr& Name) const return it->second; } +void CGUI::SetFocusedObject(IGUIObject* pObject) +{ + if (pObject == m_FocusedObject) + return; + + if (m_FocusedObject) + m_FocusedObject->HandleMessage(SGUIMessage(GUIM_LOST_FOCUS)); + + m_FocusedObject = pObject; + + if (m_FocusedObject) + m_FocusedObject->HandleMessage(SGUIMessage(GUIM_GOT_FOCUS)); +} // private struct used only in GenerateText(...) struct SGenerateTextImage diff --git a/source/gui/CGUI.h b/source/gui/CGUI.h index f09f7475da..337cb6e685 100644 --- a/source/gui/CGUI.h +++ b/source/gui/CGUI.h @@ -311,6 +311,15 @@ private: */ IGUIObject *GetFocusedObject() { return m_FocusedObject; } +public: + /** + * Change focus to new object. + * Will send LOST_FOCUS/GOT_FOCUS messages as appropriate. + * pObject can be NULL to remove all focus. + */ + void SetFocusedObject(IGUIObject* pObject); + +private: //-------------------------------------------------------- /** @name XML Reading Xeromyces specific subroutines * diff --git a/source/gui/IGUIObject.h b/source/gui/IGUIObject.h index 6f42be6c07..a656145234 100644 --- a/source/gui/IGUIObject.h +++ b/source/gui/IGUIObject.h @@ -375,9 +375,6 @@ protected: */ virtual float GetBufferedZ() const; - // This is done internally - CGUI *GetGUI() { return m_pGUI; } - const CGUI *GetGUI() const { return m_pGUI; } void SetGUI(CGUI * const &pGUI) { m_pGUI = pGUI; } // Set parent @@ -389,6 +386,9 @@ protected: } public: + CGUI *GetGUI() { return m_pGUI; } + const CGUI *GetGUI() const { return m_pGUI; } + /** * Take focus! */ diff --git a/source/gui/scripting/JSInterface_IGUIObject.cpp b/source/gui/scripting/JSInterface_IGUIObject.cpp index f03f7b3e3c..4205af01be 100644 --- a/source/gui/scripting/JSInterface_IGUIObject.cpp +++ b/source/gui/scripting/JSInterface_IGUIObject.cpp @@ -48,6 +48,7 @@ JSFunctionSpec JSI_IGUIObject::JSI_methods[] = { { "toString", JSI_IGUIObject::toString, 0, 0, 0 }, { "focus", JSI_IGUIObject::focus, 0, 0, 0 }, + { "blur", JSI_IGUIObject::blur, 0, 0, 0 }, { 0 } }; @@ -63,7 +64,8 @@ JSBool JSI_IGUIObject::getProperty(JSContext* cx, JSObject* obj, jsval id, jsval if (propName == "constructor" || propName == "prototype" || propName == "toString" || - propName == "focus" + propName == "focus" || + propName == "blur" ) return JS_TRUE; @@ -580,11 +582,18 @@ JSBool JSI_IGUIObject::toString(JSContext* cx, JSObject* obj, uintN UNUSED(argc) return JS_TRUE; } -JSBool JSI_IGUIObject::focus(JSContext* cx, JSObject* obj, uintN UNUSED(argc), jsval* UNUSED(argv), jsval* rval) +JSBool JSI_IGUIObject::focus(JSContext* cx, JSObject* obj, uintN UNUSED(argc), jsval* UNUSED(argv), jsval* UNUSED(rval)) { IGUIObject* e = (IGUIObject*)JS_GetPrivate( cx, obj ); - e->SetFocus(); - e->HandleMessage(SGUIMessage(GUIM_GOT_FOCUS)); + e->GetGUI()->SetFocusedObject(e); + return JS_TRUE; +} + +JSBool JSI_IGUIObject::blur(JSContext* cx, JSObject* obj, uintN UNUSED(argc), jsval* UNUSED(argv), jsval* UNUSED(rval)) +{ + IGUIObject* e = (IGUIObject*)JS_GetPrivate( cx, obj ); + + e->GetGUI()->SetFocusedObject(NULL); return JS_TRUE; } diff --git a/source/gui/scripting/JSInterface_IGUIObject.h b/source/gui/scripting/JSInterface_IGUIObject.h index 783b8ac34f..28ae20e051 100644 --- a/source/gui/scripting/JSInterface_IGUIObject.h +++ b/source/gui/scripting/JSInterface_IGUIObject.h @@ -32,6 +32,7 @@ namespace JSI_IGUIObject JSBool construct(JSContext* cx, JSObject* obj, uintN argc, jsval* argv, jsval* rval); JSBool toString(JSContext* cx, JSObject* obj, uintN argc, jsval* argv, jsval* rval); JSBool focus(JSContext* cx, JSObject* obj, uintN argc, jsval* argv, jsval* rval); + JSBool blur(JSContext* cx, JSObject* obj, uintN argc, jsval* argv, jsval* rval); void init(); }