Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added variables in OTUI #10

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions data/styles/0-vars.otui
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Labels
$var-font: verdana-11px-antialised
$var-text-color: #dfdfdf
$var-text-color-disabled: #dfdfdf88
16 changes: 8 additions & 8 deletions data/styles/10-labels.otui
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
Label < UILabel
font: verdana-11px-antialised
color: #dfdfdf
font: $var-font
color: $var-text-color

$disabled:
color: #dfdfdf88
color: $var-text-color-disabled

FlatLabel < UILabel
font: verdana-11px-antialised
color: #dfdfdf
font: $var-font
color: $var-text-color
size: 86 20
text-offset: 3 3
image-source: /images/ui/panel_flat
image-border: 1

$disabled:
color: #dfdfdf88
color: $var-text-color-disabled

MenuLabel < Label

GameLabel < UILabel
font: verdana-11px-antialised
color: #dfdfdf
font: $var-font
color: $var-text-color
10 changes: 5 additions & 5 deletions src/framework/luaengine/luavaluecasts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ int push_luavalue(const std::string& str)

bool luavalue_cast(int index, std::string& str)
{
str = g_lua.toString(index);
str = g_ui.getOTUIVarSafe(g_lua.toString(index));
return true;
}

Expand Down Expand Up @@ -121,7 +121,7 @@ bool luavalue_cast(int index, Color& color)
color.setAlpha((int)g_lua.popInteger());
return true;
} else if(g_lua.isString()) {
return stdext::cast(g_lua.toString(index), color);
return stdext::cast(g_ui.getOTUIVarSafe(g_lua.toString(index)), color);
} else if(g_lua.isNil()) {
color = Color::white;
return true;
Expand Down Expand Up @@ -157,7 +157,7 @@ bool luavalue_cast(int index, Rect& rect)
rect.setHeight(g_lua.popInteger());
return true;
} else if(g_lua.isString()) {
return stdext::cast(g_lua.toString(index), rect);
return stdext::cast(g_ui.getOTUIVarSafe(g_lua.toString(index)), rect);
} else if(g_lua.isNil()) {
rect = Rect();
return true;
Expand Down Expand Up @@ -185,7 +185,7 @@ bool luavalue_cast(int index, Point& point)
point.y = g_lua.popInteger();
return true;
} else if(g_lua.isString()) {
return stdext::cast(g_lua.toString(index), point);
return stdext::cast(g_ui.getOTUIVarSafe(g_lua.toString(index)), point);
} else if(g_lua.isNil()) {
point = Point();
return true;
Expand Down Expand Up @@ -213,7 +213,7 @@ bool luavalue_cast(int index, Size& size)
size.setHeight(g_lua.popInteger());
return true;
} else if(g_lua.isString()) {
return stdext::cast(g_lua.toString(index), size);
return stdext::cast(g_ui.getOTUIVarSafe(g_lua.toString(index)), size);
} else if(g_lua.isNil()) {
size = Size();
return true;
Expand Down
4 changes: 4 additions & 0 deletions src/framework/luafunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,10 @@ void Application::registerLuaFunctions()
g_lua.bindSingletonFunction("g_ui", "isDrawingDebugBoxes", &UIManager::isDrawingDebugBoxes, &g_ui);
g_lua.bindSingletonFunction("g_ui", "isMouseGrabbed", &UIManager::isMouseGrabbed, &g_ui);
g_lua.bindSingletonFunction("g_ui", "isKeyboardGrabbed", &UIManager::isKeyboardGrabbed, &g_ui);
g_lua.bindSingletonFunction("g_ui", "hasOTUIVar", &UIManager::hasOTUIVar, &g_ui);
g_lua.bindSingletonFunction("g_ui", "getOTUIVar", &UIManager::getOTUIVar, &g_ui);
g_lua.bindSingletonFunction("g_ui", "getOTUIVarSafe", &UIManager::getOTUIVarSafe, &g_ui);
g_lua.bindSingletonFunction("g_ui", "addOTUIVar", &UIManager::addOTUIVar, &g_ui);

// FontManager
g_lua.registerSingletonClass("g_fonts");
Expand Down
1 change: 1 addition & 0 deletions src/framework/otml/declarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ class OTMLEmitter;
typedef stdext::shared_object_ptr<OTMLNode> OTMLNodePtr;
typedef stdext::shared_object_ptr<OTMLDocument> OTMLDocumentPtr;
typedef std::vector<OTMLNodePtr> OTMLNodeList;
typedef std::unordered_map<std::string, std::string> OTUIVars;

#endif
19 changes: 15 additions & 4 deletions src/framework/otml/otmlnode.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#define OTMLNODE_H

#include "declarations.h"
#include <framework/ui/uimanager.h>

class OTMLNode : public stdext::shared_object
{
Expand Down Expand Up @@ -73,7 +74,7 @@ class OTMLNode : public stdext::shared_object
OTMLNodePtr clone();

template<typename T = std::string>
T value();
T value(bool raw = false);
template<typename T = std::string>
T valueAt(const std::string& childTag);
template<typename T = std::string>
Expand Down Expand Up @@ -110,7 +111,7 @@ class OTMLNode : public stdext::shared_object
#include "otmlexception.h"

template<>
inline std::string OTMLNode::value<std::string>() {
inline std::string OTMLNode::value<std::string>(bool raw) {
std::string value = m_value;
if(stdext::starts_with(value, "\"") && stdext::ends_with(value, "\"")) {
value = value.substr(1, value.length()-2);
Expand All @@ -120,13 +121,23 @@ inline std::string OTMLNode::value<std::string>() {
stdext::replace_all(value, "\\n", "\n");
stdext::replace_all(value, "\\'", "\'");
}
if (!raw) {
if (stdext::starts_with(value, "$var-")) {
return g_ui.getOTUIVar(value);
}
}
return value;
}

template<typename T>
T OTMLNode::value() {
T OTMLNode::value(bool raw) {
T ret;
if(!stdext::cast(m_value, ret))
if (stdext::starts_with(m_value, "$var-")) {
if (!stdext::cast(g_ui.getOTUIVar(m_value), ret))
throw OTMLException(asOTMLNode(), stdext::format("failed to cast var value '%s' (var value '%s') to type '%s'", m_value, g_ui.getOTUIVar(m_value), stdext::demangle_type<T>()));
return ret;
}
if (!stdext::cast(m_value, ret))
throw OTMLException(asOTMLNode(), stdext::format("failed to cast node value '%s' to type '%s'", m_value, stdext::demangle_type<T>()));
return ret;
}
Expand Down
12 changes: 12 additions & 0 deletions src/framework/ui/uimanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "uimanager.h"
#include "ui.h"
#include "uiwidget.h"

#include <framework/otml/otml.h>
#include <framework/graphics/graphics.h>
Expand All @@ -40,6 +41,7 @@ void UIManager::init()
m_rootWidget->setId("root");
m_mouseReceiver = m_rootWidget;
m_keyboardReceiver = m_rootWidget;
m_vars.reserve(100);
}

void UIManager::terminate()
Expand All @@ -56,6 +58,7 @@ void UIManager::terminate()
m_styles.clear();
m_destroyedWidgets.clear();
m_checkEvent = nullptr;
m_vars.clear();
}

void UIManager::render(Fw::DrawPane drawPane)
Expand Down Expand Up @@ -371,6 +374,15 @@ bool UIManager::importStyleFromString(std::string data)
void UIManager::importStyleFromOTML(const OTMLNodePtr& styleNode)
{
std::string tag = styleNode->tag();

// parse otui variable
if (stdext::starts_with(tag, "$var-")) {
std::string var = tag.substr(6);
if (!hasOTUIVar(var))
addOTUIVar(var, styleNode->rawValue());
return;
}

std::vector<std::string> split = stdext::split(tag, "<");
if(split.size() != 2)
throw OTMLException(styleNode, "not a valid style declaration");
Expand Down
34 changes: 33 additions & 1 deletion src/framework/ui/uimanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@
#define UIMANAGER_H

#include "declarations.h"
#include "uiwidget.h"
#include <framework/core/inputevent.h>
#include <framework/otml/declarations.h>

class UIWidget;

//@bindsingleton g_ui
class UIManager
{
Expand Down Expand Up @@ -72,6 +73,36 @@ class UIManager

bool isDrawingDebugBoxes() { return m_drawDebugBoxes; }

const OTUIVars& getOTUIVars() {
return m_vars;
}
bool hasOTUIVar(const std::string& key) {
if (stdext::starts_with(key, "$var-")) {
return m_vars.find(key.substr(6)) != m_vars.end();
}
return m_vars.find(key) != m_vars.end();
}
std::string getOTUIVar(const std::string& key) {
if (stdext::starts_with(key, "$var-")) {
return m_vars[key.substr(6)];
}
return m_vars[key];
}
std::string getOTUIVarSafe(const std::string& key) {
if (!hasOTUIVar(key)) {
return key;
}
if (stdext::starts_with(key, "$var-")) {
return m_vars[key.substr(6)];
}
return m_vars[key];
}
void addOTUIVar(const std::string& key, const std::string& value) {
if (m_vars.find(key) != m_vars.end()) return;

m_vars.insert(std::make_pair(key, value));
}

protected:
void onWidgetAppear(const UIWidgetPtr& widget);
void onWidgetDisappear(const UIWidgetPtr& widget);
Expand All @@ -89,6 +120,7 @@ class UIManager
stdext::boolean<false> m_hoverUpdateScheduled;
stdext::boolean<false> m_drawDebugBoxes;
std::unordered_map<std::string, OTMLNodePtr> m_styles;
OTUIVars m_vars;
UIWidgetList m_destroyedWidgets;
ScheduledEventPtr m_checkEvent;
stdext::timer m_moveTimer;
Expand Down
57 changes: 29 additions & 28 deletions src/framework/ui/uiwidgetbasestyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <framework/graphics/painter.h>
#include <framework/graphics/texture.h>
#include <framework/graphics/texturemanager.h>
#include <framework/ui/uimanager.h>

void UIWidget::initBaseStyle()
{
Expand Down Expand Up @@ -155,10 +156,10 @@ void UIWidget::parseBaseStyle(const OTMLNodePtr& styleNode)
else if(node->tag() == "clipping")
setClipping(node->value<bool>());
else if(node->tag() == "border") {
auto split = stdext::split(node->value(), " ");
auto split = stdext::split(node->value(true), " ");
if(split.size() == 2) {
setBorderWidth(stdext::safe_cast<int>(split[0]));
setBorderColor(stdext::safe_cast<Color>(split[1]));
setBorderWidth(stdext::safe_cast<int>(g_ui.getOTUIVarSafe(split[0])));
setBorderColor(stdext::safe_cast<Color>(g_ui.getOTUIVarSafe(split[1])));
} else
throw OTMLException(node, "border param must have its width followed by its color");
}
Expand Down Expand Up @@ -191,30 +192,30 @@ void UIWidget::parseBaseStyle(const OTMLNodePtr& styleNode)
else if(node->tag() == "margin-left")
setMarginLeft(node->value<int>());
else if(node->tag() == "margin") {
std::string marginDesc = node->value();
std::string marginDesc = node->value(true);
std::vector<std::string> split = stdext::split(marginDesc, " ");
if(split.size() == 4) {
setMarginTop(stdext::safe_cast<int>(split[0]));
setMarginRight(stdext::safe_cast<int>(split[1]));
setMarginBottom(stdext::safe_cast<int>(split[2]));
setMarginLeft(stdext::safe_cast<int>(split[3]));
setMarginTop(stdext::safe_cast<int>(g_ui.getOTUIVarSafe(split[0])));
setMarginRight(stdext::safe_cast<int>(g_ui.getOTUIVarSafe(split[1])));
setMarginBottom(stdext::safe_cast<int>(g_ui.getOTUIVarSafe(split[2])));
setMarginLeft(stdext::safe_cast<int>(g_ui.getOTUIVarSafe(split[3])));
} else if(split.size() == 3) {
int marginTop = stdext::safe_cast<int>(split[0]);
int marginHorizontal = stdext::safe_cast<int>(split[1]);
int marginBottom = stdext::safe_cast<int>(split[2]);
int marginTop = stdext::safe_cast<int>(g_ui.getOTUIVarSafe(split[0]));
int marginHorizontal = stdext::safe_cast<int>(g_ui.getOTUIVarSafe(split[1]));
int marginBottom = stdext::safe_cast<int>(g_ui.getOTUIVarSafe(split[2]));
setMarginTop(marginTop);
setMarginRight(marginHorizontal);
setMarginBottom(marginBottom);
setMarginLeft(marginHorizontal);
} else if(split.size() == 2) {
int marginVertical = stdext::safe_cast<int>(split[0]);
int marginHorizontal = stdext::safe_cast<int>(split[1]);
int marginVertical = stdext::safe_cast<int>(g_ui.getOTUIVarSafe(split[0]));
int marginHorizontal = stdext::safe_cast<int>(g_ui.getOTUIVarSafe(split[1]));
setMarginTop(marginVertical);
setMarginRight(marginHorizontal);
setMarginBottom(marginVertical);
setMarginLeft(marginHorizontal);
} else if(split.size() == 1) {
int margin = stdext::safe_cast<int>(split[0]);
int margin = stdext::safe_cast<int>(g_ui.getOTUIVarSafe(split[0]));
setMarginTop(margin);
setMarginRight(margin);
setMarginBottom(margin);
Expand All @@ -230,30 +231,30 @@ void UIWidget::parseBaseStyle(const OTMLNodePtr& styleNode)
else if(node->tag() == "padding-left")
setPaddingLeft(node->value<int>());
else if(node->tag() == "padding") {
std::string paddingDesc = node->value();
std::string paddingDesc = node->value(true);
std::vector<std::string> split = stdext::split(paddingDesc, " ");
if(split.size() == 4) {
setPaddingTop(stdext::safe_cast<int>(split[0]));
setPaddingRight(stdext::safe_cast<int>(split[1]));
setPaddingBottom(stdext::safe_cast<int>(split[2]));
setPaddingLeft(stdext::safe_cast<int>(split[3]));
setPaddingTop(stdext::safe_cast<int>(g_ui.getOTUIVarSafe(split[0])));
setPaddingRight(stdext::safe_cast<int>(g_ui.getOTUIVarSafe(split[1])));
setPaddingBottom(stdext::safe_cast<int>(g_ui.getOTUIVarSafe(split[2])));
setPaddingLeft(stdext::safe_cast<int>(g_ui.getOTUIVarSafe(split[3])));
} else if(split.size() == 3) {
int paddingTop = stdext::safe_cast<int>(split[0]);
int paddingHorizontal = stdext::safe_cast<int>(split[1]);
int paddingBottom = stdext::safe_cast<int>(split[2]);
int paddingTop = stdext::safe_cast<int>(g_ui.getOTUIVarSafe(split[0]));
int paddingHorizontal = stdext::safe_cast<int>(g_ui.getOTUIVarSafe(split[1]));
int paddingBottom = stdext::safe_cast<int>(g_ui.getOTUIVarSafe(split[2]));
setPaddingTop(paddingTop);
setPaddingRight(paddingHorizontal);
setPaddingBottom(paddingBottom);
setPaddingLeft(paddingHorizontal);
} else if(split.size() == 2) {
int paddingVertical = stdext::safe_cast<int>(split[0]);
int paddingHorizontal = stdext::safe_cast<int>(split[1]);
int paddingVertical = stdext::safe_cast<int>(g_ui.getOTUIVarSafe(split[0]));
int paddingHorizontal = stdext::safe_cast<int>(g_ui.getOTUIVarSafe(split[1]));
setPaddingTop(paddingVertical);
setPaddingRight(paddingHorizontal);
setPaddingBottom(paddingVertical);
setPaddingLeft(paddingHorizontal);
} else if(split.size() == 1) {
int padding = stdext::safe_cast<int>(split[0]);
int padding = stdext::safe_cast<int>(g_ui.getOTUIVarSafe(split[0]));
setPaddingTop(padding);
setPaddingRight(padding);
setPaddingBottom(padding);
Expand Down Expand Up @@ -315,12 +316,12 @@ void UIWidget::parseBaseStyle(const OTMLNodePtr& styleNode)
if(node->value() == "none") {
removeAnchor(anchoredEdge);
} else {
std::vector<std::string> split = stdext::split(node->value(), ".");
std::vector<std::string> split = stdext::split(node->value(true), ".");
if(split.size() != 2)
throw OTMLException(node, "invalid anchor description");

std::string hookedWidgetId = split[0];
Fw::AnchorEdge hookedEdge = Fw::translateAnchorEdge(split[1]);
std::string hookedWidgetId = g_ui.getOTUIVarSafe(split[0]);
Fw::AnchorEdge hookedEdge = Fw::translateAnchorEdge(g_ui.getOTUIVarSafe(split[1]));

if(anchoredEdge == Fw::AnchorNone)
throw OTMLException(node, "invalid anchor edge");
Expand Down
Loading