1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
//! Property editor for [`InheritableVariable`]. It acts like a proxy to inner property, but also
//! adds special "revert" button that is used to revert value to its parent's value.

use crate::{
    button::{ButtonBuilder, ButtonMessage},
    core::{pool::Handle, reflect::prelude::*, variable::InheritableVariable},
    define_constructor,
    grid::{Column, GridBuilder, Row},
    inspector::{
        editors::{
            PropertyEditorBuildContext, PropertyEditorDefinition, PropertyEditorInstance,
            PropertyEditorMessageContext, PropertyEditorTranslationContext,
        },
        InspectorError, PropertyChanged,
    },
    inspector::{FieldKind, InheritableAction},
    message::UiMessage,
    utils::make_simple_tooltip,
    widget::WidgetBuilder,
    BuildContext, Control, MessageDirection, Thickness, UiNode, UserInterface, VerticalAlignment,
    Widget, WidgetMessage,
};
use fyrox_core::reflect::FieldValue;
use std::{
    any::{Any, TypeId},
    fmt::{Debug, Formatter},
    marker::PhantomData,
    ops::{Deref, DerefMut},
};

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum InheritablePropertyEditorMessage {
    Revert,
    Modified(bool),
}

impl InheritablePropertyEditorMessage {
    define_constructor!(InheritablePropertyEditorMessage:Revert => fn revert(), layout: false);
    define_constructor!(InheritablePropertyEditorMessage:Modified => fn modified(bool), layout: false);
}

#[derive(Debug, Clone)]
pub struct InheritablePropertyEditor {
    widget: Widget,
    revert: Handle<UiNode>,
    inner_editor: Handle<UiNode>,
}

impl Deref for InheritablePropertyEditor {
    type Target = Widget;

    fn deref(&self) -> &Self::Target {
        &self.widget
    }
}

impl DerefMut for InheritablePropertyEditor {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.widget
    }
}

impl Control for InheritablePropertyEditor {
    fn query_component(&self, type_id: TypeId) -> Option<&dyn Any> {
        if type_id == TypeId::of::<Self>() {
            Some(self)
        } else {
            None
        }
    }

    fn handle_routed_message(&mut self, ui: &mut UserInterface, message: &mut UiMessage) {
        self.widget.handle_routed_message(ui, message);

        if let Some(ButtonMessage::Click) = message.data() {
            if message.destination() == self.revert {
                ui.send_message(InheritablePropertyEditorMessage::revert(
                    self.handle,
                    MessageDirection::FromWidget,
                ));
            }
        } else if let Some(InheritablePropertyEditorMessage::Modified(modified)) = message.data() {
            if message.destination() == self.handle {
                ui.send_message(WidgetMessage::visibility(
                    self.revert,
                    MessageDirection::ToWidget,
                    *modified,
                ));
            }
        }

        // Re-cast messages from inner editor as message from this editor.
        if message.destination() == self.inner_editor {
            let mut clone = message.clone();
            clone.destination = self.handle;
            ui.send_message(clone);
        }
    }
}

struct InheritablePropertyEditorBuilder {
    widget_builder: WidgetBuilder,
    inner_editor: Handle<UiNode>,
    container: Handle<UiNode>,
    modified: bool,
}

impl InheritablePropertyEditorBuilder {
    pub fn new(widget_builder: WidgetBuilder) -> Self {
        Self {
            widget_builder,
            inner_editor: Handle::NONE,
            container: Handle::NONE,
            modified: false,
        }
    }

    pub fn with_inner_editor(mut self, inner_editor: Handle<UiNode>) -> Self {
        self.inner_editor = inner_editor;
        self
    }

    pub fn with_container(mut self, container: Handle<UiNode>) -> Self {
        self.container = container;
        self
    }

    pub fn with_modified(mut self, modified: bool) -> Self {
        self.modified = modified;
        self
    }

    pub fn build(self, ctx: &mut BuildContext) -> Handle<UiNode> {
        let revert;
        let grid = GridBuilder::new(WidgetBuilder::new().with_child(self.container).with_child({
            revert = ButtonBuilder::new(
                WidgetBuilder::new()
                    .with_visibility(self.modified)
                    .with_width(16.0)
                    .with_height(16.0)
                    .with_vertical_alignment(VerticalAlignment::Top)
                    .with_tooltip(make_simple_tooltip(ctx, "Revert To Parent"))
                    .with_margin(Thickness::uniform(1.0))
                    .on_column(1),
            )
            .with_text("<")
            .build(ctx);
            revert
        }))
        .add_row(Row::auto())
        .add_column(Column::stretch())
        .add_column(Column::auto())
        .build(ctx);

        ctx.add_node(UiNode::new(InheritablePropertyEditor {
            widget: self.widget_builder.with_child(grid).build(),
            revert,
            inner_editor: self.inner_editor,
        }))
    }
}

pub struct InheritablePropertyEditorDefinition<T>
where
    T: FieldValue,
{
    phantom: PhantomData<T>,
}

impl<T> InheritablePropertyEditorDefinition<T>
where
    T: FieldValue,
{
    pub fn new() -> Self {
        Self {
            phantom: PhantomData,
        }
    }
}

