Skip to content

Commit d41a813

Browse files
committed
MusicStudio: Draw some buttons
1 parent d4a68a4 commit d41a813

File tree

1 file changed

+57
-64
lines changed

1 file changed

+57
-64
lines changed

MusicStudio/main.cpp

+57-64
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <UI/Application.h>
1414
#include <UI/Window.h>
1515
#include <Render/Render.h>
16+
#include <UI/UI.h>
1617

1718
template <typename T>
1819
void swap(T* a, T* b)
@@ -29,6 +30,8 @@ static Vec4f magenta = { .r = 1.0f, .g = 0.0f, .b = 1.0f, .a = 1.0f };
2930
static Vec4f yellow = { .r = 1.0f, .g = 1.0f, .b = 0.0f, .a = 1.0f };
3031
static Vec4f white = { .r = 1.0f, .g = 1.0f, .b = 1.0f, .a = 1.0f };
3132
static Vec4f red = { .r = 1.0f, .g = 0.0f, .b = 0.0f, .a = 1.0f };
33+
static Vec4f green = { .r = 0.0f, .g = 1.0f, .b = 0.0f, .a = 1.0f };
34+
static Vec4f blue = { .r = 0.0f, .g = 0.0f, .b = 1.0f, .a = 1.0f };
3235

3336
static void render_frame(UIWindow* window, Render* render);
3437

@@ -112,83 +115,73 @@ ErrorOr<int> Main::main(int argc, c_string *argv)
112115
Defer destroy_render = [&] {
113116
render_destroy(render);
114117
};
118+
auto ui = ui_create(window, render);
115119

116-
ui_window_set_resize_callback(window, render, [](UIWindow* window, void* user) {
117-
auto* render = (Render*)user;
118-
ui_window_gl_make_current_context(window);
119-
120-
i32 width = 0;
121-
i32 height = 1;
122-
ui_window_size(window, &width, &height);
123-
render_set_resolution(render, vec2f(width, height) * 2.0);
124-
125-
render_frame(window, render);
126-
127-
render_flush(render);
128-
ui_window_gl_flush(window);
120+
ui_window_set_resize_callback(window, &ui, [](UIWindow*, void* user) {
121+
auto* ui = (UI*)user;
122+
ui_begin_frame(ui);
123+
render_frame(ui);
124+
ui_end_frame(ui);
129125
});
130126

131-
{
132-
i32 width = 0;
133-
i32 height = 1;
134-
ui_window_size(window, &width, &height);
135-
render_set_resolution(render, vec2f(width, height) * 2.0);
136-
}
137-
138127
while (!ui_window_should_close(window)) {
139128
ui_application_poll_events(app);
140-
ui_window_gl_make_current_context(window);
141-
142-
i32 mouse_x = 0;
143-
i32 mouse_y = 0;
144-
ui_window_mouse_pos(window, &mouse_x, &mouse_y);
145-
render_set_mouse_position(render, vec2f(mouse_x, mouse_y));
146129

147-
render_frame(window, render);
148-
render_flush(render);
149-
ui_window_gl_flush(window);
130+
ui_begin_frame(&ui);
131+
render_frame(&ui);
132+
ui_end_frame(&ui);
150133
}
151134

152135
return 0;
153136
}
154137

155-
static void render_frame(UIWindow* window, Render* render)
138+
static void buttons(UI* ui);
139+
140+
static void render_frame(UI* ui)
156141
{
157-
render_clear(render, vec4f(0, 0, 0, 1));
158-
159-
if (ui_window_is_fullscreen(window)) {
160-
render_quad(render,
161-
vec2f(0.0f, 0.0f), cyan, zero,
162-
vec2f(1.0f, 0.0f), yellow, zero,
163-
vec2f(0.0f, 1.0f), white, zero,
164-
vec2f(1.0f, 1.0f), magenta, zero
165-
);
166-
} else {
167-
f32 titlebar_height = 0;
168-
i32 height = 1;
169-
ui_window_size(window, 0, &height);
170-
titlebar_height = 28.0f / height;
171-
172-
Vec4f color = cyan / 2;
173-
i32 mouse_y = 0;
174-
ui_window_mouse_pos(window, 0, &mouse_y);
175-
if (ui_window_mouse_state(window).left_down >= 2) {
176-
color = red;
177-
}
178-
179-
render_quad(render,
180-
vec2f(0.0f, titlebar_height), cyan, zero,
181-
vec2f(1.0f, titlebar_height), yellow, zero,
182-
vec2f(0.0f, 1.0f), white, zero,
183-
vec2f(1.0f, 1.0f), magenta, zero
184-
);
185-
render_quad(render,
186-
vec2f(0.0, 0.0), color, zero,
187-
vec2f(1.0, 0.0), color, zero,
188-
vec2f(0.0, titlebar_height), color, zero,
189-
vec2f(1.0, titlebar_height), color, zero
190-
);
142+
render_clear(ui->render, vec4f(0, 0, 0, 1));
143+
i32 height = 1;
144+
i32 width = 1;
145+
ui_window_size(ui->window, &width, &height);
146+
147+
Vec4f color = cyan / 2;
148+
i32 mouse_x = 0;
149+
i32 mouse_y = 0;
150+
ui_window_mouse_pos(ui->window, &mouse_x, &mouse_y);
151+
if (ui_window_mouse_state(ui->window).left_down >= 2) {
152+
color = red;
191153
}
154+
155+
f32 titlebar_height = 28.0f / height;
156+
render_quad(ui->render,
157+
vec2f(0.0f, titlebar_height), cyan, zero,
158+
vec2f(1.0f, titlebar_height), yellow, zero,
159+
vec2f(0.0f, 1.0f), white, zero,
160+
vec2f(1.0f, 1.0f), magenta, zero
161+
);
162+
ui_spacer(ui, vec2f(0, 28.0f));
163+
buttons(ui);
164+
165+
render_quad(ui->render,
166+
vec2f(0.0, 0.0), color, zero,
167+
vec2f(1.0, 0.0), color, zero,
168+
vec2f(0.0, titlebar_height), color, zero,
169+
vec2f(1.0, titlebar_height), color, zero
170+
);
192171
}
193172

173+
static void buttons(UI* ui)
174+
{
175+
ui_spacer(ui, vec2f(8, 8));
176+
if (ui_button(ui, "Button 1", red)) {
177+
dprintln("Button 1");
178+
}
179+
ui_spacer(ui, vec2f(0, 8));
180+
if (ui_button(ui, "Button 2", green)) {
181+
dprintln("Button 2");
182+
}
183+
ui_spacer(ui, vec2f(0, 8));
184+
if (ui_button(ui, "Button 3", blue)) {
185+
dprintln("Button 3");
186+
}
194187
}

0 commit comments

Comments
 (0)