Use power!=1.0 for logarithmic sliders. Adjust display_format to decorate the value with a prefix or a suffix. "%.3f" 1.234 "%5.2f secs" 01.23 secs "Gold: %.0f" Gold: 1
| 8955 | // "%5.2f secs" 01.23 secs |
| 8956 | // "Gold: %.0f" Gold: 1 |
| 8957 | bool ImGui::SliderFloat(const char* label, float* v, float v_min, float v_max, const char* display_format, float power) |
| 8958 | { |
| 8959 | ImGuiWindow* window = GetCurrentWindow(); |
| 8960 | if (window->SkipItems) |
| 8961 | return false; |
| 8962 | |
| 8963 | ImGuiContext& g = *GImGui; |
| 8964 | const ImGuiStyle& style = g.Style; |
| 8965 | const ImGuiID id = window->GetID(label); |
| 8966 | const float w = CalcItemWidth(); |
| 8967 | |
| 8968 | const ImVec2 label_size = CalcTextSize(label, NULL, true); |
| 8969 | const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, label_size.y + style.FramePadding.y * 2.0f)); |
| 8970 | const ImRect total_bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0.0f)); |
| 8971 | |
| 8972 | // NB- we don't call ItemSize() yet because we may turn into a text edit box below |
| 8973 | if (!ItemAdd(total_bb, id, &frame_bb)) |
| 8974 | { |
| 8975 | ItemSize(total_bb, style.FramePadding.y); |
| 8976 | return false; |
| 8977 | } |
| 8978 | const bool hovered = ItemHoverable(frame_bb, id); |
| 8979 | |
| 8980 | if (!display_format) |
| 8981 | display_format = "%.3f"; |
| 8982 | int decimal_precision = ParseFormatPrecision(display_format, 3); |
| 8983 | |
| 8984 | // Tabbing or CTRL-clicking on Slider turns it into an input box |
| 8985 | bool start_text_input = false; |
| 8986 | const bool tab_focus_requested = FocusableItemRegister(window, id); |
| 8987 | if (tab_focus_requested || (hovered && g.IO.MouseClicked[0]) || g.NavActivateId == id || (g.NavInputId == id && g.ScalarAsInputTextId != id)) |
| 8988 | { |
| 8989 | SetActiveID(id, window); |
| 8990 | SetFocusID(id, window); |
| 8991 | FocusWindow(window); |
| 8992 | g.ActiveIdAllowNavDirFlags = (1 << ImGuiDir_Up) | (1 << ImGuiDir_Down); |
| 8993 | if (tab_focus_requested || g.IO.KeyCtrl || g.NavInputId == id) |
| 8994 | { |
| 8995 | start_text_input = true; |
| 8996 | g.ScalarAsInputTextId = 0; |
| 8997 | } |
| 8998 | } |
| 8999 | if (start_text_input || (g.ActiveId == id && g.ScalarAsInputTextId == id)) |
| 9000 | return InputScalarAsWidgetReplacement(frame_bb, label, ImGuiDataType_Float, v, id, decimal_precision); |
| 9001 | |
| 9002 | // Actual slider behavior + render grab |
| 9003 | ItemSize(total_bb, style.FramePadding.y); |
| 9004 | const bool value_changed = SliderBehavior(frame_bb, id, v, v_min, v_max, power, decimal_precision); |
| 9005 | |
| 9006 | // Display value using user-provided display format so user can add prefix/suffix/decorations to the value. |
| 9007 | char value_buf[64]; |
| 9008 | const char* value_buf_end = value_buf + ImFormatString(value_buf, IM_ARRAYSIZE(value_buf), display_format, *v); |
| 9009 | RenderTextClipped(frame_bb.Min, frame_bb.Max, value_buf, value_buf_end, NULL, ImVec2(0.5f, 0.5f)); |
| 9010 | |
| 9011 | if (label_size.x > 0.0f) |
| 9012 | RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label); |
| 9013 | |
| 9014 | return value_changed; |
nothing calls this directly
no test coverage detected