impl<T> Debug for InheritablePropertyEditorDefinition<T>
where
    T: Reflect + FieldValue,
{
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "InheritablePropertyEditorDefinition")
    }
}

fn make_proxy<'a, 'b, T>(property_info: &'b FieldInfo<'a>) -> Result<FieldInfo<'a>, InspectorError>
where
    T: Reflect + FieldValue,
    'b: 'a,
{
    let value = property_info.cast_value::<InheritableVariable<T>>()?;

    Ok(FieldInfo {
        owner_type_id: TypeId::of::<T>(),
        name: property_info.name,
        display_name: property_info.display_name,
        value: &**value,
        reflect_value: &**value,
        read_only: property_info.read_only,
        immutable_collection: property_info.immutable_collection,
        min_value: property_info.min_value,
        max_value: property_info.max_value,
        step: property_info.step,
        precision: property_info.precision,
        description: property_info.description,
        type_name: property_info.type_name,
        doc: property_info.doc,
    })
}

impl<T> PropertyEditorDefinition for InheritablePropertyEditorDefinition<T>
where
    T: Reflect + FieldValue,
{
    fn value_type_id(&self) -> TypeId {
        TypeId::of::<InheritableVariable<T>>()
    }

    fn create_instance(
        &self,
        ctx: PropertyEditorBuildContext,
    ) -> Result<PropertyEditorInstance, InspectorError> {
        if let Some(definition) = ctx
            .definition_container
            .definitions()
            .get(&TypeId::of::<T>())
        {
            let instance = definition.create_instance(PropertyEditorBuildContext {
                build_context: ctx.build_context,
                property_info: &make_proxy::<T>(ctx.property_info)?,
                environment: ctx.environment.clone(),
                definition_container: ctx.definition_container.clone(),
                sync_flag: ctx.sync_flag,
                layer_index: ctx.layer_index,
                generate_property_string_values: ctx.generate_property_string_values,
                filter: ctx.filter,
            })?;

            let wrapper = InheritablePropertyEditorBuilder::new(WidgetBuilder::new())
                .with_container(match instance {
                    PropertyEditorInstance::Simple { editor } => editor,
                    PropertyEditorInstance::Custom { container, .. } => container,
                })
                .with_inner_editor(match instance {
                    PropertyEditorInstance::Simple { editor } => editor,
                    PropertyEditorInstance::Custom { editor, .. } => editor,
                })
                .with_modified(
                    ctx.property_info
                        .cast_value::<InheritableVariable<T>>()?
                        .is_modified(),
                )
                .build(ctx.build_context);

            Ok(match instance {
                PropertyEditorInstance::Simple { .. } => {
                    PropertyEditorInstance::Simple { editor: wrapper }
                }
                PropertyEditorInstance::Custom { .. } => PropertyEditorInstance::Custom {
                    container: wrapper,
                    editor: wrapper,
                },
            })
        } else {
            Err(InspectorError::Custom("No editor!".to_string()))
        }
    }

    fn create_message(
        &self,
        ctx: PropertyEditorMessageContext,
    ) -> Result<Option<UiMessage>, InspectorError> {
        if let Some(definition) = ctx
            .definition_container
            .definitions()
            .get(&TypeId::of::<T>())
        {
            let instance = ctx
                .ui
                .node(ctx.instance)
                .cast::<InheritablePropertyEditor>()
                .unwrap();

            ctx.ui
                .send_message(InheritablePropertyEditorMessage::modified(
                    instance.handle,
                    MessageDirection::ToWidget,
                    ctx.property_info
                        .cast_value::<InheritableVariable<T>>()?
                        .is_modified(),
                ));

            return definition.create_message(PropertyEditorMessageContext {
                property_info: &make_proxy::<T>(ctx.property_info)?,
                environment: ctx.environment.clone(),
                definition_container: ctx.definition_container.clone(),
                sync_flag: ctx.sync_flag,
                instance: instance.inner_editor,
                layer_index: ctx.layer_index,
                ui: ctx.ui,
                generate_property_string_values: ctx.generate_property_string_values,
                filter: ctx.filter,
            });
        }

        Err(InspectorError::Custom("No editor!".to_string()))
    }

    fn translate_message(&self, ctx: PropertyEditorTranslationContext) -> Option<PropertyChanged> {
        if let Some(InheritablePropertyEditorMessage::Revert) = ctx.message.data() {
            return Some(PropertyChanged {
                name: ctx.name.to_string(),
                owner_type_id: ctx.owner_type_id,
                value: FieldKind::Inheritable(InheritableAction::Revert),
            });
        }

        // Try translate other messages using inner property editor.
        if let Some(definition) = ctx
            .definition_container
            .definitions()
            .get(&TypeId::of::<T>())
        {
            return definition.translate_message(PropertyEditorTranslationContext {
                environment: ctx.environment.clone(),
                name: ctx.name,
                owner_type_id: ctx.owner_type_id,
                message: ctx.message,
                definition_container: ctx.definition_container.clone(),
            });
        }

        None
    }
}