|
| 1 | +use std::any::Any; |
| 2 | + |
| 3 | +use serde::{Deserialize, Serialize}; |
| 4 | +use spring_native::prelude::NativeInterfaceRef; |
| 5 | + |
| 6 | +use crate::sbc::command_system::model::{Model, ModelFactory}; |
| 7 | +use crate::sbc::lua_bridge; |
| 8 | + |
| 9 | +inventory::submit! { ModelFactory { make: |iface| Box::new(ScenarioInfoManager::new(iface)) } } |
| 10 | + |
| 11 | +/// Project-level scenario metadata (name / description / version / author). |
| 12 | +/// Mirrors `scen_edit/model/scenario_info.lua`. Pure project state; on change it |
| 13 | +/// notifies the widget's mirror so the UI updates. |
| 14 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 15 | +pub struct ScenarioInfo { |
| 16 | + pub name: String, |
| 17 | + pub description: String, |
| 18 | + pub version: String, |
| 19 | + pub author: String, |
| 20 | +} |
| 21 | + |
| 22 | +impl Default for ScenarioInfo { |
| 23 | + fn default() -> Self { |
| 24 | + ScenarioInfo { |
| 25 | + name: String::new(), |
| 26 | + description: String::new(), |
| 27 | + version: "1".to_string(), |
| 28 | + author: String::new(), |
| 29 | + } |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +/// Partial-update payload: any field omitted leaves the current value untouched |
| 34 | +/// (mirrors Lua's `data.x or self.x` merge in `ScenarioInfo:Set`). |
| 35 | +#[derive(Debug, Clone, Default, Deserialize)] |
| 36 | +pub struct ScenarioInfoPatch { |
| 37 | + pub name: Option<String>, |
| 38 | + pub description: Option<String>, |
| 39 | + pub version: Option<String>, |
| 40 | + pub author: Option<String>, |
| 41 | +} |
| 42 | + |
| 43 | +pub struct ScenarioInfoManager { |
| 44 | + interface: NativeInterfaceRef, |
| 45 | + info: ScenarioInfo, |
| 46 | +} |
| 47 | + |
| 48 | +impl Model for ScenarioInfoManager { |
| 49 | + fn as_any_mut(&mut self) -> &mut dyn Any { |
| 50 | + self |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +impl ScenarioInfoManager { |
| 55 | + pub fn new(interface: NativeInterfaceRef) -> Self { |
| 56 | + ScenarioInfoManager { |
| 57 | + interface, |
| 58 | + info: ScenarioInfo::default(), |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /// Merge a partial patch over the current info (each absent field keeps its |
| 63 | + /// current value). |
| 64 | + pub fn set(&mut self, patch: &ScenarioInfoPatch) { |
| 65 | + if let Some(name) = &patch.name { |
| 66 | + self.info.name = name.clone(); |
| 67 | + } |
| 68 | + if let Some(description) = &patch.description { |
| 69 | + self.info.description = description.clone(); |
| 70 | + } |
| 71 | + if let Some(version) = &patch.version { |
| 72 | + self.info.version = version.clone(); |
| 73 | + } |
| 74 | + if let Some(author) = &patch.author { |
| 75 | + self.info.author = author.clone(); |
| 76 | + } |
| 77 | + self.notify(); |
| 78 | + } |
| 79 | + |
| 80 | + /// Replace the whole info (used by undo to restore a prior full snapshot). |
| 81 | + pub fn restore(&mut self, info: ScenarioInfo) { |
| 82 | + self.info = info; |
| 83 | + self.notify(); |
| 84 | + } |
| 85 | + |
| 86 | + pub fn serialize(&self) -> ScenarioInfo { |
| 87 | + self.info.clone() |
| 88 | + } |
| 89 | + |
| 90 | + fn notify(&self) { |
| 91 | + lua_bridge::widget_command( |
| 92 | + &self.interface, |
| 93 | + serde_json::json!({ |
| 94 | + "className": "WidgetSetScenarioInfoCommand", |
| 95 | + "data": self.info, |
| 96 | + }), |
| 97 | + ); |
| 98 | + } |
| 99 | +} |
0 commit comments