|
| 1 | +# Connecting a New Model |
| 2 | + |
| 3 | +The `Viewer` is decoupled from concrete business classes using an Anti-Corruption Layer (ACL) pattern. To connect a new data model, you must wrap it in an adapter class that adheres to the `IViewable` protocol. |
| 4 | + |
| 5 | +## The IViewable Protocol |
| 6 | + |
| 7 | +Every adapter must implement the following 4 methods: |
| 8 | +1. `bind_update(callback)`: Registers a callback that triggers whenever the underlying model is modified. |
| 9 | +2. `unbind_update()`: Clears the registered callback. |
| 10 | +3. `get_delta_load()`: Packs the entire initial state of the model into an `ADD` operation payload. |
| 11 | +4. `handle_event(event)`: Translates inbound `ViewEvent` user actions into a list of executable `ViewerCommand` payloads. |
| 12 | + |
| 13 | +## Step-by-Step Implementation Example |
| 14 | + |
| 15 | +Let's write a fully compliant adapter for a hypothetical custom polygonal model component (`CustomPolylineModel`). |
| 16 | + |
| 17 | +### 1. Build the Adapter Class |
| 18 | + |
| 19 | +```python |
| 20 | +import logging |
| 21 | +from bot.viewer.viewable import IViewable |
| 22 | +from bot.viewer.contracts import ScenePayload, SceneUpdateOp, ViewEventType, ViewerCommandType, ViewerCommand, ViewEvent |
| 23 | +from bot.viewer.serialize import pack_curve_delta |
| 24 | +from bot.viewer.tags import encode, decode, is_namespaced |
| 25 | + |
| 26 | +_logger = logging.getLogger(__name__) |
| 27 | + |
| 28 | +class CustomPolylineAdapter(IViewable): |
| 29 | + NAMESPACE = "polyline" |
| 30 | + |
| 31 | + def __init__(self, model): |
| 32 | + self._model = model |
| 33 | + self._update_callback = None |
| 34 | + self._last_hovered = None |
| 35 | + # Connect to your model's observer pattern |
| 36 | + self._model.add_observer(self) |
| 37 | + |
| 38 | + def bind_update(self, callback): |
| 39 | + self._update_callback = callback |
| 40 | + |
| 41 | + def unbind_update(self): |
| 42 | + self._update_callback = None |
| 43 | + |
| 44 | + def get_delta_load(self): |
| 45 | + """Constructs the initial full scene payload.""" |
| 46 | + return { |
| 47 | + "op": SceneUpdateOp.ADD, |
| 48 | + "changed_curves": self._build_render_deltas(), |
| 49 | + "bounds": { |
| 50 | + "min": [-10, -10, -10], |
| 51 | + "max": [10, 10, 10], |
| 52 | + "center": [0, 0, 0], |
| 53 | + "size": [20, 20, 20] |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + def handle_event(self, event): |
| 58 | + """Processes user clicks or selections on this specific model namespace.""" |
| 59 | + commands = [] |
| 60 | + event_type = event.get("event_type") |
| 61 | + tag = event.get("curve_tag") or event.get("tag") |
| 62 | + |
| 63 | + # Validate that the tag belongs to this adapter's namespace |
| 64 | + if tag and is_namespaced(str(tag)): |
| 65 | + ns, local_id = decode(str(tag)) |
| 66 | + if ns != self.NAMESPACE: |
| 67 | + return [] |
| 68 | + else: |
| 69 | + return [] |
| 70 | + |
| 71 | + if event_type == ViewEventType.CURVE_SELECTED: |
| 72 | + commands.append({ |
| 73 | + "cmd": ViewerCommandType.UPDATE_HUD, |
| 74 | + "text": f"Selected Polyline Local ID: {local_id}" |
| 75 | + }) |
| 76 | + commands.append({ |
| 77 | + "cmd": ViewerCommandType.HIGHLIGHT_CURVE, |
| 78 | + "tag": str(tag), |
| 79 | + "color": [0, 0.8, 1, 1] # Highlight blue |
| 80 | + }) |
| 81 | + return commands |
| 82 | + |
| 83 | + def update(self, model): |
| 84 | + """Triggered automatically when the domain model emits a change notice.""" |
| 85 | + if self._update_callback is not None: |
| 86 | + payload = { |
| 87 | + "op": SceneUpdateOp.UPDATE, |
| 88 | + "changed_curves": self._build_render_deltas() |
| 89 | + } |
| 90 | + self._update_callback(payload) |
| 91 | + |
| 92 | + def _build_render_deltas(self): |
| 93 | + """Converts internal raw coordinates to flat float32 byte payloads.""" |
| 94 | + deltas = {} |
| 95 | + for item_id, polyline in self._model.get_all_items().items(): |
| 96 | + # Generate a namespaced tag boundary (e.g., 'polyline:1') |
| 97 | + ns_tag = encode(self.NAMESPACE, item_id) |
| 98 | + |
| 99 | + # pack_curve_delta converts vertex arrays to structural bytes |
| 100 | + deltas[ns_tag] = pack_curve_delta( |
| 101 | + curve_points=polyline.vertices, # list of [x, y, z] |
| 102 | + edges=polyline.edges, # list of (idx_a, idx_b) |
| 103 | + curve_type="linear" |
| 104 | + ) |
| 105 | + return deltas |
| 106 | +``` |
| 107 | + |
| 108 | +### 2. Connect and Execute |
| 109 | +To initialize and bind your new adapter configuration directly via the Viewer: |
| 110 | + |
| 111 | + |
| 112 | +```python |
| 113 | +from bot.viewer.viewer import Viewer |
| 114 | +# Assuming your models exist: |
| 115 | +my_model = CustomPolylineModel() |
| 116 | + |
| 117 | +viewer = Viewer() |
| 118 | +# Inject the custom adapter into the private interface adapter slot |
| 119 | +viewer._connect(CustomPolylineAdapter(my_model)) |
| 120 | +viewer.run() |
| 121 | +``` |
| 122 | + |
0 commit comments