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 a0b68cb
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 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
29 changes: 27 additions & 2 deletions imgui_node_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3449,8 +3449,7 @@ bool ed::NavigateAction::HandleZoom(const Control& control)
m_Animation.Finish();

auto mousePos = io.MousePos;
auto steps = (int)io.MouseWheel;
auto newZoom = MatchZoom(steps, m_ZoomLevels[steps < 0 ? 0 : m_ZoomLevelCount - 1]);
auto newZoom = GetNextZoom(io.MouseWheel);

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

float ed::NavigateAction::GetNextZoom(float steps)
{
if (this->Editor->GetConfig().EnableSmoothZoom)
{
return MatchSmoothZoom(steps);
}
else
{
auto fixedSteps = (int)steps;
return MatchZoom(fixedSteps, m_ZoomLevels[fixedSteps < 0 ? 0 : m_ZoomLevelCount - 1]);
}
}

float ed::NavigateAction::MatchSmoothZoom(float steps)
{
const auto power = Editor->GetConfig().SmoothZoomPower;

const auto newZoom = m_Zoom * powf(power, 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
10 changes: 9 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,12 @@ struct Config
, SelectButtonIndex(0)
, NavigateButtonIndex(1)
, ContextMenuButtonIndex(1)
, EnableSmoothZoom(true)
# ifdef __APPLE__
, SmoothZoomPower(1.1f)
# else
, SmoothZoomPower(1.3f)
# endif
{
}
};
Expand Down Expand Up @@ -519,4 +527,4 @@ struct PinId final: Details::SafePointerType<PinId>


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

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

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

Expand Down

0 comments on commit a0b68cb

Please sign in to comment.