Skip to content

Commit ab9236d

Browse files
committed
Editor: Add canvas size mode (#170)
Config can now decide how editor should resize view when changing size.
1 parent 03adb8f commit ab9236d

File tree

3 files changed

+52
-17
lines changed

3 files changed

+52
-17
lines changed

Diff for: docs/CHANGELOG.txt

+8
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,14 @@ v0.9.1 (WIP):
141141
New API:
142142
ImVector<float> Config::CustomZoomLevels;
143143

144+
NEW: Editor: Add canvas size mode (#170)
145+
146+
Config can now decide how editor should resize view when changing size.
147+
148+
New API:
149+
enum class CanvasSizeMode;
150+
Config::CanvasSizeMode;
151+
144152
BUGFIX: Avoid crash while destroying editor.
145153

146154
BUGFIX: Save draw list used by editor between Begin() and End()

Diff for: imgui_node_editor.cpp

+21-3
Original file line numberDiff line numberDiff line change
@@ -1186,15 +1186,33 @@ void ed::EditorContext::Begin(const char* id, const ImVec2& size)
11861186
{
11871187
m_NavigateAction.FinishNavigation();
11881188

1189+
auto centerX = (previousVisibleRect.Max.x + previousVisibleRect.Min.x) * 0.5f;
1190+
auto centerY = (previousVisibleRect.Max.y + previousVisibleRect.Min.y) * 0.5f;
11891191
auto currentVisibleRect = m_Canvas.ViewRect();
11901192
auto currentAspectRatio = currentVisibleRect.GetHeight() ? (currentVisibleRect.GetWidth() / currentVisibleRect.GetHeight()) : 0.0f;
1193+
auto width = previousVisibleRect.GetHeight();
1194+
auto height = previousVisibleRect.GetHeight();
11911195

1192-
auto centerX = (previousVisibleRect.Max.x + previousVisibleRect.Min.x) * 0.5f;
1193-
auto height = previousVisibleRect.GetHeight();
1194-
auto width = currentAspectRatio * height;
1196+
if (m_Config.CanvasSizeMode == ax::NodeEditor::CanvasSizeMode::FitVerticalView)
1197+
{
1198+
height = previousVisibleRect.GetHeight();
1199+
width = height * currentAspectRatio;
1200+
}
1201+
else if (m_Config.CanvasSizeMode == ax::NodeEditor::CanvasSizeMode::FitHorizontalView)
1202+
{
1203+
width = previousVisibleRect.GetWidth();
1204+
height = width / currentAspectRatio;
1205+
}
1206+
else if (m_Config.CanvasSizeMode == ax::NodeEditor::CanvasSizeMode::CenterOnly)
1207+
{
1208+
width = currentVisibleRect.GetWidth();
1209+
height = currentVisibleRect.GetHeight();
1210+
}
11951211

11961212
previousVisibleRect.Min.x = centerX - 0.5f * width;
11971213
previousVisibleRect.Max.x = centerX + 0.5f * width;
1214+
previousVisibleRect.Min.y = centerY - 0.5f * height;
1215+
previousVisibleRect.Max.y = centerY + 0.5f * height;
11981216

11991217
m_NavigateAction.NavigateTo(previousVisibleRect, Detail::NavigateAction::ZoomMode::Exact, 0.0f);
12001218
}

Diff for: imgui_node_editor.h

+23-14
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,27 @@ struct LinkId;
3131
struct PinId;
3232

3333

34+
//------------------------------------------------------------------------------
35+
enum class PinKind
36+
{
37+
Input,
38+
Output
39+
};
40+
41+
enum class FlowDirection
42+
{
43+
Forward,
44+
Backward
45+
};
46+
47+
enum class CanvasSizeMode
48+
{
49+
FitVerticalView, // Previous view will be scaled to fit new view on Y axis
50+
FitHorizontalView, // Previous view will be scaled to fit new view on X axis
51+
CenterOnly, // Previous view will be centered on new view
52+
};
53+
54+
3455
//------------------------------------------------------------------------------
3556
enum class SaveReasonFlags: uint32_t
3657
{
@@ -66,6 +87,7 @@ struct Config
6687
ConfigLoadNodeSettings LoadNodeSettings;
6788
void* UserPointer;
6889
ImVector<float> CustomZoomLevels;
90+
typename CanvasSizeMode CanvasSizeMode;
6991
int DragButtonIndex; // Mouse button index drag action will react to (0-left, 1-right, 2-middle)
7092
int SelectButtonIndex; // Mouse button index select action will react to (0-left, 1-right, 2-middle)
7193
int NavigateButtonIndex; // Mouse button index navigate action will react to (0-left, 1-right, 2-middle)
@@ -81,6 +103,7 @@ struct Config
81103
, LoadNodeSettings(nullptr)
82104
, UserPointer(nullptr)
83105
, CustomZoomLevels()
106+
, CanvasSizeMode(CanvasSizeMode::FitVerticalView)
84107
, DragButtonIndex(0)
85108
, SelectButtonIndex(0)
86109
, NavigateButtonIndex(1)
@@ -90,20 +113,6 @@ struct Config
90113
};
91114

92115

93-
//------------------------------------------------------------------------------
94-
enum class PinKind
95-
{
96-
Input,
97-
Output
98-
};
99-
100-
enum class FlowDirection
101-
{
102-
Forward,
103-
Backward
104-
};
105-
106-
107116
//------------------------------------------------------------------------------
108117
enum StyleColor
109118
{

0 commit comments

Comments
 (0)