Skip to content

Commit 20af84a

Browse files
committed
Small fixes in node graph example
1 parent d7194ca commit 20af84a

File tree

1 file changed

+41
-36
lines changed

1 file changed

+41
-36
lines changed

examples/CustomNodeGraph.pas

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,16 @@
1919
{$IfDef FPC}
2020
{$mode Delphi}{$H+}
2121
{$ModeSwitch advancedrecords}
22+
//{$CODEALIGN LOCALMIN=16}
23+
{$inline on }
2224
{$EndIf}
2325

2426
Interface
2527

2628
Uses
2729
Generics.Collections,
2830
SysUtils,
29-
PasImGui,
30-
PasImGui.Utils;
31+
PasImGui;
3132

3233
Type
3334
{ TNode }
@@ -41,8 +42,8 @@
4142
InputsCount, OutputsCount: Integer;
4243
Function GetInputSlotPos(slotNo: Integer): ImVec2;
4344
Function GetOutputSlotPos(slotNo: Integer): ImVec2;
44-
Constructor Create(id_: Integer; Const Name_: PAnsiChar;
45-
pos_: ImVec2; Value_: Single; color_: ImVec4; inputsCount_, outputsCount_: Integer);
45+
Constructor Create(id_: Integer; Const Name_: PAnsiChar; pos_: ImVec2; Value_: Single;
46+
color_: ImVec4; inputsCount_, outputsCount_: Integer);
4647
End;
4748

4849
{ TNodeLink }
@@ -64,7 +65,7 @@
6465
links: TList<TNodeLink>;
6566
scrolling: ImVec2 = (x: 0.0; y: 0.0);
6667
inited: Boolean = False;
67-
show_grid: Boolean = False;
68+
show_grid: Boolean = True;
6869
node_selected: Integer = -1;
6970

7071
Procedure ShowExampleAppCustomNodeGraph(opened: PBoolean);
@@ -76,12 +77,13 @@
7677
offset, win_pos, canvas_sz, p1, p2, node_rect_min, node_rect_max, scene_pos: ImVec2;
7778
draw_list: PImDrawList;
7879
GRID_COLOR, node_bg_color: ImU32;
79-
GRID_SZ, x, y: Single;
80+
x, y: Single;
8081
old_any_active, node_widgets_active, node_moving_active: Boolean;
8182
open_context_menu: Boolean;
8283
node_hovered_in_list: Integer;
8384
node_hovered_in_scene: Integer;
8485
Const
86+
GRID_SZ : Single = 64.0;
8587
NODE_SLOT_RADIUS: Single = 4.0;
8688
NODE_WINDOW_PADDING: ImVec2 = (x: 8.0; y: 8.0);
8789
Begin
@@ -101,8 +103,8 @@
101103
links := TList<TNodeLink>.Create();
102104

103105
nodes.Add(TNode.Create(0, 'MainTex', ImVec2.New(40, 50), 0.5, ImVec4.New(255, 100, 100), 1, 1));
104-
nodes.Add(TNode.Create(1, 'BumpMap', ImVec2.New(40, 150), 0.42, ImVec4.New(200, 100, 200), 1, 1));
105-
nodes.Add(TNode.Create(2, 'Combine', ImVec2.New(270, 80), 1.0, ImVec4.New(0, 200, 100), 2, 2));
106+
nodes.Add(TNode.Create(1, 'BumpMap', ImVec2.New(40, 160), 0.42, ImVec4.New(200, 100, 200), 1, 1));
107+
nodes.Add(TNode.Create(2, 'Combine', ImVec2.New(270, 80), 0.0, ImVec4.New(0, 200, 100), 2, 2));
106108
links.Add(TNodeLink.Create(0, 0, 2, 0));
107109
links.Add(TNodeLink.Create(1, 0, 2, 1));
108110

@@ -113,7 +115,7 @@
113115
node_hovered_in_scene := -1;
114116

115117
// Draw a list of nodes on the left side
116-
ImGui.BeginChild('node_list', ImVec2.New(100, 0));
118+
ImGui.BeginChild('node_list', ImVec2.New(150, 0));
117119
ImGui.Text('Nodes');
118120
ImGui.Separator();
119121

@@ -127,8 +129,7 @@
127129
If ImGui.IsItemHovered() Then
128130
Begin
129131
node_hovered_in_list := node.ID;
130-
open_context_menu := Boolean(Ord(open_context_menu) Or
131-
Ord(ImGui.IsMouseClicked(ImGuiMouseButton_Left)));
132+
open_context_menu := open_context_menu Or ImGui.IsMouseClicked(ImGuiMouseButton_Right);
132133
End;
133134
ImGui.PopID();
134135
End;
@@ -138,13 +139,13 @@
138139
ImGui.BeginGroup();
139140

