Skip to content

Commit 6ed06a8

Browse files
committed
DragFloat() added power parameter for logarithmic drag on both side of zero #180
1 parent f5ad362 commit 6ed06a8

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

imgui.cpp

+31-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// ImGui library v1.38 WIP
1+
// ImGui library v1.38 WIP
22
// See ImGui::ShowTestWindow() for sample code.
33
// Read 'Programmer guide' below for notes on how to setup ImGui in your codebase.
44
// Get latest version at https://github.com/ocornut/imgui
@@ -1145,7 +1145,7 @@ struct ImGuiState
11451145
ImGuiID ScalarAsInputTextId; // Temporary text input when CTRL+clicking on a slider, etc.
11461146
ImGuiStorage ColorEditModeStorage; // for user selection
11471147
ImGuiID ActiveComboID;
1148-
float DragCurrentValue;
1148+
float DragCurrentValue; // current dragged value, always float, not rounded by end-user precision settings
11491149
ImVec2 DragLastMouseDelta;
11501150
float DragSpeedScaleSlow;
11511151
float DragSpeedScaleFast;
@@ -5478,7 +5478,7 @@ bool ImGui::SliderInt4(const char* label, int v[4], int v_min, int v_max, const
54785478
}
54795479

54805480
// FIXME-WIP: Work in progress. May change API / behavior.
5481-
static bool DragScalarBehavior(const ImRect& frame_bb, ImGuiID id, float* v, float v_speed, float v_min, float v_max, int decimal_precision)
5481+
static bool DragScalarBehavior(const ImRect& frame_bb, ImGuiID id, float* v, float v_speed, float v_min, float v_max, int decimal_precision, float power)
54825482
{
54835483
ImGuiState& g = *GImGui;
54845484
ImGuiWindow* window = GetCurrentWindow();
@@ -5505,22 +5505,40 @@ static bool DragScalarBehavior(const ImRect& frame_bb, ImGuiID id, float* v, flo
55055505
const ImVec2 mouse_drag_delta = ImGui::GetMouseDragDelta(0, 1.0f);
55065506
if (fabsf(mouse_drag_delta.x - g.DragLastMouseDelta.x) > 0.0f)
55075507
{
5508-
float step = v_speed;
5508+
float speed = v_speed;
55095509
if (g.IO.KeyShift && g.DragSpeedScaleFast >= 0.0f)
5510-
step = v_speed * g.DragSpeedScaleFast;
5510+
speed = v_speed * g.DragSpeedScaleFast;
55115511
if (g.IO.KeyAlt && g.DragSpeedScaleSlow >= 0.0f)
5512-
step = v_speed * g.DragSpeedScaleSlow;
5512+
speed = v_speed * g.DragSpeedScaleSlow;
55135513

5514-
g.DragCurrentValue += (mouse_drag_delta.x - g.DragLastMouseDelta.x) * step;
5514+
float v_cur = g.DragCurrentValue;
5515+
float delta = (mouse_drag_delta.x - g.DragLastMouseDelta.x) * speed;
5516+
if (fabsf(power - 1.0f) > 0.001f)
5517+
{
5518+
// Logarithmic curve on both side of 0.0
5519+
float v0_abs = v_cur >= 0.0f ? v_cur : -v_cur;
5520+
float v0_sign = v_cur >= 0.0f ? 1.0f : -1.0f;
5521+
float v1 = powf(v0_abs, 1.0f / power) + (delta * v0_sign);
5522+
float v1_abs = v1 >= 0.0f ? v1 : -v1;
5523+
float v1_sign = v1 >= 0.0f ? 1.0f : -1.0f; // Crossed sign line
5524+
v_cur = powf(v1_abs, power) * v0_sign * v1_sign; // Reapply sign
5525+
}
5526+
else
5527+
{
5528+
v_cur += delta;
5529+
}
55155530
g.DragLastMouseDelta.x = mouse_drag_delta.x;
55165531

5532+
// Clamp
55175533
if (v_min < v_max)
5518-
g.DragCurrentValue = ImClamp(g.DragCurrentValue, v_min, v_max);
5534+
g.DragCurrentValue = ImClamp(v_cur, v_min, v_max);
5535+
g.DragCurrentValue = v_cur;
55195536

5520-
float new_value = RoundScalar(g.DragCurrentValue, decimal_precision);
5521-
if (*v != new_value)
5537+
// Round to user desired precision, then apply
5538+
v_cur = RoundScalar(v_cur, decimal_precision);
5539+
if (*v != v_cur)
55225540
{
5523-
*v = new_value;
5541+
*v = v_cur;
55245542
value_changed = true;
55255543
}
55265544
}
@@ -5534,7 +5552,7 @@ static bool DragScalarBehavior(const ImRect& frame_bb, ImGuiID id, float* v, flo
55345552
return value_changed;
55355553
}
55365554

5537-
bool ImGui::DragFloat(const char* label, float *v, float v_speed, float v_min, float v_max, const char* display_format)
5555+
bool ImGui::DragFloat(const char* label, float *v, float v_speed, float v_min, float v_max, const char* display_format, float power)
55385556
{
55395557
ImGuiState& g = *GImGui;
55405558
ImGuiWindow* window = GetCurrentWindow();
@@ -5586,7 +5604,7 @@ bool ImGui::DragFloat(const char* label, float *v, float v_speed, float v_min, f
55865604
ItemSize(total_bb, style.FramePadding.y);
55875605

55885606
// Actual drag behavior
5589-
const bool value_changed = DragScalarBehavior(frame_bb, id, v, v_speed, v_min, v_max, decimal_precision);
5607+
const bool value_changed = DragScalarBehavior(frame_bb, id, v, v_speed, v_min, v_max, decimal_precision, power);
55905608

55915609
// Display value using user-provided display format so user can add prefix/suffix/decorations to the value.
55925610
char value_buf[64];

imgui.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,8 @@ namespace ImGui
316316

317317
// Widgets: Drags (tip: ctrl+click on a drag box to input text)
318318
// ImGui 1.38+ work-in-progress, may change name or API.
319-
IMGUI_API bool DragFloat(const char* label, float* v, float v_speed = 1.0f, float v_min = 0.0f, float v_max = 0.0f, const char* display_format = "%.3f"); // If v_max >= v_max we have no bound
320-
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_max >= v_max we have no bound
319+
IMGUI_API bool DragFloat(const char* label, float* v, float v_speed = 1.0f, float v_min = 0.0f, float v_max = 0.0f, const char* display_format = "%.3f", float power = 1.0f); // If v_max >= v_max we have no bound
320+
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_max >= v_max we have no bound
321321

322322
// Widgets: Input
323323
IMGUI_API bool InputText(const char* label, char* buf, size_t buf_size, ImGuiInputTextFlags flags = 0, ImGuiTextEditCallback callback = NULL, void* user_data = NULL);

0 commit comments

Comments
 (0)