summaryrefslogtreecommitdiffstats
path: root/source/diagram/diagram.h
blob: d0fe8890974d8f2ef97019479dd4e175b7569fd3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#ifndef DIAGRAM_H
#define DIAGRAM_H

// NOTE: data-oriented software structure graph. everything lives in flat pools addressed by
// index/id (never pointers), so diagrams are cache-friendly, trivially serializable, fast to
// snapshot for undo/redo, and survive hot reloads of the ui .so.

#define DIAGRAM_MAX_NODES     4096
#define DIAGRAM_MAX_EDGES     8192
#define DIAGRAM_MAX_SECTIONS  8192
#define DIAGRAM_MAX_MEMBERS   32768
#define DIAGRAM_MAX_TAGS      64
#define DIAGRAM_MAX_WAYPOINTS 8
#define DIAGRAM_TEXT_CAP      MiB(4) // doc text heap (node / edge / diagram docs)
#define DIAGRAM_LINK_SIZE     192
#define DIAGRAM_UNDO_LEVELS   32

typedef enum NodeKind {
	NODE_STRUCT,    // data layout / C struct / value type
	NODE_CLASS,     // OOP class / controller / manager
	NODE_INTERFACE, // trait / protocol / abstract interface
	NODE_ENUM,      // enum / tagged union / variant
	NODE_MODULE,    // container / package / subsystem (frames child nodes)
	NODE_SYSTEM,    // service / engine / worker / thread / process
	NODE_STORAGE,   // database / ring buffer / arena / queue / cache
	NODE_STATE,     // FSM state / lifecycle phase
	NODE_NOTE,      // architecture note / invariant / ADR
	NODE_LIFELINE,  // sequence diagram participant: header box + dashed lifeline down
	NODE_ACTOR,     // sequence diagram actor: stick figure + lifeline
	NODE_KIND_COUNT,
} NodeKind;

typedef enum NodeBorderStyle {
	BORDER_SOLID,
	BORDER_DASHED,
	BORDER_DOUBLE,
	BORDER_STYLE_COUNT,
} NodeBorderStyle;

typedef enum EdgeKind {
	EDGE_ASSOCIATION, // ->   solid, open chevron
	EDGE_DEPENDENCY,  // ..>  dashed, open chevron
	EDGE_INHERITANCE, // -|>  solid, hollow triangle
	EDGE_IMPLEMENTS,  // ..|> dashed, hollow triangle
	EDGE_COMPOSITION, // *->  solid, filled diamond at src + chevron at dst
	EDGE_AGGREGATION, // o->  solid, hollow diamond at src + chevron at dst
	EDGE_DATAFLOW,    // ==>  thick accent line, filled triangle, optional pulse
	EDGE_ASYNC,       // ~>   dashed, filled triangle
	EDGE_BIDIR,       // <->  solid, chevrons at both ends
	EDGE_LINK,        // --   undirected solid line
	EDGE_MESSAGE,     // ->>  sequence call: solid, filled triangle, horizontal at msg_y
	EDGE_REPLY,       // -->> sequence return: dashed, open chevron, horizontal at msg_y
	EDGE_KIND_COUNT,
} EdgeKind;

typedef enum EdgeRouting {
	ROUTE_AUTO,       // inherit diagram default
	ROUTE_ORTHOGONAL, // Manhattan / circuit-board elbows
	ROUTE_STRAIGHT,   // direct clipped segment
	ROUTE_BEZIER,     // smooth cubic S-curve
	ROUTE_COUNT,
} EdgeRouting;

typedef enum PortSide {
	PORT_AUTO,
	PORT_NORTH,
	PORT_SOUTH,
	PORT_EAST,
	PORT_WEST,
	PORT_COUNT,
} PortSide;

typedef struct NodeMember NodeMember;
struct NodeMember {
	b32  alive;
	s32  section;       // parent section index
	s32  next;          // next member in section (-1 end)
	char visibility;    // '+', '-', '#', '~', '@', '*', or 0
	char name[64];
	char type[80];
	char badge[24];     // e.g. "atomic", "0x10", "const", "hot", "O(1)"
};

typedef struct NodeSection NodeSection;
struct NodeSection {
	b32  alive;
	s32  node;          // parent node index
	s32  next;          // next section in node (-1 end)
	s32  first_member;  // -1 empty
	s32  last_member;
	u32  member_count;
	b32  collapsed;
	char title[40];     // e.g. "fields", "methods", "ports", "invariants"
};

