rg3d_ui/inspector/editors/
string.rs1use crate::{
2 inspector::{
3 editors::{
4 PropertyEditorBuildContext, PropertyEditorDefinition, PropertyEditorInstance,
5 PropertyEditorMessageContext,
6 },
7 FieldKind, InspectorError, PropertyChanged,
8 },
9 message::{MessageDirection, UiMessage},
10 text_box::{TextBoxBuilder, TextBoxMessage},
11 widget::WidgetBuilder,
12 Thickness, VerticalAlignment,
13};
14use std::any::TypeId;
15
16#[derive(Debug)]
17pub struct StringPropertyEditorDefinition;
18
19impl PropertyEditorDefinition for StringPropertyEditorDefinition {
20 fn value_type_id(&self) -> TypeId {
21 TypeId::of::<String>()
22 }
23
24 fn create_instance(
25 &self,
26 ctx: PropertyEditorBuildContext,
27 ) -> Result<PropertyEditorInstance, InspectorError> {
28 let value = ctx.property_info.cast_value::<String>()?;
29 Ok(PropertyEditorInstance::Simple {
30 editor: TextBoxBuilder::new(WidgetBuilder::new().with_margin(Thickness::uniform(1.0)))
31 .with_text(value)
32 .with_vertical_text_alignment(VerticalAlignment::Center)
33 .build(ctx.build_context),
34 })
35 }
36
37 fn create_message(
38 &self,
39 ctx: PropertyEditorMessageContext,
40 ) -> Result<Option<UiMessage>, InspectorError> {
41 let value = ctx.property_info.cast_value::<String>()?;
42 Ok(Some(TextBoxMessage::text(
43 ctx.instance,
44 MessageDirection::ToWidget,
45 value.clone(),
46 )))
47 }
48
49 fn translate_message(
50 &self,
51 name: &str,
52 owner_type_id: TypeId,
53 message: &UiMessage,
54 ) -> Option<PropertyChanged> {
55 if message.direction() == MessageDirection::FromWidget {
56 if let Some(TextBoxMessage::Text(value)) = message.data::<TextBoxMessage>() {
57 return Some(PropertyChanged {
58 owner_type_id,
59 name: name.to_string(),
60 value: FieldKind::object(value.clone()),
61 });
62 }
63 }
64 None
65 }
66}