-
Notifications
You must be signed in to change notification settings - Fork 1
/
gui.cpp
236 lines (202 loc) · 6.47 KB
/
gui.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
#include "gui.hpp"
#include "widgets.hpp"
#include "exmview.hpp"
#include <stddef.h>
// creates a checkbox control for a pointer to a boolean
int checkbox(const char *label, bool *checked) {
// create new ui item
int item = uiItem();
// set minimum size of wiget; horizontal size is dynamic, vertical is fixed
uiSetSize(item, 0, APP_WIDGET_HEIGHT);
// store some custom data with the checkbox that we use for rendering
// and value changes.
CheckBoxData *data = (CheckBoxData *)uiAllocHandle(item, sizeof(CheckBoxData));
// assign a custom typeid to the data so the renderer knows how to
// render this control, and our event handler
data->head.type = WT_CHECKBOX;
// called when the item is clicked
data->head.handler = [](int item, UIevent event) {
CheckBoxData *data = (CheckBoxData *)uiGetHandle(item);
if (event == UI_BUTTON0_HOT_UP) {
*data->checked = !(*data->checked);
}
};
data->label = label;
data->checked = checked;
// set to fire as soon as the left button is pressed and released
uiSetEvents(item, UI_BUTTON0_HOT_UP | UI_BUTTON0_DOWN);
return item;
}
int checkboxf(const char *label, float *state, float low, float high) {
int item = uiItem();
uiSetSize(item, 0, APP_WIDGET_HEIGHT);
CheckBoxFloatData *data = (CheckBoxFloatData*)uiAllocHandle(item, sizeof(CheckBoxFloatData));
data->head.type = WT_CHECKBOX_FLOAT;
data->head.handler = [](int item, UIevent event) {
CheckBoxFloatData *data = (CheckBoxFloatData*)uiGetHandle(item);
if (event == UI_BUTTON0_HOT_UP) {
if (*data->checked <= data->low) {
*data->checked = data->high;
}
else {
*data->checked = data->low;
}
}
};
data->label = label;
data->checked = state;
data->low = low;
data->high = high;
uiSetEvents(item, UI_BUTTON0_HOT_UP | UI_BUTTON0_DOWN);
return item;
}
int button(const char *label, UIhandler handler) {
int item = uiItem();
uiSetSize(item, 0, APP_WIDGET_HEIGHT);
ButtonData *data = (ButtonData *)uiAllocHandle(item, sizeof(ButtonData));
data->head.type = WT_BUTTON;
data->head.handler = handler;
data->label = label;
uiSetEvents(item, UI_BUTTON0_HOT_UP | UI_BUTTON0_DOWN);
return item;
}
int label(const char *label, bool heading) {
int item = uiItem();
uiSetSize(item, 0, APP_WIDGET_HEIGHT);
ButtonData *data = (ButtonData *)uiAllocHandle(item, sizeof(ButtonData));
data->head.type = heading ? WT_HEADING : WT_LABEL;
data->head.handler = NULL;
data->label = label;
return item;
}
int hbox() {
int item = uiItem();
Data *data = (Data *)uiAllocHandle(item, sizeof(Data));
data->type = WT_HBOX;
data->handler = NULL;
uiSetBox(item, UI_ROW);
return item;
}
int vbox() {
int item = uiItem();
Data *data = (Data *)uiAllocHandle(item, sizeof(Data));
data->type = WT_VBOX;
data->handler = NULL;
uiSetBox(item, UI_COLUMN);
return item;
}
int column_append(int parent, int item) {
uiInsert(parent, item);
// fill parent horizontally, anchor to previous item vertically
uiSetLayout(item, UI_HFILL);
uiSetMargins(item, 0, 1, 0, 0);
return item;
}
int column() {
int item = uiItem();
uiSetBox(item, UI_COLUMN);
return item;
}
/*
int vgroup_append(int parent, int item) {
uiInsert(parent, item);
// fill parent horizontally, anchor to previous item vertically
uiSetLayout(item, UI_HFILL);
return item;
}
int vgroup() {
int item = uiItem();
uiSetBox(item, UI_COLUMN);
return item;
}
*/
int hgroup_append(int parent, int item) {
uiInsert(parent, item);
uiSetLayout(item, UI_HFILL);
return item;
}
int hgroup_append_fixed(int parent, int item) {
uiInsert(parent, item);
return item;
}
/*
int hgroup() {
int item = uiItem();
uiSetBox(item, UI_ROW);
return item;
}
int row_append(int parent, int item) {
uiInsert(parent, item);
uiSetLayout(item, UI_HFILL);
return item;
}
*/
int row() {
int item = uiItem();
uiSetBox(item, UI_ROW);
return item;
}
int separator() {
int item = uiItem();
uiSetSize(item, 0, 10);
Data *data = (Data *)uiAllocHandle(item, sizeof(Data));
data->type = WT_SEPARATOR;
data->handler = NULL;
return item;
}
static void layout_window(int x, int y, int w, int h) {
// create root item; the first item always has index 0
// this is the window frame
int root = uiItem();
// assign fixed size
uiSetMargins(root, x, y, 0, 0);
uiSetSize(root, w, 0);
WindowData *data = (WindowData *)uiAllocHandle(root, sizeof(WindowData));
data->head.type = WT_WINDOW;
data->head.handler = NULL;
// next, the content region
int col = uiInsert(root, column());
int pad = 5;
uiSetMargins(col, pad, pad, pad, pad);
uiSetLayout(col, UI_TOP | UI_HFILL);
//int box = column_append(col, hbox());
//hgroup_append(box, button("Wireframe", [](int item, UIevent e) {}));
//hgroup_append(box, button("Flat", NULL));
//hgroup_append(box, button("Shaded", NULL));
//column_append(col, separator());
// todo: radio buttons
//static bool grid = false;
//static bool bounds = false;
//static bool joints = false;
//column_append(col, checkbox("Show Grid", &grid));
column_append(col, checkbox("Show Wireframe", &g_state.wireframe));
column_append(col, separator());
column_append(col, checkbox("Show Backfaces", &g_state.show_backfaces));
column_append(col, checkboxf("Highlight Backfaces", &g_state.highlight_backfaces, 0.0f, 1.0f));
column_append(col, separator());
//column_append(col, checkbox("Show Bounds", &bounds));
column_append(col, checkboxf("Show Vertex Colors", &g_state.show_vcols, 0.0f, 1.0f));
column_append(col, checkboxf("Show Materials", &g_state.show_materials, 0.0f, 1.0f));
column_append(col, checkboxf("Show UVs", &g_state.show_uv, 0.0f, 1.0f));
//column_append(col, checkbox("Show Joints", &joints));
//column_append(col, button("button", [](int item, UIevent e) {}));
//static bool checked = false;
// add a checkbox to the same parent as item; this is faster than
// calling uiInsert on the same parent repeatedly.
//item = uiAppend(item, checkbox("Checked:", &checked));
// set a fixed height for the checkbox
//uiSetSize(item, 0, APP_WIDGET_HEIGHT);
// span the checkbox in the same way as the label
//uiSetLayout(item, UI_HFILL);
}
static int imaxf(int a, int b) {
return a > b ? a : b;
}
static int iminf(int a, int b) {
return a < b ? a : b;
}
void gui_layout(int w, int h) {
//layout_window(10, 10, 200, 400);
layout_window(imaxf(w - 300, 0), 0, iminf(w, 300), h);
//layout_window(0, 0, w, h);
}