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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use crate::{
interaction::InteractionMode,
scene::{commands::ChangeSelectionCommand, EditorScene, Selection},
settings::Settings,
world::graph::selection::GraphSelection,
GameEngine, Message,
};
use fyrox::{
core::{algebra::Vector2, pool::Handle},
gui::{message::MessageDirection, widget::WidgetMessage, UiNode},
scene::node::Node,
};
use std::sync::mpsc::Sender;
pub struct SelectInteractionMode {
preview: Handle<UiNode>,
selection_frame: Handle<UiNode>,
message_sender: Sender<Message>,
stack: Vec<Handle<Node>>,
click_pos: Vector2<f32>,
}
impl SelectInteractionMode {
pub fn new(
preview: Handle<UiNode>,
selection_frame: Handle<UiNode>,
message_sender: Sender<Message>,
) -> Self {
Self {
preview,
selection_frame,
message_sender,
stack: Vec::new(),
click_pos: Vector2::default(),
}
}
}
impl InteractionMode for SelectInteractionMode {
fn on_left_mouse_button_down(
&mut self,
_editor_scene: &mut EditorScene,
engine: &mut GameEngine,
mouse_pos: Vector2<f32>,
_frame_size: Vector2<f32>,
_settings: &Settings,
) {
self.click_pos = mouse_pos;
let ui = &mut engine.user_interface;
ui.send_message(WidgetMessage::visibility(
self.selection_frame,
MessageDirection::ToWidget,
true,
));
ui.send_message(WidgetMessage::desired_position(
self.selection_frame,
MessageDirection::ToWidget,
mouse_pos,
));
ui.send_message(WidgetMessage::width(
self.selection_frame,
MessageDirection::ToWidget,
0.0,
));
ui.send_message(WidgetMessage::height(
self.selection_frame,
MessageDirection::ToWidget,
0.0,
));
}
fn on_left_mouse_button_up(
&mut self,
editor_scene: &mut EditorScene,
engine: &mut GameEngine,
_mouse_pos: Vector2<f32>,
frame_size: Vector2<f32>,
_settings: &Settings,
) {
let scene = &engine.scenes[editor_scene.scene];
let camera = scene.graph[editor_scene.camera_controller.camera].as_camera();
let preview_screen_bounds = engine.user_interface.node(self.preview).screen_bounds();
let frame_screen_bounds = engine
.user_interface
.node(self.selection_frame)
.screen_bounds();
let relative_bounds = frame_screen_bounds.translate(-preview_screen_bounds.position);
self.stack.clear();
self.stack.push(scene.graph.get_root());
let mut graph_selection = GraphSelection::default();
while let Some(handle) = self.stack.pop() {
let node = &scene.graph[handle];
if handle == editor_scene.editor_objects_root {
continue;
}
if handle == scene.graph.get_root() {
self.stack.extend_from_slice(node.children());
continue;
}
for screen_corner in node
.local_bounding_box()
.corners()
.iter()
.filter_map(|&p| camera.project(p + node.global_position(), frame_size))
{
if relative_bounds.contains(screen_corner) {
graph_selection.insert_or_exclude(handle);
break;
}
}
self.stack.extend_from_slice(node.children());
}
let new_selection = Selection::Graph(graph_selection);
if new_selection != editor_scene.selection {
self.message_sender
.send(Message::do_scene_command(ChangeSelectionCommand::new(
new_selection,
editor_scene.selection.clone(),
)))
.unwrap();
}
engine
.user_interface
.send_message(WidgetMessage::visibility(
self.selection_frame,
MessageDirection::ToWidget,
false,
));
}
fn on_mouse_move(
&mut self,
_mouse_offset: Vector2<f32>,
mouse_position: Vector2<f32>,
_camera: Handle<Node>,
_editor_scene: &mut EditorScene,
engine: &mut GameEngine,
_frame_size: Vector2<f32>,
_settings: &Settings,
) {
let ui = &mut engine.user_interface;
let width = mouse_position.x - self.click_pos.x;
let height = mouse_position.y - self.click_pos.y;
let position = Vector2::new(
if width < 0.0 {
mouse_position.x
} else {
self.click_pos.x
},
if height < 0.0 {
mouse_position.y
} else {
self.click_pos.y
},
);
ui.send_message(WidgetMessage::desired_position(
self.selection_frame,
MessageDirection::ToWidget,
position,
));
ui.send_message(WidgetMessage::width(
self.selection_frame,
MessageDirection::ToWidget,
width.abs(),
));
ui.send_message(WidgetMessage::height(
self.selection_frame,
MessageDirection::ToWidget,
height.abs(),
));
}
fn update(
&mut self,
_editor_scene: &mut EditorScene,
_camera: Handle<Node>,
_engine: &mut GameEngine,
) {
}
fn deactivate(&mut self, _editor_scene: &EditorScene, _engine: &mut GameEngine) {}
}