Use push/pop mechanism for cursors

This commit is contained in:
Eduardo Bart 2013-01-25 11:47:51 -02:00
parent 3a47c4f2e1
commit a144174c41
10 changed files with 45 additions and 29 deletions

View File

@ -19,7 +19,7 @@ end
function UIResizeBorder:onDestroy() function UIResizeBorder:onDestroy()
if self.hovering then if self.hovering then
g_mouse.restoreCursor() g_mouse.popCursor(self.cursortype)
end end
end end
@ -27,19 +27,20 @@ function UIResizeBorder:onHoverChange(hovered)
if hovered then if hovered then
if g_mouse.isCursorChanged() or g_mouse.isPressed() then return end if g_mouse.isCursorChanged() or g_mouse.isPressed() then return end
if self:getWidth() > self:getHeight() then if self:getWidth() > self:getHeight() then
g_mouse.setCursor('vertical')
self.vertical = true self.vertical = true
self.cursortype = 'vertical'
else else
g_mouse.setCursor('horizontal')
self.vertical = false self.vertical = false
self.cursortype = 'horizontal'
end end
g_mouse.pushCursor(self.cursortype)
self.hovering = true self.hovering = true
if not self:isPressed() then if not self:isPressed() then
g_effects.fadeIn(self) g_effects.fadeIn(self)
end end
else else
if not self:isPressed() and self.hovering then if not self:isPressed() and self.hovering then
g_mouse.restoreCursor() g_mouse.popCursor(self.cursortype)
g_effects.fadeOut(self) g_effects.fadeOut(self)
self.hovering = false self.hovering = false
end end
@ -67,7 +68,7 @@ end
function UIResizeBorder:onMouseRelease(mousePos, mouseButton) function UIResizeBorder:onMouseRelease(mousePos, mouseButton)
if not self:isHovered() then if not self:isHovered() then
g_mouse.restoreCursor() g_mouse.popCursor(self.cursortype)
g_effects.fadeOut(self) g_effects.fadeOut(self)
self.hovering = false self.hovering = false
end end

View File

@ -14,19 +14,20 @@ function UISplitter:onHoverChange(hovered)
if hovered and (self:canUpdateMargin(margin + 1) ~= margin or self:canUpdateMargin(margin - 1) ~= margin) then 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 g_mouse.isCursorChanged() or g_mouse.isPressed() then return end
if self:getWidth() > self:getHeight() then if self:getWidth() > self:getHeight() then
g_mouse.setCursor('vertical')
self.vertical = true self.vertical = true
self.cursortype = 'vertical'
else else
g_mouse.setCursor('horizontal')
self.vertical = false self.vertical = false
self.cursortype = 'horizontal'
end end
self.hovering = true self.hovering = true
g_mouse.pushCursor(self.cursortype)
if not self:isPressed() then if not self:isPressed() then
g_effects.fadeIn(self) g_effects.fadeIn(self)
end end
else else
if not self:isPressed() and self.hovering then if not self:isPressed() and self.hovering then
g_mouse.restoreCursor() g_mouse.popCursor(self.cursortype)
g_effects.fadeOut(self) g_effects.fadeOut(self)
self.hovering = false self.hovering = false
end end
@ -67,7 +68,7 @@ end
function UISplitter:onMouseRelease(mousePos, mouseButton) function UISplitter:onMouseRelease(mousePos, mouseButton)
if not self:isHovered() then if not self:isHovered() then
g_mouse.restoreCursor() g_mouse.popCursor(self.cursortype)
g_effects.fadeOut(self) g_effects.fadeOut(self)
self.hovering = false self.hovering = false
end end

View File

@ -222,7 +222,7 @@ function onChooseItemMouseRelease(self, mousePosition, mouseButton)
show() show()
g_mouse.restoreCursor() g_mouse.popCursor('target')
self:ungrabMouse() self:ungrabMouse()
self:destroy() self:destroy()
end end
@ -235,7 +235,7 @@ function startChooseItem()
connect(mouseGrabberWidget, { onMouseRelease = onChooseItemMouseRelease }) connect(mouseGrabberWidget, { onMouseRelease = onChooseItemMouseRelease })
mouseGrabberWidget:grabMouse() mouseGrabberWidget:grabMouse()
g_mouse.setCursor('target-cursor') g_mouse.pushCursor('target-cursor')
hide() hide()
end end

View File

@ -296,7 +296,7 @@ function onMouseGrabberRelease(self, mousePosition, mouseButton)
end end
selectedThing = nil selectedThing = nil
g_mouse.restoreCursor() g_mouse.popCursor('target')
self:ungrabMouse() self:ungrabMouse()
return true return true
end end
@ -331,14 +331,14 @@ function startUseWith(thing)
selectedType = 'use' selectedType = 'use'
selectedThing = thing selectedThing = thing
mouseGrabberWidget:grabMouse() mouseGrabberWidget:grabMouse()
g_mouse.setCursor('target') g_mouse.pushCursor('target')
end end
function startTradeWith(thing) function startTradeWith(thing)
selectedType = 'trade' selectedType = 'trade'
selectedThing = thing selectedThing = thing
mouseGrabberWidget:grabMouse() mouseGrabberWidget:grabMouse()
g_mouse.setCursor('target') g_mouse.pushCursor('target')
end end
function createThingMenu(menuPosition, lookThing, useThing, creatureThing) function createThingMenu(menuPosition, lookThing, useThing, creatureThing)