140141
// Create our child canvas
141-
ImGui.Text('Hold middle mouse button to scroll (%.2f,%.2f)',[scrolling.x, scrolling.y]);
142+
ImGui.Text('Hold middle mouse button to scroll (%.2f,%.2f)', [scrolling.x, scrolling.y]);
142143
ImGui.SameLine();
143144
ImGui.Checkbox('Show grid', @show_grid);
144145
ImGui.PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2.New(1, 1));
145146
ImGui.PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2.New(0, 0));
146147
ImGui.PushStyleColor(ImGuiCol_ChildBg, IM_COL32(60, 60, 70, 200));
147-
ImGui.BeginChild('scrolling_region', ImVec2.New(0, 0), ImGuiChildFlags_None,
148+
ImGui.BeginChild('scrolling_region', ImVec2.New(0, 0), ImGuiChildFlags_Border,
148149
ImGuiWindowFlags_NoScrollbar Or ImGuiWindowFlags_NoMove);
149150
ImGui.PopStyleVar(); // WindowPadding
150151
ImGui.PushItemWidth(120.0);
@@ -156,22 +157,22 @@
156157
If show_grid Then
157158
Begin
158159
GRID_COLOR := IM_COL32(200, 200, 200, 40);
159-
GRID_SZ := 64.0;
160160
win_pos := ImGui.GetCursorScreenPos();
161161
canvas_sz := ImGui.GetWindowSize();
162162

163-
x := FMod(scrolling.x, GRID_SZ);
164-
while x < canvas_sz.x do
165-
begin
163+
x := Math.FMod(scrolling.x, GRID_SZ);
164+
While x < canvas_sz.x Do
165+
Begin
166166
draw_list^.AddLine(ImVec2.New(x, 0.0) + win_pos, ImVec2.New(x, canvas_sz.y) + win_pos, GRID_COLOR);
167-
x := x + GRID_SZ;
168-
end;
167+
x += GRID_SZ;
168+
End;
169169

170-
y := fmod(scrolling.y, GRID_SZ);
170+
//y := scrolling.y - GRID_SZ * Int(scrolling.y/GRID_SZ);
171+
y := FMod(scrolling.y, GRID_SZ);
171172
While y < canvas_sz.y Do
172173
Begin
173174
draw_list^.AddLine(ImVec2.New(0.0, y) + win_pos, ImVec2.New(canvas_sz.x, y) + win_pos, GRID_COLOR);
174-
y := y + GRID_SZ;
175+
y += GRID_SZ;
175176
End;
176177
End;
177178

@@ -184,9 +185,12 @@
184185
link := links[link_idx];
185186
node_inp := nodes[link.InputIdx];
186187
node_out := nodes[link.OutputIdx];
188+
189+
node_out.Value += node_inp.Value;
190+
187191
p1 := offset + node_inp.GetOutputSlotPos(link.InputSlot);
188192
p2 := offset + node_out.GetInputSlotPos(link.OutputSlot);
189-
draw_list^.AddBezierCubic(p1, p1 + ImVec2.New(-50, 0), p2 + ImVec2.New(+50, 0), p2, IM_COL32(200, 200, 100, 255), 3.0);
193+
draw_list^.AddBezierCubic(p1, p1 + ImVec2.New(+50, 0), p2 + ImVec2.New(-50, 0), p2, IM_COL32(200, 200, 100, 255), 3.0);
190194
End;
191195

192196
// Display nodes
@@ -200,6 +204,7 @@
200204
// Display node contents first
201205
draw_list^.ChannelsSetCurrent(1); // Foreground
202206
old_any_active := ImGui.IsAnyItemActive();
207+
203208
ImGui.SetCursorScreenPos(node_rect_min + NODE_WINDOW_PADDING);
204209
ImGui.BeginGroup(); // Lock horizontal position
205210
ImGui.Text('%s', [node.Name]);
@@ -208,7 +213,7 @@
208213
ImGui.EndGroup();
209214

210215
// Save the size of what we have emitted and whether any of the widgets are being used
211-
node_widgets_active := (Not old_any_active And ImGui.IsAnyItemActive());
216+
node_widgets_active := ((Not old_any_active) And ImGui.IsAnyItemActive());
212217
node.Size := ImGui.GetItemRectSize() + NODE_WINDOW_PADDING + NODE_WINDOW_PADDING;
213218
node_rect_max := node_rect_min + node.Size;
214219

@@ -227,7 +232,7 @@
227232
node_selected := node.ID;
228233

229234
If (node_moving_active And ImGui.IsMouseDragging(ImGuiMouseButton_Left)) Then
230-
node.Pos := node.Pos + io^.MouseDelta;
235+
node.Pos += io^.MouseDelta;
231236

232237
If ((node_hovered_in_list = node.ID) Or (node_hovered_in_scene = node.ID) Or
233238
((node_hovered_in_list = -1) And (node_selected = node.ID))) Then
@@ -253,15 +258,15 @@
253258
// Open context menu
254259
If ImGui.IsMouseReleased(ImGuiMouseButton_Right) Then
255260
Begin
256-
If ImGui.IsWindowHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup) Or
257-
(Not ImGui.IsAnyItemHovered()) Then
261+
If ImGui.IsWindowHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup) Or (Not ImGui.IsAnyItemHovered()) Then
258262
Begin
263+
node_selected := -1;
259264
node_hovered_in_list := -1;
260265
node_hovered_in_scene := -1;
261-
node_selected := -1;
262266
open_context_menu := True;
263267
End;
264268
End;
269+
265270
If open_context_menu Then
266271
Begin
267272
ImGui.OpenPopup('context_menu');
@@ -270,6 +275,7 @@
270275
If (node_hovered_in_scene <> -1) Then
271276
node_selected := node_hovered_in_scene;
272277
End;
278+
273279
// Draw context menu
274280
ImGui.PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2.New(8, 8));
275281
If ImGui.BeginPopup('context_menu') Then
@@ -294,8 +300,7 @@
294300
Else
295301
Begin
296302
If ImGui.MenuItem('Add') Then
297-
nodes.Add(Node.Create(nodes.Count, 'New node', scene_pos, 0.5,
298-
ImVec4.New(100, 100, 200), 2, 2));
303+
nodes.Add(Node.Create(nodes.Count, 'New node', scene_pos, 0.5, ImVec4.New(100, 100, 200), 2, 2));
299304
If ImGui.MenuItem('Past', nil, False, False) Then
300305
Begin
301306
End;
@@ -305,9 +310,9 @@
305310
ImGui.PopStyleVar();
306311