typedef struct DiagramNode DiagramNode;
struct DiagramNode {
	b32  alive;
	u32  id;            // stable unique id (>0)
	u32  kind;          // NodeKind
	u32  border_style;  // NodeBorderStyle
	s32  parent;        // container node index (-1 = top-level)
	b32  collapsed;     // hide compartments (or collapse children for MODULE)
	b32  pinned;        // locked against auto-layout
	b32  selected;      // multi-select flag

	// world-space geometry (computed w/h updated from content unless manual_size)
	f32  x, y;
	f32  w, h;
	b32  manual_size;
	f32  min_w, min_h;

	u32  color;         // 0 = use theme/kind default, else 0xRRGGBBAA accent
	s32  first_section; // -1 none
	s32  last_section;
	u32  section_count;

	char key[48];       // short DSL identifier (e.g. "draw_list")
	char name[80];      // display title (e.g. "DrawList")
	char stereotype[48];// optional subtitle/stereotype (e.g. "vulkan 1.3", "lock-free")
	char tag[32];       // layer/tag name (e.g. "render", "core")
	char note[128];     // optional inline description/summary
	char link[DIAGRAM_LINK_SIZE]; // source link: "path/in/repo.c:42" (relative to the repo) or a full url

	// long form documentation, multi-line, lives in Diagram.text (doc_len 0 = none)
	u32  doc_off;
	u32  doc_len;

	// transient per-frame metrics for member-level edge anchoring
	f32  header_h;
};

typedef struct Vec2f Vec2f;
struct Vec2f {
	f32 x, y;
};

typedef struct DiagramEdge DiagramEdge;
struct DiagramEdge {
	b32   alive;
	u32   id;
	s32   src;          // node index
	s32   dst;          // node index
	u32   kind;         // EdgeKind
	u32   routing;      // EdgeRouting
	u32   src_port;     // PortSide
	u32   dst_port;     // PortSide
	s32   src_member;   // member pool index to pin to (-1 = whole node)
	s32   dst_member;   // member pool index to pin to (-1 = whole node)
	b32   selected;
	u32   color;        // 0 = theme default

	u32   waypoint_count;
	Vec2f waypoints[DIAGRAM_MAX_WAYPOINTS];

	char  label[64];
	char  src_mult[16]; // e.g. "1"
	char  dst_mult[16]; // e.g. "0..*"
	char  tag[32];

	f32   msg_y;        // sequence messages (EDGE_MESSAGE / EDGE_REPLY / between lifelines): world y
	u32   doc_off;      // documentation in Diagram.text
	u32   doc_len;
};

typedef struct TagState TagState;
struct TagState {
	char name[32];
	b32  hidden;
	b32  dimmed;
	u32  node_count;
	u32  edge_count;
};

// NOTE: compact undo snapshot stored in a ring arena (only the live arrays, no wasted slots)
typedef struct DiagramSnapshot DiagramSnapshot;
struct DiagramSnapshot {
	u32          node_count;
	u32          edge_count;
	u32          section_count;
	u32          member_count;
	u32          next_id;
	u32          text_size;
	u32          doc_off;
	u32          doc_len;
	char        *text;
	DiagramNode *nodes;
	DiagramEdge *edges;
	NodeSection *sections;
	NodeMember  *members;
};

typedef struct Diagram Diagram;
struct Diagram {
	Arena       *arena;
	char         title[96];
	char         file_path[512];
	b32          dirty;         // unsaved changes
	u32          version;       // bumped on any structural edit (invalidates caches)
	u32          next_id;
	u32          default_routing; // EdgeRouting (default ROUTE_ORTHOGONAL)

	//- documentation & source links
	u32          doc_off;       // diagram level doc in text
	u32          doc_len;
	char         repo[256];     // "https://github.com/user/project" (or a template with {path} {line})
	char         repo_ref[64];  // branch / tag / commit for repo urls, default "main"
	char         repo_root[512];// local checkout that node links are relative to ("" = find .git up from the file)

	//- doc text heap: append only, compacted when full. nodes / edges refer to it by offset
	char        *text;
	u32          text_size;
	u32          text_cap;

	DiagramNode *nodes;
	u32          node_cap;
	u32          node_count;    // high-water mark of used slots
	u32          live_nodes;

