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

@@ -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,19 +126,18 @@ 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];
return nullptr;
}
UIWidgetPtr UIManager::loadUI(const std::string& file, const UIWidgetPtr& parent)
@@ -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", "");
UILayoutPtr layout;
if(layoutType == "verticalBox")
layout = UILayoutPtr(new UIVerticalLayout(asUIWidget()));
else if(layoutType == "anchor")
layout = UILayoutPtr(new UIAnchorLayout(asUIWidget()));
else
throw OTMLException(node, "cannot determine layout type");
if(!layoutType.empty()) {
UILayoutPtr layout;
if(layoutType == "verticalBox")
layout = UIVerticalLayout::create(asUIWidget());
else if(layoutType == "anchor")
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.")) {