init menu

This commit is contained in:
Eduardo Bart 2012-01-02 18:46:40 -02:00
parent a52ff707fe
commit 43c16a1643
21 changed files with 217 additions and 78 deletions

View File

@ -31,6 +31,10 @@ function UI.display(arg1, options)
widget:lock()
elseif option == 'visible' then
widget:setVisible(value)
elseif option == 'x' then
widget:setX(value)
elseif option == 'y' then
widget:setY(value)
end
end
end

View File

@ -16,4 +16,5 @@ Module
importStyles 'styles/items.otui'
importStyles 'styles/creatures.otui'
importStyles 'styles/comboboxes.otui'
importStyles 'styles/popupmenus.otui'
return true

Binary file not shown.

After

Width:  |  Height:  |  Size: 227 B

View File

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

View File

@ -7,7 +7,7 @@ RectPanel < UIWidget
FlatPanel < Panel
border-image:
source: /core_styles/images/panel_flat.png
border: 4
border: 1
TopPanel < Panel
height: 36

View File

@ -0,0 +1,40 @@
PopupMenuButton < UIButton
font: verdana-11px-antialised
background-color: alpha
color: #aaaaaa
height: 18
margin-left: 3
margin-right: 3
image:
source: /core_styles/images/empty_rect.png
repeated: true
$hover:
color: #ffffff
background-color: #ffffff44
$disabled:
color: #555555
PopupMenuFirstButton < PopupMenuButton
margin-top: 3
PopupMenuLastButton < PopupMenuButton
margin-bottom: 3
PopupMenuSeparator < UIWidget
margin-left: 2
margin-right: 2
image:
source: /core_styles/images/menubox.png
repeated: true
coords: 3 0 26 3
height: 3
phantom: true
PopupMenu < UIPopupMenu
width: 100
border-image:
source: /core_styles/images/menubox.png
border: 3

View File

@ -7,5 +7,6 @@ Module
onLoad: |
require 'tooltip/tooltip'
require 'messagebox/messagebox'
require 'uicombobox/uicombobox'
require 'uicombobox'
require 'uipopupmenu'
return true

View File

@ -0,0 +1,34 @@
-- extends UIWidget
UIPopupMenu = extends(UIWidget)
-- public functions
function UIPopupMenu.create()
local menu = UIPopupMenu.internalCreate()
local layout = UIVerticalLayout.create(menu)
layout:setFitParent(true)
menu:setLayout(layout)
return menu
end
function UIPopupMenu.display(otui, pos)
local menu = UI.display(otui, {x = pos.x, y = pos.y})
return menu
end
-- hooked events
local function onWidgetStyleApply(widget, style)
if style and style.popupmenu then
widget.popupmenu = style.popupmenu
end
end
local function onWidgetMousePress(widget, mousePos, mouseButton)
if widget.popupmenu and mouseButton == MouseRightButton then
UIPopupMenu.display(widget.popupmenu, mousePos)
return true
end
return false
end
connect(UIWidget, { onStyleApply = onWidgetStyleApply,
onMousePress = onWidgetMousePress })

View File

@ -1,3 +1,6 @@
InvetoryItem < Item
popupmenu: /game_inventory/itempopupmenu.otui
UIWindow
width: 192
margin-top: 10
@ -5,63 +8,62 @@ UIWindow
margin-right: 6
move-policy: free updated
Item
InvetoryItem
id: head
anchors.top: parent.top
anchors.horizontalCenter: parent.horizontalCenter
popup menu: /inventory/itempopupmenu.otui
Item
InvetoryItem
id: armor
anchors.top: prev.bottom
anchors.horizontalCenter: prev.horizontalCenter
margin-top: 5
Item
InvetoryItem
id: legs
anchors.top: prev.bottom
anchors.horizontalCenter: prev.horizontalCenter
margin-top: 5
Item
InvetoryItem
id: feet
anchors.top: prev.bottom
anchors.horizontalCenter: prev.horizontalCenter
margin-top: 5
Item
InvetoryItem
id: necklace
anchors.top: parent.top
anchors.right: head.left
margin-top: 10
margin-right: 5
Item
InvetoryItem
id: left
anchors.top: prev.bottom
anchors.horizontalCenter: prev.horizontalCenter
margin-top: 5
Item
InvetoryItem
id: ring
anchors.top: prev.bottom
anchors.horizontalCenter: prev.horizontalCenter
margin-top: 5
Item
InvetoryItem
id: backpack
anchors.top: parent.top
anchors.left: head.right
margin-top: 10
margin-left: 5
Item
InvetoryItem
id: right
anchors.top: prev.bottom
anchors.horizontalCenter: prev.horizontalCenter
margin-top: 5
Item
InvetoryItem
id: ammo
anchors.top: prev.bottom
anchors.horizontalCenter: prev.horizontalCenter

