Skip to content
This repository was archived by the owner on May 26, 2025. It is now read-only.

Commit 105be58

Browse files
Oen44Luan Luciano
authored and
Luan Luciano
committed
Added variables in OTUI
- Syntax: $var-<name>: value - Can be used in Lua, ie. label:setColor("$var-color") - g_ui.getOTUIVars() returns map with all variables and their values - g_ui.hasOTUIVar(variable) returns true if variable exists - g_ui.getOTUIVar(variable) returns value of variable - g_ui.getOTUIVarSafe(variable) returns value of variable or variable - g_ui.addOTUIVar(var, value) adds new variable with value - Added data/styles/0-vars.otui with variables for 10-labels.otui
1 parent d923c48 commit 105be58

File tree

9 files changed

+111
-46
lines changed

9 files changed

+111
-46
lines changed

data/styles/0-vars.otui

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Labels
2+
$var-font: verdana-11px-antialised
3+
$var-text-color: #dfdfdf
4+
$var-text-color-disabled: #dfdfdf88

data/styles/10-labels.otui

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
Label < UILabel
2-
font: verdana-11px-antialised
3-
color: #dfdfdf
2+
font: $var-font
3+
color: $var-text-color
44

55
$disabled:
6-
color: #dfdfdf88
6+
color: $var-text-color-disabled
77

88
FlatLabel < UILabel
9-
font: verdana-11px-antialised
10-
color: #dfdfdf
9+
font: $var-font
10+
color: $var-text-color
1111
size: 86 20
1212
text-offset: 3 3
1313
image-source: /images/ui/panel_flat
1414
image-border: 1
1515

1616
$disabled:
17-
color: #dfdfdf88
17+
color: $var-text-color-disabled
1818

1919
MenuLabel < Label
2020

2121
GameLabel < UILabel
22-
font: verdana-11px-antialised
23-
color: #dfdfdf
22+
font: $var-font
23+
color: $var-text-color

src/framework/luaengine/luavaluecasts.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ int push_luavalue(const std::string& str)
8282

8383
bool luavalue_cast(int index, std::string& str)
8484
{
85-
str = g_lua.toString(index);
85+
str = g_ui.getOTUIVarSafe(g_lua.toString(index));
8686
return true;
8787
}
8888

