From 1ac4f33736d5122f7dcb1a20433d22bd21378d77 Mon Sep 17 00:00:00 2001 From: ocornut Date: Sun, 15 Mar 2015 01:58:14 +0000 Subject: [PATCH] Added VSliderFloat() --- imgui.cpp | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ imgui.h | 1 + 2 files changed, 82 insertions(+) diff --git a/imgui.cpp b/imgui.cpp index 4a85f59f8..53b24a1a4 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -4947,6 +4947,56 @@ bool ImGui::SliderFloat(const char* label, float* v, float v_min, float v_max, c return value_changed; } +bool ImGui::VSliderFloat(const char* label, const ImVec2& size, float* v, float v_min, float v_max, const char* display_format, float power) +{ + ImGuiState& g = *GImGui; + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return false; + + const ImGuiStyle& style = g.Style; + const ImGuiID id = window->GetID(label); + + const ImVec2 label_size = CalcTextSize(label, NULL, true); + const ImGuiAabb frame_bb(window->DC.CursorPos, window->DC.CursorPos + size); + const ImGuiAabb slider_bb(frame_bb.Min + style.FramePadding, frame_bb.Max - style.FramePadding); + const ImGuiAabb bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0.0f)); + + ItemSize(bb); + if (!ItemAdd(frame_bb, &id)) + return false; + + const bool hovered = IsHovered(frame_bb, id); + if (hovered) + g.HoveredId = id; + + if (!display_format) + display_format = "%.3f"; + int decimal_precision = 3; + ParseFormat(display_format, decimal_precision); + + if (hovered && g.IO.MouseClicked[0]) + { + SetActiveId(id); + FocusWindow(window); + } + + // Actual slider behavior + render grab + bool value_changed = SliderBehavior(frame_bb, slider_bb, id, v, v_min, v_max, power, decimal_precision, false); + + // Display value using user-provided display format so user can add prefix/suffix/decorations to the value. + // For the vertical slider we allow centered text to overlap the frame padding + char value_buf[64]; + char* value_buf_end = value_buf + ImFormatString(value_buf, IM_ARRAYSIZE(value_buf), display_format, *v); + const ImVec2 value_text_size = CalcTextSize(value_buf, value_buf_end, true); + RenderTextClipped(ImVec2(ImMax(frame_bb.Min.x, slider_bb.GetCenter().x - value_text_size.x*0.5f), frame_bb.Min.y + style.FramePadding.y), value_buf, value_buf_end, &value_text_size, frame_bb.Max); + + if (label_size.x > 0.0f) + RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, slider_bb.Min.y), label); + + return value_changed; +} + bool ImGui::SliderAngle(const char* label, float* v, float v_degrees_min, float v_degrees_max) { float v_deg = *v * 360.0f / (2*PI); @@ -9012,6 +9062,37 @@ void ImGui::ShowTestWindow(bool* opened) //ImGui::PushItemWidth(-1); //ImGui::ListBox("##listbox2", &listbox_item_current2, listbox_items, IM_ARRAYSIZE(listbox_items), 4); //ImGui::PopItemWidth(); + + if (ImGui::TreeNode("Vertical Sliders")) + { + const int spacing = 4; + + ImGui::PushID("set1"); + static float values[4] = { 0.0f, 0.60f, 0.35f, 0.9f }; + for (size_t i = 0; i < IM_ARRAYSIZE(values); i++) + { + if (i > 0) ImGui::SameLine(0, spacing); + ImGui::PushID(i); + ImGui::VSliderFloat("##v", ImVec2(18,160), &values[i], 0.0f, 1.0f, ""); + ImGui::PopID(); + } + ImGui::PopID(); + + ImGui::SameLine(); + ImGui::PushID("set2"); + ImGui::PushStyleVar(ImGuiStyleVar_GrabMinSize, 40); + for (size_t i = 0; i < IM_ARRAYSIZE(values); i++) + { + if (i > 0) ImGui::SameLine(0, spacing); + ImGui::PushID(i); + ImGui::VSliderFloat("##v", ImVec2(40,160), &values[i], 0.0f, 1.0f, "%.2f"); + ImGui::PopID(); + } + ImGui::PopStyleVar(); + ImGui::PopID(); + + ImGui::TreePop(); + } } if (ImGui::CollapsingHeader("Graphs widgets")) diff --git a/imgui.h b/imgui.h index f3c51b6de..54b5a90de 100644 --- a/imgui.h +++ b/imgui.h @@ -283,6 +283,7 @@ namespace ImGui IMGUI_API bool SliderInt2(const char* label, int v[2], int v_min, int v_max, const char* display_format = "%.0f"); IMGUI_API bool SliderInt3(const char* label, int v[3], int v_min, int v_max, const char* display_format = "%.0f"); IMGUI_API bool SliderInt4(const char* label, int v[4], int v_min, int v_max, const char* display_format = "%.0f"); + IMGUI_API bool VSliderFloat(const char* label, const ImVec2& size, float* v, float v_min, float v_max, const char* display_format = "%.3f", float power = 1.0f); IMGUI_API void PlotLines(const char* label, const float* values, int values_count, int values_offset = 0, const char* overlay_text = NULL, float scale_min = FLT_MAX, float scale_max = FLT_MAX, ImVec2 graph_size = ImVec2(0,0), size_t stride = sizeof(float)); IMGUI_API void PlotLines(const char* label, float (*values_getter)(void* data, int idx), void* data, int values_count, int values_offset = 0, const char* overlay_text = NULL, float scale_min = FLT_MAX, float scale_max = FLT_MAX, ImVec2 graph_size = ImVec2(0,0)); IMGUI_API void PlotHistogram(const char* label, const float* values, int values_count, int values_offset = 0, const char* overlay_text = NULL, float scale_min = FLT_MAX, float scale_max = FLT_MAX, ImVec2 graph_size = ImVec2(0,0), size_t stride = sizeof(float));