TachyonUI 1.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
gui_node_view_helpers.h
Go to the documentation of this file.
1#pragma once
2
3#include <gui_controls/gui_node_view_types.h> // NodeModel, NodeView
4
6#include <gui_core/gui_id.h>
8
9#include <span>
10#include <unordered_map>
11#include <unordered_set>
12#include <vector>
13
14namespace tachyon
15{
18
20 struct LineStyle;
21
24 inline ID NodeElementID(const NodeViewState& view, ID stableID) { return IDManager::ScopedID(view.canvasID, stableID); }
25
30 void ZoomNodeView(NodeViewRef view, float factor, int2 pivot = {});
32 void SetNodeViewCenter(NodeViewRef view, float2 center, float zoom);
37 void FrameNodes(NodeViewRef view, std::span<const int> indices);
43 void ScrollNodeIntoView(NodeViewRef view, int nodeIdx, int32_t margin);
44
56 void RenderCanvasGrid(CustomDrawContext& ctx, int32_t baseGridSize,
57 color4f dotColor, float dotSize, int2 originOffset = {},
58 IntRect clipCurrent = {});
59
60 // --- Canvas grid lattice ----------------------------------------------
61 // Lines fall on origin + k*grid; snap to the line at-or-below / at-or-above /
62 // nearest a value. grid must be > 0.
63
65 constexpr int32_t FloorDiv(int32_t a, int32_t b)
66 {
67 int32_t q = a / b, r = a % b;
68 if (r != 0 && ((r < 0) != (b < 0))) --q;
69 return q;
70 }
71
72 constexpr int32_t GridLineLE(int32_t v, int32_t origin, int32_t grid)
73 {
74 return origin + FloorDiv(v - origin, grid) * grid;
75 }
76
77 constexpr int32_t GridLineGE(int32_t v, int32_t origin, int32_t grid)
78 {
79 const int32_t d = GridLineLE(v, origin, grid);
80 return (d == v) ? d : d + grid;
81 }
82
83 constexpr int32_t GridLineNearest(int32_t v, int32_t origin, int32_t grid)
84 {
85 const int32_t d = GridLineLE(v, origin, grid);
86 return ((v - d) * 2 <= grid) ? d : d + grid;
87 }
88
100 IntRect srcRect, IntRect tgtRect,
101 int32_t grid, int2 gridOrigin,
102 const WireRouteOverrides& overrides = {});
103
108 WireRoute BuildCurvedWireRoute(int2 outPt, int2 inPt, int32_t reach);
109
113 float2 WireCurvePoint(int2 p0, int2 p1, int2 p2, int2 p3, float t);
114
117 constexpr uint32_t WireKey(int targetNode, int targetProp)
118 {
119 return ((uint32_t)targetNode << 16) | (uint32_t)(uint16_t)targetProp;
120 }
121
123 constexpr PropertyEndpoint WireInput(uint32_t wireKey)
124 {
125 return { int(wireKey >> 16), int(wireKey & 0xFFFFu) };
126 }
127
130 std::optional<uint32_t> WireAt(std::span<const DrawnWire> wires, int2 p, float tolerance);
131
132 // Edits: the node view makes its changes through these, so what it reports is what it did.
133
135 void ApplyNodeViewEdit(NodeViewRef view, const NodeViewEdit& edit, bool undo = false);
141 void ReleaseNodeViewEditIDs(IDManager& ids, const NodeViewEdit& edit, bool undone);
160 NodeViewEdit RemoveNodesEdit(ConstNodeViewRef view, std::span<const int> indices);
166 NodeViewEdit RerouteWireEdit(ConstNodeViewRef view, uint32_t wireKey, ID stableID, int2 pos);
177 NodeViewEdit RemoveFramesEdit(ConstNodeViewRef view, std::span<const int> indices);
186 NodeFrame FrameAroundNodes(ConstNodeViewRef view, std::span<const int> indices);
197 std::vector<int> NodesInFrame(ConstNodeViewRef view, int frame);
200 NodeViewEdit CollapseFrameEdit(ConstNodeViewRef view, int frame, bool collapse);
206 void RenameFrame(NodeViewRef view, int frame);
207
217 NodeViewEdit RemoveTransitionsEdit(ConstNodeViewRef view, std::span<const ID> transitions);
224 int NodeIndex(ConstNodeViewRef view, ID stableID);
227 void RenameTransition(NodeViewRef view, int transition);
231 void StartTransition(NodeViewRef view, int node);
232
235 {
236 int2 from {}, to {};
237 bool loop = false;
238 int2 c1 {}, c2 {};
240 bool valid = false;
241 };
242
245 TransitionPath StateTransitionPath(ConstNodeViewRef view, const StateTransition& transition, int32_t gap, int32_t loopSize);
249 int2 TransitionLabelCentre(const TransitionPath& path, int2 size, int32_t margin);
253 std::optional<ID> TransitionAt(ConstNodeViewRef view, float2 canvasPos, float tolerance, int32_t gap, int32_t loopSize);
255
264 std::unordered_map<uint32_t, WireLane> ComputeWireLaneOffsets(ConstNodeViewRef view,
265 PropertyEndpoint pendingOut = {},
266 PropertyEndpoint pendingIn = {});
267
272 std::optional<int2> inPt, IntRect tgtRect,
273 int32_t grid, int2 gridOrigin, WireLane lane = {});
278 std::optional<int2> outPt, IntRect srcRect,
279 int32_t grid, int2 gridOrigin, WireLane lane = {});
280
285
292
297
301
303 void BringNodeToFront(std::vector<int>& ordering, int n);
304
308 float2 NodeViewScreenToCanvas(const float3x2& viewTransform, IntRect canvasInnerRectScreen, int2 screen);
318
328 std::unordered_set<ID> ComputeNodesInMarquee(
329 std::span<const NodeModel> nodes,
330 IntRect canvasInnerRectScreen,
331 const float3x2& viewTransform,
332 int2 marqueeStartScreen,
333 int2 marqueeCurrentScreen);
335}
Passed to custom-draw callbacks (ElementStorage::AddCustomDraw / AddCustomDrawAfter): geometry lookup...
Definition gui_custom_draw_context.h:43
Makes element IDs: per-frame dynamic / ranged IDs (hashed from the call site or a hint and the parent...
Definition gui_id_manager.h:31
static ID ScopedID(ID scope, ID key)
The ID of the item key names (a node's stableID, say) inside scope (the container showing it): the sa...
Definition gui_id_manager.h:120
constexpr PropertyEndpoint WireInput(uint32_t wireKey)
The input a WireKey names.
Definition gui_node_view_helpers.h:123
void FrameNodes(NodeViewRef view, std::span< const int > indices)
Centres and zooms the view on the nodes at indices (ones out of range skipped); none leaves the view ...
std::unordered_map< uint32_t, WireLane > ComputeWireLaneOffsets(ConstNodeViewRef view, PropertyEndpoint pendingOut={}, PropertyEndpoint pendingIn={})
Default lane offsets (grid steps) for wires between the same two nodes, so their lanes don't coincide...
float2 NodeViewScreenToCanvas(const float3x2 &viewTransform, IntRect canvasInnerRectScreen, int2 screen)
A screen point in node.pos space, for a canvas whose padding box is canvasInnerRectScreen (screen pix...
void ApplyNodeViewEdit(NodeViewRef view, const NodeViewEdit &edit, bool undo=false)
Redoes edit (its to values), or with undo restores its from values.
WireRoute BuildCurvedWireRoute(int2 outPt, int2 inPt, int32_t reach)
A curved wire from outPt to inPt, leaving the output to the right and entering the input from the lef...
std::vector< int > NodesInFrame(ConstNodeViewRef view, int frame)
The nodes (indices) frame frame holds, which move with it: those lying wholly inside it as the view l...
float NodeViewZoom(ConstNodeViewRef view)
The node view's zoom (1 = 100%).
BasicNodeViewRef< const NodeGraph, const NodeViewState > ConstNodeViewRef
Definition gui_node_view_types.h:594
void RenderCanvasGrid(CustomDrawContext &ctx, int32_t baseGridSize, color4f dotColor, float dotSize, int2 originOffset={}, IntRect clipCurrent={})
Infinite background dot grid for a pan/zoom canvas, drawn from a CustomDrawContext callback in the ca...
int TransitionIndex(ConstNodeViewRef view, ID stableID)
Index of the transition with stableID in view.graph.transitions; -1 when there's none.
float2 WireCurvePoint(int2 p0, int2 p1, int2 p2, int2 p3, float t)
The point at t (0 to 1) along the curve the renderer draws for a line with points p0,...
void FrameSelectedNodes(NodeViewRef view)
Centres and zooms the view on the selected nodes, and in a state machine the states a selected transi...
float2 NodeViewScreenToNodePos(ConstNodeViewRef view, int2 screen)
A screen point in node.pos space, where the nodes' boxes are (NodeViewScreenToCanvas less the canvas'...
void StartTransition(NodeViewRef view, int node)
Starts a transition from state node without a drag: it follows the pointer until a click on a state (...
NodeViewEdit RerouteWireEdit(ConstNodeViewRef view, uint32_t wireKey, ID stableID, int2 pos)
An edit that puts a reroute (NodeModel::reroute) on the wire wireKey (the WireKey of its input): the ...
std::unordered_set< ID > ComputeNodesInMarquee(std::span< const NodeModel > nodes, IntRect canvasInnerRectScreen, const float3x2 &viewTransform, int2 marqueeStartScreen, int2 marqueeCurrentScreen)
Stable IDs of nodes whose {pos, lastSize} rect lies fully inside the marquee, compared in node....
constexpr int32_t GridLineNearest(int32_t v, int32_t origin, int32_t grid)
The grid line (origin + k x grid) nearest v, ties going down; grid must be > 0.
Definition gui_node_view_helpers.h:83
ID NodeElementID(const NodeViewState &view, ID stableID)
The element a view last built for the node, frame or transition stableID names: its stable ID scoped ...
Definition gui_node_view_helpers.h:24
NodeViewEdit CollapseFrameEdit(ConstNodeViewRef view, int frame, bool collapse)
An edit that collapses frame frame (or expands it), noting what it holds as it collapses (NodeFrame::...
constexpr int32_t FloorDiv(int32_t a, int32_t b)
a / b rounded toward negative infinity.
Definition gui_node_view_helpers.h:65
NodeViewEdit RemoveFramesEdit(ConstNodeViewRef view, std::span< const int > indices)
An edit that removes the frames at indices (in any order; ones out of range and repeats are skipped).
PropertyEndpoint FirstOutputAccepting(ConstNodeViewRef view, PropertyEndpoint input, int nodeIdx)
The first output of node nodeIdx, top to bottom, that may feed input; invalid when there is none.
NodeViewEdit SetInitialStateEdit(ConstNodeViewRef view, ID state)
An edit that makes state (a stableID; Invalid: none) the initial state.
NodeViewEdit ConnectEdit(ConstNodeViewRef view, PropertyEndpoint output, PropertyEndpoint input)
An edit that wires output into input; the input's old wire goes, the output keeps its others.
void ZoomNodeView(NodeViewRef view, float factor, int2 pivot={})
Zooms by factor around pivot (pixels from the canvas centre), within the view's minZoom....
void BringNodeToFront(std::vector< int > &ordering, int n)
Moves n to the front (last) of a z-order list, keeping the others' order.
void SetNodeViewCenter(NodeViewRef view, float2 center, float zoom)
Centres the view on canvas point center at zoom (clamped to minZoom..maxZoom).
NodeViewEdit DisconnectEdit(ConstNodeViewRef view, PropertyEndpoint input)
An edit that removes the wire into input, with its lane overrides.
void RenameFrame(NodeViewRef view, int frame)
Starts renaming frame frame as a double-click on its title does: a field over the title bar,...
std::optional< ID > TransitionAt(ConstNodeViewRef view, float2 canvasPos, float tolerance, int32_t gap, int32_t loopSize)
The transition whose arrow passes within tolerance of canvasPos (node.pos space), the closest when se...
NodeViewEdit RemoveRerouteEdit(ConstNodeViewRef view, int node)
An edit that takes the reroute at node out, with its wire running on as it did through it: every inpu...
NodeViewEdit RemoveNodesEdit(ConstNodeViewRef view, std::span< const int > indices)
An edit that removes the nodes at indices (in any order; ones out of range and repeats are skipped),...
NodeViewEdit AddTransitionEdit(ConstNodeViewRef view, StateTransition transition, int index=-1)
int FrameTitleAtCanvasPos(ConstNodeViewRef view, float2 canvasPos)
The frame whose title bar holds canvasPos (node.pos space), the frontmost when several do; -1 when no...
BasicNodeViewRef< NodeGraph, NodeViewState > NodeViewRef
Definition gui_node_view_types.h:593
bool ConnectionAllowed(ConstNodeViewRef view, PropertyEndpoint out, PropertyEndpoint in)
Whether a wire from output out to input in is allowed: never a same-node loop, otherwise the view's c...
NodeViewEdit RemoveTransitionsEdit(ConstNodeViewRef view, std::span< const ID > transitions)
An edit that removes the transitions with these stableIDs (unknown ones are skipped).
TransitionPath StateTransitionPath(ConstNodeViewRef view, const StateTransition &transition, int32_t gap, int32_t loopSize)
The arrow of transition: a straight line between the two states' edges, aimed from centre to centre; ...
int NodeAtCanvasPos(ConstNodeViewRef view, float2 canvasPos)
The node whose {pos, lastSize} rect holds canvasPos (node.pos space), the frontmost in view....
void ReleaseNodeViewEditIDs(IDManager &ids, const NodeViewEdit &edit, bool undone)
Releases the static IDs of the nodes, frames and transitions only the side of edit not in effect hold...
void ConnectSockets(NodeViewRef view, PropertyEndpoint output, PropertyEndpoint input)
Wires output into input without reporting to view.graph.onEdit.
void FrameAllNodes(NodeViewRef view)
Centres and zooms the view on all nodes.
void CommitNodeViewEdit(NodeViewRef view, const NodeViewEdit &edit)
Applies edit and reports it to view.graph.onEdit.
int2 TransitionLabelCentre(const TransitionPath &path, int2 size, int32_t margin)
Where the middle of a label size big goes on path: half way along it and beside it,...
void ScrollNodeIntoView(NodeViewRef view, int nodeIdx, int32_t margin)
Pans the view, at the same zoom, by as little as brings node nodeIdx inside it with margin pixels to ...
WireRoute BuildWireRoute(int2 outPt, int2 inPt, IntRect srcRect, IntRect tgtRect, int32_t grid, int2 gridOrigin, const WireRouteOverrides &overrides={})
Orthogonal route for a wire from an output socket (outPt, on srcRect's right edge) to an input socket...
int NodeIndex(ConstNodeViewRef view, ID stableID)
Index of the node with stableID in view.graph.nodes; -1 when there's none.
NodeViewEdit AddNodeEdit(ConstNodeViewRef view, NodeModel node, int index=-1)
An edit that adds node at index (-1, or past the end: last), drawn in front of the others.
WireRoute BuildPendingWireRoute(int2 outPt, IntRect srcRect, int2 cursor, std::optional< int2 > inPt, IntRect tgtRect, int32_t grid, int2 gridOrigin, WireLane lane={})
A wire being dragged from an output: routed as the connection it would make when it is over an input ...
void RenameTransition(NodeViewRef view, int transition)
Starts relabelling transition transition as a double-click on it does: a field on its arrow,...
NodeViewEdit AddFrameEdit(ConstNodeViewRef view, NodeFrame frame, int index=-1)
An edit that adds frame at index (-1, or past the end: last, in front of the other frames,...
NodeFrame FrameAroundNodes(ConstNodeViewRef view, std::span< const int > indices)
A frame round the nodes at indices, as the view last laid them out: a grid cell of room on each side,...
std::optional< uint32_t > WireAt(std::span< const DrawnWire > wires, int2 p, float tolerance)
The wire within tolerance of point p (the closest when several are); none when no wire is.
PropertyEndpoint FirstInputAccepting(ConstNodeViewRef view, PropertyEndpoint output, int nodeIdx)
The first input of node nodeIdx, top to bottom, that a wire from output may connect to; invalid when ...
NodeViewEdit GroupSelectionEdit(ConstNodeViewRef view, NodeFrame frame)
An edit that puts the selected nodes and frames in frame: its place and size are set round them as Fr...
constexpr uint32_t WireKey(int targetNode, int targetProp)
Stable key for a wire, identified by its target (input) endpoint - each input carries at most one inc...
Definition gui_node_view_helpers.h:117
float2 NodeViewCanvasToScreen(ConstNodeViewRef view, float2 canvasPos)
A point in node.pos space on the screen, where the view last drew it: the other way round from NodeVi...
WireRoute BuildPendingWireRouteToInput(int2 inPt, IntRect tgtRect, int2 cursor, std::optional< int2 > outPt, IntRect srcRect, int32_t grid, int2 gridOrigin, WireLane lane={})
A wire being dragged from an input: routed as the connection it would make when it is over an output ...
constexpr int32_t GridLineGE(int32_t v, int32_t origin, int32_t grid)
The grid line (origin + k x grid) at or above v; grid must be > 0.
Definition gui_node_view_helpers.h:77
constexpr int32_t GridLineLE(int32_t v, int32_t origin, int32_t grid)
The grid line (origin + k x grid) at or below v; grid must be > 0.
Definition gui_node_view_helpers.h:72
Definition gui_logging.h:24
A 64-bit element ID whose bit layout depends on its IDMode.
Definition gui_id.h:23
Definition gui_base_types.h:406
Definition gui_renderer.h:33
A frame (a comment box): a titled, colored box behind a group of nodes.
Definition gui_node_view_types.h:119
One node of a node view.
Definition gui_node_view_types.h:69
One thing the user did in a node view, as values before and after, so a host can keep the undo histor...
Definition gui_node_view_types.h:226
How one view shows a NodeGraph: its pan and zoom, what's selected in it, what the user is doing in it...
Definition gui_node_view_types.h:403
tachyon::ID canvasID
Internal: the canvas element as last built; its nodes' element IDs are scoped under it (NodeElementID...
Definition gui_node_view_types.h:570
A property on a node. Default-constructed is invalid: in a socket it means "present,...
Definition gui_node_view_types.h:30
A transition of a state machine: from one state to another, or to itself.
Definition gui_node_view_types.h:107
A transition's arrow, in node.pos space, between the states as last laid out.
Definition gui_node_view_helpers.h:235
bool loop
a state to itself: a curve over its top, shaped by c1 and c2 (as RenderLine draws four points)
Definition gui_node_view_helpers.h:237
int2 from
Definition gui_node_view_helpers.h:236
int2 c2
Definition gui_node_view_helpers.h:238
int2 to
where it leaves its state's edge and where its head touches the other's
Definition gui_node_view_helpers.h:236
int2 labelAt
half way along it (a loop's top)
Definition gui_node_view_helpers.h:239
int2 c1
Definition gui_node_view_helpers.h:238
bool valid
false: a state is missing, or not laid out yet
Definition gui_node_view_helpers.h:240
A wire's place among the wires between the same two nodes, so their default lanes nest instead of coi...
Definition gui_node_view_types.h:167
User-pinned lanes of one wire (unset = default), in the route's space.
Definition gui_node_view_types.h:178
Polyline plus its draggable interior segments (never the end stubs).
Definition gui_node_view_types.h:198
Definition gui_base_types.h:394
Definition gui_base_types.h:120
2D affine transform matrix (2 rows x 3 columns): x' = r0.x*x + r0.y*y + r0.z y' = r1....
Definition gui_base_types.h:218
Integer 2D vector.
Definition gui_base_types.h:18