Skip to content

Commit

Permalink
Columns: mouse dragging uses absolute mouse coords. Fixed dragging le…
Browse files Browse the repository at this point in the history
…ft-most column of an auto-resizable window. #125
  • Loading branch information
ocornut committed Apr 18, 2015
1 parent c82f909 commit c46d563
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,7 @@ struct ImGuiState
ImGuiID ScalarAsInputTextId; // Temporary text input when CTRL+clicking on a slider, etc.
ImGuiStorage ColorEditModeStorage; // for user selection
ImGuiID ActiveComboID;
ImVec2 ActiveClickDeltaToCenter;
float DragCurrentValue; // current dragged value, always float, not rounded by end-user precision settings
ImVec2 DragLastMouseDelta;
float DragSpeedDefaultRatio; // if speed == 0.0f, uses (max-min) * DragSpeedDefaultRatio
Expand Down Expand Up @@ -1214,6 +1215,7 @@ struct ImGuiState

ScalarAsInputTextId = 0;
ActiveComboID = 0;
ActiveClickDeltaToCenter = ImVec2(0.0f, 0.0f);
DragCurrentValue = 0.0f;
DragLastMouseDelta = ImVec2(0.0f, 0.0f);
DragSpeedDefaultRatio = 0.01f;
Expand Down Expand Up @@ -7424,7 +7426,7 @@ void ImGui::NextColumn()
window->DC.CurrentLineTextBaseOffset = 0.0f;

PushColumnClipRect();
ImGui::PushItemWidth(ImGui::GetColumnWidth() * 0.65f);
ImGui::PushItemWidth(ImGui::GetColumnWidth() * 0.65f); // FIXME
}
}

Expand All @@ -7440,13 +7442,35 @@ int ImGui::GetColumnsCount()
return window->DC.ColumnsCount;
}

static float GetDraggedColumnOffset(int column_index)
{
// Active (dragged) column always follow mouse. The reason we need this is that dragging a column to the right edge of an auto-resizing
// window creates a feedback loop because we store normalized positions/ So while dragging we enforce absolute positioning
ImGuiState& g = *GImGui;
ImGuiWindow* window = GetCurrentWindow();
IM_ASSERT(g.ActiveId == window->DC.ColumnsSetID + ImGuiID(column_index));

float x = g.IO.MousePos.x + g.ActiveClickDeltaToCenter.x;
x -= window->Pos.x;
x = ImClamp(x, ImGui::GetColumnOffset(column_index-1)+g.Style.ColumnsMinSpacing, ImGui::GetColumnOffset(column_index+1)-g.Style.ColumnsMinSpacing);

return x;
}

float ImGui::GetColumnOffset(int column_index)
{
ImGuiState& g = *GImGui;
ImGuiWindow* window = GetCurrentWindow();
if (column_index < 0)
column_index = window->DC.ColumnsCurrent;

if (g.ActiveId)
{
const ImGuiID column_id = window->DC.ColumnsSetID + ImGuiID(column_index);
if (g.ActiveId == column_id)
return GetDraggedColumnOffset(column_index);
}

// Read from cache
IM_ASSERT(column_index < (int)window->DC.ColumnsOffsetsT.size());
const float t = window->DC.ColumnsOffsetsT[column_index];
Expand Down Expand Up @@ -7538,10 +7562,11 @@ void ImGui::Columns(int columns_count, const char* id, bool border)

if (held)
{
x -= window->Pos.x;
x = ImClamp(x + g.IO.MouseDelta.x, ImGui::GetColumnOffset(i-1)+g.Style.ColumnsMinSpacing, ImGui::GetColumnOffset(i+1)-g.Style.ColumnsMinSpacing);
if (g.ActiveIdIsJustActivated)
g.ActiveClickDeltaToCenter.x = x - g.IO.MousePos.x;

x = GetDraggedColumnOffset(i);
SetColumnOffset(i, x);
x += window->Pos.x;
}
}
}
Expand Down

0 comments on commit c46d563

Please sign in to comment.