fyroxed_base/scene/
controller.rs

1// Copyright (c) 2019-present Dmitry Stepanov and Fyrox Engine contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in all
11// copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21use crate::{
22    command::{Command, CommandStack},
23    fyrox::{
24        core::{algebra::Vector2, math::Rect, pool::Handle, reflect::Reflect, Downcast},
25        engine::Engine,
26        gui::{
27            inspector::PropertyChanged,
28            message::{KeyCode, MouseButton},
29            UiNode,
30        },
31        resource::texture::TextureResource,
32        scene::SceneContainer,
33    },
34    scene::Selection,
35    settings::{keys::KeyBindings, Settings},
36    Message,
37};
38use std::path::Path;
39
40pub trait SceneController: Downcast {
41    fn on_key_up(&mut self, key: KeyCode, engine: &mut Engine, key_bindings: &KeyBindings) -> bool;
42
43    fn on_key_down(
44        &mut self,
45        key: KeyCode,
46        engine: &mut Engine,
47        key_bindings: &KeyBindings,
48    ) -> bool;
49
50    fn on_mouse_move(
51        &mut self,
52        pos: Vector2<f32>,
53        offset: Vector2<f32>,
54        screen_bounds: Rect<f32>,
55        engine: &mut Engine,
56        settings: &Settings,
57    );
58
59    fn on_mouse_up(
60        &mut self,
61        button: MouseButton,
62        pos: Vector2<f32>,
63        screen_bounds: Rect<f32>,
64        engine: &mut Engine,
65        settings: &Settings,
66    );
67
68    fn on_mouse_down(
69        &mut self,
70        button: MouseButton,
71        pos: Vector2<f32>,
72        screen_bounds: Rect<f32>,
73        engine: &mut Engine,
74        settings: &Settings,
75    );
76
77    fn on_mouse_wheel(&mut self, amount: f32, engine: &mut Engine, settings: &Settings);
78
79    fn on_mouse_leave(&mut self, engine: &mut Engine, settings: &Settings);
80
81    fn on_drag_over(
82        &mut self,
83        handle: Handle<UiNode>,
84        screen_bounds: Rect<f32>,
85        engine: &mut Engine,
86        settings: &Settings,
87    );
88
89    fn on_drop(
90        &mut self,
91        handle: Handle<UiNode>,
92        screen_bounds: Rect<f32>,
93        engine: &mut Engine,
94        settings: &Settings,
95    );
96
97    fn render_target(&self, engine: &Engine) -> Option<TextureResource>;
98
99    fn extension(&self) -> &str;
100
101    fn save(
102        &mut self,
103        path: &Path,
104        settings: &Settings,
105        engine: &mut Engine,
106    ) -> Result<String, String>;
107
108    fn do_command(
109        &mut self,
110        command_stack: &mut CommandStack,
111        command: Command,
112        selection: &mut Selection,
113        engine: &mut Engine,
114    );
115
116    fn undo(
117        &mut self,
118        command_stack: &mut CommandStack,
119        selection: &mut Selection,
120        engine: &mut Engine,
121    );
122
123    fn redo(
124        &mut self,
125        command_stack: &mut CommandStack,
126        selection: &mut Selection,
127        engine: &mut Engine,
128    );
129
130    fn clear_command_stack(
131        &mut self,
132        command_stack: &mut CommandStack,
133        selection: &mut Selection,
134        scenes: &mut SceneContainer,
135    );
136
137    fn on_before_render(&mut self, editor_selection: &Selection, engine: &mut Engine);
138
139    fn on_after_render(&mut self, engine: &mut Engine);
140
141    fn update(
142        &mut self,
143        editor_selection: &Selection,
144        engine: &mut Engine,
145        dt: f32,
146        path: Option<&Path>,
147        settings: &mut Settings,
148        screen_bounds: Rect<f32>,
149    ) -> Option<TextureResource>;
150
151    fn is_interacting(&self) -> bool;
152
153    fn on_destroy(
154        &mut self,
155        command_stack: &mut CommandStack,
156        engine: &mut Engine,
157        selection: &mut Selection,
158    );
159
160    fn on_message(&mut self, message: &Message, selection: &Selection, engine: &mut Engine)
161        -> bool;
162
163    fn command_names(
164        &mut self,
165        command_stack: &mut CommandStack,
166        selection: &mut Selection,
167        engine: &mut Engine,
168    ) -> Vec<String>;
169
170    fn first_selected_entity(
171        &self,
172        selection: &Selection,
173        scenes: &SceneContainer,
174        callback: &mut dyn FnMut(&dyn Reflect),
175    );
176
177    fn on_property_changed(
178        &mut self,
179        args: &PropertyChanged,
180        selection: &Selection,
181        engine: &mut Engine,
182    );
183
184    fn provide_docs(&self, selection: &Selection, engine: &Engine) -> Option<String>;
185}
186
187impl dyn SceneController {
188    pub fn downcast_ref<T>(&self) -> Option<&T>
189    where
190        T: SceneController,
191    {
192        self.as_any().downcast_ref::<T>()
193    }
194
195    pub fn downcast_mut<T>(&mut self) -> Option<&mut T>
196    where
197        T: SceneController,
198    {
199        self.as_any_mut().downcast_mut::<T>()
200    }
201}