-
Notifications
You must be signed in to change notification settings - Fork 0
/
uii.cpp
317 lines (271 loc) · 9.08 KB
/
uii.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include "uii"
#include <cstdio>
#include <string>
namespace ui
{
struct UiToText : UniversalInterface
{
typedef std::pair<std::string, std::string> tuple;
KeyValueStream* stream;
std::vector<std::string> groupPath;
size_t groupCount;
size_t groupLabelPending;
UiToText(KeyValueStream& stream)
: stream(&stream)
, groupCount(0)
, groupLabelPending(0) { }
bool pushContext(UniqueElementIdentifier id) override { return true; }
void popContext(UniqueElementIdentifier id) override { }
bool pushGroup(UniqueElementIdentifier id) override
{
++groupLabelPending;
return true;
}
void popGroup(UniqueElementIdentifier id) override
{
if (groupLabelPending > 0)
--groupLabelPending;
else
{
stream->leaveSection(groupPath.back().c_str());
--groupCount;
}
}
void addItemOrGroup(char const* key, char const* value)
{
if (groupLabelPending > 0)
{
if (groupCount >= groupPath.size())
groupPath.resize(1 + groupCount * 3 / 2);
groupPath[groupCount] = key;
++groupCount;
--groupLabelPending;
stream->enterSection(key, value);
}
else if (value)
stream->addItem(key, value);
}
void addText(UniqueElementIdentifier id, char const* label, char const* text, InteractionParam<char const*> interact) override
{
addItemOrGroup(label
// Always store something for interactive text
, (interact) ? (text ? text : "")
// Otherwise store nothing, but maybe add group
: nullptr);
}
void addHidden(UniqueElementIdentifier id, char const* label, char const* text, InteractionParam<char const*> interact, InteractionParam<std::string&> onDemand = nullptr) override
{
std::string onDemandStorage;
if (onDemand) { onDemandStorage = text; onDemand->updateValue(onDemandStorage); text = onDemandStorage.c_str(); }
stream->addItem(label, text);
}
void addInteractiveButton(UniqueElementIdentifier id, char const* label, char const* text, bool value, InteractionParam<ButtonEvent::T> interact, InteractionParam<Button> control = nullptr) override
{
}
void addButton(UniqueElementIdentifier id, char const* label, char const* text, InteractionParam<void> interact, char const* fullText = nullptr, InteractionParam<Button> control = nullptr) override
{
// auto key = keyFromLabel(label);
// (fullText) ? fullText : text;
}
void addOption(UniqueElementIdentifier id, char const* text, bool value, InteractionParam<bool> interact, InteractionParam<Button> control = nullptr) override
{
addItemOrGroup(text, (interact) ? (value ? "true" : "false") : nullptr);
}
void addSlider(UniqueElementIdentifier id, char const* label, float value, float range, InteractionParam<float> interact, float ticks = 0.0f, float fullBarThreshold = 0.0f
, InteractionParam<Slider> control = nullptr) override
{
char const* storedVal = nullptr;
char buf[1024];
if (interact)
{
sprintf(buf, "%f", value);
storedVal = buf;
}
addItemOrGroup(label, storedVal);
}
void addColor(UniqueElementIdentifier id, char const* label, glm::vec3 const& value, float valueRange, InteractionParam<glm::vec3> interact, float valueTicks = 0.0f) override
{
char const* storedVal = nullptr;
char buf[1024];
if (interact)
{
sprintf(buf, "%f %f %f", value.x, value.y, value.z);
storedVal = buf;
}
addItemOrGroup(label, storedVal);
}
};
void write(KeyValueStream& out, stdx::fun_ref<void(UniversalInterface&)> ui)
{
UiToText uitt(out);
ui.dispatch(ui, uitt);
}
struct TextToUi : UniversalInterface
{
typedef std::pair<std::string, std::string> tuple;
KeyValueStore* stream;
std::string nullterminatedStore;
size_t groupLabelPending;
TextToUi(KeyValueStore& stream)
: stream(&stream)
, groupLabelPending(0) { }
bool pushContext(UniqueElementIdentifier id) override { return true; }
void popContext(UniqueElementIdentifier id) override { }
bool pushGroup(UniqueElementIdentifier id) override
{
++groupLabelPending;
return true;
}
void popGroup(UniqueElementIdentifier id) override
{
if (groupLabelPending > 0)
--groupLabelPending;
else
stream->leaveSection();
}
struct ItemSource : UniversalInterface::ItemSource
{
struct Data
{
char itemSourceID[4];
KeyValueStore::Item const* item;
#define ItemSource_ID(_0, _1, _2, _3) _0 1 _1 5 _2 6 _3 4
#define ItemSource_ID_Expr(x, e, d) ItemSource_ID(x[0] e, d x[1] e, d x[2] e, d x[3] e)
Data()
{
ItemSource_ID_Expr(itemSourceID, =, ;);
}
static KeyValueStore::Item const* getItem(char const* label)
{
return (ItemSource_ID_Expr(label, ==, &&))
? reinterpret_cast<Data const*>(label)->item
: nullptr;
}
#undef ItemSource_ID_Expr
#undef ItemSource_ID
} data;
KeyValueStore* stream;
KeyValueStore::Item const* nextItem;
size_t estimateCount() const override
{
return stream->estimateCount(data.item);
}
char const* next() override
{
data.item = nextItem;
nextItem = stream->nextItem(nextItem);
return (data.item) ? data.itemSourceID : nullptr;
}
};
void addItems(stdx::fun_ref<void(UniversalInterface&, UniversalInterface::ItemSource&)> addItem, char const* filterLabel = nullptr) override
{
ItemSource is;
is.stream = stream;
is.nextItem = stream->findItem(filterLabel);
addItem.dispatch(addItem, *this, is);
}
// Clear all item groups before addition of new items.
bool clearItems() const override { return true; }
KeyValueStore::Item const* getItem(char const* label)
{
auto item = ItemSource::Data::getItem(label);
if (!item)
item = stream->findItem(label);
return item;
}
stdx::range<char const*> getValueAndMaybeEnter(char const* label)
{
auto item = getItem(label);
if (groupLabelPending > 0)
{
stream->enterSection(item);
--groupLabelPending;
}
return stream->getValue(item);
}
void addText(UniqueElementIdentifier id, char const* label, char const* text, InteractionParam<char const*> interact) override
{
auto val = getValueAndMaybeEnter(label);
if (interact)
if (val.first) // check if value explicitly given
{
nullterminatedStore.assign(val.first, val.last);
interact->updateValue(nullterminatedStore.c_str());
}
}
void addHidden(UniqueElementIdentifier id, char const* label, char const* text, InteractionParam<char const*> interact, InteractionParam<std::string&> onDemand = nullptr) override
{
if (interact)
{
auto val = stream->getValue(getItem(label));
if (val.first) // check if value explicitly given
{
nullterminatedStore.assign(val.first, val.last);
interact->updateValue(nullterminatedStore.c_str());
}
}
}
void addInteractiveButton(UniqueElementIdentifier id, char const* label, char const* text, bool value, InteractionParam<ButtonEvent::T> interact, InteractionParam<Button> control = nullptr) override
{
}
void addButton(UniqueElementIdentifier id, char const* label, char const* text, InteractionParam<void> interact, char const* fullText = nullptr, InteractionParam<Button> control = nullptr) override
{
// auto key = keyFromLabel(label);
// (fullText) ? fullText : text;
}
void addOption(UniqueElementIdentifier id, char const* text, bool value, InteractionParam<bool> interact, InteractionParam<Button> control = nullptr) override
{
auto val = getValueAndMaybeEnter(text);
if (interact)
if (val.first)
interact->updateValue( !stdx::memeq(val, stdx::strlit_range("false")) && !(val[0] == '0' && val[1] == 0) );
}
void addSlider(UniqueElementIdentifier id, char const* label, float value, float range, InteractionParam<float> interact, float ticks = 0.0f, float fullBarThreshold = 0.0f
, InteractionParam<Slider> control = nullptr) override
{
auto val = getValueAndMaybeEnter(label);
if (interact)
if (val.first)
{
float newVal = value;
if (sscanf(val.first, "%f", &newVal) == 1)
interact->updateValue(newVal);
}
}
void addColor(UniqueElementIdentifier id, char const* label, glm::vec3 const& value, float valueRange, InteractionParam<glm::vec3> interact, float valueTicks = 0.0f) override
{
auto val = getValueAndMaybeEnter(label);
if (interact)
if (val.first)
{
glm::vec3 newVal = value;
if (sscanf(val.first, "%f %f %f", &newVal.x, &newVal.y, &newVal.z) == 3)
interact->updateValue(newVal);
}
}
};
void read(KeyValueStore& in, stdx::fun_ref<void(UniversalInterface&)> ui)
{
TextToUi ttui(in);
ui.dispatch(ui, ttui);
}
} // namespace
#include "uiix"
#include "filex"
namespace ui
{
void load_ini_file(char const* file, stdx::fun_ref<void(UniversalInterface&)> ui)
{
auto presetFile = stdx::load_file(file);
auto presets = stdx::parse_ini_file<char>(stdx::data_range(presetFile));
presets[0].sort();
auto valStore = make_SortedKeyValueStore(presets.get());
read(valStore, ui);
}
void save_ini_file(char const* file, stdx::fun_ref<void(UniversalInterface&)> ui)
{
auto iniFile = stdx::write_file(file);
IniStream iniStream(iniFile);
write(iniStream, ui);
}
} // namespace