307312
// Scrolling
308-
If (ImGui.IsWindowHovered() And (Not ImGui.IsAnyItemActive()) And
309-
ImGui.IsMouseDragging(ImGuiMouseButton_Middle, 0.0)) Then
310-
scrolling := scrolling + io.MouseDelta;
313+
If (ImGui.IsWindowHovered() And (Not ImGui.IsAnyItemActive()) And ImGui.IsMouseDragging(
314+
ImGuiMouseButton_Middle, 0.0)) Then
315+
scrolling += io.MouseDelta;
311316

312317
ImGui.PopItemWidth();
313318
ImGui.EndChild();
@@ -323,17 +328,17 @@
323328
Function TNode.GetInputSlotPos(slotNo: Integer): ImVec2;
324329
Begin
325330
Result.x := Pos.x;
326-
Result.y := Pos.y + Size.y * Single(slotNo + 1) / (Single(InputsCount) + 1);
331+
Result.y := Pos.y + Size.y * Single(slotNo + 1) / Single(InputsCount + 1);
327332
End;
328333

329334
Function TNode.GetOutputSlotPos(slotNo: Integer): ImVec2;
330335
Begin
331336
Result.x := Pos.x + Size.x;
332-
Result.y := Pos.y + Size.y * Single(slotNo + 1) / (Single(OutputsCount) + 1);
337+
Result.y := Pos.y + Size.y * Single(slotNo + 1) / Single(OutputsCount + 1);
333338
End;
334339

335-
Constructor TNode.Create(id_: Integer; Const Name_: PAnsiChar;
336-
pos_: ImVec2; Value_: Single; color_: ImVec4; inputsCount_, outputsCount_: Integer);
340+
Constructor TNode.Create(id_: Integer; Const Name_: PAnsiChar; pos_: ImVec2; Value_: Single;
341+
color_: ImVec4; inputsCount_, outputsCount_: Integer);
337342
Begin
338343
Self.ID := id_;
339344
StrLCopy(Self.Name, name_, SizeOf(Self.Name) - 1);

0 commit comments

Comments
 (0)