Implement pipeline hot-rebuild with state preservation

- Add save_state/restore_state methods to CameraStage
- Add save_state/restore_state methods to DisplayStage
- Extend Pipeline._copy_stage_state() to preserve camera/display state
- Add save_state/restore_state methods to UIPanel for UI state preservation
- Update pipeline_runner to preserve UI state across preset changes

Camera state preserved:
- Position (x, y)
- Mode (feed, scroll, horizontal, etc.)
- Speed, zoom, canvas dimensions
- Internal timing state

Display state preserved:
- Initialization status
- Dimensions
- Reuse flag for display reinitialization

UI Panel state preserved:
- Stage enabled/disabled status
- Parameter values
- Selected stage and focused parameter
- Scroll position

This enables manual/event-driven rebuilds when inlet-outlet connections change,
while preserving all relevant state across pipeline mutations.
This commit is contained in:
2026-03-18 23:30:24 -07:00
parent e684666774
commit 14d622f0d6
5 changed files with 164 additions and 1 deletions

View File

@@ -16,6 +16,58 @@ class CameraStage(Stage):
self.optional = True
self._last_frame_time: float | None = None
def save_state(self) -> dict[str, Any]:
"""Save camera state for restoration after pipeline rebuild.
Returns:
Dictionary containing camera state that can be restored
"""
return {
"x": self._camera.x,
"y": self._camera.y,
"mode": self._camera.mode.value
if hasattr(self._camera.mode, "value")
else self._camera.mode,
"speed": self._camera.speed,
"zoom": self._camera.zoom,
"canvas_width": self._camera.canvas_width,
"canvas_height": self._camera.canvas_height,
"_x_float": getattr(self._camera, "_x_float", 0.0),
"_y_float": getattr(self._camera, "_y_float", 0.0),
"_time": getattr(self._camera, "_time", 0.0),
}
def restore_state(self, state: dict[str, Any]) -> None:
"""Restore camera state from saved state.
Args:
state: Dictionary containing camera state from save_state()
"""
from engine.camera import CameraMode
self._camera.x = state.get("x", 0)
self._camera.y = state.get("y", 0)
# Restore mode - handle both enum value and direct enum
mode_value = state.get("mode", 0)
if isinstance(mode_value, int):
self._camera.mode = CameraMode(mode_value)
else:
self._camera.mode = mode_value
self._camera.speed = state.get("speed", 1.0)
self._camera.zoom = state.get("zoom", 1.0)
self._camera.canvas_width = state.get("canvas_width", 200)
self._camera.canvas_height = state.get("canvas_height", 200)
# Restore internal state
if hasattr(self._camera, "_x_float"):
self._camera._x_float = state.get("_x_float", 0.0)
if hasattr(self._camera, "_y_float"):
self._camera._y_float = state.get("_y_float", 0.0)
if hasattr(self._camera, "_time"):
self._camera._time = state.get("_time", 0.0)
@property
def stage_type(self) -> str:
return "camera"

View File

@@ -13,6 +13,39 @@ class DisplayStage(Stage):
self.name = name
self.category = "display"
self.optional = False
self._initialized = False
self._init_width = 80
self._init_height = 24
def save_state(self) -> dict[str, Any]:
"""Save display state for restoration after pipeline rebuild.
Returns:
Dictionary containing display state that can be restored
"""
return {
"initialized": self._initialized,
"init_width": self._init_width,
"init_height": self._init_height,
"width": getattr(self._display, "width", 80),
"height": getattr(self._display, "height", 24),
}
def restore_state(self, state: dict[str, Any]) -> None:
"""Restore display state from saved state.
Args:
state: Dictionary containing display state from save_state()
"""
self._initialized = state.get("initialized", False)
self._init_width = state.get("init_width", 80)
self._init_height = state.get("init_height", 24)
# Restore display dimensions if the display supports it
if hasattr(self._display, "width"):
self._display.width = state.get("width", 80)
if hasattr(self._display, "height"):
self._display.height = state.get("height", 24)
@property
def capabilities(self) -> set[str]:
@@ -37,7 +70,17 @@ class DisplayStage(Stage):
def init(self, ctx: PipelineContext) -> bool:
w = ctx.params.viewport_width if ctx.params else 80
h = ctx.params.viewport_height if ctx.params else 24
result = self._display.init(w, h, reuse=False)
# Try to reuse display if already initialized
reuse = self._initialized
result = self._display.init(w, h, reuse=reuse)
# Update initialization state
if result is not False:
self._initialized = True
self._init_width = w
self._init_height = h
return result is not False
def process(self, data: Any, ctx: PipelineContext) -> Any: