From d76bc3434e4bb5b22eed5807e5b313291e6189d5 Mon Sep 17 00:00:00 2001 From: ocornut Date: Sat, 4 Jul 2015 12:17:04 -0600 Subject: [PATCH] Added DragFloatRange2() DragIntRange2() helpers (#76) --- imgui.cpp | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ imgui.h | 2 ++ 2 files changed, 67 insertions(+) diff --git a/imgui.cpp b/imgui.cpp index 9cb91d86..2c3458b7 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -719,8 +719,10 @@ void ImGuiIO::AddInputCharacter(ImWchar c) const float IM_PI = 3.14159265358979323846f; #ifdef INT_MAX +#define IM_INT_MIN INT_MIN #define IM_INT_MAX INT_MAX #else +#define IM_INT_MIN (-2147483647-1) #define IM_INT_MAX (2147483647) #endif @@ -6406,6 +6408,31 @@ bool ImGui::DragFloat4(const char* label, float v[2], float v_speed, float v_min return DragFloatN(label, v, 4, v_speed, v_min, v_max, display_format, power); } +bool ImGui::DragFloatRange2(const char* label, float* v_current_min, float* v_current_max, float v_speed, float v_min, float v_max, const char* display_format, const char* display_format_max, float power) +{ + ImGuiState& g = *GImGui; + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return false; + + ImGui::PushID(label); + ImGui::BeginGroup(); + PushMultiItemsWidths(2); + + bool value_changed = ImGui::DragFloat("##min", v_current_min, v_speed, (v_min >= v_max) ? -FLT_MAX : v_min, (v_min >= v_max) ? *v_current_max : ImMin(v_max, *v_current_max), display_format, power); + ImGui::PopItemWidth(); + ImGui::SameLine(0, (int)g.Style.ItemInnerSpacing.x); + value_changed |= ImGui::DragFloat("##max", v_current_max, v_speed, (v_min >= v_max) ? *v_current_min : ImMax(v_min, *v_current_min), (v_min >= v_max) ? FLT_MAX : v_max, display_format_max ? display_format_max : display_format, power); + ImGui::PopItemWidth(); + ImGui::SameLine(0, (int)g.Style.ItemInnerSpacing.x); + + ImGui::TextUnformatted(label, FindTextDisplayEnd(label)); + ImGui::EndGroup(); + ImGui::PopID(); + + return value_changed; +} + // NB: v_speed is float to allow adjusting the drag speed with more precision bool ImGui::DragInt(const char* label, int* v, float v_speed, int v_min, int v_max, const char* display_format) { @@ -6459,6 +6486,31 @@ bool ImGui::DragInt4(const char* label, int v[4], float v_speed, int v_min, int return DragIntN(label, v, 4, v_speed, v_min, v_max, display_format); } +bool ImGui::DragIntRange2(const char* label, int* v_current_min, int* v_current_max, float v_speed, int v_min, int v_max, const char* display_format, const char* display_format_max) +{ + ImGuiState& g = *GImGui; + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return false; + + ImGui::PushID(label); + ImGui::BeginGroup(); + PushMultiItemsWidths(2); + + bool value_changed = ImGui::DragInt("##min", v_current_min, v_speed, (v_min >= v_max) ? IM_INT_MIN : v_min, (v_min >= v_max) ? *v_current_max : ImMin(v_max, *v_current_max), display_format); + ImGui::PopItemWidth(); + ImGui::SameLine(0, (int)g.Style.ItemInnerSpacing.x); + value_changed |= ImGui::DragInt("##max", v_current_max, v_speed, (v_min >= v_max) ? *v_current_min : ImMax(v_min, *v_current_min), (v_min >= v_max) ? IM_INT_MAX : v_max, display_format_max ? display_format_max : display_format); + ImGui::PopItemWidth(); + ImGui::SameLine(0, (int)g.Style.ItemInnerSpacing.x); + + ImGui::TextUnformatted(label, FindTextDisplayEnd(label)); + ImGui::EndGroup(); + ImGui::PopID(); + + return value_changed; +} + enum ImGuiPlotType { ImGuiPlotType_Lines, @@ -11171,6 +11223,19 @@ void ImGui::ShowTestWindow(bool* opened) //ImGui::ListBox("##listbox2", &listbox_item_current2, listbox_items, IM_ARRAYSIZE(listbox_items), 4); //ImGui::PopItemWidth(); + if (ImGui::TreeNode("Ranges")) + { + ImGui::Unindent(); + + static float begin = 10, end = 90; + static int begin_i = 100, end_i = 1000; + ImGui::DragFloatRange2("range", &begin, &end, 0.25f, 0.0f, 100.0f, "Min: %.1f %%", "Max: %.1f %%"); + ImGui::DragIntRange2("range int (no bounds)", &begin_i, &end_i, 5, 0, 0, "Min: %.0f units", "Max: %.0f units"); + + ImGui::Indent(); + ImGui::TreePop(); + } + if (ImGui::TreeNode("Multi-component Widgets")) { ImGui::Unindent(); diff --git a/imgui.h b/imgui.h index 1f6ae128..46fc13b5 100644 --- a/imgui.h +++ b/imgui.h @@ -250,10 +250,12 @@ namespace ImGui IMGUI_API bool DragFloat2(const char* label, float v[2], float v_speed = 1.0f, float v_min = 0.0f, float v_max = 0.0f, const char* display_format = "%.3f", float power = 1.0f); IMGUI_API bool DragFloat3(const char* label, float v[3], float v_speed = 1.0f, float v_min = 0.0f, float v_max = 0.0f, const char* display_format = "%.3f", float power = 1.0f); IMGUI_API bool DragFloat4(const char* label, float v[4], float v_speed = 1.0f, float v_min = 0.0f, float v_max = 0.0f, const char* display_format = "%.3f", float power = 1.0f); + IMGUI_API bool DragFloatRange2(const char* label, float* v_current_min, float* v_current_max, float v_speed = 1.0f, float v_min = 0.0f, float v_max = 0.0f, const char* display_format = "%.3f", const char* display_format_max = NULL, float power = 1.0f); IMGUI_API bool DragInt(const char* label, int* v, float v_speed = 1.0f, int v_min = 0, int v_max = 0, const char* display_format = "%.0f"); // If v_min >= v_max we have no bound IMGUI_API bool DragInt2(const char* label, int v[2], float v_speed = 1.0f, int v_min = 0, int v_max = 0, const char* display_format = "%.0f"); IMGUI_API bool DragInt3(const char* label, int v[3], float v_speed = 1.0f, int v_min = 0, int v_max = 0, const char* display_format = "%.0f"); IMGUI_API bool DragInt4(const char* label, int v[4], float v_speed = 1.0f, int v_min = 0, int v_max = 0, const char* display_format = "%.0f"); + IMGUI_API bool DragIntRange2(const char* label, int* v_current_min, int* v_current_max, float v_speed = 1.0f, int v_min = 0, int v_max = 0, const char* display_format = "%.0f", const char* display_format_max = NULL); // Widgets: Input IMGUI_API bool InputText(const char* label, char* buf, size_t buf_size, ImGuiInputTextFlags flags = 0, ImGuiTextEditCallback callback = NULL, void* user_data = NULL);