@@ -121,7 +121,7 @@ bool luavalue_cast(int index, Color& color)
121121
color.setAlpha((int)g_lua.popInteger());
122122
return true;
123123
} else if(g_lua.isString()) {
124-
return stdext::cast(g_lua.toString(index), color);
124+
return stdext::cast(g_ui.getOTUIVarSafe(g_lua.toString(index)), color);
125125
} else if(g_lua.isNil()) {
126126
color = Color::white;
127127
return true;
@@ -157,7 +157,7 @@ bool luavalue_cast(int index, Rect& rect)
157157
rect.setHeight(g_lua.popInteger());
158158
return true;
159159
} else if(g_lua.isString()) {
160-
return stdext::cast(g_lua.toString(index), rect);
160+
return stdext::cast(g_ui.getOTUIVarSafe(g_lua.toString(index)), rect);
161161
} else if(g_lua.isNil()) {
162162
rect = Rect();
163163
return true;
@@ -185,7 +185,7 @@ bool luavalue_cast(int index, Point& point)
185185
point.y = g_lua.popInteger();
186186
return true;
187187
} else if(g_lua.isString()) {
188-
return stdext::cast(g_lua.toString(index), point);
188+
return stdext::cast(g_ui.getOTUIVarSafe(g_lua.toString(index)), point);
189189
} else if(g_lua.isNil()) {
190190
point = Point();
191191
return true;
@@ -213,7 +213,7 @@ bool luavalue_cast(int index, Size& size)
213213
size.setHeight(g_lua.popInteger());
214214
return true;
215215
} else if(g_lua.isString()) {
216-
return stdext::cast(g_lua.toString(index), size);
216+
return stdext::cast(g_ui.getOTUIVarSafe(g_lua.toString(index)), size);
217217
} else if(g_lua.isNil()) {
218218
size = Size();
219219
return true;

src/framework/luafunctions.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,10 @@ void Application::registerLuaFunctions()
422422
g_lua.bindSingletonFunction("g_ui", "isDrawingDebugBoxes", &UIManager::isDrawingDebugBoxes, &g_ui);
423423
g_lua.bindSingletonFunction("g_ui", "isMouseGrabbed", &UIManager::isMouseGrabbed, &g_ui);
424424
g_lua.bindSingletonFunction("g_ui", "isKeyboardGrabbed", &UIManager::isKeyboardGrabbed, &g_ui);
425+
g_lua.bindSingletonFunction("g_ui", "hasOTUIVar", &UIManager::hasOTUIVar, &g_ui);
426+
g_lua.bindSingletonFunction("g_ui", "getOTUIVar", &UIManager::getOTUIVar, &g_ui);
427+
g_lua.bindSingletonFunction("g_ui", "getOTUIVarSafe", &UIManager::getOTUIVarSafe, &g_ui);
428+
g_lua.bindSingletonFunction("g_ui", "addOTUIVar", &UIManager::addOTUIVar, &g_ui);
425429

426430
// FontManager
427431
g_lua.registerSingletonClass("g_fonts");

src/framework/otml/declarations.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@ class OTMLEmitter;
3333
typedef stdext::shared_object_ptr<OTMLNode> OTMLNodePtr;
3434
typedef stdext::shared_object_ptr<OTMLDocument> OTMLDocumentPtr;
3535
typedef std::vector<OTMLNodePtr> OTMLNodeList;
36+
typedef std::unordered_map<std::string, std::string> OTUIVars;
3637

3738
#endif

src/framework/otml/otmlnode.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#define OTMLNODE_H
2525

2626
#include "declarations.h"
27+
#include <framework/ui/uimanager.h>
2728

2829
class OTMLNode : public stdext::shared_object
2930
{
@@ -73,7 +74,7 @@ class OTMLNode : public stdext::shared_object
7374
OTMLNodePtr clone();
7475

7576
template<typename T = std::string>
76-
T value();
77+
T value(bool raw = false);
7778
template<typename T = std::string>
7879
T valueAt(const std::string& childTag);
7980
template<typename T = std::string>
@@ -110,7 +111,7 @@ class OTMLNode : public stdext::shared_object
110111
#include "otmlexception.h"
111112

112113
template<>
113-
inline std::string OTMLNode::value<std::string>() {
114+
inline std::string OTMLNode::value<std::string>(bool raw) {
114115
std::string value = m_value;
115116
if(stdext::starts_with(value, "\"") && stdext::ends_with(value, "\"")) {
116117
value = value.substr(1, value.length()-2);
@@ -120,13 +121,23 @@ inline std::string OTMLNode::value<std::string>() {
120121
stdext::replace_all(value, "\\n", "\n");
121122
stdext::replace_all(value, "\\'", "\'");
122123
}
124+
if (!raw) {
125+
if (stdext::starts_with(value, "$var-")) {
126+
return g_ui.getOTUIVar(value);
127+
}
128+
}
123129
return value;
124130
}
125131

126132
template<typename T>
127-
T OTMLNode::value() {
133+
T OTMLNode::value(bool raw) {
128134
T ret;
129-
if(!stdext::cast(m_value, ret))
135+
if (stdext::starts_with(m_value, "$var-")) {
136+
if (!stdext::cast(g_ui.getOTUIVar(m_value), ret))
137+
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>()));
138+
return ret;
139+
}
140+
if (!stdext::cast(m_value, ret))
130141
throw OTMLException(asOTMLNode(), stdext::format("failed to cast node value '%s' to type '%s'", m_value, stdext::demangle_type<T>()));
131142
return ret;
132143
}

src/framework/ui/uimanager.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include "uimanager.h"
2424
#include "ui.h"
25+
#include "uiwidget.h"
2526

2627
#include <framework/otml/otml.h>
2728
#include <framework/graphics/graphics.h>
@@ -40,6 +41,7 @@ void UIManager::init()
4041
m_rootWidget->setId("root");
4142
m_mouseReceiver = m_rootWidget;
4243
m_keyboardReceiver = m_rootWidget;
44+
m_vars.reserve(100);
4345
}
4446

4547
void UIManager::terminate()
@@ -56,6 +58,7 @@ void UIManager::terminate()
5658
m_styles.clear();
5759
m_destroyedWidgets.clear();
5860
m_checkEvent = nullptr;
61+
m_vars.clear();
5962
}
6063

6164
void UIManager::render(Fw::DrawPane drawPane)
@@ -371,6 +374,15 @@ bool UIManager::importStyleFromString(std::string data)
371374
void UIManager::importStyleFromOTML(const OTMLNodePtr& styleNode)
372375
{
373376
std::string tag = styleNode->tag();
377+
378+
// parse otui variable
379+
if (stdext::starts_with(tag, "$var-")) {
380+
std::string var = tag.substr(6);
381+
if (!hasOTUIVar(var))
382+
addOTUIVar(var, styleNode->rawValue());
383+
return;
384+
}
385+
374386
std::vector<std::string> split = stdext::split(tag, "<");
375387
if(split.size() != 2)
376388
throw OTMLException(styleNode, "not a valid style declaration");

src/framework/ui/uimanager.h

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@
2424
#define UIMANAGER_H
2525

2626
#include "declarations.h"
27-
#include "uiwidget.h"
2827
#include <framework/core/inputevent.h>
2928
#include <framework/otml/declarations.h>
3029

30+
class UIWidget;
31+
3132
//@bindsingleton g_ui
3233
class UIManager
3334
{
@@ -72,6 +73,36 @@ class UIManager
7273

7374
bool isDrawingDebugBoxes() { return m_drawDebugBoxes; }
7475

76+
const OTUIVars& getOTUIVars() {
77+
return m_vars;
78+
}
79+
bool hasOTUIVar(const std::string& key) {
80+
if (stdext::starts_with(key, "$var-")) {
81+
return m_vars.find(key.substr(6)) != m_vars.end();
82+
}
83+
return m_vars.find(key) != m_vars.end();
84+
}
85+
std::string getOTUIVar(const std::string& key) {
86+
if (stdext::starts_with(key, "$var-")) {
87+
return m_vars[key.substr(6)];
88+
}
89+
return m_vars[key];
90+
}
91+
std::string getOTUIVarSafe(const std::string& key) {
92+
if (!hasOTUIVar(key)) {
93+
return key;
94+
}
95+
if (stdext::starts_with(key, "$var-")) {
96+
return m_vars[key.substr(6)];
97+
}
98+
return m_vars[key];
99+
}
100+
void addOTUIVar(const std::string& key, const std::string& value) {
101+
if (m_vars.find(key) != m_vars.end()) return;
102+
103+
m_vars.insert(std::make_pair(key, value));
104+
}
105+
75106
protected:
76107
void onWidgetAppear(const UIWidgetPtr& widget);
77108
void onWidgetDisappear(const UIWidgetPtr& widget);
@@ -89,6 +120,7 @@ class UIManager
89120
stdext::boolean<false> m_hoverUpdateScheduled;
90121
stdext::boolean<false> m_drawDebugBoxes;
91122
std::unordered_map<std::string, OTMLNodePtr> m_styles;
123+
OTUIVars m_vars;
92124
UIWidgetList m_destroyedWidgets;
93125
ScheduledEventPtr m_checkEvent;
94126
stdext::timer m_moveTimer;

src/framework/ui/uiwidgetbasestyle.cpp

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <framework/graphics/painter.h>
3131
#include <framework/graphics/texture.h>
3232
#include <framework/graphics/texturemanager.h>
33+
#include <framework/ui/uimanager.h>
3334

3435
void UIWidget::initBaseStyle()
3536
{
@@ -155,10 +156,10 @@ void UIWidget::parseBaseStyle(const OTMLNodePtr& styleNode)
155156
else if(node->tag() == "clipping")
156157
setClipping(node->value<bool>());
157158
else if(node->tag() == "border") {
158-
auto split = stdext::split(node->value(), " ");
159+
auto split = stdext::split(node->value(true), " ");
159160
if(split.size() == 2) {
160-
setBorderWidth(stdext::safe_cast<int>(split[0]));
161-
setBorderColor(stdext::safe_cast<Color>(split[1]));
161+
setBorderWidth(stdext::safe_cast<int>(g_ui.getOTUIVarSafe(split[0])));
162+
setBorderColor(stdext::safe_cast<Color>(g_ui.getOTUIVarSafe(split[1])));
162163
} else
163164
throw OTMLException(node, "border param must have its width followed by its color");
164165
}
@@ -191,30 +192,30 @@ void UIWidget::parseBaseStyle(const OTMLNodePtr& styleNode)
191192
else if(node->tag() == "margin-left")
192193
setMarginLeft(node->value<int>());
193194
else if(node->tag() == "margin") {
194-
std::string marginDesc = node->value();
195+
std::string marginDesc = node->value(true);
195196
std::vector<std::string> split = stdext::split(marginDesc, " ");
196197
if(split.size() == 4) {
197-
setMarginTop(stdext::safe_cast<int>(split[0]));
198-
setMarginRight(stdext::safe_cast<int>(split[1]));
199-
setMarginBottom(stdext::safe_cast<int>(split[2]));
200-
setMarginLeft(stdext::safe_cast<int>(split[3]));
198+
setMarginTop(stdext::safe_cast<int>(g_ui.getOTUIVarSafe(split[0])));
199+
setMarginRight(stdext::safe_cast<int>(g_ui.getOTUIVarSafe(split[1])));
200+
setMarginBottom(stdext::safe_cast<int>(g_ui.getOTUIVarSafe(split[2])));
201+
setMarginLeft(stdext::safe_cast<int>(g_ui.getOTUIVarSafe(split[3])));
201202
} else if(split.size() == 3) {
202-
int marginTop = stdext::safe_cast<int>(split[0]);
203-
int marginHorizontal = stdext::safe_cast<int>(split[1]);
204-
int marginBottom = stdext::safe_cast<int>(split[2]);
203+
int marginTop = stdext::safe_cast<int>(g_ui.getOTUIVarSafe(split[0]));
204+
int marginHorizontal = stdext::safe_cast<int>(g_ui.getOTUIVarSafe(split[1]));
205+
int marginBottom = stdext::safe_cast<int>(g_ui.getOTUIVarSafe(split[2]));
205206
setMarginTop(marginTop);
206207
setMarginRight(marginHorizontal);
207208
setMarginBottom(marginBottom);
208209
setMarginLeft(marginHorizontal);
209210
} else if(split.size() == 2) {
210-
int marginVertical = stdext::safe_cast<int>(split[0]);
211-
int marginHorizontal = stdext::safe_cast<int>(split[1]);
211+
int marginVertical = stdext::safe_cast<int>(g_ui.getOTUIVarSafe(split[0]));
212+
int marginHorizontal = stdext::safe_cast<int>(g_ui.getOTUIVarSafe(split[1]));
212213
setMarginTop(marginVertical);
213214
setMarginRight(marginHorizontal);
214215
setMarginBottom(marginVertical);
215216
setMarginLeft(marginHorizontal);
216217
} else if(split.size() == 1) {
217-
int margin = stdext::safe_cast<int>(split[0]);
218+
int margin = stdext::safe_cast<int>(g_ui.getOTUIVarSafe(split[0]));
218219
setMarginTop(margin);
219220
setMarginRight(margin);
220221
setMarginBottom(margin);
@@ -230,30 +231,30 @@ void UIWidget::parseBaseStyle(const OTMLNodePtr& styleNode)
230231
else if(node->tag() == "padding-left")
231232
setPaddingLeft(node->value<int>());
232233
else if(node->tag() == "padding") {
233-
std::string paddingDesc = node->value();
234+
std::string paddingDesc = node->value(true);
234235
std::vector<std::string> split = stdext::split(paddingDesc, " ");
235236
if(split.size() == 4) {
236-
setPaddingTop(stdext::safe_cast<int>(split[0]));
237-
setPaddingRight(stdext::safe_cast<int>(split[1]));
238-
setPaddingBottom(stdext::safe_cast<int>(split[2]));
239-
setPaddingLeft(stdext::safe_cast<int>(split[3]));
237+
setPaddingTop(stdext::safe_cast<int>(g_ui.getOTUIVarSafe(split[0])));
238+
setPaddingRight(stdext::safe_cast<int>(g_ui.getOTUIVarSafe(split[1])));
239+
setPaddingBottom(stdext::safe_cast<int>(g_ui.getOTUIVarSafe(split[2])));
240+
setPaddingLeft(stdext::safe_cast<int>(g_ui.getOTUIVarSafe(split[3])));
240241
} else if(split.size() == 3) {
241-
int paddingTop = stdext::safe_cast<int>(split[0]);
242-
int paddingHorizontal = stdext::safe_cast<int>(split[1]);
243-
int paddingBottom = stdext::safe_cast<int>(split[2]);
242+
int paddingTop = stdext::safe_cast<int>(g_ui.getOTUIVarSafe(split[0]));
243+
int paddingHorizontal = stdext::safe_cast<int>(g_ui.getOTUIVarSafe(split[1]));
244+
int paddingBottom = stdext::safe_cast<int>(g_ui.getOTUIVarSafe(split[2]));
244245
setPaddingTop(paddingTop);
245246
setPaddingRight(paddingHorizontal);
246247
setPaddingBottom(paddingBottom);
247248
setPaddingLeft(paddingHorizontal);
248249
} else if(split.size() == 2) {
249-
int paddingVertical = stdext::safe_cast<int>(split[0]);
250-
int paddingHorizontal = stdext::safe_cast<int>(split[1]);
250+
int paddingVertical = stdext::safe_cast<int>(g_ui.getOTUIVarSafe(split[0]));
251+
int paddingHorizontal = stdext::safe_cast<int>(g_ui.getOTUIVarSafe(split[1]));
251252
setPaddingTop(paddingVertical);
252253
setPaddingRight(paddingHorizontal);
253254
setPaddingBottom(paddingVertical);
254255
setPaddingLeft(paddingHorizontal);
255256
} else if(split.size() == 1) {
256-
int padding = stdext::safe_cast<int>(split[0]);
257+
int padding = stdext::safe_cast<int>(g_ui.getOTUIVarSafe(split[0]));
257258
setPaddingTop(padding);
258259
setPaddingRight(padding);
259260
setPaddingBottom(padding);
@@ -315,12 +316,12 @@ void UIWidget::parseBaseStyle(const OTMLNodePtr& styleNode)
315316
if(node->value() == "none") {
316317
removeAnchor(anchoredEdge);
317318
} else {
318-
std::vector<std::string> split = stdext::split(node->value(), ".");
319+
std::vector<std::string> split = stdext::split(node->value(true), ".");
319320
if(split.size() != 2)
320321
throw OTMLException(node, "invalid anchor description");
321322

322-
std::string hookedWidgetId = split[0];
323-
Fw::AnchorEdge hookedEdge = Fw::translateAnchorEdge(split[1]);
323+
std::string hookedWidgetId = g_ui.getOTUIVarSafe(split[0]);
324+
Fw::AnchorEdge hookedEdge = Fw::translateAnchorEdge(g_ui.getOTUIVarSafe(split[1]));
324325

325326
if(anchoredEdge == Fw::AnchorNone)
326327
throw OTMLException(node, "invalid anchor edge");

0 commit comments

Comments
 (0)