ugui.c3l/src/main.c3

160 lines
3.7 KiB
Plaintext
Raw Normal View History

2024-10-29 22:45:47 +01:00
import std::io;
import vtree;
import cache;
import ugui;
import rl;
fn int main(String[] args)
{
ugui::Ctx ctx;
ctx.init()!!;
short width = 800;
short height = 450;
rl::set_config_flags(rl::FLAG_WINDOW_RESIZABLE);
rl::init_window(width, height, "Ugui Test");
ctx.input_window_size(width, height)!!;
isz frame;
// Main loop
while (!rl::window_should_close()) {
const int PARTIAL_INPUT = 0;
const int PARTIAL_LAYOUT = 1;
const int PARTIAL_DRAW = 2;
2024-10-31 00:04:49 +01:00
/* Start Input Handling */
2024-10-29 22:45:47 +01:00
if (rl::is_window_resized()) {
width = (short)rl::get_screen_width();
height = (short)rl::get_screen_height();
ctx.input_window_size(width, height)!!;
}
ctx.input_changefocus(rl::is_window_focused());
// FIXME: In raylib it doesn't seem to be a quick way to check if
// a mouse input event was received, so for now just use
// the delta information
rl::Vector2 mousedelta = rl::get_mouse_delta();
if (mousedelta.x || mousedelta.y) {
ctx.input_mouse_delta((short)mousedelta.x, (short)mousedelta.y);
}
ugui::MouseButtons buttons;
buttons.btn_left = rl::is_mouse_button_down(rl::MOUSE_BUTTON_LEFT);
buttons.btn_right = rl::is_mouse_button_down(rl::MOUSE_BUTTON_RIGHT);
buttons.btn_middle = rl::is_mouse_button_down(rl::MOUSE_BUTTON_MIDDLE);
ctx.input_mouse_button(buttons);
2024-10-31 00:04:49 +01:00
/* End Input Handling */
2024-10-29 22:45:47 +01:00
2024-10-31 00:04:49 +01:00
/* Start UI Handling */
2024-10-29 22:45:47 +01:00
ctx.frame_begin()!!;
// main div, fill the whole window
2024-11-07 18:35:20 +01:00
ctx.div_begin("main", ugui::Rect{.w=ctx.width/2})!!;
2024-10-29 22:45:47 +01:00
{|
ctx.layout_set_column()!!;
2024-11-07 18:35:20 +01:00
if (ctx.button("button0", ugui::Rect{0,0,30,30})!!.mouse_press) {
2024-10-31 00:04:49 +01:00
io::printn("press button0");
2024-10-29 22:45:47 +01:00
}
ctx.layout_next_column()!!;
2024-10-31 00:04:49 +01:00
if (ctx.button("button1", ugui::Rect{0,0,30,30})!!.mouse_press) {
io::printn("press button1");
}
2024-10-29 22:45:47 +01:00
ctx.layout_next_column()!!;
2024-10-31 00:04:49 +01:00
if (ctx.button("button2", ugui::Rect{0,0,30,30})!!.mouse_release) {
io::printn("release button2");
}
2024-11-02 09:41:37 +01:00
if (ctx.slider_ver("slider", ugui::Rect{0,0,30,100})!!.update) {
2024-10-31 13:04:30 +01:00
ugui::Elem* e = ctx.get_elem_by_label("slider")!!;
io::printfn("slider: %f", e.slider.value);
}
2024-10-29 22:45:47 +01:00
|};
ctx.div_end()!!;
2024-11-07 18:35:20 +01:00
ctx.div_begin("second", ugui::DIV_FILL)!!;
{|
if (ctx.slider_ver("slider_other", ugui::Rect{0,0,30,100})!!.update) {
ugui::Elem* e = ctx.get_elem_by_label("slider_other")!!;
io::printfn("other slider: %f", e.slider.value);
}
|};
ctx.div_end()!!;
2024-10-29 22:45:47 +01:00
ctx.frame_end()!!;
2024-10-31 00:04:49 +01:00
/* End UI Handling */
2024-10-29 22:45:47 +01:00
2024-10-31 00:04:49 +01:00
/* Start UI Drawing */
2024-10-29 22:45:47 +01:00
rl::begin_drawing();
// ClearBackground(BLACK);
rl::Color c;
for (Cmd* cmd; (cmd = ctx.cmd_queue.dequeue() ?? null) != null;) {
switch (cmd.type) {
case ugui::CmdType.CMD_RECT:
c = rl::Color{
.r = cmd.rect.color.r,
.g = cmd.rect.color.g,
.b = cmd.rect.color.b,
.a = cmd.rect.color.a,
};
rl::draw_rectangle(
cmd.rect.rect.x,
cmd.rect.rect.y,
cmd.rect.rect.w,
cmd.rect.rect.h,
c
);
default:
io::printfn("Unknown cmd type: %d", cmd.type);
}
}
rl::end_drawing();
2024-10-31 00:04:49 +01:00
// TODO: throttle FPS
2024-10-29 22:45:47 +01:00
}
rl::close_window();
ctx.free();
return 0;
}
2024-10-31 00:04:49 +01:00
/*
2024-10-29 22:45:47 +01:00
fn void! test_vtree() @test
{
vtree::VTree(<String>) vt;
vt.init(10)!!;
defer vt.free();
assert(vt.size() == 10, "Size is incorrect");
isz ref = vt.add("Ciao Mamma", 0)!!;
String s = vt.get(ref)!!;
assert(s == "Ciao Mamma", "String is incorrect");
isz par = vt.parentof(0)!!;
assert(ref == par, "Not Root");
vt.print();
}
def StrCache = cache::Cache(<int, String, 256>);
fn void! test_cache() @test
{
StrCache cc;
cc.init()!!;
defer cc.free();
String*! r = cc.search(1);
if (catch ex = r) {
if (ex != SearchResult.MISSING) {
return ex?;
}
}
r = cc.get_or_insert(&&"Ciao Mamma", 1)!;
assert(*r!! == "Ciao Mamma", "incorrect string");
}
2024-10-31 00:04:49 +01:00
*/