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