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
188
189
190
191
192
193
194
use crate::{
core::{algebra::Vector3, color::Color, pool::Handle},
define_constructor,
grid::{Column, GridBuilder, Row},
message::{MessageDirection, UiMessage},
numeric::{NumericType, NumericUpDownMessage},
vec::{make_mark, make_numeric_input},
BuildContext, Control, NodeHandleMapping, UiNode, UserInterface, Widget, WidgetBuilder,
};
use std::{
any::{Any, TypeId},
ops::{Deref, DerefMut},
};
#[derive(Debug, Clone, PartialEq)]
pub enum Vec3EditorMessage<T: NumericType> {
Value(Vector3<T>),
}
impl<T: NumericType> Vec3EditorMessage<T> {
define_constructor!(Vec3EditorMessage:Value => fn value(Vector3<T>), layout: false);
}
#[derive(Clone)]
pub struct Vec3Editor<T: NumericType> {
pub widget: Widget,
pub x_field: Handle<UiNode>,
pub y_field: Handle<UiNode>,
pub z_field: Handle<UiNode>,
pub value: Vector3<T>,
}
impl<T: NumericType> Deref for Vec3Editor<T> {
type Target = Widget;
fn deref(&self) -> &Self::Target {
&self.widget
}
}
impl<T: NumericType> DerefMut for Vec3Editor<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.widget
}
}
impl<T: NumericType> Control for Vec3Editor<T> {
fn query_component(&self, type_id: TypeId) -> Option<&dyn Any> {
if type_id == TypeId::of::<Self>() {
Some(self)
} else {
None
}
}
fn resolve(&mut self, node_map: &NodeHandleMapping) {
node_map.resolve(&mut self.x_field);
node_map.resolve(&mut self.y_field);
node_map.resolve(&mut self.z_field);
}
fn handle_routed_message(&mut self, ui: &mut UserInterface, message: &mut UiMessage) {
self.widget.handle_routed_message(ui, message);
if let Some(&NumericUpDownMessage::Value(value)) = message.data::<NumericUpDownMessage<T>>()
{
if message.direction() == MessageDirection::FromWidget {
if message.destination() == self.x_field {
ui.send_message(Vec3EditorMessage::value(
self.handle(),
MessageDirection::ToWidget,
Vector3::new(value, self.value.y, self.value.z),
));
} else if message.destination() == self.y_field {
ui.send_message(Vec3EditorMessage::value(
self.handle(),
MessageDirection::ToWidget,
Vector3::new(self.value.x, value, self.value.z),
));
} else if message.destination() == self.z_field {
ui.send_message(Vec3EditorMessage::value(
self.handle(),
MessageDirection::ToWidget,
Vector3::new(self.value.x, self.value.y, value),
));
}
}
} else if let Some(&Vec3EditorMessage::Value(value)) =
message.data::<Vec3EditorMessage<T>>()
{
if message.direction() == MessageDirection::ToWidget {
let mut changed = false;
if self.value.x != value.x {
self.value.x = value.x;
ui.send_message(NumericUpDownMessage::value(
self.x_field,
MessageDirection::ToWidget,
value.x,
));
changed = true;
}
if self.value.y != value.y {
self.value.y = value.y;
ui.send_message(NumericUpDownMessage::value(
self.y_field,
MessageDirection::ToWidget,
value.y,
));
changed = true;
}
if self.value.z != value.z {
self.value.z = value.z;
ui.send_message(NumericUpDownMessage::value(
self.z_field,
MessageDirection::ToWidget,
value.z,
));
changed = true;
}
if changed {
ui.send_message(message.reverse());
}
}
}
}
}
pub struct Vec3EditorBuilder<T: NumericType> {
widget_builder: WidgetBuilder,
value: Vector3<T>,
editable: bool,
}
impl<T: NumericType> Vec3EditorBuilder<T> {
pub fn new(widget_builder: WidgetBuilder) -> Self {
Self {
widget_builder,
value: Vector3::new(T::zero(), T::zero(), T::zero()),
editable: true,
}
}
pub fn with_value(mut self, value: Vector3<T>) -> Self {
self.value = value;
self
}
pub fn with_editable(mut self, editable: bool) -> Self {
self.editable = editable;
self
}
pub fn build(self, ctx: &mut BuildContext) -> Handle<UiNode> {
let x_field;
let y_field;
let z_field;
let grid = GridBuilder::new(
WidgetBuilder::new()
.with_child(make_mark(ctx, "X", 0, Color::opaque(120, 0, 0)))
.with_child({
x_field = make_numeric_input(ctx, 1, self.value.x, self.editable);
x_field
})
.with_child(make_mark(ctx, "Y", 2, Color::opaque(0, 120, 0)))
.with_child({
y_field = make_numeric_input(ctx, 3, self.value.y, self.editable);
y_field
})
.with_child(make_mark(ctx, "Z", 4, Color::opaque(0, 0, 120)))
.with_child({
z_field = make_numeric_input(ctx, 5, self.value.z, self.editable);
z_field
}),
)
.add_row(Row::stretch())
.add_column(Column::auto())
.add_column(Column::stretch())
.add_column(Column::auto())
.add_column(Column::stretch())
.add_column(Column::auto())
.add_column(Column::stretch())
.build(ctx);
let node = Vec3Editor {
widget: self.widget_builder.with_child(grid).build(),
x_field,
y_field,
z_field,
value: self.value,
};
ctx.add_node(UiNode::new(node))
}
}