fyrox_ui/inspector/editors/
key.rs1use crate::{
22 inspector::{
23 editors::{
24 PropertyEditorBuildContext, PropertyEditorDefinition, PropertyEditorInstance,
25 PropertyEditorMessageContext, PropertyEditorTranslationContext,
26 },
27 FieldKind, 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 editor: HotKeyEditorBuilder::new(
54 WidgetBuilder::new().with_margin(Thickness::uniform(1.0)),
55 )
56 .with_value(value.clone())
57 .build(ctx.build_context),
58 })
59 }
60
61 fn create_message(
62 &self,
63 ctx: PropertyEditorMessageContext,
64 ) -> Result<Option<UiMessage>, InspectorError> {
65 let value = ctx.property_info.cast_value::<HotKey>()?;
66 Ok(Some(HotKeyEditorMessage::value(
67 ctx.instance,
68 MessageDirection::ToWidget,
69 value.clone(),
70 )))
71 }
72
73 fn translate_message(&self, ctx: PropertyEditorTranslationContext) -> Option<PropertyChanged> {
74 if ctx.message.direction() == MessageDirection::FromWidget {
75 if let Some(HotKeyEditorMessage::Value(value)) = ctx.message.data() {
76 return Some(PropertyChanged {
77 name: ctx.name.to_string(),
78 owner_type_id: ctx.owner_type_id,
79 value: FieldKind::object(value.clone()),
80 });
81 }
82 }
83 None
84 }
85}
86
87#[derive(Debug)]
88pub struct KeyBindingPropertyEditorDefinition;
89
90impl PropertyEditorDefinition for KeyBindingPropertyEditorDefinition {
91 fn value_type_id(&self) -> TypeId {
92 TypeId::of::<KeyBinding>()
93 }
94
95 fn create_instance(
96 &self,
97 ctx: PropertyEditorBuildContext,
98 ) -> Result<PropertyEditorInstance, InspectorError> {
99 let value = ctx.property_info.cast_value::<KeyBinding>()?;
100 Ok(PropertyEditorInstance::Simple {
101 editor: KeyBindingEditorBuilder::new(
102 WidgetBuilder::new().with_margin(Thickness::uniform(1.0)),
103 )
104 .with_value(value.clone())
105 .build(ctx.build_context),
106 })
107 }
108
109 fn create_message(
110 &self,
111 ctx: PropertyEditorMessageContext,
112 ) -> Result<Option<UiMessage>, InspectorError> {
113 let value = ctx.property_info.cast_value::<KeyBinding>()?;
114 Ok(Some(KeyBindingEditorMessage::value(
115 ctx.instance,
116 MessageDirection::ToWidget,
117 value.clone(),
118 )))
119 }
120
121 fn translate_message(&self, ctx: PropertyEditorTranslationContext) -> Option<PropertyChanged> {
122 if ctx.message.direction() == MessageDirection::FromWidget {
123 if let Some(KeyBindingEditorMessage::Value(value)) = ctx.message.data() {
124 return Some(PropertyChanged {
125 name: ctx.name.to_string(),
126 owner_type_id: ctx.owner_type_id,
127 value: FieldKind::object(value.clone()),
128 });
129 }
130 }
131 None
132 }
133}