|
| 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