ugui.c3l/src/ugui_slider.c3

135 lines
3.1 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()!;
2024-12-19 15:24:39 +01:00
Elem *elem = ctx.get_elem(id)!;
2024-10-31 13:04:30 +01:00
// add it to the tree
ctx.tree.add(id, ctx.active_div)!;
// 1. Fill the element fields
2024-12-19 15:24:39 +01:00
if (elem.flags.is_new) {
elem.type = ETYPE_SLIDER;
} else if (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
2024-12-19 15:24:39 +01:00
elem.bounds = ctx.position_element(parent, size, true);
2024-12-18 14:58:40 +01:00
// handle width
2024-12-19 15:24:39 +01:00
short hw = (short)(elem.bounds.w * hpercent);
2024-12-18 14:58:40 +01:00
Rect handle = {
2024-12-19 15:24:39 +01:00
.x = calc_slider(elem.bounds.x, elem.bounds.w-hw, *value),
.y = elem.bounds.y,
2024-12-18 14:58:40 +01:00
.w = hw,
2024-12-19 15:24:39 +01:00
.h = elem.bounds.h,
2024-12-18 14:58:40 +01:00
};
2024-12-19 15:24:39 +01:00
elem.slider.handle = handle;
2024-12-18 14:58:40 +01:00
Point m = ctx.input.mouse.pos;
2024-12-19 15:24:39 +01:00
elem.events = ctx.get_elem_events(elem);
2024-12-18 14:58:40 +01:00
2024-12-20 02:17:41 +01:00
if (ctx.elem_focus(elem) && ctx.is_mouse_down(BTN_LEFT)) {
2024-12-19 15:24:39 +01:00
*value = calc_value(elem.bounds.x, m.x, elem.bounds.w, hw);
elem.slider.handle.x = calc_slider(elem.bounds.x, elem.bounds.w-hw, *value);
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-12-19 15:24:39 +01:00
ctx.push_rect(elem.bounds, bgcolor)!;
ctx.push_rect(elem.slider.handle, handlecolor)!;
2024-10-31 13:04:30 +01:00
2024-12-19 15:24:39 +01:00
return elem.events;
2024-10-31 13:04:30 +01:00
}
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()!;
2024-12-19 15:24:39 +01:00
Elem *elem = ctx.get_elem(id)!;
2024-11-02 09:41:37 +01:00
// add it to the tree
ctx.tree.add(id, ctx.active_div)!;
// 1. Fill the element fields
2024-12-19 15:24:39 +01:00
if (elem.flags.is_new) {
elem.type = ETYPE_SLIDER;
} else if (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
2024-12-19 15:24:39 +01:00
elem.bounds = ctx.position_element(parent, size, true);
2024-12-18 14:58:40 +01:00
// handle height
2024-12-19 15:24:39 +01:00
short hh = (short)(elem.bounds.h * hpercent);
2024-12-18 14:58:40 +01:00
Rect handle = {
2024-12-19 15:24:39 +01:00
.x = elem.bounds.x,
.y = calc_slider(elem.bounds.y, elem.bounds.h-hh, *value),
.w = elem.bounds.w,
2024-12-18 14:58:40 +01:00
.h = hh,
};
2024-12-19 15:24:39 +01:00
elem.slider.handle = handle;
2024-12-18 14:58:40 +01:00
Point m = ctx.input.mouse.pos;
2024-12-19 15:24:39 +01:00
elem.events = ctx.get_elem_events(elem);
2024-12-20 01:45:10 +01:00
2024-12-20 02:17:41 +01:00
if (ctx.elem_focus(elem) && ctx.is_mouse_down(BTN_LEFT)) {
2024-12-19 15:24:39 +01:00
*value = calc_value(elem.bounds.y, m.y, elem.bounds.h, hh);
elem.slider.handle.y = calc_slider(elem.bounds.y, elem.bounds.h-hh, *value);
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-12-19 15:24:39 +01:00
ctx.push_rect(elem.bounds, bgcolor)!;
ctx.push_rect(elem.slider.handle, handlecolor)!;
2024-11-02 09:41:37 +01:00
2024-12-19 15:24:39 +01:00
return elem.events;
2024-11-02 09:41:37 +01:00
}
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);