-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathelements_demo.cpp
178 lines (153 loc) · 5.29 KB
/
elements_demo.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
#include <sstream>
#include <string>
#include <SDL.h>
// #include "DarkTheme.hpp" // enable this to check the dark theme
#include "dui.hpp"
int
main(int argc, char** argv)
{
SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR, "0");
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "%s\n", SDL_GetError());
return 1;
}
SDL_Window* window = nullptr;
SDL_Renderer* renderer = nullptr;
if (SDL_CreateWindowAndRenderer(
800, 600, SDL_WINDOW_SHOWN, &window, &renderer) < 0) {
fprintf(stderr, "%s\n", SDL_GetError());
return 1;
}
// The ui state
dui::State state{renderer};
// Some test variables
int clickCount = 0;
std::string clickMeStr{"Click me!"};
bool toggleOption = false;
enum MultiOption
{
OPTION1,
OPTION2,
OPTION3,
};
MultiOption multiOption{};
constexpr size_t str1Size = 100;
char str1[str1Size] = "str1";
std::string str2 = "str2";
int value1 = 42;
double value2 = 11.25;
std::string basePath = SDL_GetBasePath();
SDL_Surface* surface = SDL_LoadBMP((basePath + "../dui.bmp").c_str());
SDL_SetColorKey(surface, 1, 0);
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FreeSurface(surface);
for (;;) {
SDL_Event ev;
while (SDL_PollEvent(&ev)) {
state.event(ev);
if (ev.type == SDL_QUIT) {
return 0;
}
}
// UI
auto f = dui::frame(state);
// Free label
dui::label(f, "Hello world", {320, 10});
// Main panel
auto p = dui::window(f, "Elements", {10, 10, 300, 580});
// dui::label(f, "Error"); // You can not add anything to the frame until
// you call p.end()
// Labels inside the panel
dui::label(p, "Hello world");
dui::label(p,
"Hello Styled World",
{5},
dui::themeFor<dui::Label>()
.withText({0xf0, 0x80, 0x80, 0xff})
.withScale(1));
// Push button example. It returns true only when you click on it (press and
// release)
if (dui::button(p, "Click me!", clickMeStr)) {
clickCount += 1;
std::stringstream ss;
ss << "Click count: " << clickCount;
clickMeStr = ss.str();
}
// A button that presents the state of boolean, being pressed if true and
// released if false, inverting its value if clicked.
if (dui::toggleButton(p, "Toggle", &toggleOption)) {
// Like button(), it also returns true on click, so you can do a special
// action
SDL_Log("Toggled options, new value is, %s",
toggleOption ? "true" : "false");
}
dui::label(p, toggleOption ? "activated" : "not activated", {5});
// Choice buttons: similar to toggle buttons, but for any types
if (dui::choiceButton(p, "Option 1", &multiOption, OPTION1, {0, 5})) {
// Like button(), it also returns true on click, so you can do a special
// action.
SDL_Log("Selected Option %d", 1 + multiOption);
// Pay attention we only log when optin 1 was chose and not when 2 or 3.
}
dui::choiceButton(p, "Option 2", &multiOption, OPTION2);
dui::choiceButton(p, "Option 3", &multiOption, OPTION3);
// Using a panel to group elements. You can replace panel() by group() if
// you don't want borders/custom color
if (auto g = dui::panel(p, "group1")) {
dui::label(g, "Grouped Label");
dui::button(g, "Grouped button");
}
// Example changing background color of the next panel
auto panelStyle =
dui::themeFor<dui::Panel>().withBackgroundColor({224, 255, 224, 255});
// And also making it grow horizontally
if (auto g =
dui::panel(p, "group2", {0}, dui::Layout::HORIZONTAL, panelStyle)) {
dui::label(g, "Grouped Label");
dui::button(g, "Grouped button");
}
static SDL_Point scrollOffset{0};
if (auto g = dui::scrollablePanel(p, "group3", &scrollOffset)) {
dui::label(g, "Grouped Label1");
dui::button(g, "Grouped button1");
dui::label(g, "Grouped Label2");
dui::button(g, "Grouped button2");
}
// Text input examples
dui::label(p, "Text input", {0, 10});
dui::textField(p, "Str1", str1, str1Size);
dui::textField(p, "Str2", &str2);
// numeric input examples
dui::label(p, "Number input", {0, 10});
dui::numberField(p, "value1", &value1);
dui::numberField(p, "value2", &value2);
dui::sliderField(p, "value1 b", &value1, 0, 100);
// New panel for images
p.end();
p = dui::window(f, "Textures", {480, 10});
// images
dui::textureBox(p, texture, {0, 0, 8, 8});
dui::textureBox(p, texture, {0, 1, 64, 64});
dui::textureBox(p, texture, {0, 1, 128, 128});
// Here we explicitly end the panel p, so we can add elements to the frame
// directly again after that.
p.end();
static SDL_Point scrollOffset2{0};
if (auto w = dui::scrollableWindow(
f, "Scroll Window", &scrollOffset2, {320, 30, 150, 0})) {
for (int i = 0; i < 10; ++i) {
dui::label(w, "Some label");
}
dui::button(w, "button");
}
// For example, we can add this big texture
dui::textureBox(f, texture, {400, 300, 256, 256});
// Render
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderFillRect(renderer, nullptr);
f.render();
SDL_RenderPresent(renderer);
SDL_Delay(1);
}
return 1;
}