ugui.c3l/src/ugui_slider.c3

138 lines
3.4 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;
}
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,
float hpercent = 0.25,
Color bgcolor = uint_to_rgba(0x0000ffff),
Color handlecolor = uint_to_rgba(0x0ff000ff))
2024-10-31 13:04:30 +01:00
{
Id id = ctx.gen_id(label)!;
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.type = ETYPE_SLIDER;
} else if (c_elem.type != ETYPE_SLIDER) {
2024-12-18 14:58:40 +01:00
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 * hpercent);
2024-12-18 14:58:40 +01:00
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
ctx.push_rect(c_elem.bounds, bgcolor)!;
ctx.push_rect(c_elem.slider.handle, handlecolor)!;
2024-10-31 13:04:30 +01:00
return c_elem.events;
}
2024-11-02 09:41:37 +01:00
/*
* +-+
* | |
* | |
* +-+
* |#| handle
* |#|
* +-+
* | |
* | |
* +-+
*/
fn ElemEvents! Ctx.slider_ver(&ctx,
String label,
Rect size,
float* value,
float hpercent = 0.25,
Color bgcolor = uint_to_rgba(0x0000ffff),
Color handlecolor = uint_to_rgba(0x0ff000ff))
2024-11-02 09:41:37 +01:00
{
Id id = ctx.gen_id(label)!;
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.type = ETYPE_SLIDER;
} else if (c_elem.type != ETYPE_SLIDER) {
2024-12-18 14:58:40 +01:00
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 * hpercent);
2024-12-18 14:58:40 +01:00
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
ctx.push_rect(c_elem.bounds, bgcolor)!;
ctx.push_rect(c_elem.slider.handle, handlecolor)!;
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);