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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
use crate::{
    border::BorderBuilder,
    brush::Brush,
    core::{algebra::SVector, color::Color, num_traits, pool::Handle},
    define_constructor,
    grid::{Column, GridBuilder, Row},
    message::{MessageDirection, UiMessage},
    numeric::{NumericType, NumericUpDownBuilder, NumericUpDownMessage},
    widget::WidgetBuilder,
    BuildContext, Control, NodeHandleMapping, Thickness, UiNode, UserInterface, Widget,
};
use std::{
    any::{Any, TypeId},
    ops::{Deref, DerefMut},
};

fn make_numeric_input<T: NumericType>(
    ctx: &mut BuildContext,
    column: usize,
    value: T,
    min: T,
    max: T,
    step: T,
    editable: bool,
) -> Handle<UiNode> {
    NumericUpDownBuilder::new(
        WidgetBuilder::new()
            .on_row(0)
            .on_column(column)
            .with_margin(Thickness {
                left: 1.0,
                top: 0.0,
                right: 1.0,
                bottom: 0.0,
            }),
    )
    .with_precision(3)
    .with_value(value)
    .with_min_value(min)
    .with_max_value(max)
    .with_step(step)
    .with_editable(editable)
    .build(ctx)
}

pub fn make_mark(ctx: &mut BuildContext, column: usize, color: Color) -> Handle<UiNode> {
    BorderBuilder::new(
        WidgetBuilder::new()
            .on_row(0)
            .on_column(column)
            .with_background(Brush::Solid(color))
            .with_foreground(Brush::Solid(Color::TRANSPARENT))
            .with_width(4.0),
    )
    .build(ctx)
}

#[derive(Debug, Clone, PartialEq)]
pub enum VecEditorMessage<T, const D: usize>
where
    T: NumericType,
{
    Value(SVector<T, D>),
}

impl<T, const D: usize> VecEditorMessage<T, D>
where
    T: NumericType,
{
    define_constructor!(VecEditorMessage:Value => fn value(SVector<T, D>), layout: false);
}

#[derive(Clone)]
pub struct VecEditor<T, const D: usize>
where
    T: NumericType,
{
    pub widget: Widget,
    pub fields: Vec<Handle<UiNode>>,
    pub value: SVector<T, D>,
    pub min: SVector<T, D>,
    pub max: SVector<T, D>,
    pub step: SVector<T, D>,
}

impl<T, const D: usize> Deref for VecEditor<T, D>
where
    T: NumericType,
{
    type Target = Widget;

    fn deref(&self) -> &Self::Target {
        &self.widget
    }
}

impl<T, const D: usize> DerefMut for VecEditor<T, D>
where
    T: NumericType,
{
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.widget
    }
}

impl<T, const D: usize> Control for VecEditor<T, D>
where
    T: NumericType,
{
    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_slice(&mut self.fields);
    }

    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 {
                for (i, field) in self.fields.iter().enumerate() {
                    if message.destination() == *field {
                        let mut new_value = self.value;
                        new_value[i] = value;
                        ui.send_message(VecEditorMessage::value(
                            self.handle(),
                            MessageDirection::ToWidget,
                            new_value,
                        ));
                    }
                }
            }
        } else if let Some(&VecEditorMessage::Value(new_value)) =
            message.data::<VecEditorMessage<T, D>>()
        {
            if message.direction() == MessageDirection::ToWidget {
                let mut changed = false;

                for i in 0..D {
                    let editor = self.fields[i];
                    let current = &mut self.value[i];
                    let min = self.min[i];
                    let max = self.max[i];
                    let new = num_traits::clamp(new_value[i], min, max);

                    if *current != new {
                        *current = new;
                        ui.send_message(NumericUpDownMessage::value(
                            editor,
                            MessageDirection::ToWidget,
                            new,
                        ));
                        changed = true;
                    }
                }

                if changed {
                    ui.send_message(message.reverse());
                }
            }
        }
    }
}

pub struct VecEditorBuilder<T, const D: usize>
where
    T: NumericType,
{
    widget_builder: WidgetBuilder,
    value: SVector<T, D>,
    editable: bool,
    min: SVector<T, D>,
    max: SVector<T, D>,
    step: SVector<T, D>,
}

impl<T, const D: usize> VecEditorBuilder<T, D>
where
    T: NumericType,
{
    pub fn new(widget_builder: WidgetBuilder) -> Self {
        Self {
            widget_builder,
            value: SVector::repeat(Default::default()),
            editable: true,
            min: SVector::repeat(T::min_value()),
            max: SVector::repeat(T::max_value()),
            step: SVector::repeat(T::one()),
        }
    }

    pub fn with_value(mut self, value: SVector<T, D>) -> Self {
        self.value = value;
        self
    }

    pub fn with_editable(mut self, editable: bool) -> Self {
        self.editable = editable;
        self
    }

    pub fn with_min(mut self, min: SVector<T, D>) -> Self {
        self.min = min;
        self
    }

    pub fn with_max(mut self, max: SVector<T, D>) -> Self {
        self.max = max;
        self
    }

    pub fn with_step(mut self, step: SVector<T, D>) -> Self {
        self.step = step;
        self
    }

    pub fn build(self, ctx: &mut BuildContext) -> Handle<UiNode> {
        let mut fields = Vec::new();
        let mut children = Vec::new();
        let mut columns = Vec::new();

        let colors = [
            Color::opaque(120, 0, 0),
            Color::opaque(0, 120, 0),
            Color::opaque(0, 0, 120),
            Color::opaque(120, 0, 120),
            Color::opaque(0, 120, 120),
            Color::opaque(120, 120, 0),
        ];

        for i in 0..D {
            children.push(make_mark(
                ctx,
                i * 2,
                colors.get(i).cloned().unwrap_or(Color::ORANGE),
            ));

            let field = make_numeric_input(
                ctx,
                i * 2 + 1,
                self.value[i],
                self.min[i],
                self.max[i],
                self.step[i],
                self.editable,
            );
            children.push(field);
            fields.push(field);

            columns.push(Column::auto());
            columns.push(Column::stretch());
        }

        let grid = GridBuilder::new(WidgetBuilder::new().with_children(children))
            .add_row(Row::stretch())
            .add_columns(columns)
            .build(ctx);

        let node = VecEditor {
            widget: self.widget_builder.with_child(grid).build(),
            fields,
            value: self.value,
            min: self.min,
            max: self.max,
            step: self.step,
        };

        ctx.add_node(UiNode::new(node))
    }
}

pub type Vec2Editor<T> = VecEditor<T, 2>;
pub type Vec3Editor<T> = VecEditor<T, 3>;
pub type Vec4Editor<T> = VecEditor<T, 4>;
pub type Vec5Editor<T> = VecEditor<T, 5>;
pub type Vec6Editor<T> = VecEditor<T, 6>;

pub type Vec2EditorMessage<T> = VecEditorMessage<T, 2>;
pub type Vec3EditorMessage<T> = VecEditorMessage<T, 3>;
pub type Vec4EditorMessage<T> = VecEditorMessage<T, 4>;
pub type Vec5EditorMessage<T> = VecEditorMessage<T, 5>;
pub type Vec6EditorMessage<T> = VecEditorMessage<T, 6>;

pub type Vec2EditorBuilder<T> = VecEditorBuilder<T, 2>;
pub type Vec3EditorBuilder<T> = VecEditorBuilder<T, 3>;
pub type Vec4EditorBuilder<T> = VecEditorBuilder<T, 4>;
pub type Vec5EditorBuilder<T> = VecEditorBuilder<T, 5>;
pub type Vec6EditorBuilder<T> = VecEditorBuilder<T, 6>;