Ejemplo n.º 1
0
        public void SetActiveItem(ISceneContext sceneContext)
        {
            var selectedItem = sceneContext?.Scene?.SelectedItem;

            if (this.item == selectedItem)
            {
                return;
            }

            this.item = selectedItem;
            editorPanel.CloseChildren();

            // Allow caller to clean up with passing null for selectedItem
            if (item == null)
            {
                editorSectionWidget.Text = editorTitle;
                return;
            }

            var selectedItemType = selectedItem.GetType();

            primaryActionsPanel.RemoveChildren();

            IEnumerable <SceneOperation> primaryActions;

            if ((primaryActions = SceneOperations.GetPrimaryOperations(selectedItemType)) == null)
            {
                primaryActions = new List <SceneOperation>();
            }
            else
            {
                // Loop over primary actions creating a button for each
                foreach (var primaryAction in primaryActions)
                {
                    // TODO: Run visible/enable rules on actions, conditionally add/enable as appropriate
                    var button = new IconButton(primaryAction.Icon(theme.InvertIcons), theme)
                    {
                        // Name = namedAction.Title + " Button",
                        ToolTipText     = primaryAction.Title,
                        Margin          = theme.ButtonSpacing,
                        BackgroundColor = theme.ToolbarButtonBackground,
                        HoverColor      = theme.ToolbarButtonHover,
                        MouseDownColor  = theme.ToolbarButtonDown,
                    };

                    button.Click += (s, e) =>
                    {
                        primaryAction.Action.Invoke(sceneContext);
                    };

                    primaryActionsPanel.AddChild(button);
                }
            }

            if (primaryActionsPanel.Children.Any())
            {
                // add in a separator from the apply and cancel buttons
                primaryActionsPanel.AddChild(new ToolbarSeparator(theme.GetBorderColor(50), theme.SeparatorMargin));
            }

            editorSectionWidget.Text = selectedItem.Name ?? selectedItemType.Name;

            HashSet <IObject3DEditor> mappedEditors = ApplicationController.Instance.Extensions.GetEditorsForType(selectedItemType);

            var undoBuffer = sceneContext.Scene.UndoBuffer;

            // put in a color edit field
            var colorField = new ColorField(theme, selectedItem.Color);

            colorField.Initialize(0);
            colorField.ValueChanged += (s, e) =>
            {
                if (selectedItem.Color != colorField.Color)
                {
                    undoBuffer.AddAndDo(new ChangeColor(selectedItem, colorField.Color));
                }
            };

            colorField.Content.MouseDown += (s, e) =>
            {
                // make sure the render mode is set to shaded or outline
                if (sceneContext.ViewState.RenderType != RenderOpenGl.RenderTypes.Shaded &&
                    sceneContext.ViewState.RenderType != RenderOpenGl.RenderTypes.Outlines)
                {
                    // make sure the render mode is set to outline
                    sceneContext.ViewState.RenderType = RenderOpenGl.RenderTypes.Outlines;
                }
            };

            // color row
            var row = new SettingsRow("Color".Localize(), null, colorField.Content, theme);

            // Special top border style for first item in editor
            row.Border = new BorderDouble(0, 1);

            editorPanel.AddChild(row);

            // put in a material edit field
            var materialField = new MaterialIndexField(sceneContext.Printer, theme, selectedItem.MaterialIndex);

            materialField.Initialize(0);
            materialField.ValueChanged += (s, e) =>
            {
                if (selectedItem.MaterialIndex != materialField.MaterialIndex)
                {
                    undoBuffer.AddAndDo(new ChangeMaterial(selectedItem, materialField.MaterialIndex));
                }
            };

            materialField.Content.MouseDown += (s, e) =>
            {
                if (sceneContext.ViewState.RenderType != RenderOpenGl.RenderTypes.Materials)
                {
                    // make sure the render mode is set to material
                    sceneContext.ViewState.RenderType = RenderOpenGl.RenderTypes.Materials;
                }
            };

            // material row
            editorPanel.AddChild(
                new SettingsRow("Material".Localize(), null, materialField.Content, theme));

            // put in the normal editor
            if (selectedItem is ComponentObject3D componentObject &&
                componentObject.Finalized)
            {
                PublicPropertyEditor.AddUnlockLinkIfRequired(selectedItem, editorPanel, theme);
                foreach (var selector in componentObject.SurfacedEditors)
                {
                    // Get the named property via reflection
                    // Selector example:            '$.Children<CylinderObject3D>'
                    var match = pathResolver.Select(componentObject, selector).ToList();

                    //// TODO: Create editor row for each property
                    //// - Use the type of the property to find a matching editor (ideally all datatype -> editor functionality would resolve consistently)
                    //// - Add editor row for each

                    foreach (var instance in match)
                    {
                        if (instance is IObject3D object3D)
                        {
                            if (ApplicationController.Instance.Extensions.GetEditorsForType(object3D.GetType())?.FirstOrDefault() is IObject3DEditor editor)
                            {
                                ShowObjectEditor((editor, object3D, object3D.Name), selectedItem);
                            }
                        }
                        else if (JsonPath.JsonPathContext.ReflectionValueSystem.LastMemberValue is ReflectionTarget reflectionTarget)
                        {
                            var context = new PPEContext();

                            if (reflectionTarget.Source is IObject3D editedChild)
                            {
                                context.item = editedChild;
                            }
                            else
                            {
                                context.item = item;
                            }

                            var editableProperty = new EditableProperty(reflectionTarget.PropertyInfo, reflectionTarget.Source);

                            var editor = PublicPropertyEditor.CreatePropertyEditor(editableProperty, undoBuffer, context, theme);
                            if (editor != null)
                            {
                                editorPanel.AddChild(editor);
                            }
                        }
                    }
                }

                // Enforce panel padding
                foreach (var sectionWidget in editorPanel.Descendants <SectionWidget>())
                {
                    sectionWidget.Margin = 0;
                }
            }
Ejemplo n.º 2
0
        public void SetActiveItem(ISceneContext sceneContext)
        {
            var selectedItem = sceneContext?.Scene?.SelectedItem;

            if (this.item == selectedItem)
            {
                return;
            }

            this.item = selectedItem;
            editorPanel.CloseChildren();

            // Allow caller to clean up with passing null for selectedItem
            if (item == null)
            {
                editorSectionWidget.Text = editorTitle;
                return;
            }

            var selectedItemType = selectedItem.GetType();

            primaryActionsPanel.RemoveChildren();

            IEnumerable <SceneOperation> primaryActions;

            if ((primaryActions = SceneOperations.GetPrimaryOperations(selectedItemType)) == null)
            {
                primaryActions = new List <SceneOperation>();
            }
            else
            {
                // Loop over primary actions creating a button for each
                foreach (var primaryAction in primaryActions)
                {
                    // TODO: Run visible/enable rules on actions, conditionally add/enable as appropriate
                    var button = new IconButton(primaryAction.Icon(theme), theme)
                    {
                        // Name = namedAction.Title + " Button",
                        ToolTipText     = primaryAction.Title,
                        Margin          = theme.ButtonSpacing,
                        BackgroundColor = theme.ToolbarButtonBackground,
                        HoverColor      = theme.ToolbarButtonHover,
                        MouseDownColor  = theme.ToolbarButtonDown,
                    };

                    button.Click += (s, e) =>
                    {
                        primaryAction.Action.Invoke(sceneContext);
                    };

                    primaryActionsPanel.AddChild(button);
                }
            }

            if (primaryActionsPanel.Children.Any())
            {
                // add in a separator from the apply and cancel buttons
                primaryActionsPanel.AddChild(new ToolbarSeparator(theme.GetBorderColor(50), theme.SeparatorMargin));
            }

            editorSectionWidget.Text = selectedItem.Name ?? selectedItemType.Name;

            HashSet <IObject3DEditor> mappedEditors = ApplicationController.Instance.Extensions.GetEditorsForType(selectedItemType);

            var undoBuffer = sceneContext.Scene.UndoBuffer;

            if (!(selectedItem.GetType().GetCustomAttributes(typeof(HideMeterialAndColor), true).FirstOrDefault() is HideMeterialAndColor))
            {
                // put in a color edit field
                var colorField = new ColorField(theme, selectedItem.Color);
                colorField.Initialize(0);
                colorField.ValueChanged += (s, e) =>
                {
                    if (selectedItem.Color != colorField.Color)
                    {
                        undoBuffer.AddAndDo(new ChangeColor(selectedItem, colorField.Color));
                    }
                };

                colorField.Content.MouseDown += (s, e) =>
                {
                    // make sure the render mode is set to shaded or outline
                    if (sceneContext.ViewState.RenderType != RenderOpenGl.RenderTypes.Shaded &&
                        sceneContext.ViewState.RenderType != RenderOpenGl.RenderTypes.Outlines)
                    {
                        // make sure the render mode is set to outline
                        sceneContext.ViewState.RenderType = RenderOpenGl.RenderTypes.Outlines;
                    }
                };

                // color row
                var row = new SettingsRow("Color".Localize(), null, colorField.Content, theme);

                // Special top border style for first item in editor
                row.Border = new BorderDouble(0, 1);

                editorPanel.AddChild(row);

                // put in a material edit field
                var materialField = new MaterialIndexField(sceneContext.Printer, theme, selectedItem.MaterialIndex);
                materialField.Initialize(0);
                materialField.ValueChanged += (s, e) =>
                {
                    if (selectedItem.MaterialIndex != materialField.MaterialIndex)
                    {
                        undoBuffer.AddAndDo(new ChangeMaterial(selectedItem, materialField.MaterialIndex));
                    }
                };

                materialField.Content.MouseDown += (s, e) =>
                {
                    if (sceneContext.ViewState.RenderType != RenderOpenGl.RenderTypes.Materials)
                    {
                        // make sure the render mode is set to material
                        sceneContext.ViewState.RenderType = RenderOpenGl.RenderTypes.Materials;
                    }
                };

                // material row
                editorPanel.AddChild(
                    new SettingsRow("Material".Localize(), null, materialField.Content, theme));
            }

            var rows = new SafeList <SettingsRow>();

            // put in the normal editor
            if (selectedItem is ComponentObject3D componentObject &&
                componentObject.Finalized)
            {
                var context = new PPEContext();
                PublicPropertyEditor.AddUnlockLinkIfRequired(selectedItem, editorPanel, theme);
                foreach (var selector in componentObject.SurfacedEditors)
                {
                    // if it is a reference to a sheet cell
                    if (selector.StartsWith("!"))
                    {
                        var firtSheet = componentObject.Descendants <SheetObject3D>().FirstOrDefault();
                        if (firtSheet != null)
                        {
                            var cellId = selector.Substring(1);
                            var cell   = firtSheet.SheetData[cellId];
                            if (cell != null)
                            {
                                // add an editor for the cell
                                var field = new DoubleField(theme);
                                field.Initialize(0);
                                double.TryParse(firtSheet.SheetData.EvaluateExpression(cellId), out double value);
                                field.DoubleValue = value;
                                field.ClearUndoHistory();

                                field.Content.Descendants <InternalNumberEdit>().First().MaxDecimalsPlaces = 3;
                                field.ValueChanged += (s, e) =>
                                {
                                    cell.Expression = field.Value;
                                    firtSheet.SheetData.Recalculate();
                                };

                                var row = new SettingsRow(cell.Name == null ? cellId : cell.Name, null, field.Content, theme);

                                editorPanel.AddChild(row);
                            }
                        }
                    }
                    else                     // parse it as a path to an object
                    {
                        // Get the named property via reflection
                        // Selector example:            '$.Children<CylinderObject3D>'
                        var match = pathResolver.Select(componentObject, selector).ToList();

                        //// - Add editor row for each
                        foreach (var instance in match)
                        {
                            if (instance is IObject3D object3D)
                            {
                                if (ApplicationController.Instance.Extensions.GetEditorsForType(object3D.GetType())?.FirstOrDefault() is IObject3DEditor editor)
                                {
                                    ShowObjectEditor((editor, object3D, object3D.Name), selectedItem);
                                }
                            }
                            else if (JsonPathContext.ReflectionValueSystem.LastMemberValue is ReflectionTarget reflectionTarget)
                            {
                                if (reflectionTarget.Source is IObject3D editedChild)
                                {
                                    context.item = editedChild;
                                }
                                else
                                {
                                    context.item = item;
                                }

                                var editableProperty = new EditableProperty(reflectionTarget.PropertyInfo, reflectionTarget.Source);

                                var editor = PublicPropertyEditor.CreatePropertyEditor(rows, editableProperty, undoBuffer, context, theme);
                                if (editor != null)
                                {
                                    editorPanel.AddChild(editor);
                                }

                                // Init with custom 'UpdateControls' hooks
                                (context.item as IPropertyGridModifier)?.UpdateControls(new PublicPropertyChange(context, "Update_Button"));
                            }
                        }
                    }
                }

                // Enforce panel padding
                foreach (var sectionWidget in editorPanel.Descendants <SectionWidget>())
                {
                    sectionWidget.Margin = 0;
                }
            }