|
| 1 | +## Introduction |
| 2 | + |
| 3 | +Create node and edge game objects from text. |
| 4 | + |
| 5 | +- Author: Rex |
| 6 | +- Method |
| 7 | + |
| 8 | +## Live demos |
| 9 | + |
| 10 | +- [Graph in container](https://codepen.io/rexrainbow/pen/pvgbjGz) |
| 11 | + |
| 12 | +## Usage |
| 13 | + |
| 14 | +[Sample code](https://github.com/rexrainbow/phaser3-rex-notes/tree/master/examples/graph-layout) |
| 15 | + |
| 16 | +### Install plugin |
| 17 | + |
| 18 | +#### Load minify file |
| 19 | + |
| 20 | +- Load plugin (minify file) in preload stage |
| 21 | + ```javascript |
| 22 | + scene.load.scenePlugin('rexgraphplugin', 'https://raw.githubusercontent.com/rexrainbow/phaser3-rex-notes/master/dist/rexgraphplugin.min.js', 'rexGraph', 'rexGraph'); |
| 23 | + ``` |
| 24 | +- Add graph object |
| 25 | + ```javascript |
| 26 | + var graph = scene.rexGraph.add.graph(config); |
| 27 | + ``` |
| 28 | +- Create node and edge game objects from text |
| 29 | + ```javascript |
| 30 | + scene.rexGraph.buildGraphFromText(graph, config); |
| 31 | + ``` |
| 32 | + |
| 33 | +#### Import plugin |
| 34 | + |
| 35 | +- Install rex plugins from npm |
| 36 | + ``` |
| 37 | + npm i phaser3-rex-plugins |
| 38 | + ``` |
| 39 | +- Install plugin in [configuration of game](game.md#configuration) |
| 40 | + ```javascript |
| 41 | + import GraphPlugin from 'phaser3-rex-plugins/plugins/graph-plugin.js'; |
| 42 | + var config = { |
| 43 | + // ... |
| 44 | + plugins: { |
| 45 | + scene: [{ |
| 46 | + key: 'rexGraph', |
| 47 | + plugin: GraphPlugin, |
| 48 | + mapping: 'rexGraph' |
| 49 | + }, |
| 50 | + // ... |
| 51 | + ] |
| 52 | + } |
| 53 | + // ... |
| 54 | + }; |
| 55 | + var game = new Phaser.Game(config); |
| 56 | + ``` |
| 57 | +- Add graph object |
| 58 | + ```javascript |
| 59 | + var graph = scene.rexGraph.add.graph(config); |
| 60 | + ``` |
| 61 | +- Create node and edge game objects from text |
| 62 | + ```javascript |
| 63 | + scene.rexGraph.buildGraphFromText(graph, config); |
| 64 | + ``` |
| 65 | + |
| 66 | +#### Import class |
| 67 | + |
| 68 | +- Install rex plugins from npm |
| 69 | + ``` |
| 70 | + npm i phaser3-rex-plugins |
| 71 | + ``` |
| 72 | +- Import class |
| 73 | + ```javascript |
| 74 | + import { Graph, BuildGraphFromText } from 'phaser3-rex-plugins/plugins/graph-components.js'; |
| 75 | + ``` |
| 76 | +- Add graph object |
| 77 | + ```javascript |
| 78 | + var graph = new Graph(scene, config); |
| 79 | + ``` |
| 80 | +- Create node and edge game objects from text |
| 81 | + ```javascript |
| 82 | + BuildGraphFromText(graph, config); |
| 83 | + ``` |
| 84 | + |
| 85 | +### Create node and edge game objects from text |
| 86 | + |
| 87 | +```javascript |
| 88 | +scene.rexGraph.buildGraphFromText(graph, { |
| 89 | + onCreateNodeGameObject(scene, id, parameters) { |
| 90 | + return gameObject; |
| 91 | + }, |
| 92 | +
|
| 93 | + onCreateEdgeGameObject(scene, id, parameters) { |
| 94 | + return gameObject; |
| 95 | + }, |
| 96 | +
|
| 97 | + text: text |
| 98 | +}); |
| 99 | +``` |
| 100 | + |
| 101 | +- `graph` : [Graph](graph.md) object |
| 102 | +- `onCreateNodeGameObject` : Callback to create node game object |
| 103 | + ```javascript |
| 104 | + function(scene, id, parameters) { |
| 105 | + return gameObject; |
| 106 | + } |
| 107 | + ``` |
| 108 | + - `gameObject` : Game object with position (`{x, y}`) and size (`{width, height}`) |
| 109 | + - `parameters` : See [Syntax of text](#syntax-of-text) |
| 110 | +- `onCreateEdgeGameObject` : Callback to create edge game object |
| 111 | + ```javascript |
| 112 | + function(scene, id, parameters) { |
| 113 | + return scene.rexGraph.add.line(config) |
| 114 | + } |
| 115 | + ``` |
| 116 | + - `gameObject` : [Line-shape](shape-line2.md) |
| 117 | + - `parameters` : See [Syntax of text](#syntax-of-text) |
| 118 | +- `text` : See [Syntax of text](#syntax-of-text) |
| 119 | + |
| 120 | + |
| 121 | +### Syntax of Text |
| 122 | + |
| 123 | +Example: |
| 124 | + |
| 125 | +```text |
| 126 | +NODE [ |
| 127 | + padding=3, |
| 128 | + color=0x888888 |
| 129 | + ] |
| 130 | +
|
| 131 | +A [label=A, elk.layered.priority=1] |
| 132 | +B [label=B] |
| 133 | +
|
| 134 | +A -> B -> C [color=0xFF0000] |
| 135 | +A -> * -> D |
| 136 | +
|
| 137 | +E -> *1 -> F |
| 138 | +G -> *1 |
| 139 | +
|
| 140 | +H *> I |
| 141 | +``` |
| 142 | + |
| 143 | +#### Node Defaults |
| 144 | + |
| 145 | +``` |
| 146 | +NODE [key=value, ...] |
| 147 | +``` |
| 148 | +
|
| 149 | +- Declares default parameters for all subsequent nodes. |
| 150 | +- Example: |
| 151 | + ```text |
| 152 | + NODE [padding=3, color=0x888888] |
| 153 | + ``` |
| 154 | +- Every new node inherits these attributes unless overridden. |
| 155 | +
|
| 156 | +#### Edge Defaults |
| 157 | +
|
| 158 | +``` |
| 159 | +EDGE [key=value, ...] |
| 160 | +``` |
| 161 | +
|
| 162 | +- Declares default parameters for all subsequent edges. |
| 163 | +- Example: |
| 164 | + ```text |
| 165 | + EDGE [color=0x888888, width=2] |
| 166 | + ``` |
| 167 | +
|
| 168 | +#### Node Definition |
| 169 | +
|
| 170 | +``` |
| 171 | +<nodeId> [key=value, ...] |
| 172 | +``` |
| 173 | +
|
| 174 | +- Declares or updates a single node. |
| 175 | +- Attributes in brackets overwrite the default `NODE` parameters. |
| 176 | +- Example: |
| 177 | + ```text |
| 178 | + A [label=A, elk.layered.priority=1] |
| 179 | + B [label=B] |
| 180 | + ``` |
| 181 | +- If no attribute block is provided, the node is still created with defaults. |
| 182 | +
|
| 183 | +#### Edge Definition |
| 184 | +
|
| 185 | +``` |
| 186 | +<source> -> <target> |
| 187 | +<source> -> <mid> -> <target> |
| 188 | +``` |
| 189 | +
|
| 190 | +- Defines connections between nodes. |
| 191 | +- A chain like `A -> B -> C` creates two edges: |
| 192 | + ``` |
| 193 | + A -> B |
| 194 | + B -> C |
| 195 | + ``` |
| 196 | +- Attributes in brackets (`[ ... ]`) after the chain apply to all edges in that chain. |
| 197 | +- Example: |
| 198 | + ``` |
| 199 | + A -> B -> C [color=0xFF0000] |
| 200 | + ``` |
| 201 | +
|
| 202 | +#### Dummy Nodes |
| 203 | +
|
| 204 | +Used to control layout (width=0, height=0) without creating visible node objects. |
| 205 | +
|
| 206 | +Syntax: |
| 207 | +
|
| 208 | +- `*` : Anonymous dummy node |
| 209 | +- `*name` : Named dummy node |
| 210 | +
|
| 211 | +Examples: |
| 212 | +
|
| 213 | +``` |
| 214 | +A -> * -> D # Creates a new dummy node between A and D |
| 215 | +E -> *1 -> F # Creates or reuses a named dummy node "*1" |
| 216 | +G -> *1 # Reuses the same named dummy node |
| 217 | +``` |
| 218 | +
|
| 219 | +Builder won't create game object for dummy node, they exist only for layout. |
| 220 | +
|
| 221 | +#### Invisible Edges |
| 222 | +
|
| 223 | +``` |
| 224 | +<nodeA> *> <nodeB> |
| 225 | +``` |
| 226 | +
|
| 227 | +- Defines an invisible edge. |
| 228 | +- Builder won't create game object for invisible edge, they exist only for layout. |
| 229 | +- Example: |
| 230 | + ```text |
| 231 | + H *> I |
| 232 | + ``` |
| 233 | +
|
| 234 | +--- |
| 235 | +
|
| 236 | +#### Attribute Blocks |
| 237 | +
|
| 238 | +``` |
| 239 | +[key=value, key2=value2, ...] |
| 240 | +``` |
| 241 | +
|
| 242 | +- Enclosed in square brackets. |
| 243 | +- Commas are optional before newlines. |
| 244 | +- Acceptable value types: |
| 245 | + - Numbers: `3`, `2.5` |
| 246 | + - Hexadecimal: `0x888888` |
| 247 | + - Strings: `"text"` or `'text'` |
| 248 | + - Identifiers: `solid`, `circle` |
| 249 | +- Example: |
| 250 | + ``` |
| 251 | + [label="Start", color=0xFF0000, elk.layered.priority=1] |
| 252 | + ``` |
| 253 | +
|
| 254 | +#### Comments |
| 255 | +
|
| 256 | +Three comment styles are supported: |
| 257 | +
|
| 258 | +```text |
| 259 | +# comment |
| 260 | +// comment |
| 261 | +/* multi-line |
| 262 | + comment */ |
| 263 | +``` |
| 264 | + |
| 265 | +Comments can appear anywhere and are ignored. |
| 266 | + |
| 267 | +#### Statement Endings |
| 268 | + |
| 269 | +Statements may be separated by: |
| 270 | + |
| 271 | +- A newline (`\n`) |
| 272 | +- A semicolon (`;`) |
| 273 | + |
| 274 | +Example: |
| 275 | + |
| 276 | +```text |
| 277 | +A [label="Node A"]; |
| 278 | +B [label="Node B"] |
| 279 | +A -> B |
| 280 | +``` |
| 281 | + |
| 282 | + |
| 283 | +#### Summary of Core Constructs |
| 284 | + |
| 285 | +| Type | Syntax | Description | |
| 286 | +| --------------- | ---------------------- | --------------------------------------------- | |
| 287 | +| Node defaults | `NODE [key=value]` | Define attributes applied to all future nodes | |
| 288 | +| Edge defaults | `EDGE [key=value]` | Define attributes applied to all future edges | |
| 289 | +| Node | `A [key=value]` | Create or update node A | |
| 290 | +| Edge | `A -> B` | Create edge A→B | |
| 291 | +| Edge chain | `A -> B -> C` | Create multiple edges | |
| 292 | +| Edge attributes | `A -> B [key=value]` | Attributes applied to all edges in the chain | |
| 293 | +| Dummy node | `*` or `*name` | Invisible node for layout | |
| 294 | +| Invisible edge | `A *> B` | Hidden edge for layout | |
| 295 | +| Comment | `#`, `//`, `/* ... */` | Ignored | |
| 296 | +| Statement end | `;` or newline | Both accepted | |
| 297 | + |
| 298 | +--- |
| 299 | + |
| 300 | +#### Complete Example |
| 301 | + |
| 302 | +``` |
| 303 | +# Define node and edge defaults |
| 304 | +NODE [shape="circle", padding=5] |
| 305 | +EDGE [color=0x999999, width=2] |
| 306 | +
|
| 307 | +# Define nodes |
| 308 | +A [label="Start"] |
| 309 | +B [label="Middle"] |
| 310 | +C [label="End"] |
| 311 | +
|
| 312 | +# Define edges |
| 313 | +A -> B -> C [color=0xFF0000] # Colored chain |
| 314 | +A -> * -> D # Dummy node between A and D |
| 315 | +E -> *1 -> F # Named dummy node used twice |
| 316 | +G -> *1 # Reuse *1 again |
| 317 | +H *> I # Invisible edge |
| 318 | +``` |
0 commit comments