fyrox_ui/inspector/editors/
utf32.rs1use crate::{
22 core::algebra::Vector2,
23 formatted_text::WrapMode,
24 inspector::{
25 editors::{
26 PropertyEditorBuildContext, PropertyEditorDefinition, PropertyEditorInstance,
27 PropertyEditorMessageContext, PropertyEditorTranslationContext,
28 },
29 FieldKind, InspectorError, PropertyChanged,
30 },
31 message::{MessageDirection, UiMessage},
32 text::TextMessage,
33 text_box::TextBoxBuilder,
34 widget::WidgetBuilder,
35 Thickness, VerticalAlignment,
36};
37use std::any::TypeId;
38
39#[derive(Debug)]
40pub struct Utf32StringPropertyEditorDefinition;
41
42impl PropertyEditorDefinition for Utf32StringPropertyEditorDefinition {
43 fn value_type_id(&self) -> TypeId {
44 TypeId::of::<Vec<char>>()
45 }
46
47 fn create_instance(
48 &self,
49 ctx: PropertyEditorBuildContext,
50 ) -> Result<PropertyEditorInstance, InspectorError> {
51 let value = ctx.property_info.cast_value::<Vec<char>>()?;
52 Ok(PropertyEditorInstance::Simple {
53 editor: TextBoxBuilder::new(
54 WidgetBuilder::new()
55 .with_min_size(Vector2::new(0.0, 17.0))
56 .with_margin(Thickness::uniform(1.0)),
57 )
58 .with_wrap(WrapMode::Word)
59 .with_text(value.iter().collect::<String>())
60 .with_vertical_text_alignment(VerticalAlignment::Center)
61 .build(ctx.build_context),
62 })
63 }
64
65 fn create_message(
66 &self,
67 ctx: PropertyEditorMessageContext,
68 ) -> Result<Option<UiMessage>, InspectorError> {
69 let value = ctx.property_info.cast_value::<Vec<char>>()?;
70 Ok(Some(TextMessage::text(
71 ctx.instance,
72 MessageDirection::ToWidget,
73 value.iter().collect::<String>(),
74 )))
75 }
76
77 fn translate_message(&self, ctx: PropertyEditorTranslationContext) -> Option<PropertyChanged> {
78 if ctx.message.direction() == MessageDirection::FromWidget {
79 if let Some(TextMessage::Text(value)) = ctx.message.data::<TextMessage>() {
80 return Some(PropertyChanged {
81 owner_type_id: ctx.owner_type_id,
82 name: ctx.name.to_string(),
83 value: FieldKind::object(value.chars().collect::<Vec<_>>()),
84 });
85 }
86 }
87 None
88 }
89}