fyrox_ui/uuid.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
//! UUID editor is used to show an arbitrary UUID and give an ability to generate a new value. See [`UuidEditor`] docs for
//! more info and usage examples.
#![warn(missing_docs)]
use crate::{
button::{ButtonBuilder, ButtonMessage},
core::{pool::Handle, reflect::prelude::*, type_traits::prelude::*, visitor::prelude::*},
define_constructor, define_widget_deref,
grid::{Column, GridBuilder, Row},
message::{MessageDirection, UiMessage},
text::{TextBuilder, TextMessage},
widget::{Widget, WidgetBuilder},
BuildContext, Control, Thickness, UiNode, UserInterface, VerticalAlignment,
};
use fyrox_core::uuid_provider;
use std::ops::{Deref, DerefMut};
/// A set of messages that is used to fetch or modify values of [`UuidEditor`] widgets.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum UuidEditorMessage {
/// Fetches or modifies a value of a [`UuidEditor`] widget.
Value(Uuid),
}
impl UuidEditorMessage {
define_constructor!(
/// Creates [`UuidEditorMessage::Value`] message.
UuidEditorMessage:Value => fn value(Uuid), layout: false
);
}
/// UUID editor is used to show an arbitrary UUID and give an ability to generate a new value. It is widely used in
/// [`crate::inspector::Inspector`] to show and edit UUIDs.
///
/// ## Example
///
/// ```rust
/// # use fyrox_ui::{
/// # core::{pool::Handle, uuid::Uuid},
/// # uuid::UuidEditorBuilder,
/// # widget::WidgetBuilder,
/// # BuildContext, UiNode,
/// # };
/// fn create_uuid_editor(ctx: &mut BuildContext) -> Handle<UiNode> {
/// UuidEditorBuilder::new(WidgetBuilder::new())
/// .with_value(Uuid::new_v4())
/// .build(ctx)
/// }
/// ```
#[derive(Default, Clone, Visit, Reflect, Debug, ComponentProvider)]
pub struct UuidEditor {
widget: Widget,
value: Uuid,
text: Handle<UiNode>,
generate: Handle<UiNode>,
}
define_widget_deref!(UuidEditor);
uuid_provider!(UuidEditor = "667f7f48-2448-42da-91dd-cd743ca7117e");
impl Control for UuidEditor {
fn handle_routed_message(&mut self, ui: &mut UserInterface, message: &mut UiMessage) {
self.widget.handle_routed_message(ui, message);
if message.destination() == self.handle && message.direction() == MessageDirection::ToWidget
{
if let Some(UuidEditorMessage::Value(value)) = message.data() {
if self.value != *value {
self.value = *value;
ui.send_message(message.reverse());
ui.send_message(TextMessage::text(
self.text,
MessageDirection::ToWidget,
value.to_string(),
));
}
}
} else if message.destination() == self.generate {
if let Some(ButtonMessage::Click) = message.data() {
ui.send_message(UuidEditorMessage::value(
self.handle,
MessageDirection::ToWidget,
Uuid::new_v4(),
));
}
}
}
}
/// Creates [`UuidEditor`] widgets and add them to the user interface.
pub struct UuidEditorBuilder {
widget_builder: WidgetBuilder,
value: Uuid,
}
impl UuidEditorBuilder {
/// Creates new builder instance.
pub fn new(widget_builder: WidgetBuilder) -> Self {
Self {
widget_builder,
value: Default::default(),
}
}
/// Sets a desired value of the [`UuidEditor`].
pub fn with_value(mut self, value: Uuid) -> Self {
self.value = value;
self
}
/// Finishes widget building.
pub fn build(self, ctx: &mut BuildContext) -> Handle<UiNode> {
let text;
let generate;
let grid = GridBuilder::new(
WidgetBuilder::new()
.with_child({
text = TextBuilder::new(
WidgetBuilder::new()
.on_column(0)
.on_row(0)
.with_margin(Thickness::uniform(1.0))
.with_vertical_alignment(VerticalAlignment::Center),
)
.with_text(self.value.to_string())
.build(ctx);
text
})
.with_child({
generate = ButtonBuilder::new(
WidgetBuilder::new()
.on_column(1)
.on_row(0)
.with_width(24.0)
.with_margin(Thickness::uniform(1.0)),
)
.with_text("^/v")
.build(ctx);
generate
}),
)
.add_row(Row::stretch())
.add_column(Column::stretch())
.add_column(Column::auto())
.build(ctx);
let uuid_editor = UuidEditor {
widget: self.widget_builder.with_child(grid).build(),
value: self.value,
text,
generate,
};
ctx.add_node(UiNode::new(uuid_editor))
}
}