summaryrefslogtreecommitdiff
path: root/rust/crates/commands/src/scene/create_scene.rs
blob: 1f2de02b75699c2a8f615436cf5b414fb05cc7b9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use scene::{Project, Scene};

use crate::command::{Command, CommandError, CommandResult};

pub struct CreateSceneCommand {
    name: String,
    is_main: bool,
    saved_scenes: Option<Vec<Scene>>,
    created_scene_id: Option<String>,
}

impl CreateSceneCommand {
    pub fn new(name: impl Into<String>, is_main: bool) -> Self {
        Self {
            name: name.into(),
            is_main,
            saved_scenes: None,
            created_scene_id: None,
        }
    }

    pub fn scene_id(&self) -> Option<&str> {
        self.created_scene_id.as_deref()
    }
}

impl Command for CreateSceneCommand {
    fn execute(
        &mut self,
        project: &mut Project,
    ) -> Result<Option<CommandResult>, CommandError> {
        self.saved_scenes = Some(project.scenes.clone());

        let created = timeline::scenes::build_default_scene(&self.name, self.is_main);
        self.created_scene_id = Some(created.id.clone());
        project.scenes.push(created);
        Ok(None)
    }

    fn undo(&mut self, project: &mut Project) {
        let Some(saved) = &self.saved_scenes else {
            return;
        };
        project.scenes = saved.clone();
    }
}