Skip to main content

fyrox_ui/inspector/editors/
range.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    inspector::{
23        editors::{
24            PropertyEditorBuildContext, PropertyEditorDefinition, PropertyEditorInstance,
25            PropertyEditorMessageContext, PropertyEditorTranslationContext,
26        },
27        FieldAction, InspectorError, PropertyChanged,
28    },
29    message::{MessageDirection, UiMessage},
30    numeric::NumericType,
31    range::{RangeEditorBuilder, RangeEditorMessage},
32    widget::WidgetBuilder,
33};
34use std::{any::TypeId, marker::PhantomData, ops::Range};
35
36#[derive(Debug)]
37pub struct RangePropertyEditorDefinition<T: NumericType> {
38    phantom: PhantomData<T>,
39}
40
41impl<T: NumericType> RangePropertyEditorDefinition<T> {
42    pub fn new() -> Self {
43        Self {
44            phantom: PhantomData,
45        }
46    }
47}
48
49impl<T: NumericType> PropertyEditorDefinition for RangePropertyEditorDefinition<T> {
50    fn value_type_id(&self) -> TypeId {
51        TypeId::of::<Range<T>>()
52    }
53
54    fn create_instance(
55        &self,
56        ctx: PropertyEditorBuildContext,
57    ) -> Result<PropertyEditorInstance, InspectorError> {
58        let value = ctx.property_info.cast_value::<Range<T>>()?;
59
60        Ok(PropertyEditorInstance::simple(
61            RangeEditorBuilder::new(WidgetBuilder::new())
62                .with_value(value.clone())
63                .build(ctx.build_context),
64        ))
65    }
66
67    fn create_message(
68        &self,
69        ctx: PropertyEditorMessageContext,
70    ) -> Result<Option<UiMessage>, InspectorError> {
71        let value = ctx.property_info.cast_value::<Range<T>>()?;
72
73        Ok(Some(UiMessage::for_widget(
74            ctx.instance,
75            RangeEditorMessage::Value(value.clone()),
76        )))
77    }
78
79    fn translate_message(&self, ctx: PropertyEditorTranslationContext) -> Option<PropertyChanged> {
80        if ctx.message.direction() == MessageDirection::FromWidget {
81            if let Some(RangeEditorMessage::Value(value)) =
82                ctx.message.data::<RangeEditorMessage<T>>()
83            {
84                return Some(PropertyChanged {
85                    name: ctx.name.to_string(),
86
87                    action: FieldAction::object(value.clone()),
88                });
89            }
90        }
91
92        None
93    }
94}