mattercontrol/MatterControlLib/PartPreviewWindow/SelectedObjectPanel.cs

437 lines
14 KiB
C#
Raw Normal View History

/*
Copyright (c) 2018, Lars Brubaker, John Lewin
All rights reserved.
Redistribution and use in source and binary forms, with or without
2015-04-08 15:20:10 -07:00
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
2015-04-08 15:20:10 -07:00
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
2015-04-08 15:20:10 -07:00
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those
2015-04-08 15:20:10 -07:00
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
*/
using System;
using System.Collections.Generic;
using System.Linq;
2018-06-08 08:41:15 -07:00
using JsonPath;
2017-03-15 16:17:06 -07:00
using MatterHackers.Agg;
using MatterHackers.Agg.Platform;
using MatterHackers.Agg.UI;
using MatterHackers.DataConverters3D;
2017-03-15 16:17:06 -07:00
using MatterHackers.Localizations;
using MatterHackers.MatterControl.CustomWidgets;
using MatterHackers.MatterControl.DesignTools;
using MatterHackers.MatterControl.DesignTools.Operations;
using MatterHackers.MatterControl.Library;
using MatterHackers.MatterControl.SlicerConfiguration;
2018-06-08 08:41:15 -07:00
using static JsonPath.JsonPathContext.ReflectionValueSystem;
2017-03-15 16:17:06 -07:00
namespace MatterHackers.MatterControl.PartPreviewWindow
{
public class SelectedObjectPanel : FlowLayoutWidget, IContentStore
2017-03-15 16:17:06 -07:00
{
private IObject3D item = new Object3D();
2019-05-18 22:06:06 -07:00
private readonly ThemeConfig theme;
private readonly ISceneContext sceneContext;
private readonly SectionWidget editorSectionWidget;
2019-05-18 22:06:06 -07:00
private readonly GuiWidget editorPanel;
2019-05-18 22:06:06 -07:00
private readonly string editorTitle = "Properties".Localize();
public SelectedObjectPanel(View3DWidget view3DWidget, ISceneContext sceneContext, ThemeConfig theme)
: base(FlowDirection.TopToBottom)
2015-04-08 15:20:10 -07:00
{
this.HAnchor = HAnchor.Stretch;
this.VAnchor = VAnchor.Top | VAnchor.Fit;
this.Padding = 0;
this.theme = theme;
this.sceneContext = sceneContext;
var toolbar = new LeftClipFlowLayoutWidget()
{
BackgroundColor = theme.BackgroundColor,
Padding = theme.ToolbarPadding,
HAnchor = HAnchor.Fit,
VAnchor = VAnchor.Fit
};
2018-11-16 08:44:56 -08:00
scene = sceneContext.Scene;
// put in a make permanent button
var icon = AggContext.StaticData.LoadIcon("noun_766157.png", 16, 16, theme.InvertIcons).SetPreMultiply();
2018-11-16 08:44:56 -08:00
flattenButton = new IconButton(icon, theme)
{
Margin = theme.ButtonSpacing,
ToolTipText = "Flatten".Localize(),
Enabled = true
};
flattenButton.Click += (s, e) =>
{
if (this.item.CanFlatten)
{
2019-01-26 09:31:50 -08:00
var item = this.item;
using (new SelectionMaintainer(view3DWidget.Scene))
{
item.Flatten(view3DWidget.Scene.UndoBuffer);
}
}
else
{
// try to ungroup it
sceneContext.Scene.UngroupSelection();
}
};
toolbar.AddChild(flattenButton);
// put in a remove button
2018-11-16 08:44:56 -08:00
removeButton = new IconButton(AggContext.StaticData.LoadIcon("remove.png", 16, 16, theme.InvertIcons), theme)
{
Margin = theme.ButtonSpacing,
2018-08-28 18:45:58 -07:00
ToolTipText = "Delete".Localize(),
Enabled = scene.SelectedItem != null
};
removeButton.Click += (s, e) =>
{
2019-01-26 09:31:50 -08:00
var item = this.item;
using (new SelectionMaintainer(view3DWidget.Scene))
{
2019-01-26 09:31:50 -08:00
item.Remove(view3DWidget.Scene.UndoBuffer);
}
};
toolbar.AddChild(removeButton);
primaryActionsPanel = new FlowLayoutWidget()
{
HAnchor = HAnchor.Fit,
VAnchor = VAnchor.Center | VAnchor.Fit
};
toolbar.AddChild(primaryActionsPanel);
2018-11-16 08:44:56 -08:00
overflowButton = new OverflowBar.OverflowMenuButton(theme)
2018-08-28 18:45:58 -07:00
{
Enabled = scene.SelectedItem != null,
};
overflowButton.DynamicPopupContent = () =>
{
var remainingOperations = ApplicationController.Instance.Graph.Operations.Values.Except(primaryActions);
return ApplicationController.Instance.GetActionMenuForSceneItem(item, sceneContext.Scene, false, remainingOperations);
};
toolbar.AddChild(overflowButton);
editorPanel = new FlowLayoutWidget(FlowDirection.TopToBottom)
{
HAnchor = HAnchor.Stretch,
VAnchor = VAnchor.Fit,
Name = "editorPanel",
Padding = new BorderDouble(right: theme.DefaultContainerPadding + 1)
};
// Wrap editorPanel with scrollable container
var scrollableWidget = new ScrollableWidget(true)
{
HAnchor = HAnchor.Stretch,
VAnchor = VAnchor.Stretch
};
scrollableWidget.AddChild(editorPanel);
scrollableWidget.ScrollArea.HAnchor = HAnchor.Stretch;
editorSectionWidget = new SectionWidget(editorTitle, scrollableWidget, theme, toolbar, expandingContent: false, defaultExpansion: true, setContentVAnchor: false)
{
VAnchor = VAnchor.Stretch
};
this.AddChild(editorSectionWidget);
this.ContentPanel = editorPanel;
editorPanel.Padding = new BorderDouble(theme.DefaultContainerPadding, 0);
2018-11-16 08:44:56 -08:00
// Register listeners
scene.SelectionChanged += Scene_SelectionChanged;
}
public GuiWidget ContentPanel { get; set; }
2019-05-18 22:06:06 -07:00
private readonly JsonPathContext pathResolver = new JsonPathContext();
private readonly IconButton flattenButton;
private readonly IconButton removeButton;
private readonly OverflowBar.OverflowMenuButton overflowButton;
private readonly InteractiveScene scene;
private readonly FlowLayoutWidget primaryActionsPanel;
private List<NodeOperation> primaryActions;
2018-06-08 08:41:15 -07:00
public void SetActiveItem(IObject3D selectedItem)
{
if (this.item == selectedItem)
{
return;
}
this.item = selectedItem;
editorPanel.CloseAllChildren();
// Allow caller to clean up with passing null for selectedItem
if (item == null)
{
editorSectionWidget.Text = editorTitle;
return;
}
var selectedItemType = selectedItem.GetType();
primaryActionsPanel.RemoveAllChildren();
var graph = ApplicationController.Instance.Graph;
if (!graph.PrimaryOperations.TryGetValue(selectedItemType, out primaryActions))
{
primaryActions = new List<NodeOperation>();
}
else
{
// Loop over primary actions creating a button for each
2019-05-18 22:06:06 -07:00
foreach (var primaryAction in primaryActions)
{
// TODO: Run visible/enable rules on actions, conditionally add/enable as appropriate
var button = new IconButton(primaryAction.IconCollector(theme.InvertIcons), theme)
{
2019-05-18 22:06:06 -07:00
// Name = namedAction.Title + " Button",
ToolTipText = primaryAction.Title,
Margin = theme.ButtonSpacing,
BackgroundColor = theme.ToolbarButtonBackground,
HoverColor = theme.ToolbarButtonHover,
MouseDownColor = theme.ToolbarButtonDown,
};
button.Click += (s, e) =>
{
primaryAction.Operation.Invoke(item, scene);
};
primaryActionsPanel.AddChild(button);
}
}
editorSectionWidget.Text = selectedItem.Name ?? selectedItemType.Name;
2019-02-04 08:41:08 -08:00
HashSet<IObject3DEditor> mappedEditors = ApplicationController.Instance.Extensions.GetEditorsForType(selectedItemType);
2018-06-08 08:41:15 -07:00
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));
}
};
var colorRow = PublicPropertyEditor.CreateSettingsRow("Color".Localize());
colorRow.AddChild(colorField.Content);
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)
{
2019-05-18 22:06:06 -07:00
// make sure the render mode is set to outline
sceneContext.ViewState.RenderType = RenderOpenGl.RenderTypes.Outlines;
}
};
editorPanel.AddChild(colorRow);
// put in a material edit field
var materialField = new MaterialIndexField(theme, selectedItem.MaterialIndex);
materialField.Initialize(0);
materialField.ValueChanged += (s, e) =>
{
if (selectedItem.MaterialIndex != materialField.MaterialIndex)
{
undoBuffer.AddAndDo(new ChangeMaterial(selectedItem, materialField.MaterialIndex));
}
};
var materialRow = PublicPropertyEditor.CreateSettingsRow("Material".Localize());
materialRow.AddChild(materialField.Content);
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;
}
2018-10-08 09:44:06 -07:00
};
editorPanel.AddChild(materialRow);
// put in the normal editor
if (selectedItem is ComponentObject3D componentObject
&& componentObject.Finalized)
2018-06-08 08:41:15 -07:00
{
foreach (var selector in componentObject.SurfacedEditors)
{
// Get the named property via reflection
// Selector example: '$.Children<CylinderObject3D>'
2018-08-11 11:12:16 -07:00
var match = pathResolver.Select(componentObject, selector).ToList();
2018-06-08 08:41:15 -07:00
//// 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)
{
2019-02-04 08:41:08 -08:00
if (ApplicationController.Instance.Extensions.GetEditorsForType(object3D.GetType())?.FirstOrDefault() is IObject3DEditor editor)
{
2019-05-18 22:06:06 -07:00
ShowObjectEditor((editor, object3D, object3D.Name), selectedItem);
}
2018-06-08 08:41:15 -07:00
}
else if (JsonPath.JsonPathContext.ReflectionValueSystem.LastMemberValue is ReflectionTarget reflectionTarget)
{
var context = new PPEContext();
2018-06-27 11:58:11 -07:00
if (reflectionTarget.Source is IObject3D editedChild)
{
context.item = editedChild;
}
else
2018-06-08 08:41:15 -07:00
{
context.item = item;
}
2018-06-08 08:41:15 -07:00
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 = new BorderDouble(0, theme.DefaultContainerPadding / 2);
}
2018-06-08 08:41:15 -07:00
}
2018-06-27 11:58:11 -07:00
else
{
2019-02-04 08:41:08 -08:00
if (ApplicationController.Instance.Extensions.GetEditorsForType(item.GetType())?.FirstOrDefault() is IObject3DEditor editor)
2018-06-27 11:58:11 -07:00
{
2019-05-18 22:06:06 -07:00
ShowObjectEditor((editor, item, item.Name), selectedItem);
2018-06-27 11:58:11 -07:00
}
}
}
2019-05-18 22:06:06 -07:00
private class OperationButton : TextButton
{
2019-05-18 22:06:06 -07:00
private readonly NodeOperation graphOperation;
private readonly IObject3D sceneItem;
public OperationButton(NodeOperation graphOperation, IObject3D sceneItem, ThemeConfig theme)
: base(graphOperation.Title, theme)
{
this.graphOperation = graphOperation;
this.sceneItem = sceneItem;
}
public void EnsureAvailablity()
{
this.Enabled = graphOperation.IsEnabled?.Invoke(sceneItem) != false;
}
}
2019-05-18 22:06:06 -07:00
private void ShowObjectEditor((IObject3DEditor editor, IObject3D item, string displayName) scopeItem, IObject3D rootSelection)
{
var selectedItem = scopeItem.item;
var editorWidget = scopeItem.editor.Create(selectedItem, sceneContext.Scene.UndoBuffer, theme);
editorWidget.HAnchor = HAnchor.Stretch;
editorWidget.VAnchor = VAnchor.Fit;
2018-05-21 13:30:06 -07:00
if (scopeItem.item != rootSelection
&& scopeItem.editor is PublicPropertyEditor)
{
editorWidget.Padding = new BorderDouble(10, 10, 10, 0);
// EditOutline section
var sectionWidget = new SectionWidget(
scopeItem.displayName ?? "Unknown",
editorWidget,
theme);
theme.ApplyBoxStyle(sectionWidget, margin: 0);
editorWidget = sectionWidget;
2018-05-22 07:26:22 -07:00
}
else
{
editorWidget.Padding = 0;
}
editorPanel.AddChild(editorWidget);
2015-04-08 15:20:10 -07:00
}
public void Save(ILibraryItem item, IObject3D content)
{
this.item.Parent.Children.Modify(children =>
{
children.Remove(this.item);
children.Add(content);
});
}
2018-11-16 08:44:56 -08:00
public override void OnClosed(EventArgs e)
{
// Unregister listeners
scene.SelectionChanged -= Scene_SelectionChanged;
base.OnClosed(e);
}
private void Scene_SelectionChanged(object sender, EventArgs e)
{
if (editorPanel.Children.FirstOrDefault()?.DescendantsAndSelf<SectionWidget>().FirstOrDefault() is SectionWidget firstSectionWidget)
{
firstSectionWidget.Margin = firstSectionWidget.Margin.Clone(top: 0);
}
var selectedItem = scene.SelectedItem;
flattenButton.Enabled = selectedItem != null
&& (selectedItem is GroupObject3D
|| selectedItem.GetType() == typeof(Object3D)
|| selectedItem.CanFlatten);
2018-11-16 08:44:56 -08:00
removeButton.Enabled = selectedItem != null;
overflowButton.Enabled = selectedItem != null;
2019-05-18 22:06:06 -07:00
if (selectedItem == null)
{
primaryActionsPanel.RemoveAllChildren();
}
2018-11-16 08:44:56 -08:00
}
2015-04-08 15:20:10 -07:00
}
}