range_slider.inl 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // https://github.com/ocornut/imgui/issues/76
  2. // Taken from: https://github.com/wasikuss/imgui/commit/a50515ace6d9a62ebcd69817f1da927d31c39bb1
  3. namespace ImGui
  4. {
  5. extern float RoundScalarWithFormatFloat(const char* format, ImGuiDataType data_type, float v);
  6. extern float SliderCalcRatioFromValueFloat(ImGuiDataType data_type, float v, float v_min, float v_max, float power, float linear_zero_pos);
  7. // ~80% common code with ImGui::SliderBehavior
  8. bool RangeSliderBehavior(const ImRect& frame_bb, ImGuiID id, float* v1, float* v2, float v_min, float v_max, float power, int decimal_precision, ImGuiSliderFlags flags)
  9. {
  10. ImGuiContext& g = *GImGui;
  11. ImGuiWindow* window = GetCurrentWindow();
  12. const ImGuiStyle& style = g.Style;
  13. // Draw frame
  14. RenderFrame(frame_bb.Min, frame_bb.Max, GetColorU32(ImGuiCol_FrameBg), true, style.FrameRounding);
  15. const bool is_non_linear = (power < 1.0f-0.00001f) || (power > 1.0f+0.00001f);
  16. const bool is_horizontal = (flags & ImGuiSliderFlags_Vertical) == 0;
  17. const float grab_padding = 2.0f;
  18. const float slider_sz = is_horizontal ? (frame_bb.GetWidth() - grab_padding * 2.0f) : (frame_bb.GetHeight() - grab_padding * 2.0f);
  19. float grab_sz;
  20. if (decimal_precision > 0)
  21. grab_sz = ImMin(style.GrabMinSize, slider_sz);
  22. else
  23. grab_sz = ImMin(ImMax(1.0f * (slider_sz / ((v_min < v_max ? v_max - v_min : v_min - v_max) + 1.0f)), style.GrabMinSize), slider_sz); // Integer sliders, if possible have the grab size represent 1 unit
  24. const float slider_usable_sz = slider_sz - grab_sz;
  25. const float slider_usable_pos_min = (is_horizontal ? frame_bb.Min.x : frame_bb.Min.y) + grab_padding + grab_sz*0.5f;
  26. const float slider_usable_pos_max = (is_horizontal ? frame_bb.Max.x : frame_bb.Max.y) - grab_padding - grab_sz*0.5f;
  27. // For logarithmic sliders that cross over sign boundary we want the exponential increase to be symmetric around 0.0f
  28. float linear_zero_pos = 0.0f; // 0.0->1.0f
  29. if (v_min * v_max < 0.0f)
  30. {
  31. // Different sign
  32. const float linear_dist_min_to_0 = powf(fabsf(0.0f - v_min), 1.0f/power);
  33. const float linear_dist_max_to_0 = powf(fabsf(v_max - 0.0f), 1.0f/power);
  34. linear_zero_pos = linear_dist_min_to_0 / (linear_dist_min_to_0+linear_dist_max_to_0);
  35. }
  36. else
  37. {
  38. // Same sign
  39. linear_zero_pos = v_min < 0.0f ? 1.0f : 0.0f;
  40. }
  41. // Process clicking on the slider
  42. bool value_changed = false;
  43. if (g.ActiveId == id)
  44. {
  45. if (g.IO.MouseDown[0])
  46. {
  47. const float mouse_abs_pos = is_horizontal ? g.IO.MousePos.x : g.IO.MousePos.y;
  48. float clicked_t = (slider_usable_sz > 0.0f) ? ImClamp((mouse_abs_pos - slider_usable_pos_min) / slider_usable_sz, 0.0f, 1.0f) : 0.0f;
  49. if (!is_horizontal)
  50. clicked_t = 1.0f - clicked_t;
  51. float new_value;
  52. if (is_non_linear)
  53. {
  54. // Account for logarithmic scale on both sides of the zero
  55. if (clicked_t < linear_zero_pos)
  56. {
  57. // Negative: rescale to the negative range before powering
  58. float a = 1.0f - (clicked_t / linear_zero_pos);
  59. a = powf(a, power);
  60. new_value = ImLerp(ImMin(v_max,0.0f), v_min, a);
  61. }
  62. else
  63. {
  64. // Positive: rescale to the positive range before powering
  65. float a;
  66. if (fabsf(linear_zero_pos - 1.0f) > 1.e-6f)
  67. a = (clicked_t - linear_zero_pos) / (1.0f - linear_zero_pos);
  68. else
  69. a = clicked_t;
  70. a = powf(a, power);
  71. new_value = ImLerp(ImMax(v_min,0.0f), v_max, a);
  72. }
  73. }
  74. else
  75. {
  76. // Linear slider
  77. new_value = ImLerp(v_min, v_max, clicked_t);
  78. }
  79. char fmt[64];
  80. snprintf(fmt, 64, "%%.%df", decimal_precision);
  81. // Round past decimal precision
  82. new_value = RoundScalarWithFormatFloat(fmt, ImGuiDataType_Float, new_value);
  83. if (*v1 != new_value || *v2 != new_value)
  84. {
  85. if (fabsf(*v1 - new_value) < fabsf(*v2 - new_value))
  86. {
  87. *v1 = new_value;
  88. }
  89. else
  90. {
  91. *v2 = new_value;
  92. }
  93. value_changed = true;
  94. }
  95. }
  96. else
  97. {
  98. ClearActiveID();
  99. }
  100. }
  101. // Calculate slider grab positioning
  102. float grab_t = SliderCalcRatioFromValueFloat(ImGuiDataType_Float, *v1, v_min, v_max, power, linear_zero_pos);
  103. // Draw
  104. if (!is_horizontal)
  105. grab_t = 1.0f - grab_t;
  106. float grab_pos = ImLerp(slider_usable_pos_min, slider_usable_pos_max, grab_t);
  107. ImRect grab_bb1;
  108. if (is_horizontal)
  109. grab_bb1 = ImRect(ImVec2(grab_pos - grab_sz*0.5f, frame_bb.Min.y + grab_padding), ImVec2(grab_pos + grab_sz*0.5f, frame_bb.Max.y - grab_padding));
  110. else
  111. grab_bb1 = ImRect(ImVec2(frame_bb.Min.x + grab_padding, grab_pos - grab_sz*0.5f), ImVec2(frame_bb.Max.x - grab_padding, grab_pos + grab_sz*0.5f));
  112. window->DrawList->AddRectFilled(grab_bb1.Min, grab_bb1.Max, GetColorU32(g.ActiveId == id ? ImGuiCol_SliderGrabActive : ImGuiCol_SliderGrab), style.GrabRounding);
  113. // Calculate slider grab positioning
  114. grab_t = SliderCalcRatioFromValueFloat(ImGuiDataType_Float, *v2, v_min, v_max, power, linear_zero_pos);
  115. // Draw
  116. if (!is_horizontal)
  117. grab_t = 1.0f - grab_t;
  118. grab_pos = ImLerp(slider_usable_pos_min, slider_usable_pos_max, grab_t);
  119. ImRect grab_bb2;
  120. if (is_horizontal)
  121. grab_bb2 = ImRect(ImVec2(grab_pos - grab_sz*0.5f, frame_bb.Min.y + grab_padding), ImVec2(grab_pos + grab_sz*0.5f, frame_bb.Max.y - grab_padding));
  122. else
  123. grab_bb2 = ImRect(ImVec2(frame_bb.Min.x + grab_padding, grab_pos - grab_sz*0.5f), ImVec2(frame_bb.Max.x - grab_padding, grab_pos + grab_sz*0.5f));
  124. window->DrawList->AddRectFilled(grab_bb2.Min, grab_bb2.Max, GetColorU32(g.ActiveId == id ? ImGuiCol_SliderGrabActive : ImGuiCol_SliderGrab), style.GrabRounding);
  125. ImRect connector(grab_bb1.Min, grab_bb2.Max);
  126. connector.Min.x += grab_sz;
  127. connector.Min.y += grab_sz*0.3f;
  128. connector.Max.x -= grab_sz;
  129. connector.Max.y -= grab_sz*0.3f;
  130. window->DrawList->AddRectFilled(connector.Min, connector.Max, GetColorU32(ImGuiCol_SliderGrab), style.GrabRounding);
  131. return value_changed;
  132. }
  133. // ~95% common code with ImGui::SliderFloat
  134. bool RangeSliderFloat(const char* label, float* v1, float* v2, float v_min, float v_max, const char* display_format, float power)
  135. {
  136. ImGuiWindow* window = GetCurrentWindow();
  137. if (window->SkipItems)
  138. return false;
  139. ImGuiContext& g = *GImGui;
  140. const ImGuiStyle& style = g.Style;
  141. const ImGuiID id = window->GetID(label);
  142. const float w = CalcItemWidth();
  143. const ImVec2 label_size = CalcTextSize(label, NULL, true);
  144. const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, label_size.y + style.FramePadding.y*2.0f));
  145. 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));
  146. // NB- we don't call ItemSize() yet because we may turn into a text edit box below
  147. if (!ItemAdd(total_bb, id))
  148. {
  149. ItemSize(total_bb, style.FramePadding.y);
  150. return false;
  151. }
  152. const bool hovered = ItemHoverable(frame_bb, id, g.LastItemData.ItemFlags);
  153. if (hovered)
  154. SetHoveredID(id);
  155. if (!display_format)
  156. display_format = "(%.3f, %.3f)";
  157. int decimal_precision = ImParseFormatPrecision(display_format, 3);
  158. // Tabbing or CTRL-clicking on Slider turns it into an input box
  159. bool start_text_input = false;
  160. if (hovered && g.IO.MouseClicked[0])
  161. {
  162. SetActiveID(id, window);
  163. FocusWindow(window);
  164. if (g.IO.KeyCtrl)
  165. {
  166. start_text_input = true;
  167. g.TempInputId = 0;
  168. }
  169. }
  170. if (start_text_input || (g.ActiveId == id && g.TempInputId == id))
  171. {
  172. char fmt[64];
  173. snprintf(fmt, 64, "%%.%df", decimal_precision);
  174. return TempInputScalar(frame_bb, id, label, ImGuiDataType_Float, v1, fmt);
  175. }
  176. ItemSize(total_bb, style.FramePadding.y);
  177. // Actual slider behavior + render grab
  178. const bool value_changed = RangeSliderBehavior(frame_bb, id, v1, v2, v_min, v_max, power, decimal_precision, 0);
  179. // Display value using user-provided display format so user can add prefix/suffix/decorations to the value.
  180. char value_buf[64];
  181. const char* value_buf_end = value_buf + ImFormatString(value_buf, IM_ARRAYSIZE(value_buf), display_format, *v1, *v2);
  182. RenderTextClipped(frame_bb.Min, frame_bb.Max, value_buf, value_buf_end, NULL, ImVec2(0.5f,0.5f));
  183. if (label_size.x > 0.0f)
  184. RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label);
  185. return value_changed;
  186. }
  187. } // namespace ImGui