fyrox_ui/inspector/editors/
range.rs1use 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}