Slider code tweaks

This commit is contained in:
ocornut 2015-03-14 23:33:12 +00:00
parent 06fbeed41a
commit 93befadc0b

View File

@ -4680,6 +4680,7 @@ static void ApplyNumericalTextInput(const char* buf, float *v)
*v = op_v; *v = op_v;
} }
// Create text input in place of a slider (when CTRL+Clicking on slider)
static bool SliderFloatAsInputText(const char* label, float* v, const ImGuiID& id, int decimal_precision) static bool SliderFloatAsInputText(const char* label, float* v, const ImGuiID& id, int decimal_precision)
{ {
ImGuiState& g = *GImGui; ImGuiState& g = *GImGui;
@ -4749,11 +4750,6 @@ bool ImGui::SliderFloat(const char* label, float* v, float v_min, float v_max, c
const ImGuiID id = window->GetID(label); const ImGuiID id = window->GetID(label);
const float w = ImGui::CalcItemWidth(); const float w = ImGui::CalcItemWidth();
if (!display_format)
display_format = "%.3f";
int decimal_precision = 3;
ParseFormat(display_format, decimal_precision);
const ImVec2 label_size = CalcTextSize(label, NULL, true); const ImVec2 label_size = CalcTextSize(label, NULL, true);
const ImGuiAabb frame_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, label_size.y) + style.FramePadding*2.0f); const ImGuiAabb frame_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, label_size.y) + style.FramePadding*2.0f);
const ImGuiAabb slider_bb(frame_bb.Min + style.FramePadding, frame_bb.Max - style.FramePadding); const ImGuiAabb slider_bb(frame_bb.Min + style.FramePadding, frame_bb.Max - style.FramePadding);
@ -4766,10 +4762,37 @@ bool ImGui::SliderFloat(const char* label, float* v, float v_min, float v_max, c
return false; return false;
} }
const bool tab_focus_requested = window->FocusItemRegister(g.ActiveId == id); const bool hovered = IsHovered(slider_bb, id);
if (hovered)
g.HoveredId = id;
const bool is_unbound = v_min == -FLT_MAX || v_min == FLT_MAX || v_max == -FLT_MAX || v_max == FLT_MAX; if (!display_format)
const bool is_logarithmic = abs(power - 1.0f) > 0.0001f; display_format = "%.3f";
int decimal_precision = 3;
ParseFormat(display_format, decimal_precision);
const bool tab_focus_requested = window->FocusItemRegister(g.ActiveId == id);
const bool is_unbound = (v_min == -FLT_MAX || v_min == FLT_MAX || v_max == -FLT_MAX || v_max == FLT_MAX);
bool start_text_input = false;
if (tab_focus_requested || (hovered && g.IO.MouseClicked[0]))
{
SetActiveId(id);
FocusWindow(window);
const bool is_ctrl_down = g.IO.KeyCtrl;
if (tab_focus_requested || is_ctrl_down || is_unbound)
{
start_text_input = true;
g.SliderAsInputTextId = 0;
}
}
// Tabbing or CTRL-clicking through slider turns into an input box
if (start_text_input || (g.ActiveId == id && id == g.SliderAsInputTextId))
return SliderFloatAsInputText(label, v, id, decimal_precision);
ItemSize(bb);
const float grab_size_in_units = 1.0f; // In 'v' units. Probably needs to be parametrized, based on a 'v_step' value? decimal precision? const float grab_size_in_units = 1.0f; // In 'v' units. Probably needs to be parametrized, based on a 'v_step' value? decimal precision?
float grab_size_in_pixels; float grab_size_in_pixels;
@ -4799,39 +4822,16 @@ bool ImGui::SliderFloat(const char* label, float* v, float v_min, float v_max, c
} }
} }
const bool hovered = IsHovered(slider_bb, id);
if (hovered)
g.HoveredId = id;
bool start_text_input = false;
if (tab_focus_requested || (hovered && g.IO.MouseClicked[0]))
{
SetActiveId(id);
FocusWindow(window);
const bool is_ctrl_down = g.IO.KeyCtrl;
if (tab_focus_requested || is_ctrl_down || is_unbound)
{
start_text_input = true;
g.SliderAsInputTextId = 0;
}
}
// Tabbing or CTRL-clicking through slider turns into an input box
bool value_changed = false;
if (start_text_input || (g.ActiveId == id && id == g.SliderAsInputTextId))
{
value_changed = SliderFloatAsInputText(label, v, id, decimal_precision);
return value_changed;
}
ItemSize(bb);
RenderFrame(frame_bb.Min, frame_bb.Max, window->Color(ImGuiCol_FrameBg), true, style.FrameRounding); RenderFrame(frame_bb.Min, frame_bb.Max, window->Color(ImGuiCol_FrameBg), true, style.FrameRounding);
const bool is_logarithmic = abs(power - 1.0f) > 0.0001f;
bool value_changed = false;
if (!is_unbound)
{
// Process clicking on the slider // Process clicking on the slider
if (g.ActiveId == id) if (g.ActiveId == id)
{ {
if (!is_unbound && g.IO.MouseDown[0]) if (g.IO.MouseDown[0])
{ {
const float normalized_pos = ImClamp((g.IO.MousePos.x - slider_effective_x1) / slider_effective_w, 0.0f, 1.0f); const float normalized_pos = ImClamp((g.IO.MousePos.x - slider_effective_x1) / slider_effective_w, 0.0f, 1.0f);
@ -4886,8 +4886,6 @@ bool ImGui::SliderFloat(const char* label, float* v, float v_min, float v_max, c
} }
} }
if (!is_unbound)
{
// Calculate slider grab positioning // Calculate slider grab positioning
float grab_t; float grab_t;
if (is_logarithmic) if (is_logarithmic)