fyrox_ui/inspector/editors/
rect.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::math::Rect,
23    inspector::{
24        editors::{
25            PropertyEditorBuildContext, PropertyEditorDefinition, PropertyEditorInstance,
26            PropertyEditorMessageContext, PropertyEditorTranslationContext,
27        },
28        FieldKind, InspectorError, PropertyChanged,
29    },
30    message::{MessageDirection, UiMessage},
31    numeric::NumericType,
32    rect::{RectEditorBuilder, RectEditorMessage},
33    widget::WidgetBuilder,
34};
35use std::{any::TypeId, fmt::Debug, marker::PhantomData};
36
37#[derive(Debug)]
38pub struct RectPropertyEditorDefinition<T>
39where
40    T: NumericType,
41{
42    phantom: PhantomData<T>,
43}
44
45impl<T> RectPropertyEditorDefinition<T>
46where
47    T: NumericType,
48{
49    pub fn new() -> Self {
50        Self::default()
51    }
52}
53
54impl<T> Default for RectPropertyEditorDefinition<T>
55where
56    T: NumericType,
57{
58    fn default() -> Self {
59        Self {
60            phantom: PhantomData,
61        }
62    }
63}
64
65impl<T> PropertyEditorDefinition for RectPropertyEditorDefinition<T>
66where
67    T: NumericType,
68{
69    fn value_type_id(&self) -> TypeId {
70        TypeId::of::<Rect<T>>()
71    }
72
73    fn create_instance(
74        &self,
75        ctx: PropertyEditorBuildContext,
76    ) -> Result<PropertyEditorInstance, InspectorError> {
77        let value = ctx.property_info.cast_value::<Rect<T>>()?;
78
79        Ok(PropertyEditorInstance::Simple {
80            editor: RectEditorBuilder::new(WidgetBuilder::new().with_height(36.0))
81                .with_value(*value)
82                .build(ctx.build_context),
83        })
84    }
85
86    fn create_message(
87        &self,
88        ctx: PropertyEditorMessageContext,
89    ) -> Result<Option<UiMessage>, InspectorError> {
90        let value = ctx.property_info.cast_value::<Rect<T>>()?;
91        Ok(Some(RectEditorMessage::value(
92            ctx.instance,
93            MessageDirection::ToWidget,
94            *value,
95        )))
96    }
97
98    fn translate_message(&self, ctx: PropertyEditorTranslationContext) -> Option<PropertyChanged> {
99        if ctx.message.direction() == MessageDirection::FromWidget {
100            if let Some(RectEditorMessage::Value(value)) =
101                ctx.message.data::<RectEditorMessage<T>>()
102            {
103                return Some(PropertyChanged {
104                    name: ctx.name.to_string(),
105                    owner_type_id: ctx.owner_type_id,
106                    value: FieldKind::object(*value),
107                });
108            }
109        }
110        None
111    }
112}