ugui.c3l/src/ugui_slider.c3

134 lines
3.3 KiB
Plaintext
Raw Normal View History

2024-10-31 13:04:30 +01:00
module ugui;
2024-11-17 21:36:46 +01:00
import std::io;
2024-12-18 14:58:40 +01:00
import std::math;
// slider element
struct ElemSlider {
Rect handle;
}
const Elem SLIDER_DEFAULT = {
.type = ETYPE_SLIDER,
};
2024-11-17 21:36:46 +01:00
2024-11-02 09:41:37 +01:00
/* handle
* +----+-----+---------------------+
* | |#####| |
* +----+-----+---------------------+
*/
2024-12-18 14:58:40 +01:00
<*
@require value != null
*>
fn ElemEvents! Ctx.slider_hor(&ctx, String label, Rect size, float* value)
2024-10-31 13:04:30 +01:00
{
2024-12-18 14:58:40 +01:00
Id id = label.hash();
2024-10-31 13:04:30 +01:00
Elem *parent = ctx.get_parent()!;
Elem *c_elem = ctx.get_elem(id)!;
// add it to the tree
ctx.tree.add(id, ctx.active_div)!;
// 1. Fill the element fields
2024-12-18 14:58:40 +01:00
if (c_elem.flags.is_new) {
*c_elem = SLIDER_DEFAULT;
} else if (c_elem.type != SLIDER_DEFAULT.type) {
return UgError.WRONG_ELEMENT_TYPE?;
2024-10-31 13:04:30 +01:00
}
2024-12-18 14:58:40 +01:00
// 2. Layout
c_elem.bounds = ctx.position_element(parent, size, true);
// handle width
short hw = (short)(c_elem.bounds.w * 0.25);
Rect handle = {
.x = calc_slider(c_elem.bounds.x, c_elem.bounds.w-hw, *value),
.y = c_elem.bounds.y,
.w = hw,
.h = c_elem.bounds.h,
};
c_elem.slider.handle = handle;
Point m = ctx.input.mouse.pos;
2024-10-31 13:04:30 +01:00
c_elem.events = ctx.get_elem_events(c_elem);
2024-12-18 14:58:40 +01:00
if (parent.flags.has_focus && c_elem.events.mouse_hover &&
point_in_rect(m, handle) && c_elem.events.mouse_hold) {
*value = calc_value(c_elem.bounds.x, m.x, c_elem.bounds.w, hw);
c_elem.slider.handle.x = calc_slider(c_elem.bounds.x, c_elem.bounds.w-hw, *value);
c_elem.events.update = true;
2024-10-31 13:04:30 +01:00
}
2024-12-13 13:51:23 +01:00
// Draw the slider background and handle
2024-10-31 13:04:30 +01:00
Color bg_color = uint_to_rgba(0x0000ffff);
Color handle_color = uint_to_rgba(0x0ff000ff);
2024-12-13 13:51:23 +01:00
ctx.push_rect(c_elem.bounds, bg_color)!;
ctx.push_rect(c_elem.slider.handle, handle_color)!;
2024-10-31 13:04:30 +01:00
return c_elem.events;
}
2024-11-02 09:41:37 +01:00
/*
* +-+
* | |
* | |
* +-+
* |#| handle
* |#|
* +-+
* | |
* | |
* +-+
*/
2024-12-18 14:58:40 +01:00
fn ElemEvents! Ctx.slider_ver(&ctx, String label, Rect size, float* value)
2024-11-02 09:41:37 +01:00
{
Id id = label.hash();
2024-11-02 09:41:37 +01:00
Elem *parent = ctx.get_parent()!;
Elem *c_elem = ctx.get_elem(id)!;
// add it to the tree
ctx.tree.add(id, ctx.active_div)!;
// 1. Fill the element fields
2024-12-18 14:58:40 +01:00
if (c_elem.flags.is_new) {
*c_elem = SLIDER_DEFAULT;
} else if (c_elem.type != SLIDER_DEFAULT.type) {
return UgError.WRONG_ELEMENT_TYPE?;
2024-11-02 09:41:37 +01:00
}
2024-12-18 14:58:40 +01:00
// 2. Layout
c_elem.bounds = ctx.position_element(parent, size, true);
// handle height
short hh = (short)(c_elem.bounds.h * 0.25);
Rect handle = {
.x = c_elem.bounds.x,
.y = calc_slider(c_elem.bounds.y, c_elem.bounds.h-hh, *value),
.w = c_elem.bounds.w,
.h = hh,
};
c_elem.slider.handle = handle;
Point m = ctx.input.mouse.pos;
2024-11-02 09:41:37 +01:00
c_elem.events = ctx.get_elem_events(c_elem);
2024-12-18 14:58:40 +01:00
if (parent.flags.has_focus && c_elem.events.mouse_hover &&
point_in_rect(m, handle) &&
c_elem.events.mouse_hold) {
*value = calc_value(c_elem.bounds.y, m.y, c_elem.bounds.h, hh);
c_elem.slider.handle.y = calc_slider(c_elem.bounds.y, c_elem.bounds.h-hh, *value);
c_elem.events.update = true;
2024-11-02 09:41:37 +01:00
}
2024-12-13 14:42:15 +01:00
// Draw the slider background and handle
2024-11-02 09:41:37 +01:00
Color bg_color = uint_to_rgba(0x0000ffff);
Color handle_color = uint_to_rgba(0x0ff000ff);
2024-12-13 13:51:23 +01:00
ctx.push_rect(c_elem.bounds, bg_color)!;
ctx.push_rect(c_elem.slider.handle, handle_color)!;
2024-11-02 09:41:37 +01:00
return c_elem.events;
}
2024-12-18 14:58:40 +01:00
macro short calc_slider(ushort off, ushort dim, float value) => (short)off + (short)(dim * value);
macro float calc_value(ushort off, ushort mouse, ushort dim, ushort slider) => math::clamp((float)(mouse-off-slider/2)/(float)(dim-slider), 0.0f, 1.0f);