Skip to content

Commit

Permalink
Improve float inputs with 0.5 steps
Browse files Browse the repository at this point in the history
Make sure that the float inputs value are rounded to the closest step,
even if we drag.

Since imgui doesn't support that out of the box I replaced the DragFloat
with DragInt with a custom format.  I hope this is correct.
  • Loading branch information
guillaumechereau committed Jul 10, 2024
1 parent cbc83c9 commit ad146ad
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
37 changes: 25 additions & 12 deletions src/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -839,10 +839,6 @@ bool gui_input_int(const char *label, int *v, int minv, int maxv)
float maxvf = maxv;
bool ret;
float vf = *v;
if (minv == 0 && maxv == 0) {
minvf = -FLT_MAX;
maxvf = +FLT_MAX;
}
ret = gui_input_float(label, &vf, 1, minvf, maxvf, "%.0f");
if (ret) *v = vf;
return ret;
Expand Down Expand Up @@ -945,21 +941,30 @@ bool gui_input_float(const char *label, float *v, float step,
const ImVec2 button_size = ImVec2(
GUI_ITEM_HEIGHT,
ImGui::GetFontSize() + style.FramePadding.y * 2.0f);
int v_int;
char v_label[128];

gui->item_activated = false;
gui->item_deactivated = false;
if (minv == 0.f && maxv == 0.f) {
minv = -FLT_MAX;
maxv = +FLT_MAX;

// To remove: use 0, 0 instead.
if (minv == -FLT_MAX && maxv == +FLT_MAX) {
assert(false);
minv = 0;
maxv = 0;
}

if (step == 0.f) step = 0.1f;
if (!format) format = "%.1f";

unbounded = (minv == -FLT_MAX || maxv == +FLT_MAX);
// Because imgui doesn't round to the step, we use DragInt with a
// custom label to make it work properly.
v_int = round(*v / step);
snprintf(v_label, sizeof(v_label), format, v_int * step);
unbounded = (minv == 0 && maxv == 0);

ImGui::PushID(label);


ImGui::PushStyleColor(ImGuiCol_FrameBg, COLOR(NUMBER_INPUT, INNER, false));
ImGui::PushStyleColor(ImGuiCol_FrameBgHovered,
color_lighten(COLOR(NUMBER_INPUT, INNER, false)));
Expand Down Expand Up @@ -993,7 +998,10 @@ bool gui_input_float(const char *label, float *v, float step,
ImGui::SameLine();
ImGui::PushItemWidth(
ImGui::GetContentRegionAvail().x - button_size.x - 4);
ret = ImGui::DragFloat("", v, v_speed, minv, maxv, format) || ret;
if (ImGui::DragInt("", &v_int, v_speed, minv, maxv, v_label)) {
*v = v_int * step;
ret = true;
}
update_activation_state();
is_active = ImGui::IsItemActive();
ImGui::PopItemWidth();
Expand All @@ -1006,7 +1014,10 @@ bool gui_input_float(const char *label, float *v, float step,
} else {
ImGui::SetNextItemWidth(-1);
if (unbounded) {
ret = ImGui::DragFloat("", v, step, minv, maxv, format);
if (ImGui::DragInt("", &v_int, step, minv, maxv, v_label)) {
*v = v_int * step;
ret = true;
}
} else {
ret = slider_float(v, minv, maxv, format);
}
Expand All @@ -1024,7 +1035,9 @@ bool gui_input_float(const char *label, float *v, float step,
ImGui::PopID();

if (ret) {
*v = clamp(*v, minv, maxv);
if (minv != 0 || maxv != 0) {
*v = clamp(*v, minv, maxv);
}
on_click();
}
return ret;
Expand Down
4 changes: 2 additions & 2 deletions src/gui/symmetry_panel.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ void gui_symmetry_panel(void)
gui_row_end();
gui_group_end();
for (i = 0; i < 3; i++) {
gui_input_float(labels_l[i], &goxel.painter.symmetry_origin[i],
0.5, -FLT_MAX, +FLT_MAX, "%.1f");
gui_input_float(labels_l[i], &goxel.painter.symmetry_origin[i], 0.5, 0,
0, "%.1f");
}

if (gui_section_begin(_(RECENTER), GUI_SECTION_COLLAPSABLE_CLOSED)) {
Expand Down

0 comments on commit ad146ad

Please sign in to comment.