Skip to content

Commit

Permalink
Editor: Add smooth zoom (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
thedmd committed Dec 27, 2023
1 parent 97a5d9e commit bfa43bc
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
2 changes: 2 additions & 0 deletions docs/CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ v0.10.0 (WIP):

NEW: Canvas: Add example of zooming at fixed point (#270)

NEW: Editor: Add smooth zoom (#266)

CHANGE: Canvas: Use ImDrawCallback_ImCanvas macro as draw callback sentinel (#256), thanks @nspitko

BUGFIX: Examples: Call ed::EndCreate() and ed::EndDelete() only when ed::BeginCreate() and ed::BeginDelete() returned true
Expand Down
13 changes: 12 additions & 1 deletion imgui_node_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3450,7 +3450,7 @@ bool ed::NavigateAction::HandleZoom(const Control& control)

auto mousePos = io.MousePos;
auto steps = (int)io.MouseWheel;
auto newZoom = MatchZoom(steps, m_ZoomLevels[steps < 0 ? 0 : m_ZoomLevelCount - 1]);
auto newZoom = this->Editor->GetConfig().EnableSmoothZoom ? MatchSmoothZoom(io.MouseWheel) : MatchZoom(steps, m_ZoomLevels[steps < 0 ? 0 : m_ZoomLevelCount - 1]);

auto oldView = GetView();
m_Zoom = newZoom;
Expand Down Expand Up @@ -3626,6 +3626,17 @@ ImRect ed::NavigateAction::GetViewRect() const
return m_Canvas.CalcViewRect(GetView());
}

float ed::NavigateAction::MatchSmoothZoom(float steps)
{
auto newZoom = m_Zoom * powf(Editor->GetConfig().SmoothZoomPower, steps);
if (newZoom < m_ZoomLevels[0])
return m_ZoomLevels[0];
else if (newZoom > m_ZoomLevels[m_ZoomLevelCount - 1])
return m_ZoomLevels[m_ZoomLevelCount - 1];
else
return newZoom;
}

float ed::NavigateAction::MatchZoom(int steps, float fallbackZoom)
{
auto currentZoomIndex = MatchZoomIndex(steps);
Expand Down
6 changes: 5 additions & 1 deletion imgui_node_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ struct Config
int SelectButtonIndex; // Mouse button index select action will react to (0-left, 1-right, 2-middle)
int NavigateButtonIndex; // Mouse button index navigate action will react to (0-left, 1-right, 2-middle)
int ContextMenuButtonIndex; // Mouse button index context menu action will react to (0-left, 1-right, 2-middle)
bool EnableSmoothZoom;
float SmoothZoomPower;

Config()
: SettingsFile("NodeEditor.json")
Expand All @@ -121,6 +123,8 @@ struct Config
, SelectButtonIndex(0)
, NavigateButtonIndex(1)
, ContextMenuButtonIndex(1)
, EnableSmoothZoom(true)
, SmoothZoomPower(1.1f)
{
}
};
Expand Down Expand Up @@ -519,4 +523,4 @@ struct PinId final: Details::SafePointerType<PinId>


//------------------------------------------------------------------------------
# endif // __IMGUI_NODE_EDITOR_H__
# endif // __IMGUI_NODE_EDITOR_H__
1 change: 1 addition & 0 deletions imgui_node_editor_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,7 @@ struct NavigateAction final: EditorAction

void NavigateTo(const ImRect& target, float duration = -1.0f, NavigationReason reason = NavigationReason::Unknown);

float MatchSmoothZoom(float steps);
float MatchZoom(int steps, float fallbackZoom);
int MatchZoomIndex(int direction);

Expand Down

0 comments on commit bfa43bc

Please sign in to comment.