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        FieldKind, 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            editor: BitFieldBuilder::new(
70                WidgetBuilder::new().with_margin(Thickness::top_bottom(1.0)),
71            )
72            .with_value(*value)
73            .build(ctx.build_context),
74        })
75    }
76
77    fn create_message(
78        &self,
79        ctx: PropertyEditorMessageContext,
80    ) -> Result<Option<UiMessage>, InspectorError> {
81        let value = ctx.property_info.cast_value::<T>()?;
82        Ok(Some(BitFieldMessage::value(
83            ctx.instance,
84            MessageDirection::ToWidget,
85            *value,
86        )))
87    }
88
89    fn translate_message(&self, ctx: PropertyEditorTranslationContext) -> Option<PropertyChanged> {
90        if ctx.message.direction() == MessageDirection::FromWidget {
91            if let Some(BitFieldMessage::Value(value)) = ctx.message.data::<BitFieldMessage<T>>() {
92                return Some(PropertyChanged {
93                    name: ctx.name.to_string(),
94                    owner_type_id: ctx.owner_type_id,
95                    value: FieldKind::object(*value),
96                });
97            }
98        }
99        None
100    }
101}