Skip to main content

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        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}