item menu example

This commit is contained in:
Eduardo Bart 2011-11-13 03:11:47 -02:00
parent 48c22756f5
commit ca702109d6
14 changed files with 67 additions and 65 deletions

View File

@ -131,3 +131,8 @@ KeyboardNoModifier = 0
KeyboardCtrlModifier = 1
KeyboardAltModifier = 2
KeyboardShiftModifier = 4
MouseNoButton = 0
MouseLeftButton = 1
MouseRightButton = 2
MouseMidButton = 3

View File

@ -49,3 +49,18 @@ TopButton < UIButton
state.disabled:
background-color: #ffffff66
MenuButton < UIButton
color: white
size: 40 18
align: center
border-image:
source: /core_styles/images/menu.png
size: 64 24
state.hover:
border-image:
source: /core_styles/images/menu.png
offset: 0 24
size: 64 24
color: black

View File

@ -9,6 +9,7 @@ UIWindow
id: head
anchors.top: parent.top
anchors.horizontalCenter: parent.horizontalCenter
popup menu: /inventory/itempopupmenu.otui
Item
id: armor

View File

@ -3,7 +3,7 @@ Panel
size: 64 48
MenuButton
text: New
text: Foo
MenuButton
text: Quit

View File

@ -1,29 +0,0 @@
MenuButton < UIButton
color: white
size: 40 18
align: center
border-image:
source: /core_styles/images/menu.png
size: 64 24
state.hover:
border-image:
source: /core_styles/images/menu.png
offset: 0 24
size: 64 24
color: black
TopMenuButton < MenuButton
onMousePress: |
function(self, mousePos, mouseButton)
local popupMenu = UI.loadAndDisplay(self:getStyle()['popup menu'])
if popupMenu then
popupMenu:moveTo({ x = self:getX(), y = self:getY() + self:getHeight()})
popupMenu.onMouseRelease = function(self) self:destroy() end
end
end
TopMenuButton
text: File
position: 80 0
popup menu: /playground/filemenu.otui

View File

@ -1,10 +1,24 @@
-- place any code for testing purposes here
function displayMenuPopup(file, parent)
function UIItem.onMouseRelease(self, mousePos, mouseButton)
if mouseButton ~= MouseRightButton then return end
local top = self:getY()
local bottom = self:getY() + self:getHeight()
local left = self:getX()
local right = self:getX() + self:getWidth()
if not (mousePos.y >= top and mousePos.y <= bottom and mousePos.x >= left and mousePos.x <= right) then return end
local menuFile = self:getStyle()['popup menu']
if not menuFile then return end
local popupMenu = UI.loadAndDisplay(menuFile)
if not popupMenu then return end
popupMenu:moveTo(mousePos)
popupMenu.onMouseRelease = function(self) self:destroy() end
end
local function init()
UI.loadAndDisplay('/playground/menubar.otui')
end
addEvent(init)

View File

@ -57,12 +57,10 @@ void UIButton::onStyleApply(const OTMLNodePtr& styleNode)
}
}
bool UIButton::onMouseRelease(const Point& mousePos, Fw::MouseButton button)
void UIButton::onMouseRelease(const Point& mousePos, Fw::MouseButton button)
{
if(isPressed()) {
if(m_onClick && getRect().contains(mousePos))
m_onClick();
return true;
}
return false;
}

View File

@ -41,7 +41,7 @@ public:
protected:
virtual void onStyleApply(const OTMLNodePtr& styleNode);
virtual bool onMouseRelease(const Point& mousePos, Fw::MouseButton button);
virtual void onMouseRelease(const Point& mousePos, Fw::MouseButton button);
SimpleCallback m_onClick;
Point m_textTranslate;

View File