View File

@ -1,10 +1,5 @@
Panel
layout: verticalBox
size: 64 48
MenuButton
text: Foo
MenuButton
text: Quit
@onClick: exit()
PopupMenu
PopupMenuFirstButton
text: Look
PopupMenuLastButton
text: Use

View File

@ -45,7 +45,7 @@ void Application::registerLuaFunctions()
g_lua.bindClassMemberFunction<UIWidget>("setPhantom", &UIWidget::setPhantom);
g_lua.bindClassMemberFunction<UIWidget>("setStyle", &UIWidget::setStyle);
g_lua.bindClassMemberFunction<UIWidget>("setStyleFromNode", &UIWidget::setStyleFromNode);
//g_lua.bindClassMemberFunction<UIWidget>("setLayout", &UIWidget::setLayout);
g_lua.bindClassMemberFunction<UIWidget>("setLayout", &UIWidget::setLayout);
g_lua.bindClassMemberFunction<UIWidget>("setParent", &UIWidget::setParent);
g_lua.bindClassMemberFunction<UIWidget>("setRect", &UIWidget::setRect);
g_lua.bindClassMemberFunction<UIWidget>("setX", &UIWidget::setX);
@ -89,7 +89,7 @@ void Application::registerLuaFunctions()
g_lua.bindClassMemberFunction<UIWidget>("hasChild", &UIWidget::hasChild);
g_lua.bindClassMemberFunction<UIWidget>("getId", &UIWidget::getId);
g_lua.bindClassMemberFunction<UIWidget>("getChildCount", &UIWidget::getChildCount);
//g_lua.bindClassMemberFunction<UIWidget>("getLayout", &UIWidget::getLayout);
g_lua.bindClassMemberFunction<UIWidget>("getLayout", &UIWidget::getLayout);
g_lua.bindClassMemberFunction<UIWidget>("getParent", &UIWidget::getParent);
g_lua.bindClassMemberFunction<UIWidget>("getRootParent", &UIWidget::getRootParent);
g_lua.bindClassMemberFunction<UIWidget>("getPosition", &UIWidget::getPosition);
@ -141,6 +141,26 @@ void Application::registerLuaFunctions()
g_lua.bindClassMemberFunction<UIWidget>("updateStyle", &UIWidget::updateStyle);
g_lua.bindClassMemberFunction<UIWidget>("applyStyle", &UIWidget::applyStyle);
// UILayout
g_lua.registerClass<UILayout>();
g_lua.bindClassMemberFunction<UILayout>("applyStyle", &UILayout::applyStyle);
g_lua.bindClassMemberFunction<UILayout>("update", &UILayout::update);
g_lua.bindClassMemberFunction<UILayout>("addWidget", &UILayout::addWidget);
g_lua.bindClassMemberFunction<UILayout>("removeWidget", &UILayout::removeWidget);
g_lua.bindClassMemberFunction<UILayout>("getParentWidget", &UILayout::getParentWidget);
// UIVerticalLayout
g_lua.registerClass<UIVerticalLayout, UILayout>();
g_lua.bindClassStaticFunction<UILayout>("create", &UIVerticalLayout::create);
g_lua.bindClassMemberFunction<UIVerticalLayout>("setFitParent", &UIVerticalLayout::setFitParent);
// UIAnchorLayout
g_lua.registerClass<UIAnchorLayout, UILayout>();
g_lua.bindClassStaticFunction<UIAnchorLayout>("create", &UIAnchorLayout::create);
g_lua.bindClassMemberFunction<UIAnchorLayout>("removeAnchors", &UIAnchorLayout::removeAnchors);
g_lua.bindClassMemberFunction<UIAnchorLayout>("centerIn", &UIAnchorLayout::centerIn);
g_lua.bindClassMemberFunction<UIAnchorLayout>("fill", &UIAnchorLayout::fill);
// UILabel
g_lua.registerClass<UILabel, UIWidget>();
g_lua.bindClassStaticFunction<UILabel>("create", &UIWidget::create<UILabel>);

