fix scroller inf divide, safe ratio, safe cast

This commit is contained in:
Krzosa Karol
2025-01-28 13:35:45 +01:00
parent 6f3ffc76ff
commit 18bcbbef8d
7 changed files with 103 additions and 13 deletions

View File

@@ -163,11 +163,23 @@ fn r2f32_t ui_next_rect(ui_lop_t op, r2f32_t *rect, v2f32_t required_size) {
return (r2f32_t){0};
}
fn void ui_offset_box(ui_box_t *box, v2f32_t offset) {
fn void ui_offset_one_box(ui_box_t *box, v2f32_t offset) {
box->full_rect = r2f32_sub_v2f32(box->full_rect, offset);
box->rect = r2f32_sub_v2f32(box->rect, offset);
}
fn void ui_offset_children(ui_box_t *box, v2f32_t offset) {
for (ui_box_t *it = box->first; it; it = it->next) {
ui_offset_one_box(it, offset);
ui_offset_children(it, offset);
}
}
fn void ui_offset_box(ui_box_t *box, v2f32_t offset) {
ui_offset_one_box(box, offset);
ui_offset_children(box, offset);
}
fn ui_box_t *ui_build_box_from_id(ui_code_loc_t loc, ui_box_flags_t flags, ui_id_t id) {
ui_box_t *box = ui_find_box(id);
if (box) {
@@ -1052,7 +1064,7 @@ fn void ui_demo_update(app_frame_t *frame, mt_tweak_t *tweak_table, i32 tweak_co
ui_signal_t upper_box_signal = ui_signal_from_box(upper_box);
ui_signal_t down_box_signal = ui_signal_from_box(down_box);
f32 drag = ev->mouse_delta.y;
f32 coef = (all_items_size / item_box_size) / scrollable_space;
f32 coef = f32_safe_ratio0(all_items_size / item_box_size, scrollable_space);
if (signal.dragging) {
scroller_value += drag * coef;
}
@@ -1063,6 +1075,7 @@ fn void ui_demo_update(app_frame_t *frame, mt_tweak_t *tweak_table, i32 tweak_co
if (ev->kind == app_event_kind_mouse_wheel) {
scroller_value -= ev->mouse_wheel_delta.y;
}
scroller_value = CLAMP(scroller_value, 0, all_items_size);
}
@@ -1090,7 +1103,7 @@ fn void ui_demo_update(app_frame_t *frame, mt_tweak_t *tweak_table, i32 tweak_co
ui_signal_t signal = ui_signal_from_box(slider_box);
ui_signal_t right_box_sig = ui_signal_from_box(down_box);
f32 coef = (max_size / scroller_rect_pixels) / scrollable_space;
f32 coef = f32_safe_ratio0(max_size / scroller_rect_pixels, scrollable_space);
if (signal.dragging) {
bottom_scroller_value += ev->mouse_delta.x * coef;
@@ -1106,9 +1119,7 @@ fn void ui_demo_update(app_frame_t *frame, mt_tweak_t *tweak_table, i32 tweak_co
bottom_scroller_value = CLAMP(bottom_scroller_value, 0, max_size);
}
for (ui_box_t *it = item_box->first; it; it = it->next) {
ui_offset_box(it, v2f32(bottom_scroller_value, scroller_value));
}
ui_offset_children(item_box, v2f32(bottom_scroller_value, scroller_value));
}
///////////////////////////////
@@ -1289,7 +1300,7 @@ fn void ui_demo_update(app_frame_t *frame, mt_tweak_t *tweak_table, i32 tweak_co
ui_signal_t upper_box_signal = ui_signal_from_box(upper_box);
ui_signal_t down_box_signal = ui_signal_from_box(down_box);
f32 drag = ev->mouse_delta.y;
f32 coef = (all_items_size / item_box_pixels) / scrollable_space;
f32 coef = f32_safe_ratio0((all_items_size / item_box_pixels), scrollable_space);
if (signal.dragging) {
right_scroller_value += drag * coef;
}
@@ -1326,7 +1337,7 @@ fn void ui_demo_update(app_frame_t *frame, mt_tweak_t *tweak_table, i32 tweak_co
ui_signal_t signal = ui_signal_from_box(slider_box);
ui_signal_t right_box_sig = ui_signal_from_box(down_box);
f32 coef = (max_size / scroller_rect_pixels) / scrollable_space;
f32 coef = f32_safe_ratio0((max_size / scroller_rect_pixels), scrollable_space);
if (signal.dragging) {
bottom_scroller_value += ev->mouse_delta.x * coef;
@@ -1343,9 +1354,7 @@ fn void ui_demo_update(app_frame_t *frame, mt_tweak_t *tweak_table, i32 tweak_co
}
for (ui_box_t *it = item_box->first; it; it = it->next) {
ui_offset_box(it, v2f32(bottom_scroller_value, right_scroller_value));
}
ui_offset_children(item_box, v2f32(bottom_scroller_value, right_scroller_value));
}
///////////////////////////////