fyrox_ui/inspector/editors/
key.rs1use crate::{
22 inspector::{
23 editors::{
24 PropertyEditorBuildContext, PropertyEditorDefinition, PropertyEditorInstance,
25 PropertyEditorMessageContext, PropertyEditorTranslationContext,
26 },
27 FieldAction, InspectorError, PropertyChanged,
28 },
29 key::{
30 HotKey, HotKeyEditorBuilder, HotKeyEditorMessage, KeyBinding, KeyBindingEditorBuilder,
31 KeyBindingEditorMessage,
32 },
33 message::{MessageDirection, UiMessage},
34 widget::WidgetBuilder,
35 Thickness,
36};
37use std::any::TypeId;
38
39#[derive(Debug)]
40pub struct HotKeyPropertyEditorDefinition;
41
42impl PropertyEditorDefinition for HotKeyPropertyEditorDefinition {
43 fn value_type_id(&self) -> TypeId {
44 TypeId::of::<HotKey>()
45 }
46
47 fn create_instance(
48 &self,
49 ctx: PropertyEditorBuildContext,
50 ) -> Result<PropertyEditorInstance, InspectorError> {
51 let value = ctx.property_info.cast_value::<HotKey>()?;
52 Ok(PropertyEditorInstance::simple(
53 HotKeyEditorBuilder::new(WidgetBuilder::new().with_margin(Thickness::uniform(1.0)))
54 .with_value(value.clone())
55 .build(ctx.build_context),
56 ))
57 }
58
59 fn create_message(
60 &self,
61 ctx: PropertyEditorMessageContext,
62 ) -> Result<Option<UiMessage>, InspectorError> {
63 let value = ctx.property_info.cast_value::<HotKey>()?;
64 Ok(Some(UiMessage::for_widget(
65 ctx.instance,
66 HotKeyEditorMessage::Value(value.clone()),
67 )))
68 }
69
70 fn translate_message(&self, ctx: PropertyEditorTranslationContext) -> Option<PropertyChanged> {
71 if ctx.message.direction() == MessageDirection::FromWidget {
72 if let Some(HotKeyEditorMessage::Value(value)) = ctx.message.data() {
73 return Some(PropertyChanged {
74 name: ctx.name.to_string(),
75
76 action: FieldAction::object(value.clone()),
77 });
78 }
79 }
80 None
81 }
82}
83
84#[derive(Debug)]
85pub struct KeyBindingPropertyEditorDefinition;
86
87impl PropertyEditorDefinition for KeyBindingPropertyEditorDefinition {
88 fn value_type_id(&self) -> TypeId {
89 TypeId::of::<KeyBinding>()
90 }
91
92 fn create_instance(
93 &self,
94 ctx: PropertyEditorBuildContext,
95 ) -> Result<PropertyEditorInstance, InspectorError> {
96 let value = ctx.property_info.cast_value::<KeyBinding>()?;
97 Ok(PropertyEditorInstance::simple(
98 KeyBindingEditorBuilder::new(WidgetBuilder::new().with_margin(Thickness::uniform(1.0)))
99 .with_value(value.clone())
100 .build(ctx.build_context),
101 ))
102 }
103
104 fn create_message(
105 &self,
106 ctx: PropertyEditorMessageContext,
107 ) -> Result<Option<UiMessage>, InspectorError> {
108 let value = ctx.property_info.cast_value::<KeyBinding>()?;
109 Ok(Some(UiMessage::for_widget(
110 ctx.instance,
111 KeyBindingEditorMessage::Value(value.clone()),
112 )))
113 }
114
115 fn translate_message(&self, ctx: PropertyEditorTranslationContext) -> Option<PropertyChanged> {
116 if ctx.message.direction() == MessageDirection::FromWidget {
117 if let Some(KeyBindingEditorMessage::Value(value)) = ctx.message.data() {
118 return Some(PropertyChanged {
119 name: ctx.name.to_string(),
120
121 action: FieldAction::object(value.clone()),
122 });
123 }
124 }
125 None
126 }
127}