From a144174c4188db0a261de518651fc62cfdd9d3ed Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Fri, 25 Jan 2013 11:47:51 -0200 Subject: [PATCH] Use push/pop mechanism for cursors --- modules/corelib/ui/uiresizeborder.lua | 11 +++++----- modules/corelib/ui/uisplitter.lua | 9 ++++---- modules/game_hotkeys/hotkeys_manager.lua | 4 ++-- modules/game_interface/gameinterface.lua | 6 +++--- modules/game_interface/widgets/uigamemap.lua | 4 ++-- modules/game_interface/widgets/uiitem.lua | 4 ++-- src/framework/input/mouse.cpp | 22 ++++++++++++++++---- src/framework/input/mouse.h | 6 +++--- src/framework/luafunctions.cpp | 4 ++-- src/framework/ui/uitextedit.cpp | 4 ++-- 10 files changed, 45 insertions(+), 29 deletions(-) diff --git a/modules/corelib/ui/uiresizeborder.lua b/modules/corelib/ui/uiresizeborder.lua index e3980baf..9909685e 100644 --- a/modules/corelib/ui/uiresizeborder.lua +++ b/modules/corelib/ui/uiresizeborder.lua @@ -19,7 +19,7 @@ end function UIResizeBorder:onDestroy() if self.hovering then - g_mouse.restoreCursor() + g_mouse.popCursor(self.cursortype) end end @@ -27,19 +27,20 @@ function UIResizeBorder:onHoverChange(hovered) if hovered then if g_mouse.isCursorChanged() or g_mouse.isPressed() then return end if self:getWidth() > self:getHeight() then - g_mouse.setCursor('vertical') self.vertical = true + self.cursortype = 'vertical' else - g_mouse.setCursor('horizontal') self.vertical = false + self.cursortype = 'horizontal' end + g_mouse.pushCursor(self.cursortype) self.hovering = true if not self:isPressed() then g_effects.fadeIn(self) end else if not self:isPressed() and self.hovering then - g_mouse.restoreCursor() + g_mouse.popCursor(self.cursortype) g_effects.fadeOut(self) self.hovering = false end @@ -67,7 +68,7 @@ end function UIResizeBorder:onMouseRelease(mousePos, mouseButton) if not self:isHovered() then - g_mouse.restoreCursor() + g_mouse.popCursor(self.cursortype) g_effects.fadeOut(self) self.hovering = false end diff --git a/modules/corelib/ui/uisplitter.lua b/modules/corelib/ui/uisplitter.lua index 6af6eca5..cf71a045 100644 --- a/modules/corelib/ui/uisplitter.lua +++ b/modules/corelib/ui/uisplitter.lua @@ -14,19 +14,20 @@ function UISplitter:onHoverChange(hovered) if hovered and (self:canUpdateMargin(margin + 1) ~= margin or self:canUpdateMargin(margin - 1) ~= margin) then if g_mouse.isCursorChanged() or g_mouse.isPressed() then return end if self:getWidth() > self:getHeight() then - g_mouse.setCursor('vertical') self.vertical = true + self.cursortype = 'vertical' else - g_mouse.setCursor('horizontal') self.vertical = false + self.cursortype = 'horizontal' end self.hovering = true + g_mouse.pushCursor(self.cursortype) if not self:isPressed() then g_effects.fadeIn(self) end else if not self:isPressed() and self.hovering then - g_mouse.restoreCursor() + g_mouse.popCursor(self.cursortype) g_effects.fadeOut(self) self.hovering = false end @@ -67,7 +68,7 @@ end function UISplitter:onMouseRelease(mousePos, mouseButton) if not self:isHovered() then - g_mouse.restoreCursor() + g_mouse.popCursor(self.cursortype) g_effects.fadeOut(self) self.hovering = false end diff --git a/modules/game_hotkeys/hotkeys_manager.lua b/modules/game_hotkeys/hotkeys_manager.lua index 073df542..dc98a0b2 100644 --- a/modules/game_hotkeys/hotkeys_manager.lua +++ b/modules/game_hotkeys/hotkeys_manager.lua @@ -222,7 +222,7 @@ function onChooseItemMouseRelease(self, mousePosition, mouseButton) show() - g_mouse.restoreCursor() + g_mouse.popCursor('target') self:ungrabMouse() self:destroy() end @@ -235,7 +235,7 @@ function startChooseItem() connect(mouseGrabberWidget, { onMouseRelease = onChooseItemMouseRelease }) mouseGrabberWidget:grabMouse() - g_mouse.setCursor('target-cursor') + g_mouse.pushCursor('target-cursor') hide() end diff --git a/modules/game_interface/gameinterface.lua b/modules/game_interface/gameinterface.lua index 7adcce4d..e56a6638 100644 --- a/modules/game_interface/gameinterface.lua +++ b/modules/game_interface/gameinterface.lua @@ -296,7 +296,7 @@ function onMouseGrabberRelease(self, mousePosition, mouseButton) end selectedThing = nil - g_mouse.restoreCursor() + g_mouse.popCursor('target') self:ungrabMouse() return true end @@ -331,14 +331,14 @@ function startUseWith(thing) selectedType = 'use' selectedThing = thing mouseGrabberWidget:grabMouse() - g_mouse.setCursor('target') + g_mouse.pushCursor('target') end function startTradeWith(thing) selectedType = 'trade' selectedThing = thing mouseGrabberWidget:grabMouse() - g_mouse.setCursor('target') + g_mouse.pushCursor('target') end function createThingMenu(menuPosition, lookThing, useThing, creatureThing) diff --git a/modules/game_interface/widgets/uigamemap.lua b/modules/game_interface/widgets/uigamemap.lua index d10ccc84..672d280e 100644 --- a/modules/game_interface/widgets/uigamemap.lua +++ b/modules/game_interface/widgets/uigamemap.lua @@ -17,7 +17,7 @@ function UIGameMap:onDragEnter(mousePos) self.currentDragThing = thing - g_mouse.setCursor('target') + g_mouse.pushCursor('target') self.allowNextRelease = false return true end @@ -25,7 +25,7 @@ end function UIGameMap:onDragLeave(droppedWidget, mousePos) self.currentDragThing = nil self.hoveredWho = nil - g_mouse.restoreCursor() + g_mouse.popCursor('target') return true end diff --git a/modules/game_interface/widgets/uiitem.lua b/modules/game_interface/widgets/uiitem.lua index ccd5f596..21b8d825 100644 --- a/modules/game_interface/widgets/uiitem.lua +++ b/modules/game_interface/widgets/uiitem.lua @@ -6,14 +6,14 @@ function UIItem:onDragEnter(mousePos) self:setBorderWidth(1) self.currentDragThing = item - g_mouse.setCursor('target') + g_mouse.pushCursor('target') return true end function UIItem:onDragLeave(droppedWidget, mousePos) if self:isVirtual() then return false end self.currentDragThing = nil - g_mouse.restoreCursor() + g_mouse.popCursor('target') self:setBorderWidth(0) self.hoveredWho = nil return true diff --git a/src/framework/input/mouse.cpp b/src/framework/input/mouse.cpp index 7badf3ae..e2c41930 100644 --- a/src/framework/input/mouse.cpp +++ b/src/framework/input/mouse.cpp @@ -61,7 +61,7 @@ void Mouse::addCursor(const std::string& name, const std::string& file, const Po g_logger.error(stdext::format("unable to load cursor %s", name)); } -bool Mouse::setCursor(const std::string& name) +bool Mouse::pushCursor(const std::string& name) { auto it = m_cursors.find(name); if(it == m_cursors.end()) @@ -73,12 +73,26 @@ bool Mouse::setCursor(const std::string& name) return true; } -void Mouse::restoreCursor() +void Mouse::popCursor(const std::string& name) { if(m_cursorStack.size() == 0) return; - m_cursorStack.pop_back(); + if(name.empty() || m_cursors.find(name) == m_cursors.end()) + m_cursorStack.pop_back(); + else { + int cursorId = m_cursors[name]; + int index = -1; + for(uint i=0;i= 0) + m_cursorStack.erase(m_cursorStack.begin() + index); + else + return; + } + if(m_cursorStack.size() > 0) g_window.setMouseCursor(m_cursorStack.back()); else @@ -99,6 +113,6 @@ void Mouse::checkStackSize() { if(m_cursorStack.size() > 5) { g_logger.error("mouse cursor stack is too long"); - m_cursorStack.resize(5); + m_cursorStack.pop_front(); } } diff --git a/src/framework/input/mouse.h b/src/framework/input/mouse.h index 4b0f13ce..3af0bd8f 100644 --- a/src/framework/input/mouse.h +++ b/src/framework/input/mouse.h @@ -30,8 +30,8 @@ public: void loadCursors(std::string filename); void addCursor(const std::string& name, const std::string& file, const Point& hotSpot); - bool setCursor(const std::string& name); - void restoreCursor(); + bool pushCursor(const std::string& name); + void popCursor(const std::string& name); bool isCursorChanged(); bool isPressed(Fw::MouseButton mouseButton); @@ -39,7 +39,7 @@ private: void checkStackSize(); std::map m_cursors; - std::vector m_cursorStack; + std::deque m_cursorStack; }; extern Mouse g_mouse; diff --git a/src/framework/luafunctions.cpp b/src/framework/luafunctions.cpp index 2122a19c..6241f53b 100644 --- a/src/framework/luafunctions.cpp +++ b/src/framework/luafunctions.cpp @@ -272,8 +272,8 @@ void Application::registerLuaFunctions() g_lua.registerSingletonClass("g_mouse"); g_lua.bindSingletonFunction("g_mouse", "loadCursors", &Mouse::loadCursors, &g_mouse); g_lua.bindSingletonFunction("g_mouse", "addCursor", &Mouse::addCursor, &g_mouse); - g_lua.bindSingletonFunction("g_mouse", "setCursor", &Mouse::setCursor, &g_mouse); - g_lua.bindSingletonFunction("g_mouse", "restoreCursor", &Mouse::restoreCursor, &g_mouse); + g_lua.bindSingletonFunction("g_mouse", "pushCursor", &Mouse::pushCursor, &g_mouse); + g_lua.bindSingletonFunction("g_mouse", "popCursor", &Mouse::popCursor, &g_mouse); g_lua.bindSingletonFunction("g_mouse", "isCursorChanged", &Mouse::isCursorChanged, &g_mouse); g_lua.bindSingletonFunction("g_mouse", "isPressed", &Mouse::isPressed, &g_mouse); diff --git a/src/framework/ui/uitextedit.cpp b/src/framework/ui/uitextedit.cpp index 53d5550a..0dd96462 100644 --- a/src/framework/ui/uitextedit.cpp +++ b/src/framework/ui/uitextedit.cpp @@ -590,9 +590,9 @@ void UITextEdit::onHoverChange(bool hovered) { if(m_changeCursorImage) { if(hovered) - g_mouse.setCursor("text"); + g_mouse.pushCursor("text"); else - g_mouse.restoreCursor(); + g_mouse.popCursor("text"); } }