Skip to content

Commit b6dfce5

Browse files
committed
docs: add documentation for pin-compatibility example
Adds a README.md for the pin-compatibility example explaining its type encoding, validation logic, and visual feedback features. Also updates the main project README to include the new example.
1 parent 73525a6 commit b6dfce5

2 files changed

Lines changed: 65 additions & 0 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,9 @@ let cache = tracker.cache(); // Use for hit testing
429429
- **advanced:** A comprehensive example demonstrating custom nodes, widgets inside nodes, minimap, selection logic, link validation, and manual callback implementation.
430430
- Path: `examples/advanced`
431431
- Run: `cargo run -p advanced`
432+
- **pin-compatibility:** Demonstrates type-safe connections with a compatibility matrix, visual validation feedback, and custom pin behaviors.
433+
- Path: `examples/pin-compatibility`
434+
- Run: `cargo run -p pin-compatibility`
432435

433436
## License
434437

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Pin Compatibility Example
2+
3+
This example demonstrates how to implement a robust validation system for connecting nodes with different data types. It shows how to enforce type safety in your node editor, preventing users from creating invalid connections (e.g., connecting a String output to a Boolean input).
4+
5+
## Key Concepts
6+
7+
### 1. Type Encoding
8+
In this example, pin IDs carry type information. The application encodes pin IDs as:
9+
`Pin ID = (Base ID) + (Type ID)`
10+
11+
Where:
12+
- **Source Node (Outputs):** Base ID = 100
13+
- **Sink Node (Inputs):** Base ID = 200
14+
- **Type IDs:**
15+
- 0: Execute (Flow control)
16+
- 1: Integer
17+
- 2: Float
18+
- 3: String
19+
- 4: Boolean
20+
- 5: Object
21+
- 6: Array
22+
- 7: Any
23+
24+
This allows the backend to instantly determine a pin's data type just by looking at its ID (`pin_id % 100`).
25+
26+
### 2. Custom Validation Logic
27+
The core logic resides in the `TypeCompatibilityValidator` struct which implements the `LinkValidator` trait. It enforces a compatibility matrix:
28+
29+
| Source Type | Compatible Targets |
30+
| :--- | :--- |
31+
| **Execute** | Execute |
32+
| **Integer** | Integer, Float, String, Any |
33+
| **Float** | Float, String, Any |
34+
| **String** | String, Any |
35+
| **Boolean** | Boolean, Integer, Any |
36+
| **Object** | Object, Any |
37+
| **Array** | Array, Any |
38+
| **Any** | All types |
39+
40+
### 3. Visual Feedback
41+
The UI provides immediate feedback during link creation:
42+
- **Color Coding:** Pins and links are colored by type (e.g., Red for Boolean, Cyan for Integer).
43+
- **Validation Indicators:** A green checkmark appears on compatible target pins when dragging a link, guiding the user to valid connections.
44+
45+
## Code Highlights
46+
47+
### Rust Backend
48+
- **`examples/pin-compatibility/src/main.rs`**:
49+
- `data_types` module defines the type constants.
50+
- `types_compatible` function implements the matrix logic.
51+
- `TypeCompatibilityValidator` struct plugs into the `NodeEditorController`'s validation pipeline.
52+
53+
### Slint UI
54+
- **`examples/pin-compatibility/ui/pin-compatibility.slint`**:
55+
- `ValidatedPin` component adds the green checkmark overlay.
56+
- `MainWindow` handles the `valid-target-pin-id` logic to update the UI state based on the drag operation.
57+
58+
## Running the Example
59+
60+
```bash
61+
cargo run -p pin-compatibility
62+
```

0 commit comments

Comments
 (0)