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