Skip to content

Commit e78e447

Browse files
committed
Editor: Add smooth zoom (thedmd#266)
1 parent 8e367e2 commit e78e447

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

docs/CHANGELOG.txt

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
v0.9.4 (WIP):
22

3+
NEW: Editor: Add smooth zoom (#266)
4+
35
BUGFIX: Canvas: Remember index of first command buffer to not miss updating any used (#260)
46

57
BUGFIX: Editor: Don't duplicated ImVec2/ImVec3 == != operators defined since ImGui r19002 (#268)

imgui_node_editor.cpp

+27-2
Original file line numberDiff line numberDiff line change
@@ -3449,8 +3449,7 @@ bool ed::NavigateAction::HandleZoom(const Control& control)
34493449
m_Animation.Finish();
34503450

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

34553454
auto oldView = GetView();
34563455
m_Zoom = newZoom;
@@ -3626,6 +3625,32 @@ ImRect ed::NavigateAction::GetViewRect() const
36263625
return m_Canvas.CalcViewRect(GetView());
36273626
}
36283627

3628+
float ed::NavigateAction::GetNextZoom(float steps)
3629+
{
3630+
if (this->Editor->GetConfig().EnableSmoothZoom)
3631+
{
3632+
return MatchSmoothZoom(steps);
3633+
}
3634+
else
3635+
{
3636+
auto fixedSteps = (int)steps;
3637+
return MatchZoom(fixedSteps, m_ZoomLevels[fixedSteps < 0 ? 0 : m_ZoomLevelCount - 1]);
3638+
}
3639+
}
3640+
3641+
float ed::NavigateAction::MatchSmoothZoom(float steps)
3642+
{
3643+
const auto power = Editor->GetConfig().SmoothZoomPower;
3644+
3645+
const auto newZoom = m_Zoom * powf(power, steps);
3646+
if (newZoom < m_ZoomLevels[0])
3647+
return m_ZoomLevels[0];
3648+
else if (newZoom > m_ZoomLevels[m_ZoomLevelCount - 1])
3649+
return m_ZoomLevels[m_ZoomLevelCount - 1];
3650+
else
3651+
return newZoom;
3652+
}
3653+
36293654
float ed::NavigateAction::MatchZoom(int steps, float fallbackZoom)
36303655
{
36313656
auto currentZoomIndex = MatchZoomIndex(steps);

imgui_node_editor.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ struct Config
105105
int SelectButtonIndex; // Mouse button index select action will react to (0-left, 1-right, 2-middle)
106106
int NavigateButtonIndex; // Mouse button index navigate action will react to (0-left, 1-right, 2-middle)
107107
int ContextMenuButtonIndex; // Mouse button index context menu action will react to (0-left, 1-right, 2-middle)
108+
bool EnableSmoothZoom;
109+
float SmoothZoomPower;
108110

109111
Config()
110112
: SettingsFile("NodeEditor.json")
@@ -121,6 +123,12 @@ struct Config
121123
, SelectButtonIndex(0)
122124
, NavigateButtonIndex(1)
123125
, ContextMenuButtonIndex(1)
126+
, EnableSmoothZoom(false)
127+
# ifdef __APPLE__
128+
, SmoothZoomPower(1.1f)
129+
# else
130+
, SmoothZoomPower(1.3f)
131+
# endif
124132
{
125133
}
126134
};
@@ -519,4 +527,4 @@ struct PinId final: Details::SafePointerType<PinId>
519527

520528

521529
//------------------------------------------------------------------------------
522-
# endif // __IMGUI_NODE_EDITOR_H__
530+
# endif // __IMGUI_NODE_EDITOR_H__

imgui_node_editor_internal.h

+2
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,8 @@ struct NavigateAction final: EditorAction
886886

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

889+
float GetNextZoom(float steps);
890+
float MatchSmoothZoom(float steps);
889891
float MatchZoom(int steps, float fallbackZoom);
890892
int MatchZoomIndex(int direction);
891893

0 commit comments

Comments
 (0)