Skip to content

Commit

Permalink
Slightly better number input
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumechereau committed Dec 30, 2023
1 parent 5e0ff5a commit cc84323
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,10 @@ bool gui_input_float(const char *label, float *v, float step,
float button_width = 20; // Compute exactly.
const char *left_utf = "";
const char *right_utf = "";
float v_speed = step / 10;
bool show_arrows;
bool is_active = false;
ImGuiStorage *storage = ImGui::GetStateStorage();

if (minv == 0.f && maxv == 0.f) {
minv = -FLT_MAX;
Expand All @@ -879,8 +883,7 @@ bool gui_input_float(const char *label, float *v, float step,
label_aligned(label, LABEL_SIZE);

ImGuiID key = ImGui::GetID("show_arrows");
ImGuiStorage *storage = ImGui::GetStateStorage();
bool show_arrows = storage->GetBool(key, false);
show_arrows = storage->GetBool(key, false);

if (show_arrows) {
if (ImGui::Button(left_utf)) {
Expand All @@ -890,7 +893,8 @@ bool gui_input_float(const char *label, float *v, float step,
ImGui::SameLine();
ImGui::PushItemWidth(
ImGui::GetContentRegionAvail().x - button_width);
ret = ImGui::DragFloat("", v, step, minv, maxv, format) || ret;
ret = ImGui::DragFloat("", v, v_speed, minv, maxv, format) || ret;
is_active = ImGui::IsItemActive();
ImGui::PopItemWidth();
ImGui::SameLine();
if (ImGui::Button(right_utf)) {
Expand All @@ -900,12 +904,13 @@ bool gui_input_float(const char *label, float *v, float step,
} else {
ImGui::SetNextItemWidth(-1);
ret = ImGui::DragFloat("", v, step, minv, maxv, format);
is_active = ImGui::IsItemActive();
}

ImGui::PopButtonRepeat();
ImGui::PopStyleVar(1);
ImGui::EndGroup();
storage->SetBool(key, ret || ImGui::IsItemHovered());
storage->SetBool(key, ret || is_active || ImGui::IsItemHovered());
ImGui::PopID();

// ret = ImGui::InputFloat(label, v, step, 0.0, format);
Expand Down

0 comments on commit cc84323

Please sign in to comment.