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