View File

@ -445,7 +445,7 @@ int LuaInterface::protectedCall(int numArgs, int requestedResults)
}
pop(numArgs + 1); // pops the table of function and arguments
if(requestedResults == -1 || requestedResults == 1) {
if(requestedResults == 1) {
numRets = 1;
pushBoolean(done);
}

View File

@ -26,7 +26,7 @@
#include "declarations.h"
/// All OTML errors throw this exception
class OTMLException : public std::exception
class OTMLException : public Exception
{
public:
OTMLException(const OTMLNodePtr& node, const std::string& error);

View File

@ -32,5 +32,8 @@
#include "uiframecounter.h"
#include "uiprogressbar.h"
#include "uicheckbox.h"
#include "uilayout.h"
#include "uiverticallayout.h"
#include "uianchorlayout.h"
#endif

View File

@ -62,6 +62,7 @@ class UIAnchorLayout : public UILayout
{
public:
UIAnchorLayout(UIWidgetPtr parentWidget) : UILayout(parentWidget) { }
static UIAnchorLayoutPtr create(UIWidgetPtr parentWidget) { return UIAnchorLayoutPtr(new UIAnchorLayout(parentWidget)); }
void addAnchor(const UIWidgetPtr& anchoredWidget, Fw::AnchorEdge anchoredEdge,
const std::string& hookedWidgetId, Fw::AnchorEdge hookedEdge);

View File

@ -108,14 +108,17 @@ void UIManager::importStyleFromOTML(const OTMLNodePtr& styleNode)
boost::trim(base);
// TODO: styles must be searched by widget scopes, in that way this warning could be fixed
// disable this warning because many ppl was complening about it
// this warning is disabled because many ppl was complening about it
/*
auto it = m_styles.find(name);
if(it != m_styles.end())
logWarning("style '", name, "' is being redefined");
*/
OTMLNodePtr style = getStyle(base)->clone();
OTMLNodePtr originalStyle = getStyle(base);
if(!originalStyle)
Fw::throwException("base style '", base, "' is not defined");
OTMLNodePtr style = originalStyle->clone();
style->merge(styleNode);
style->setTag(name);
m_styles[name] = style;
@ -123,21 +126,20 @@ void UIManager::importStyleFromOTML(const OTMLNodePtr& styleNode)
OTMLNodePtr UIManager::getStyle(const std::string& styleName)
{
auto it = m_styles.find(styleName);
if(it != m_styles.end())
return m_styles[styleName];
// styles starting with UI are automatically defined
if(boost::starts_with(styleName, "UI")) {
OTMLNodePtr node = OTMLNode::create();
node->writeAt("__widgetType", styleName);
return node;
}
auto it = m_styles.find(styleName);
if(it == m_styles.end()) {
logError("Unable to retrive style '", styleName, "': not a defined style");
return nullptr;
}
return m_styles[styleName];
}
UIWidgetPtr UIManager::loadUI(const std::string& file, const UIWidgetPtr& parent)
{
try {
@ -165,7 +167,11 @@ UIWidgetPtr UIManager::loadUI(const std::string& file, const UIWidgetPtr& parent
UIWidgetPtr UIManager::loadWidgetFromOTML(const OTMLNodePtr& widgetNode, const UIWidgetPtr& parent)
{
OTMLNodePtr styleNode = getStyle(widgetNode->tag())->clone();
OTMLNodePtr originalStyleNode = getStyle(widgetNode->tag());
if(!originalStyleNode)
Fw::throwException("'", widgetNode->tag(), "' is not a defined style");
OTMLNodePtr styleNode = originalStyleNode->clone();
styleNode->merge(widgetNode);
std::string widgetType = styleNode->valueAt("__widgetType");
@ -183,7 +189,7 @@ UIWidgetPtr UIManager::loadWidgetFromOTML(const OTMLNodePtr& widgetNode, const U
loadWidgetFromOTML(childNode, widget);
}
} else
logError("Unable to create widget of type '", widgetType, "'");
Fw::throwException("unable to create widget of type '", widgetType, "'");
return widget;
}

View File

@ -28,7 +28,8 @@ UIVerticalLayout::UIVerticalLayout(UIWidgetPtr parentWidget)
: UILayout(parentWidget)
{
m_alignBottom = false;
m_padding = 0;
m_fitParent = false;
m_spacing = 0;
}
void UIVerticalLayout::applyStyle(const OTMLNodePtr& styleNode)
@ -37,9 +38,11 @@ void UIVerticalLayout::applyStyle(const OTMLNodePtr& styleNode)
for(const OTMLNodePtr& node : styleNode->children()) {
if(node->tag() == "align-bottom")
m_alignBottom = node->value<bool>();
else if(node->tag() == "padding")
m_padding = node->value<int>();
setAlignBottom(node->value<bool>());
else if(node->tag() == "spacing")
setSpacing(node->value<int>());
else if(node->tag() == "fit-parent")
setFitParent(node->value<bool>());
}
}
@ -52,24 +55,41 @@ void UIVerticalLayout::update()
std::reverse(widgets.begin(), widgets.end());
Point pos = (m_alignBottom) ? parentWidget->getRect().bottomLeft() : parentWidget->getPosition();
int prefferedHeight = 0;
int gap;
for(const UIWidgetPtr& widget : widgets) {
if(!widget->isExplicitlyVisible())
continue;
Size size = widget->getSize();
pos.y += (m_alignBottom) ? -(widget->getMarginBottom()+widget->getHeight()) : widget->getMarginTop();
gap = (m_alignBottom) ? -(widget->getMarginBottom()+widget->getHeight()) : widget->getMarginTop();
pos.y += gap;
prefferedHeight += gap;
if(widget->isSizeFixed()) {
// center it
pos.x = parentWidget->getX() + (parentWidget->getWidth() - (widget->getMarginLeft() + widget->getWidth() + widget->getMarginRight()))/2;
pos.x = std::max(pos.x, parentWidget->getX());
} else {
// expand width
size.setWidth(parentWidget->getWidth() - (widget->getMarginLeft() + widget->getMarginRight()));
pos.x = std::max(pos.x, parentWidget->getX() + (parentWidget->getWidth() - size.width())/2);
pos.x = parentWidget->getX() + (parentWidget->getWidth() - size.width())/2;
}
widget->setRect(Rect(pos, size));
pos.y += (m_alignBottom) ? -widget->getMarginTop() : (widget->getHeight() + widget->getMarginBottom());
pos.y += m_padding;
gap = (m_alignBottom) ? -widget->getMarginTop() : (widget->getHeight() + widget->getMarginBottom());
gap += m_spacing;
pos.y += gap;
prefferedHeight += gap;
}
prefferedHeight -= m_spacing;
if(m_fitParent && prefferedHeight != parentWidget->getHeight())
parentWidget->setHeight(prefferedHeight);
}
void UIVerticalLayout::addWidget(const UIWidgetPtr& widget)
@ -81,3 +101,22 @@ void UIVerticalLayout::removeWidget(const UIWidgetPtr& widget)
{
update();
}
void UIVerticalLayout::setAlignBottom(bool aliginBottom)
{
m_alignBottom = aliginBottom;
update();
}
void UIVerticalLayout::setSpacing(int spacing)
{
m_spacing = spacing;
update();
}
void UIVerticalLayout::setFitParent(bool fitParent)
{
m_fitParent = fitParent;
update();
}

View File

@ -29,15 +29,21 @@ class UIVerticalLayout : public UILayout
{
public:
UIVerticalLayout(UIWidgetPtr parentWidget);
static UIVerticalLayoutPtr create(UIWidgetPtr parentWidget) { return UIVerticalLayoutPtr(new UIVerticalLayout(parentWidget)); }
virtual void applyStyle(const OTMLNodePtr& styleNode);
virtual void update();
virtual void addWidget(const UIWidgetPtr& widget);
virtual void removeWidget(const UIWidgetPtr& widget);
void setAlignBottom(bool aliginBottom);
void setSpacing(int spacing);
void setFitParent(bool fitParent);
private:
bool m_alignBottom;
int m_padding;
bool m_fitParent;
int m_spacing;
};
#endif

View File

@ -142,8 +142,10 @@ void UIWidget::setFocusable(bool focusable)
void UIWidget::setStyle(const std::string& styleName)
{
OTMLNodePtr styleNode = g_ui.getStyle(styleName);
if(!styleNode)
if(!styleNode) {
logTraceError("unable to retrive style '", styleName, "': not a defined style");
return;
}
applyStyle(styleNode);
m_style = styleNode;
updateStyle();
@ -372,7 +374,7 @@ void UIWidget::addChild(const UIWidgetPtr& child)
// create default layout
if(!m_layout)
m_layout = UILayoutPtr(new UIAnchorLayout(asUIWidget()));
m_layout = UIAnchorLayout::create(asUIWidget());
// add to layout and updates it
m_layout->addWidget(child);
@ -404,7 +406,7 @@ void UIWidget::insertChild(int index, const UIWidgetPtr& child)
// create default layout if needed
if(!m_layout)
m_layout = UILayoutPtr(new UIAnchorLayout(asUIWidget()));
m_layout = UIAnchorLayout::create(asUIWidget());
// add to layout and updates it
m_layout->addWidget(child);
@ -869,26 +871,25 @@ void UIWidget::onStyleApply(const OTMLNodePtr& styleNode)
}
// layouts
else if(node->tag() == "layout") {
// layout is set only once
assert(!m_layout);
std::string layoutType;
if(node->hasValue())
layoutType = node->value();
else
layoutType = node->valueAt("type");
layoutType = node->valueAt<std::string>("type", "");
if(!layoutType.empty()) {
UILayoutPtr layout;
if(layoutType == "verticalBox")
layout = UILayoutPtr(new UIVerticalLayout(asUIWidget()));
layout = UIVerticalLayout::create(asUIWidget());
else if(layoutType == "anchor")
layout = UILayoutPtr(new UIAnchorLayout(asUIWidget()));
layout = UIAnchorLayout::create(asUIWidget());
else
throw OTMLException(node, "cannot determine layout type");
setLayout(layout);
}
if(node->hasChildren())
layout->applyStyle(node);
setLayout(layout);
m_layout->applyStyle(node);
}
// anchors
else if(boost::starts_with(node->tag(), "anchors.")) {

View File

@ -69,7 +69,7 @@ for line in io.lines(cppclassheader) do
elseif line:match('private:') or line:match('protected:') then
publicmethods = false
elseif publicmethods then
funcname, args = line:match('^ *[%w <>&\*:_]* ([%w_]+)%(([^%)]*%))[%w ]*[;{].*$')
funcname, args = line:match('^ *[%w <>&\*:_]* ([%w_]+)%(([^%)]*%))[%w ]*[;{=].*$')
if funcname then
if funcname ~= cppclassname and funcname ~= 'create' then
numargs = args:matchcount('[^,)]+[,)]')