Skip to main content

fyrox_ui/inspector/editors/
immutable_string.rs

1// Copyright (c) 2019-present Dmitry Stepanov and Fyrox Engine contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in all
11// copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21//! Property editor for [ImmutableString] properties based upon a [TextBox](crate::text_box::TextBox) widget.
22use fyrox_core::sstorage::ImmutableString;
23
24use crate::{
25    core::algebra::Vector2,
26    formatted_text::WrapMode,
27    inspector::{
28        editors::{
29            PropertyEditorBuildContext, PropertyEditorDefinition, PropertyEditorInstance,
30            PropertyEditorMessageContext, PropertyEditorTranslationContext,
31        },
32        FieldKind, InspectorError, PropertyChanged,
33    },
34    message::{MessageDirection, UiMessage},
35    text::TextMessage,
36    text_box::{TextBoxBuilder, TextCommitMode},
37    widget::WidgetBuilder,
38    Thickness, VerticalAlignment,
39};
40use std::any::TypeId;
41
42/// Property editor for [ImmutableString] properties based upon a [TextBox](crate::text_box::TextBox) widget.
43#[derive(Debug)]
44pub struct ImmutableStringPropertyEditorDefinition;
45
46impl PropertyEditorDefinition for ImmutableStringPropertyEditorDefinition {
47    fn value_type_id(&self) -> TypeId {
48        TypeId::of::<ImmutableString>()
49    }
50
51    fn create_instance(
52        &self,
53        ctx: PropertyEditorBuildContext,
54    ) -> Result<PropertyEditorInstance, InspectorError> {
55        let value = ctx.property_info.cast_value::<ImmutableString>()?;
56        Ok(PropertyEditorInstance::simple(
57            TextBoxBuilder::new(
58                WidgetBuilder::new()
59                    .with_min_size(Vector2::new(0.0, 17.0))
60                    .with_margin(Thickness::uniform(1.0)),
61            )
62            .with_wrap(WrapMode::Word)
63            .with_text_commit_mode(TextCommitMode::Changed)
64            .with_text(value)
65            .with_vertical_text_alignment(VerticalAlignment::Center)
66            .build(ctx.build_context),
67        ))
68    }
69
70    fn create_message(
71        &self,
72        ctx: PropertyEditorMessageContext,
73    ) -> Result<Option<UiMessage>, InspectorError> {
74        let value = ctx.property_info.cast_value::<ImmutableString>()?;
75        Ok(Some(UiMessage::for_widget(
76            ctx.instance,
77            TextMessage::Text(value.to_mutable()),
78        )))
79    }
80
81    fn translate_message(&self, ctx: PropertyEditorTranslationContext) -> Option<PropertyChanged> {
82        if ctx.message.direction() == MessageDirection::FromWidget {
83            if let Some(TextMessage::Text(value)) = ctx.message.data::<TextMessage>() {
84                return Some(PropertyChanged {
85                    name: ctx.name.to_string(),
86                    value: FieldKind::object(ImmutableString::new(value)),
87                });
88            }
89        }
90        None
91    }
92}