renamed elem.rect to elem.bounds

c3
Alessandro Mauri 3 weeks ago
parent 1cad13e597
commit 8bc38452b3
  1. 4
      src/ugui_button.c3
  2. 2
      src/ugui_data.c3
  3. 18
      src/ugui_impl.c3
  4. 16
      src/ugui_layout.c3
  5. 32
      src/ugui_slider.c3

@ -18,7 +18,7 @@ fn ElemEvents! Ctx.button(&ctx, String label, Rect size)
// if the element is new or the parent was updated then redo layout
if (c_elem.flags.is_new || parent.flags.updated) {
// 2. Layout
c_elem.rect = ctx.position_element(parent, size, true);
c_elem.bounds = ctx.position_element(parent, size, true);
// TODO: 3. Fill the button specific fields
}
@ -35,7 +35,7 @@ fn ElemEvents! Ctx.button(&ctx, String label, Rect size)
Cmd cmd = {
.type = CMD_RECT,
.rect = {
.rect = c_elem.rect,
.rect = c_elem.bounds,
.color = bg_color,
},
};

@ -67,7 +67,7 @@ struct Elem {
Id id;
ElemFlags flags;
ElemEvents events;
Rect rect;
Rect bounds;
ElemType type;
union {
Div div;

@ -74,9 +74,9 @@ fn void! Ctx.frame_begin(&ctx)
if (c_elem.flags.is_new || c_elem.flags.updated) {
Elem def_root = {
.id = ROOT_ID,
.type = ETYPE_DIV,
.rect = {
.id = ROOT_ID,
.type = ETYPE_DIV,
.bounds = {
.w = ctx.width,
.h = ctx.height,
},
@ -135,20 +135,20 @@ fn void! Ctx.div_begin(&ctx, String label, Rect size)
// do layout and update flags only if the element was updated
if (c_elem.flags.is_new || parent.flags.updated) {
// 2. layout the element
c_elem.rect = ctx.position_element(parent, size);
c_elem.bounds = ctx.position_element(parent, size);
// 3. Mark the element as updated
c_elem.flags.updated = true;
// 4. Fill the div fields
c_elem.div.layout = parent.div.layout;
c_elem.div.origin_c = Point{
.x = c_elem.rect.x,
.y = c_elem.rect.y,
.x = c_elem.bounds.x,
.y = c_elem.bounds.y,
};
c_elem.div.color_bg = uint_to_rgba(0xff0000ff);
c_elem.div.origin_r = c_elem.div.origin_c;
} else if (parent.flags.has_focus) {
if (point_in_rect(ctx.input.mouse.pos, c_elem.rect)) {
if (point_in_rect(ctx.input.mouse.pos, c_elem.bounds)) {
c_elem.flags.has_focus = true;
}
} else {
@ -161,7 +161,7 @@ fn void! Ctx.div_begin(&ctx, String label, Rect size)
Cmd cmd = {
.type = CMD_RECT,
.rect = {
.rect = c_elem.rect,
.rect = c_elem.bounds,
.color = c_elem.div.color_bg,
},
};
@ -179,5 +179,5 @@ fn void! Ctx.div_end(&ctx)
**/
fn bool Ctx.is_hovered(&ctx, Elem *elem)
{
return point_in_rect(ctx.input.mouse.pos, elem.rect);
return point_in_rect(ctx.input.mouse.pos, elem.bounds);
}

@ -51,7 +51,7 @@ fn void! Ctx.layout_next_row(&ctx)
}
parent.div.origin_r = Point{
.x = parent.rect.x,
.x = parent.bounds.x,
.y = parent.div.origin_c.y,
};
parent.div.origin_c = parent.div.origin_r;
@ -69,7 +69,7 @@ fn void! Ctx.layout_next_column(&ctx)
parent.div.origin_c = Point{
.x = parent.div.origin_r.x,
.y = parent.rect.y,
.y = parent.bounds.y,
};
parent.div.origin_r = parent.div.origin_c;
}
@ -98,8 +98,8 @@ fn Rect Ctx.position_element(&ctx, Elem *parent, Rect rect, bool style = false)
// 3. Calculate width & height
// TODO: what about negative values?
// FIXME: account for origin offset!!
elem_rect.w = rect.w > 0 ? rect.w : parent.rect.w;
elem_rect.h = rect.h > 0 ? rect.h : parent.rect.h;
elem_rect.w = rect.w > 0 ? rect.w : parent.bounds.w;
elem_rect.h = rect.h > 0 ? rect.h : parent.bounds.h;
// 4. Update the origins of the parent
parent.div.origin_r = Point{
@ -139,10 +139,10 @@ fn Rect Ctx.position_element(&ctx, Elem *parent, Rect rect, bool style = false)
/*
printf(
"positioning rect: %lx {%d %d %d %d}(%d %d %d %d) . {%d %d %d
%d}\n", parent.id, rect.x, rect.y, rect.w, rect.h, parent.rect.x,
parent.rect.y,
parent.rect.w,
parent.rect.h,
%d}\n", parent.id, rect.x, rect.y, rect.w, rect.h, parent.bounds.x,
parent.bounds.y,
parent.bounds.w,
parent.bounds.h,
elem_rect.x,
elem_rect.y,
elem_rect.w,

@ -20,20 +20,20 @@ fn ElemEvents! Ctx.slider_hor(&ctx, String label, Rect size)
// if the element is new or the parent was updated then redo layout
if (c_elem.flags.is_new || parent.flags.updated) {
// 2. Layout
c_elem.rect = ctx.position_element(parent, size, true);
c_elem.bounds = ctx.position_element(parent, size, true);
c_elem.slider.handle = Rect{
.x = (short)(c_elem.rect.x + (int)(c_elem.rect.w * c_elem.slider.value)),
.y = c_elem.rect.y,
.w = (short)(c_elem.rect.w * 0.25),
.h = c_elem.rect.h,
.x = (short)(c_elem.bounds.x + (int)(c_elem.bounds.w * c_elem.slider.value)),
.y = c_elem.bounds.y,
.w = (short)(c_elem.bounds.w * 0.25),
.h = c_elem.bounds.h,
};
}
c_elem.events = ctx.get_elem_events(c_elem);
if (parent.flags.has_focus && c_elem.events.mouse_hover) {
if (point_in_rect(ctx.input.mouse.pos, c_elem.slider.handle) && c_elem.events.mouse_hold) {
short x = (short)clamp(ctx.input.mouse.pos.x - c_elem.slider.handle.w/2, c_elem.rect.x, c_elem.rect.x + c_elem.rect.w - c_elem.slider.handle.w);
float v = (float)(c_elem.slider.handle.x-c_elem.rect.x) / (float)(c_elem.rect.w-c_elem.slider.handle.w);
short x = (short)clamp(ctx.input.mouse.pos.x - c_elem.slider.handle.w/2, c_elem.bounds.x, c_elem.bounds.x + c_elem.bounds.w - c_elem.slider.handle.w);
float v = (float)(c_elem.slider.handle.x-c_elem.bounds.x) / (float)(c_elem.bounds.w-c_elem.slider.handle.w);
c_elem.slider.handle.x = x;
c_elem.slider.value = v;
c_elem.events.update = true;
@ -47,7 +47,7 @@ fn ElemEvents! Ctx.slider_hor(&ctx, String label, Rect size)
Cmd cmd = {
.type = CMD_RECT,
.rect = {
.rect = c_elem.rect,
.rect = c_elem.bounds,
.color = bg_color,
},
};
@ -92,20 +92,20 @@ fn ElemEvents! Ctx.slider_ver(&ctx, String label, Rect size)
// if the element is new or the parent was updated then redo layout
if (c_elem.flags.is_new || parent.flags.updated) {
// 2. Layout
c_elem.rect = ctx.position_element(parent, size, true);
c_elem.bounds = ctx.position_element(parent, size, true);
c_elem.slider.handle = Rect{
.x = c_elem.rect.x,
.y = (short)(c_elem.rect.y + (int)(c_elem.rect.h * c_elem.slider.value)),
.w = c_elem.rect.w,
.h = (short)(c_elem.rect.h * 0.25),
.x = c_elem.bounds.x,
.y = (short)(c_elem.bounds.y + (int)(c_elem.bounds.h * c_elem.slider.value)),
.w = c_elem.bounds.w,
.h = (short)(c_elem.bounds.h * 0.25),
};
}
c_elem.events = ctx.get_elem_events(c_elem);
if (parent.flags.has_focus && c_elem.events.mouse_hover) {
if (point_in_rect(ctx.input.mouse.pos, c_elem.slider.handle) && c_elem.events.mouse_hold) {
short y = (short)clamp(ctx.input.mouse.pos.y - c_elem.slider.handle.h/2, c_elem.rect.y, c_elem.rect.y + c_elem.rect.h - c_elem.slider.handle.h);
float v = (float)(c_elem.slider.handle.y-c_elem.rect.y) / (float)(c_elem.rect.h-c_elem.slider.handle.h);
short y = (short)clamp(ctx.input.mouse.pos.y - c_elem.slider.handle.h/2, c_elem.bounds.y, c_elem.bounds.y + c_elem.bounds.h - c_elem.slider.handle.h);
float v = (float)(c_elem.slider.handle.y-c_elem.bounds.y) / (float)(c_elem.bounds.h-c_elem.slider.handle.h);
c_elem.slider.handle.y = y;
c_elem.slider.value = v;
c_elem.events.update = true;
@ -119,7 +119,7 @@ fn ElemEvents! Ctx.slider_ver(&ctx, String label, Rect size)
Cmd cmd = {
.type = CMD_RECT,
.rect = {
.rect = c_elem.rect,
.rect = c_elem.bounds,
.color = bg_color,
},
};

Loading…
Cancel
Save