View File

@ -17,7 +17,7 @@ function UIGameMap:onDragEnter(mousePos)
self.currentDragThing = thing self.currentDragThing = thing
g_mouse.setCursor('target') g_mouse.pushCursor('target')
self.allowNextRelease = false self.allowNextRelease = false
return true return true
end end
@ -25,7 +25,7 @@ end
function UIGameMap:onDragLeave(droppedWidget, mousePos) function UIGameMap:onDragLeave(droppedWidget, mousePos)
self.currentDragThing = nil self.currentDragThing = nil
self.hoveredWho = nil self.hoveredWho = nil
g_mouse.restoreCursor() g_mouse.popCursor('target')
return true return true
end end

View File

@ -6,14 +6,14 @@ function UIItem:onDragEnter(mousePos)
self:setBorderWidth(1) self:setBorderWidth(1)
self.currentDragThing = item self.currentDragThing = item
g_mouse.setCursor('target') g_mouse.pushCursor('target')
return true return true
end end
function UIItem:onDragLeave(droppedWidget, mousePos) function UIItem:onDragLeave(droppedWidget, mousePos)
if self:isVirtual() then return false end if self:isVirtual() then return false end
self.currentDragThing = nil self.currentDragThing = nil
g_mouse.restoreCursor() g_mouse.popCursor('target')
self:setBorderWidth(0) self:setBorderWidth(0)
self.hoveredWho = nil self.hoveredWho = nil
return true return true

View File

@ -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)); 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); auto it = m_cursors.find(name);
if(it == m_cursors.end()) if(it == m_cursors.end())
@ -73,12 +73,26 @@ bool Mouse::setCursor(const std::string& name)
return true; return true;
} }
void Mouse::restoreCursor() void Mouse::popCursor(const std::string& name)
{ {
if(m_cursorStack.size() == 0) if(m_cursorStack.size() == 0)
return; return;
if(name.empty() || m_cursors.find(name) == m_cursors.end())
m_cursorStack.pop_back(); m_cursorStack.pop_back();
else {
int cursorId = m_cursors[name];
int index = -1;
for(uint i=0;i<m_cursorStack.size();++i) {
if(m_cursorStack[i] == cursorId)
index = i;
}
if(index >= 0)
m_cursorStack.erase(m_cursorStack.begin() + index);
else
return;
}
if(m_cursorStack.size() > 0) if(m_cursorStack.size() > 0)
g_window.setMouseCursor(m_cursorStack.back()); g_window.setMouseCursor(m_cursorStack.back());
else else
@ -99,6 +113,6 @@ void Mouse::checkStackSize()
{ {
if(m_cursorStack.size() > 5) { if(m_cursorStack.size() > 5) {
g_logger.error("mouse cursor stack is too long"); g_logger.error("mouse cursor stack is too long");
m_cursorStack.resize(5); m_cursorStack.pop_front();
} }
} }

View File

@ -30,8 +30,8 @@ public:
void loadCursors(std::string filename); void loadCursors(std::string filename);
void addCursor(const std::string& name, const std::string& file, const Point& hotSpot); void addCursor(const std::string& name, const std::string& file, const Point& hotSpot);
bool setCursor(const std::string& name); bool pushCursor(const std::string& name);
void restoreCursor(); void popCursor(const std::string& name);
bool isCursorChanged(); bool isCursorChanged();
bool isPressed(Fw::MouseButton mouseButton); bool isPressed(Fw::MouseButton mouseButton);
@ -39,7 +39,7 @@ private:
void checkStackSize(); void checkStackSize();
std::map<std::string, int> m_cursors; std::map<std::string, int> m_cursors;
std::vector<int> m_cursorStack; std::deque<int> m_cursorStack;
}; };
extern Mouse g_mouse; extern Mouse g_mouse;

View File

@ -272,8 +272,8 @@ void Application::registerLuaFunctions()
g_lua.registerSingletonClass("g_mouse"); g_lua.registerSingletonClass("g_mouse");
g_lua.bindSingletonFunction("g_mouse", "loadCursors", &Mouse::loadCursors, &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", "addCursor", &Mouse::addCursor, &g_mouse);
g_lua.bindSingletonFunction("g_mouse", "setCursor", &Mouse::setCursor, &g_mouse); g_lua.bindSingletonFunction("g_mouse", "pushCursor", &Mouse::pushCursor, &g_mouse);
g_lua.bindSingletonFunction("g_mouse", "restoreCursor", &Mouse::restoreCursor, &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", "isCursorChanged", &Mouse::isCursorChanged, &g_mouse);
g_lua.bindSingletonFunction("g_mouse", "isPressed", &Mouse::isPressed, &g_mouse); g_lua.bindSingletonFunction("g_mouse", "isPressed", &Mouse::isPressed, &g_mouse);

View File

@ -590,9 +590,9 @@ void UITextEdit::onHoverChange(bool hovered)
{ {
if(m_changeCursorImage) { if(m_changeCursorImage) {
if(hovered) if(hovered)
g_mouse.setCursor("text"); g_mouse.pushCursor("text");
else else
g_mouse.restoreCursor(); g_mouse.popCursor("text");
} }
} }