diff --git a/modules/core_styles/images/menu.png b/modules/core_styles/images/menu.png new file mode 100644 index 00000000..d7e28302 Binary files /dev/null and b/modules/core_styles/images/menu.png differ diff --git a/modules/playground/filemenu.otui b/modules/playground/filemenu.otui new file mode 100644 index 00000000..9c7c10a6 --- /dev/null +++ b/modules/playground/filemenu.otui @@ -0,0 +1,10 @@ +Panel + layout: verticalBox + size: 64 48 + + MenuButton + text: New + + MenuButton + text: Quit + onClick: exit() \ No newline at end of file diff --git a/modules/playground/menubar.otui b/modules/playground/menubar.otui new file mode 100644 index 00000000..06b45d2e --- /dev/null +++ b/modules/playground/menubar.otui @@ -0,0 +1,29 @@ +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 \ No newline at end of file diff --git a/modules/playground/playground.lua b/modules/playground/playground.lua index 805983c3..758f66a2 100644 --- a/modules/playground/playground.lua +++ b/modules/playground/playground.lua @@ -1 +1,10 @@ --- place any code for testing purposes here \ No newline at end of file +-- place any code for testing purposes here + +function displayMenuPopup(file, parent) +end + +local function init() + UI.loadAndDisplay('/playground/menubar.otui') +end + +addEvent(init) \ No newline at end of file diff --git a/src/framework/luascript/luafunctions.cpp b/src/framework/luascript/luafunctions.cpp index 62122bf4..827c6e7d 100644 --- a/src/framework/luascript/luafunctions.cpp +++ b/src/framework/luascript/luafunctions.cpp @@ -48,6 +48,8 @@ void LuaInterface::registerFunctions() g_lua.bindClassMemberFunction("getSize", &UIWidget::getSize); g_lua.bindClassMemberFunction("setSize", &UIWidget::resize); g_lua.bindClassMemberFunction("getPosition", &UIWidget::getPosition); + g_lua.bindClassMemberFunction("getX", &UIWidget::getX); + g_lua.bindClassMemberFunction("getY", &UIWidget::getY); g_lua.bindClassMemberFunction("moveTo", &UIWidget::moveTo); g_lua.bindClassMemberFunction("moveChildToIndex", &UIWidget::moveChildToIndex); g_lua.bindClassMemberFunction("getParent", &UIWidget::getParent); @@ -59,6 +61,7 @@ void LuaInterface::registerFunctions() g_lua.bindClassMemberFunction("getOpacity", &UIWidget::getOpacity); g_lua.bindClassMemberFunction("setOpacity", &UIWidget::setOpacity); g_lua.bindClassMemberFunction("setStyle", &UIWidget::setStyle); + g_lua.bindClassMemberFunction("applyStyle", &UIWidget::applyStyle); g_lua.bindClassMemberFunction("getStyle", &UIWidget::getStyle); g_lua.bindClassMemberFunction("getMarginTop", &UIWidget::getMarginTop); g_lua.bindClassMemberFunction("setMarginTop", &UIWidget::setMarginTop); diff --git a/src/framework/luascript/luavaluecasts.cpp b/src/framework/luascript/luavaluecasts.cpp index a285bd8b..20f24b86 100644 --- a/src/framework/luascript/luavaluecasts.cpp +++ b/src/framework/luascript/luavaluecasts.cpp @@ -252,6 +252,29 @@ void push_luavalue(const OTMLNodePtr& node) g_lua.pushNil(); } +bool luavalue_cast(int index, OTMLNodePtr& node) +{ + node = OTMLNode::create(); + node->setUnique(true); + if(g_lua.isTable(index)) { + g_lua.pushNil(); + while(g_lua.next(index < 0 ? index-1 : index)) { + std::string cnodeName = g_lua.toString(-2); + if(g_lua.isTable()) { + OTMLNodePtr cnode; + if(luavalue_cast(-1, node)) { + cnode->setTag(cnodeName); + node->addChild(cnode); + } + } else + node->writeAt(cnodeName, g_lua.toString()); + g_lua.pop(); + } + } else + return false; + return true; +} + // object ptr bool luavalue_cast(int index, LuaObjectPtr& obj) { if(g_lua.isUserdata(index)) { diff --git a/src/framework/luascript/luavaluecasts.h b/src/framework/luascript/luavaluecasts.h index f4c3abe8..124f6f40 100644 --- a/src/framework/luascript/luavaluecasts.h +++ b/src/framework/luascript/luavaluecasts.h @@ -70,6 +70,7 @@ bool luavalue_cast(int index, Size& size); // otml nodes void push_luavalue(const OTMLNodePtr& node); +bool luavalue_cast(int index, OTMLNodePtr& node); // enum template diff --git a/src/framework/ui/uiwidget.cpp b/src/framework/ui/uiwidget.cpp index d167ea56..dc6417ef 100644 --- a/src/framework/ui/uiwidget.cpp +++ b/src/framework/ui/uiwidget.cpp @@ -75,8 +75,10 @@ void UIWidget::setup() void UIWidget::destroy() { // remove itself from parent - if(UIWidgetPtr parent = getParent()) - parent->removeChild(asUIWidget()); + if(UIWidgetPtr parent = getParent()) { + if(parent->hasChild(asUIWidget())) + parent->removeChild(asUIWidget()); + } setVisible(false); setEnabled(false); } @@ -680,10 +682,10 @@ void UIWidget::updateState(Fw::WidgetState state) updateStyle(); - if(state == Fw::FocusState) - onFocusChange(newStatus, m_lastFocusReason); - else if(state == Fw::HoverState) - onHoverChange(newStatus); + if(state == Fw::FocusState) { + g_dispatcher.addEvent(std::bind(&UIWidget::onFocusChange, asUIWidget(), newStatus, m_lastFocusReason)); + } else if(state == Fw::HoverState) + g_dispatcher.addEvent(std::bind(&UIWidget::onHoverChange, asUIWidget(), newStatus)); } } @@ -884,6 +886,7 @@ void UIWidget::onStyleApply(const OTMLNodePtr& styleNode) anchorLayout->addAnchor(asUIWidget(), anchoredEdge, hookedWidgetId, hookedEdge); } } else if(node->tag() == "onClick" || + node->tag() == "onMousePress" || node->tag() == "onHoverChange") { g_lua.loadFunction(node->value(), "@" + node->source() + "[" + node->tag() + "]"); luaSetField(node->tag());