Compare commits
3 Commits
574a1f23dc
...
7e18c7a316
Author | SHA1 | Date | |
---|---|---|---|
7e18c7a316 | |||
537acd4765 | |||
d5bea68058 |
48
src/main.c3
48
src/main.c3
@ -3,6 +3,25 @@ import vtree;
|
|||||||
import cache;
|
import cache;
|
||||||
import ugui;
|
import ugui;
|
||||||
import rl;
|
import rl;
|
||||||
|
import std::time;
|
||||||
|
import std::collections::ringbuffer;
|
||||||
|
|
||||||
|
def Times = ringbuffer::RingBuffer(<time::NanoDuration, 128>);
|
||||||
|
|
||||||
|
fn void Times.print_stats(×)
|
||||||
|
{
|
||||||
|
time::NanoDuration min, max, avg, x;
|
||||||
|
min = times.get(0);
|
||||||
|
for (usz i = 0; i < times.written; i++) {
|
||||||
|
x = times.get(i);
|
||||||
|
if (x < min) { min = x; }
|
||||||
|
if (x > max) { max = x; }
|
||||||
|
avg += x;
|
||||||
|
}
|
||||||
|
avg = (NanoDuration)((ulong)avg/128.0);
|
||||||
|
|
||||||
|
io::printfn("min=%s, max=%s, avg=%s", min, max, avg);
|
||||||
|
}
|
||||||
|
|
||||||
fn int main(String[] args)
|
fn int main(String[] args)
|
||||||
{
|
{
|
||||||
@ -18,13 +37,15 @@ fn int main(String[] args)
|
|||||||
rl::enable_event_waiting();
|
rl::enable_event_waiting();
|
||||||
|
|
||||||
isz frame;
|
isz frame;
|
||||||
|
bool toggle = true;
|
||||||
|
time::Clock clock;
|
||||||
|
Times ui_times;
|
||||||
|
Times draw_times;
|
||||||
|
|
||||||
// Main loop
|
// Main loop
|
||||||
while (!rl::window_should_close()) {
|
while (!rl::window_should_close()) {
|
||||||
const int PARTIAL_INPUT = 0;
|
clock.mark();
|
||||||
const int PARTIAL_LAYOUT = 1;
|
|
||||||
const int PARTIAL_DRAW = 2;
|
|
||||||
|
|
||||||
/* Start Input Handling */
|
/* Start Input Handling */
|
||||||
if (rl::is_window_resized()) {
|
if (rl::is_window_resized()) {
|
||||||
width = (short)rl::get_screen_width();
|
width = (short)rl::get_screen_width();
|
||||||
@ -47,21 +68,25 @@ fn int main(String[] args)
|
|||||||
/* Start UI Handling */
|
/* Start UI Handling */
|
||||||
ui.frame_begin()!!;
|
ui.frame_begin()!!;
|
||||||
|
|
||||||
/*
|
|
||||||
// main div, fill the whole window
|
// main div, fill the whole window
|
||||||
ui.div_begin("main", ugui::Rect{.w=ui.width/2})!!;
|
ui.div_begin("main", ugui::Rect{.w=ui.width/2})!!;
|
||||||
{|
|
{|
|
||||||
|
|
||||||
ui.layout_set_row()!!;
|
ui.layout_set_row()!!;
|
||||||
if (ui.button("button0", ugui::Rect{0,0,30,30})!!.mouse_press) {
|
if (ui.button("button0", ugui::Rect{0,0,30,30})!!.mouse_press) {
|
||||||
io::printn("press button0");
|
io::printn("press button0");
|
||||||
|
toggle = !toggle;
|
||||||
|
ui.force_update()!!;
|
||||||
}
|
}
|
||||||
//ui.layout_next_column()!!;
|
//ui.layout_next_column()!!;
|
||||||
if (ui.button("button1", ugui::Rect{0,0,30,30})!!.mouse_press) {
|
if (ui.button("button1", ugui::Rect{0,0,30,30})!!.mouse_press) {
|
||||||
io::printn("press button1");
|
io::printn("press button1");
|
||||||
}
|
}
|
||||||
//ui.layout_next_column()!!;
|
//ui.layout_next_column()!!;
|
||||||
if (ui.button("button2", ugui::Rect{0,0,30,30})!!.mouse_release) {
|
if (toggle) {
|
||||||
io::printn("release button2");
|
if (ui.button("button2", ugui::Rect{0,0,30,30})!!.mouse_release) {
|
||||||
|
io::printn("release button2");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (ui.slider_ver("slider", ugui::Rect{0,0,30,100})!!.update) {
|
if (ui.slider_ver("slider", ugui::Rect{0,0,30,100})!!.update) {
|
||||||
ugui::Elem* e = ui.get_elem_by_label("slider")!!;
|
ugui::Elem* e = ui.get_elem_by_label("slider")!!;
|
||||||
@ -69,7 +94,7 @@ fn int main(String[] args)
|
|||||||
}
|
}
|
||||||
|};
|
|};
|
||||||
ui.div_end()!!;
|
ui.div_end()!!;
|
||||||
*/
|
|
||||||
ui.div_begin("second", ugui::DIV_FILL)!!;
|
ui.div_begin("second", ugui::DIV_FILL)!!;
|
||||||
ugui::Elem* de = ui.get_elem_by_label("second")!!;
|
ugui::Elem* de = ui.get_elem_by_label("second")!!;
|
||||||
de.div.scroll.can_y = true;
|
de.div.scroll.can_y = true;
|
||||||
@ -92,6 +117,8 @@ fn int main(String[] args)
|
|||||||
|
|
||||||
ui.frame_end()!!;
|
ui.frame_end()!!;
|
||||||
/* End UI Handling */
|
/* End UI Handling */
|
||||||
|
ui_times.push(clock.mark());
|
||||||
|
ui_times.print_stats();
|
||||||
|
|
||||||
/* Start UI Drawing */
|
/* Start UI Drawing */
|
||||||
rl::begin_drawing();
|
rl::begin_drawing();
|
||||||
@ -118,8 +145,11 @@ fn int main(String[] args)
|
|||||||
io::printfn("Unknown cmd type: %d", cmd.type);
|
io::printfn("Unknown cmd type: %d", cmd.type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
draw_times.push(clock.mark());
|
||||||
|
draw_times.print_stats();
|
||||||
rl::end_drawing();
|
rl::end_drawing();
|
||||||
|
/* End Drawing */
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rl::close_window();
|
rl::close_window();
|
||||||
|
@ -20,22 +20,22 @@ fn void! Ctx.div_begin(&ctx, String label, Rect size)
|
|||||||
c_elem.bounds = ctx.position_element(parent, size);
|
c_elem.bounds = ctx.position_element(parent, size);
|
||||||
if (c_elem.flags.is_new) {
|
if (c_elem.flags.is_new) {
|
||||||
c_elem.div.children_bounds = c_elem.bounds;
|
c_elem.div.children_bounds = c_elem.bounds;
|
||||||
|
c_elem.div.color_bg = uint_to_rgba(0xff0000ff);
|
||||||
|
c_elem.div.scroll.can_x = false;
|
||||||
|
c_elem.div.scroll.can_y = false;
|
||||||
|
c_elem.div.scroll.value_x = 0;
|
||||||
|
c_elem.div.scroll.value_y = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Mark the element as updated
|
// 3. Mark the element as updated
|
||||||
c_elem.flags.updated = true;
|
c_elem.flags.updated = true;
|
||||||
// 4. Fill the div fields
|
// 4. Fill the div fields
|
||||||
c_elem.div.layout = parent.div.layout;
|
|
||||||
c_elem.div.origin_c = Point{
|
c_elem.div.origin_c = Point{
|
||||||
.x = c_elem.bounds.x,
|
.x = c_elem.bounds.x,
|
||||||
.y = c_elem.bounds.y,
|
.y = c_elem.bounds.y,
|
||||||
};
|
};
|
||||||
c_elem.div.origin_r = c_elem.div.origin_c;
|
c_elem.div.origin_r = c_elem.div.origin_c;
|
||||||
c_elem.div.color_bg = uint_to_rgba(0xff0000ff);
|
c_elem.div.layout = parent.div.layout;
|
||||||
c_elem.div.scroll.can_x = false;
|
|
||||||
c_elem.div.scroll.can_y = false;
|
|
||||||
c_elem.div.scroll.value_x = 0;
|
|
||||||
c_elem.div.scroll.value_y = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the background to the draw stack
|
// Add the background to the draw stack
|
||||||
|
@ -28,13 +28,13 @@ macro Ctx.get_elem(&ctx, Id id)
|
|||||||
// if it does't find one
|
// if it does't find one
|
||||||
macro Ctx.get_elem_by_label(&ctx, String label)
|
macro Ctx.get_elem_by_label(&ctx, String label)
|
||||||
{
|
{
|
||||||
Id id = label.hash();
|
Id id = label.hash();
|
||||||
return ctx.cache.search(id);
|
return ctx.cache.search(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
macro Ctx.get_elem_by_tree_idx(&ctx, isz idx) @private
|
macro Ctx.get_elem_by_tree_idx(&ctx, isz idx) @private
|
||||||
{
|
{
|
||||||
Id id = ctx.tree.get(ctx.active_div)!;
|
Id id = ctx.tree.get(ctx.active_div)!;
|
||||||
return ctx.cache.search(id);
|
return ctx.cache.search(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,7 +42,6 @@ fn void! Ctx.init(&ctx)
|
|||||||
{
|
{
|
||||||
ctx.tree.init(MAX_ELEMENTS)!;
|
ctx.tree.init(MAX_ELEMENTS)!;
|
||||||
defer catch { (void)ctx.tree.free(); }
|
defer catch { (void)ctx.tree.free(); }
|
||||||
//ug_fifo_init(&ctx.fifo, MAX_CMDS);
|
|
||||||
|
|
||||||
ctx.cache.init()!;
|
ctx.cache.init()!;
|
||||||
defer catch { (void)ctx.cache.free(); }
|
defer catch { (void)ctx.cache.free(); }
|
||||||
@ -72,31 +71,32 @@ fn void! Ctx.frame_begin(&ctx)
|
|||||||
// The root should have the updated flag only if the size of the window
|
// The root should have the updated flag only if the size of the window
|
||||||
// was changed between frames, this propagates an element size recalculation
|
// was changed between frames, this propagates an element size recalculation
|
||||||
// down the element tree
|
// down the element tree
|
||||||
c_elem.flags.updated = ctx.input.events.resize;
|
c_elem.flags.updated = ctx.input.events.resize | ctx.input.events.force_update;
|
||||||
|
ctx.input.events.force_update = false;
|
||||||
// if the window has focus then the root element also has focus, no other
|
// if the window has focus then the root element also has focus, no other
|
||||||
// computation needed, child elements need to check the mouse positon and
|
// computation needed, child elements need to check the mouse positon and
|
||||||
// other stuff
|
// other stuff
|
||||||
c_elem.flags.has_focus = ctx.has_focus;
|
c_elem.flags.has_focus = ctx.has_focus;
|
||||||
|
|
||||||
if (c_elem.flags.is_new || c_elem.flags.updated) {
|
if (c_elem.flags.is_new || c_elem.flags.updated) {
|
||||||
Elem def_root = {
|
Elem def_root = {
|
||||||
.id = ROOT_ID,
|
.id = ROOT_ID,
|
||||||
.type = ETYPE_DIV,
|
.type = ETYPE_DIV,
|
||||||
.bounds = {
|
.bounds = {
|
||||||
.w = ctx.width,
|
|
||||||
.h = ctx.height,
|
|
||||||
},
|
|
||||||
.div = {
|
|
||||||
.layout = LAYOUT_ROW,
|
|
||||||
.children_bounds = {
|
|
||||||
.w = ctx.width,
|
.w = ctx.width,
|
||||||
.h = ctx.height,
|
.h = ctx.height,
|
||||||
}
|
},
|
||||||
},
|
.div = {
|
||||||
.flags = c_elem.flags,
|
.layout = LAYOUT_ROW,
|
||||||
};
|
.children_bounds = {
|
||||||
|
.w = ctx.width,
|
||||||
|
.h = ctx.height,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
.flags = c_elem.flags,
|
||||||
|
};
|
||||||
|
|
||||||
*c_elem = def_root;
|
*c_elem = def_root;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Push the root element into the element tree
|
// 3. Push the root element into the element tree
|
||||||
@ -106,13 +106,20 @@ fn void! Ctx.frame_begin(&ctx)
|
|||||||
// TODO: add a background color taken from a theme or config
|
// TODO: add a background color taken from a theme or config
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn void! Ctx.force_update(&ctx)
|
||||||
|
{
|
||||||
|
ctx.input.events.force_update = true;
|
||||||
|
}
|
||||||
|
|
||||||
fn void! Ctx.frame_end(&ctx)
|
fn void! Ctx.frame_end(&ctx)
|
||||||
{
|
{
|
||||||
// 1. clear the tree
|
// 1. clear the tree
|
||||||
ctx.tree.prune(0)!;
|
ctx.tree.prune(0)!;
|
||||||
|
|
||||||
// 2. clear input fields
|
// 2. clear input fields
|
||||||
|
bool f = ctx.input.events.force_update;
|
||||||
ctx.input.events = (InputEvents)0;
|
ctx.input.events = (InputEvents)0;
|
||||||
|
ctx.input.events.force_update = f;
|
||||||
|
|
||||||
// draw mouse position
|
// draw mouse position
|
||||||
$if 1:
|
$if 1:
|
||||||
@ -130,9 +137,9 @@ $if 1:
|
|||||||
$endif
|
$endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
<*
|
||||||
* @ensure elem != null
|
* @ensure elem != null
|
||||||
**/
|
*>
|
||||||
fn bool Ctx.is_hovered(&ctx, Elem *elem)
|
fn bool Ctx.is_hovered(&ctx, Elem *elem)
|
||||||
{
|
{
|
||||||
return point_in_rect(ctx.input.mouse.pos, elem.bounds);
|
return point_in_rect(ctx.input.mouse.pos, elem.bounds);
|
||||||
|
@ -8,6 +8,7 @@ bitstruct InputEvents : uint {
|
|||||||
bool change_focus : 1; // window focus changed
|
bool change_focus : 1; // window focus changed
|
||||||
bool mouse_move : 2; // mouse was moved
|
bool mouse_move : 2; // mouse was moved
|
||||||
bool mouse_btn : 3; // mouse button pressed or released
|
bool mouse_btn : 3; // mouse button pressed or released
|
||||||
|
bool force_update : 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Window size was changed
|
// Window size was changed
|
||||||
@ -82,16 +83,16 @@ macro Ctx.is_mouse_down(&ctx, MouseButtons btn)
|
|||||||
|
|
||||||
macro ElemEvents Ctx.get_elem_events(&ctx, Elem *elem)
|
macro ElemEvents Ctx.get_elem_events(&ctx, Elem *elem)
|
||||||
{
|
{
|
||||||
// TODO: add the other events
|
// TODO: add the other events
|
||||||
// FIXME: active should be elsewhere
|
// FIXME: active should be elsewhere
|
||||||
bool active = elem.events.mouse_hold || ctx.is_hovered(elem);
|
bool active = elem.events.mouse_hold || ctx.is_hovered(elem);
|
||||||
ElemEvents ev = {
|
ElemEvents ev = {
|
||||||
.mouse_hover = ctx.is_hovered(elem),
|
.mouse_hover = ctx.is_hovered(elem),
|
||||||
.mouse_press = active & ctx.is_mouse_pressed(BTN_ANY),
|
.mouse_press = active & ctx.is_mouse_pressed(BTN_ANY),
|
||||||
.mouse_release = active & ctx.is_mouse_released(BTN_ANY),
|
.mouse_release = active & ctx.is_mouse_released(BTN_ANY),
|
||||||
.mouse_hold = active & ctx.is_mouse_down(BTN_ANY),
|
.mouse_hold = active & ctx.is_mouse_down(BTN_ANY),
|
||||||
};
|
};
|
||||||
return ev;
|
return ev;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mouse Button moved
|
// Mouse Button moved
|
||||||
|
Loading…
x
Reference in New Issue
Block a user