fyrox_ui/inspector/editors/
utf32.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
21use 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}