fyrox_ui/inspector/editors/
path.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        FieldKind, InspectorError, PropertyChanged,
28    },
29    message::{MessageDirection, UiMessage},
30    path::{PathEditorBuilder, PathEditorMessage},
31    widget::WidgetBuilder,
32    Thickness, VerticalAlignment,
33};
34use std::{any::TypeId, path::PathBuf};
35
36#[derive(Debug)]
37pub struct PathPropertyEditorDefinition;
38
39impl PropertyEditorDefinition for PathPropertyEditorDefinition {
40    fn value_type_id(&self) -> TypeId {
41        TypeId::of::<PathBuf>()
42    }
43
44    fn create_instance(
45        &self,
46        ctx: PropertyEditorBuildContext,
47    ) -> Result<PropertyEditorInstance, InspectorError> {
48        let value = ctx.property_info.cast_value::<PathBuf>()?;
49        Ok(PropertyEditorInstance::Simple {
50            editor: PathEditorBuilder::new(
51                WidgetBuilder::new()
52                    .with_margin(Thickness::top_bottom(1.0))
53                    .with_vertical_alignment(VerticalAlignment::Center),
54            )
55            .with_path(value.clone())
56            .build(ctx.build_context),
57        })
58    }
59
60    fn create_message(
61        &self,
62        ctx: PropertyEditorMessageContext,
63    ) -> Result<Option<UiMessage>, InspectorError> {
64        let value = ctx.property_info.cast_value::<PathBuf>()?;
65        Ok(Some(PathEditorMessage::path(
66            ctx.instance,
67            MessageDirection::ToWidget,
68            value.clone(),
69        )))
70    }
71
72    fn translate_message(&self, ctx: PropertyEditorTranslationContext) -> Option<PropertyChanged> {
73        if ctx.message.direction() == MessageDirection::FromWidget {
74            if let Some(PathEditorMessage::Path(value)) = ctx.message.data() {
75                return Some(PropertyChanged {
76                    name: ctx.name.to_string(),
77                    owner_type_id: ctx.owner_type_id,
78                    value: FieldKind::object(value.clone()),
79                });
80            }
81        }
82        None
83    }
84}