Skip to main content

fyrox_ui/inspector/editors/
bit.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    bit::{BitContainer, BitFieldBuilder, BitFieldMessage},
23    inspector::{
24        editors::{
25            PropertyEditorBuildContext, PropertyEditorDefinition, PropertyEditorInstance,
26            PropertyEditorMessageContext, PropertyEditorTranslationContext,
27        },
28        FieldAction, InspectorError, PropertyChanged,
29    },
30    MessageDirection, Thickness, UiMessage, WidgetBuilder,
31};
32use fyrox_core::PhantomDataSendSync;
33use std::any::TypeId;
34
35#[derive(Debug)]
36pub struct BitFieldPropertyEditorDefinition<T>
37where
38    T: BitContainer,
39{
40    #[allow(dead_code)]
41    phantom: PhantomDataSendSync<T>,
42}
43
44impl<T> BitFieldPropertyEditorDefinition<T>
45where
46    T: BitContainer,
47{
48    pub fn new() -> Self {
49        Self {
50            phantom: Default::default(),
51        }
52    }
53}
54
55impl<T> PropertyEditorDefinition for BitFieldPropertyEditorDefinition<T>
56where
57    T: BitContainer,
58{
59    fn value_type_id(&self) -> TypeId {
60        TypeId::of::<T>()
61    }
62
63    fn create_instance(
64        &self,
65        ctx: PropertyEditorBuildContext,
66    ) -> Result<PropertyEditorInstance, InspectorError> {
67        let value = ctx.property_info.cast_value::<T>()?;
68        Ok(PropertyEditorInstance::simple(
69            BitFieldBuilder::new(WidgetBuilder::new().with_margin(Thickness::top_bottom(1.0)))
70                .with_value(*value)
71                .build(ctx.build_context),
72        ))
73    }
74
75    fn create_message(
76        &self,
77        ctx: PropertyEditorMessageContext,
78    ) -> Result<Option<UiMessage>, InspectorError> {
79        let value = ctx.property_info.cast_value::<T>()?;
80        Ok(Some(UiMessage::for_widget(
81            ctx.instance,
82            BitFieldMessage::Value(*value),
83        )))
84    }
85
86    fn translate_message(&self, ctx: PropertyEditorTranslationContext) -> Option<PropertyChanged> {
87        if ctx.message.direction() == MessageDirection::FromWidget {
88            if let Some(BitFieldMessage::Value(value)) = ctx.message.data::<BitFieldMessage<T>>() {
89                return Some(PropertyChanged {
90                    name: ctx.name.to_string(),
91                    action: FieldAction::object(*value),
92                });
93            }
94        }
95        None
96    }
97}