a lot of work on sliders
This commit is contained in:
parent
c0e9565bf6
commit
8d4b353e88
9 changed files with 200 additions and 166 deletions
|
|
@ -1,15 +1,28 @@
|
|||
module ugui;
|
||||
|
||||
import std::io;
|
||||
import std::math;
|
||||
|
||||
// slider element
|
||||
struct ElemSlider {
|
||||
Rect handle;
|
||||
}
|
||||
|
||||
const Elem SLIDER_DEFAULT = {
|
||||
.type = ETYPE_SLIDER,
|
||||
};
|
||||
|
||||
/* handle
|
||||
* +----+-----+---------------------+
|
||||
* | |#####| |
|
||||
* +----+-----+---------------------+
|
||||
*/
|
||||
fn ElemEvents! Ctx.slider_hor(&ctx, String label, Rect size)
|
||||
<*
|
||||
@require value != null
|
||||
*>
|
||||
fn ElemEvents! Ctx.slider_hor(&ctx, String label, Rect size, float* value)
|
||||
{
|
||||
Id id = label.hash();
|
||||
Id id = label.hash();
|
||||
|
||||
Elem *parent = ctx.get_parent()!;
|
||||
Elem *c_elem = ctx.get_elem(id)!;
|
||||
|
|
@ -17,29 +30,33 @@ fn ElemEvents! Ctx.slider_hor(&ctx, String label, Rect size)
|
|||
ctx.tree.add(id, ctx.active_div)!;
|
||||
|
||||
// 1. Fill the element fields
|
||||
c_elem.type = ETYPE_SLIDER;
|
||||
|
||||
// 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.bounds = ctx.position_element(parent, size, true);
|
||||
c_elem.slider.handle = Rect{
|
||||
.x = (short)(c_elem.bounds.x + (int)(c_elem.bounds.w*(1.0-0.25) * c_elem.slider.value)),
|
||||
.y = c_elem.bounds.y,
|
||||
.w = (short)(c_elem.bounds.w * 0.25),
|
||||
.h = c_elem.bounds.h,
|
||||
};
|
||||
if (c_elem.flags.is_new) {
|
||||
*c_elem = SLIDER_DEFAULT;
|
||||
} else if (c_elem.type != SLIDER_DEFAULT.type) {
|
||||
return UgError.WRONG_ELEMENT_TYPE?;
|
||||
}
|
||||
|
||||
// 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;
|
||||
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.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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// Draw the slider background and handle
|
||||
|
|
@ -63,7 +80,7 @@ fn ElemEvents! Ctx.slider_hor(&ctx, String label, Rect size)
|
|||
* | |
|
||||
* +-+
|
||||
*/
|
||||
fn ElemEvents! Ctx.slider_ver(&ctx, String label, Rect size)
|
||||
fn ElemEvents! Ctx.slider_ver(&ctx, String label, Rect size, float* value)
|
||||
{
|
||||
Id id = label.hash();
|
||||
|
||||
|
|
@ -73,29 +90,34 @@ fn ElemEvents! Ctx.slider_ver(&ctx, String label, Rect size)
|
|||
ctx.tree.add(id, ctx.active_div)!;
|
||||
|
||||
// 1. Fill the element fields
|
||||
c_elem.type = ETYPE_SLIDER;
|
||||
|
||||
// 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.bounds = ctx.position_element(parent, size, true);
|
||||
c_elem.slider.handle = Rect{
|
||||
.x = c_elem.bounds.x,
|
||||
.y = (short)(c_elem.bounds.y + (int)(c_elem.bounds.h*(1.0-0.25) * c_elem.slider.value)),
|
||||
.w = c_elem.bounds.w,
|
||||
.h = (short)(c_elem.bounds.h * 0.25),
|
||||
};
|
||||
if (c_elem.flags.is_new) {
|
||||
*c_elem = SLIDER_DEFAULT;
|
||||
} else if (c_elem.type != SLIDER_DEFAULT.type) {
|
||||
return UgError.WRONG_ELEMENT_TYPE?;
|
||||
}
|
||||
|
||||
// 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;
|
||||
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.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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// Draw the slider background and handle
|
||||
|
|
@ -106,3 +128,6 @@ fn ElemEvents! Ctx.slider_ver(&ctx, String label, Rect size)
|
|||
|
||||
return c_elem.events;
|
||||
}
|
||||
|
||||
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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue