fyrox_ui/
range.rs

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
296
//! Range editor is used to display and edit closed ranges like `0..1`. See [`Range`] docs for more info and usage
//! examples.

#![warn(missing_docs)]

use crate::{
    core::{pool::Handle, reflect::prelude::*, type_traits::prelude::*, visitor::prelude::*},
    define_constructor,
    grid::{Column, GridBuilder, Row},
    message::{MessageDirection, UiMessage},
    numeric::{NumericType, NumericUpDownBuilder, NumericUpDownMessage},
    text::TextBuilder,
    widget::{Widget, WidgetBuilder},
    BuildContext, Control, Thickness, UiNode, UserInterface, VerticalAlignment,
};
use fyrox_core::variable::InheritableVariable;
use std::ops::{Deref, DerefMut, Range};

/// A set of messages, that can be used to modify/fetch the state of a [`RangeEditor`] widget instance.
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum RangeEditorMessage<T>
where
    T: NumericType,
{
    /// A message, that is used to either modifying or fetching the value of a [`RangeEditor`] widget instance.
    Value(Range<T>),
}

impl<T: NumericType> RangeEditorMessage<T> {
    define_constructor!(
        /// Creates [`RangeEditorMessage::Value`] message.
        RangeEditorMessage:Value => fn value(Range<T>), layout: false
    );
}

/// Range editor is used to display and edit closed ranges like `0..1`. The widget is generic over numeric type,
/// so you can display and editor ranges of any type, such as `u32`, `f32`, `f64`, etc.
///
/// ## Example
///
/// You can create range editors using [`RangeEditorBuilder`], like so:
///
/// ```rust
/// # use fyrox_ui::{
/// #     core::pool::Handle, range::RangeEditorBuilder, widget::WidgetBuilder, BuildContext, UiNode,
/// # };
/// fn create_range_editor(ctx: &mut BuildContext) -> Handle<UiNode> {
///     RangeEditorBuilder::new(WidgetBuilder::new())
///         .with_value(0u32..100u32)
///         .build(ctx)
/// }
/// ```
///
/// This example creates an editor for `Range<u32>` type with `0..100` value.
///
/// ## Value
///
/// To change current value of a range editor, use [`RangeEditorMessage::Value`] message:
///
/// ```rust
/// # use fyrox_ui::{
/// #     core::pool::Handle, message::MessageDirection, range::RangeEditorMessage, UiNode,
/// #     UserInterface,
/// # };
/// fn change_value(range_editor: Handle<UiNode>, ui: &UserInterface) {
///     ui.send_message(RangeEditorMessage::value(
///         range_editor,
///         MessageDirection::ToWidget,
///         5u32..20u32,
///     ))
/// }
/// ```
///
/// To "catch" the moment when the value has changed, use the same message, but check for [`MessageDirection::FromWidget`] direction
/// on the message:
///
/// ```rust
/// # use fyrox_ui::{
/// #     core::pool::Handle,
/// #     message::{MessageDirection, UiMessage},
/// #     range::RangeEditorMessage,
/// #     UiNode,
/// # };
/// #
/// fn fetch_value(range_editor: Handle<UiNode>, message: &UiMessage) {
///     if let Some(RangeEditorMessage::Value(range)) = message.data::<RangeEditorMessage<u32>>() {
///         if message.destination() == range_editor
///             && message.direction() == MessageDirection::FromWidget
///         {
///             println!("The new value is: {:?}", range)
///         }
///     }
/// }
/// ```
///
/// Be very careful about the type of the range when sending a message, you need to send a range of exact type, that match the type
/// of your editor, otherwise the message have no effect. The same applied to fetching.
#[derive(Default, Debug, Clone, Reflect, Visit, ComponentProvider)]
pub struct RangeEditor<T>
where
    T: NumericType,
{
    /// Base widget of the range editor.
    pub widget: Widget,
    /// Current value of the range editor.
    pub value: InheritableVariable<Range<T>>,
    /// A handle to numeric field that is used to show/modify start value of current range.
    pub start: InheritableVariable<Handle<UiNode>>,
    /// A handle to numeric field that is used to show/modify end value of current range.
    pub end: InheritableVariable<Handle<UiNode>>,
}

impl<T> Deref for RangeEditor<T>
where
    T: NumericType,
{
    type Target = Widget;

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

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

const SYNC_FLAG: u64 = 1;

impl<T> TypeUuidProvider for RangeEditor<T>
where
    T: NumericType,
{
    fn type_uuid() -> Uuid {
        combine_uuids(
            uuid!("0eb2948e-8485-490e-8719-18a0bb6fe275"),
            T::type_uuid(),
        )
    }
}

impl<T> Control for RangeEditor<T>
where
    T: NumericType,
{
    fn handle_routed_message(&mut self, ui: &mut UserInterface, message: &mut UiMessage) {
        self.widget.handle_routed_message(ui, message);

        if message.direction() == MessageDirection::ToWidget && message.flags != SYNC_FLAG {
            if let Some(RangeEditorMessage::Value(range)) = message.data::<RangeEditorMessage<T>>()
            {
                if message.destination() == self.handle && *self.value != *range {
                    self.value.set_value_and_mark_modified(range.clone());

                    ui.send_message(NumericUpDownMessage::value(
                        *self.start,
                        MessageDirection::ToWidget,
                        range.start,
                    ));
                    ui.send_message(NumericUpDownMessage::value(
                        *self.end,
                        MessageDirection::ToWidget,
                        range.end,
                    ));

                    ui.send_message(message.reverse());
                }
            } else if let Some(NumericUpDownMessage::Value(value)) =
                message.data::<NumericUpDownMessage<T>>()
            {
                if message.destination() == *self.start {
                    if *value < self.value.end {
                        ui.send_message(RangeEditorMessage::value(
                            self.handle,
                            MessageDirection::ToWidget,
                            Range {
                                start: *value,
                                end: self.value.end,
                            },
                        ));
                    } else {
                        let mut msg = NumericUpDownMessage::value(
                            *self.start,
                            MessageDirection::ToWidget,
                            self.value.end,
                        );
                        msg.flags = SYNC_FLAG;
                        ui.send_message(msg);
                    }
                } else if message.destination() == *self.end {
                    if *value > self.value.start {
                        ui.send_message(RangeEditorMessage::value(
                            self.handle,
                            MessageDirection::ToWidget,
                            Range {
                                start: self.value.start,
                                end: *value,
                            },
                        ));
                    } else {
                        let mut msg = NumericUpDownMessage::value(
                            *self.end,
                            MessageDirection::ToWidget,
                            self.value.start,
                        );
                        msg.flags = SYNC_FLAG;
                        ui.send_message(msg);
                    }
                }
            }
        }
    }
}

/// Range editor builder creates [`RangeEditor`] instances and adds them to the user interface.
pub struct RangeEditorBuilder<T>
where
    T: NumericType,
{
    widget_builder: WidgetBuilder,
    value: Range<T>,
}

impl<T> RangeEditorBuilder<T>
where
    T: NumericType,
{
    /// Creates new builder instance.
    pub fn new(widget_builder: WidgetBuilder) -> Self {
        Self {
            widget_builder,
            value: Range::default(),
        }
    }

    /// Sets a desired value of the editor.
    pub fn with_value(mut self, value: Range<T>) -> Self {
        self.value = value;
        self
    }

    /// Finished widget building and adds the new instance to the user interface.
    pub fn build(self, ctx: &mut BuildContext) -> Handle<UiNode> {
        let start = NumericUpDownBuilder::new(
            WidgetBuilder::new()
                .with_margin(Thickness::uniform(1.0))
                .on_column(0),
        )
        .with_value(self.value.start)
        .build(ctx);
        let end = NumericUpDownBuilder::new(
            WidgetBuilder::new()
                .with_margin(Thickness::uniform(1.0))
                .on_column(2),
        )
        .with_value(self.value.end)
        .build(ctx);
        let editor = RangeEditor {
            widget: self
                .widget_builder
                .with_child(
                    GridBuilder::new(
                        WidgetBuilder::new()
                            .with_child(start)
                            .with_child(
                                TextBuilder::new(
                                    WidgetBuilder::new()
                                        .on_column(1)
                                        .with_margin(Thickness::uniform(1.0)),
                                )
                                .with_vertical_text_alignment(VerticalAlignment::Center)
                                .with_text("..")
                                .build(ctx),
                            )
                            .with_child(end),
                    )
                    .add_column(Column::stretch())
                    .add_column(Column::strict(10.0))
                    .add_column(Column::stretch())
                    .add_row(Row::stretch())
                    .build(ctx),
                )
                .build(),
            value: self.value.into(),
            start: start.into(),
            end: end.into(),
        };

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