	DiagramEdge *edges;
	u32          edge_cap;
	u32          edge_count;
	u32          live_edges;

	NodeSection *sections;
	u32          section_cap;
	u32          section_count;

	NodeMember  *members;
	u32          member_cap;
	u32          member_count;

	TagState     tags[DIAGRAM_MAX_TAGS];
	u32          tag_count;
	b32          focus_mode;    // dim everything outside N-hop neighborhood of selected node
	u32          focus_hops;    // 1 or 2

	//- undo/redo ring (heap-free arena slots)
	Arena           *undo_arenas[DIAGRAM_UNDO_LEVELS];
	DiagramSnapshot  undo_ring[DIAGRAM_UNDO_LEVELS];
	u32              undo_head;
	u32              undo_count;
	u32              redo_count;

	//- stats for profiler view
	u32              visible_nodes;
	u32              visible_edges;
	f32              layout_ms;
};

//- names & defaults
internal const char *node_kind_name(u32 kind);
internal const char *node_kind_badge(u32 kind);
internal const char *border_style_name(u32 style);
internal const char *edge_kind_name(u32 kind);
internal const char *edge_kind_symbol(u32 kind);
internal const char *edge_routing_name(u32 routing);
internal const char *port_side_name(u32 port);
internal b32         node_kind_is_lifeline(u32 kind);
internal b32         edge_kind_is_message(u32 kind);
internal b32         diagram_edge_is_message(Diagram *d, s32 edge_index);

//- lifecycle
internal void diagram_init(Arena *arena, Diagram *d);
internal void diagram_clear(Diagram *d);
internal void diagram_init_scratch(Arena *arena, Diagram *d); // no undo ring
internal b32  diagram_is_descendant(Diagram *d, s32 node_index, s32 ancestor);

//- mutations
internal s32  diagram_add_node(Diagram *d, NodeKind kind, const char *name, f32 x, f32 y);
internal void diagram_delete_node(Diagram *d, s32 node_index);
internal s32  diagram_duplicate_node(Diagram *d, s32 node_index, f32 dx, f32 dy);
internal s32  diagram_add_section(Diagram *d, s32 node_index, const char *title);
internal void diagram_delete_section(Diagram *d, s32 section_index);
internal s32  diagram_add_member(Diagram *d, s32 section_index, char vis, const char *name, const char *type, const char *badge);
internal void diagram_delete_member(Diagram *d, s32 member_index);
internal s32  diagram_add_edge(Diagram *d, s32 src, s32 dst, EdgeKind kind, const char *label);
internal void diagram_delete_edge(Diagram *d, s32 edge_index);
internal void diagram_set_parent(Diagram *d, s32 child, s32 parent);
internal void diagram_move_node(Diagram *d, s32 node_index, f32 dx, f32 dy); // moves container children too
internal s32  diagram_find_node_by_key(Diagram *d, const char *key);
internal s32  diagram_find_node_by_id(Diagram *d, u32 id);

//- documentation text
internal String8 diagram_doc(Diagram *d, u32 off, u32 len);
internal b32     diagram_set_doc(Diagram *d, u32 *off, u32 *len, const char *text, u64 size);
internal void    diagram_compact_text(Diagram *d);

//- sequence diagrams
internal void diagram_fit_lifelines(Diagram *d);
internal f32  diagram_next_message_y(Diagram *d);

//- selection & filtering
internal void diagram_clear_selection(Diagram *d);
internal u32  diagram_selected_node_count(Diagram *d);
internal u32  diagram_selected_edge_count(Diagram *d);
internal void diagram_rebuild_tags(Diagram *d);
internal b32  diagram_tag_hidden(Diagram *d, const char *tag);
internal b32  diagram_tag_dimmed(Diagram *d, const char *tag);
internal b32  diagram_node_hidden(Diagram *d, s32 node_index);
internal b32  diagram_edge_hidden(Diagram *d, s32 edge_index);
internal b32  diagram_node_in_focus(Diagram *d, s32 node_index, s32 focus_node);

//- auto-sizing & container fitting
internal void diagram_measure_node(Diagram *d, s32 node_index);
internal void diagram_measure_all(Diagram *d);
internal void diagram_fit_containers(Diagram *d);

//- undo / redo
internal void diagram_push_undo(Diagram *d);
internal b32  diagram_undo(Diagram *d);
internal b32  diagram_redo(Diagram *d);

#endif // DIAGRAM_H