@ -44,10 +44,15 @@ public:
UIWidgetPtr loadUI(const std::string& file, const UIWidgetPtr& parent = nullptr);
UIWidgetPtr loadWidgetFromOTML(const OTMLNodePtr& widgetNode, const UIWidgetPtr& parent);
//void setMouseGrabWidget();
//void setKeyboardGrabWidget();
UIWidgetPtr getRootWidget() { return m_rootWidget; }
private:
UIWidgetPtr m_rootWidget;
//UIWidgetPtr m_mouseGrabWidget;
//UIWidgetPtr m_keyboardGrabWidget;
std::map<std::string, OTMLNodePtr> m_styles;
};

View File

@ -909,7 +909,7 @@ void UIWidget::onHoverChange(bool hovered)
callLuaField("onHoverChange", hovered);
// check for new hovered elements when the current widget is removed
if(!hovered && !getParent())
if(!hovered && !getParent() && g_ui.getRootWidget())
g_ui.getRootWidget()->updateState(Fw::HoverState);
}
@ -1000,10 +1000,9 @@ bool UIWidget::onMousePress(const Point& mousePos, Fw::MouseButton button)
return false;
}
bool UIWidget::onMouseRelease(const Point& mousePos, Fw::MouseButton button)
void UIWidget::onMouseRelease(const Point& mousePos, Fw::MouseButton button)
{
if(callLuaField<bool>("onMouseRelease", mousePos, button))
return true;
callLuaField("onMouseRelease", mousePos, button);
// do a backup of children list, because it may change while looping it
UIWidgetList children;
@ -1017,16 +1016,11 @@ bool UIWidget::onMouseRelease(const Point& mousePos, Fw::MouseButton button)
}
for(const UIWidgetPtr& child : children) {
bool mustEnd = child->onMouseRelease(mousePos, button);
child->onMouseRelease(mousePos, button);
if(child->isPressed())
child->setPressed(false);
if(mustEnd)
return true;
}
return false;
}
bool UIWidget::onMouseMove(const Point& mousePos, const Point& mouseMoved)

View File

@ -173,7 +173,7 @@ protected:
/// Triggered when a mouse button is pressed down while mouse pointer is inside widget area
virtual bool onMousePress(const Point& mousePos, Fw::MouseButton button);
/// Triggered when a mouse button is released
virtual bool onMouseRelease(const Point& mousePos, Fw::MouseButton button);
virtual void onMouseRelease(const Point& mousePos, Fw::MouseButton button);
/// Triggered when mouse moves (even when the mouse is outside widget area)
virtual bool onMouseMove(const Point& mousePos, const Point& mouseMoved);
/// Triggered when mouse middle button wheels inside widget area

View File

@ -120,7 +120,7 @@ bool UIWindow::onMousePress(const Point& mousePos, Fw::MouseButton button)
return UIWidget::onMousePress(mousePos, button);
}
bool UIWindow::onMouseRelease(const Point& mousePos, Fw::MouseButton button)
void UIWindow::onMouseRelease(const Point& mousePos, Fw::MouseButton button)
{
if(m_moving) {
if(m_movePolicy == FREE_UPDATED_MOVE) {
@ -143,9 +143,8 @@ bool UIWindow::onMouseRelease(const Point& mousePos, Fw::MouseButton button)
updateParentLayout();
}
m_moving = false;
return true;
}
return UIWidget::onMouseRelease(mousePos, button);
UIWidget::onMouseRelease(mousePos, button);
}
bool UIWindow::onMouseMove(const Point& mousePos, const Point& mouseMoved)

View File

@ -44,7 +44,7 @@ protected:
virtual void onStyleApply(const OTMLNodePtr& styleNode);
virtual void onGeometryUpdate(const Rect& oldRect, const Rect& newRect);
virtual bool onMousePress(const Point& mousePos, Fw::MouseButton button);
virtual bool onMouseRelease(const Point& mousePos, Fw::MouseButton button);
virtual void onMouseRelease(const Point& mousePos, Fw::MouseButton button);
virtual bool onMouseMove(const Point& mousePos, const Point& mouseMoved);
virtual bool onKeyPress(uchar keyCode, char keyChar, int keyboardModifiers);