mattercontrol/PartPreviewWindow/SelectedObjectPanel.cs
John Lewin 321d24e5cd Revise rules for showing operation buttons
- Issue MatterHackers/MCCentral#2852
Wrapping operation buttons should be hidden if not in scene root
2018-03-22 13:18:27 -07:00

485 lines
No EOL
14 KiB
C#

/*
Copyright (c) 2017, Lars Brubaker, John Lewin
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
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
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
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;
using MatterHackers.Agg;
using MatterHackers.Agg.Platform;
using MatterHackers.Agg.UI;
using MatterHackers.DataConverters3D;
using MatterHackers.Localizations;
using MatterHackers.MatterControl.CustomWidgets;
using MatterHackers.MatterControl.DesignTools;
using MatterHackers.MatterControl.Library;
using MatterHackers.VectorMath;
namespace MatterHackers.MatterControl.PartPreviewWindow
{
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class IObject3DComponentAttribute: Attribute
{
}
public interface IObject3DComponent
{
}
public class SelectedObjectPanel : FlowLayoutWidget, IContentStore
{
private IObject3D item = new Object3D();
private FlowLayoutWidget scrollableContent;
private ThemeConfig theme;
private View3DWidget view3DWidget;
private InteractiveScene scene;
private PrinterConfig printer;
private Dictionary<Type, HashSet<IObject3DEditor>> objectEditorsByType;
private SectionWidget editorSection;
private TextButton editButton;
private GuiWidget editorPanel;
private InlineTitleEdit inlineTitleEdit;
public SelectedObjectPanel(View3DWidget view3DWidget, InteractiveScene scene, ThemeConfig theme, PrinterConfig printer)
: base(FlowDirection.TopToBottom)
{
this.HAnchor = HAnchor.Stretch;
this.VAnchor = VAnchor.Top | VAnchor.Fit;
this.Padding = 0; // new BorderDouble(8, 10);
//this.MinimumSize = new Vector2(220, 0);
this.view3DWidget = view3DWidget;
this.theme = theme;
this.scene = scene;
this.printer = printer;
this.AddChild(inlineTitleEdit = new InlineTitleEdit("", theme, "Object Name"));
inlineTitleEdit.TitleChanged += (s, e) =>
{
if (item != null)
{
item.Name = inlineTitleEdit.Text;
}
};
scrollableContent = new FlowLayoutWidget(FlowDirection.TopToBottom)
{
HAnchor = HAnchor.Stretch,
VAnchor = VAnchor.Fit,
};
var scrollable = new ScrollableWidget(true)
{
Name = "editorPanel",
HAnchor = HAnchor.Stretch,
VAnchor = VAnchor.Stretch,
};
scrollable.AddChild(scrollableContent);
scrollable.ScrollArea.HAnchor = HAnchor.Stretch;
this.AddChild(scrollable);
// Add heading separator
scrollableContent.AddChild(new HorizontalLine(25)
{
Margin = new BorderDouble(0)
});
var editorColumn = new FlowLayoutWidget(FlowDirection.TopToBottom)
{
HAnchor = HAnchor.Stretch,
VAnchor = VAnchor.Fit
};
var toolbar = new Toolbar()
{
Padding = theme.ToolbarPadding,
HAnchor = HAnchor.Stretch,
VAnchor = VAnchor.Fit
};
editorColumn.AddChild(toolbar);
editButton = new TextButton("Edit".Localize(), theme)
{
BackgroundColor = theme.MinimalShade,
Margin = theme.ButtonSpacing
};
scene.SelectionChanged += (s, e) => editButton.Enabled = scene.SelectedItem?.CanEdit == true;
editButton.Click += async (s, e) =>
{
var bed = new BedConfig();
var partPreviewContent = this.Parents<PartPreviewContent>().FirstOrDefault();
partPreviewContent.CreatePartTab(
"New Part",
bed,
theme);
var clonedItem = this.item.Clone();
// Edit in Identity transform
clonedItem.Matrix = Matrix4X4.Identity;
await bed.LoadContent(
new EditContext()
{
ContentStore = new DynamicContentStore((libraryItem, object3D) =>
{
var replacement = object3D.Clone();
this.item.Parent.Children.Modify(list =>
{
list.Remove(item);
// Restore matrix of item being replaced
replacement.Matrix = item.Matrix;
list.Add(replacement);
item = replacement;
});
scene.SelectedItem = replacement;
}),
SourceItem = new InMemoryLibraryItem(clonedItem),
});
};
toolbar.AddChild(editButton);
// put in a bake button
var icon = AggContext.StaticData.LoadIcon("bake.png", 16, 16).SetPreMultiply();
var bakeButton = new IconButton(icon, theme)
{
Margin = theme.ButtonSpacing,
ToolTipText = "Bake operation into parts".Localize()
};
bakeButton.Click += (s, e) =>
{
scene.SelectedItem = null;
this.item.Bake();
};
scene.SelectionChanged += (s, e) => bakeButton.Enabled = scene.SelectedItem?.CanBake == true;
toolbar.AddChild(bakeButton);
// put in a remove button
var removeButton = new IconButton(ThemeConfig.RestoreNormal, theme)
{
Margin = theme.ButtonSpacing,
ToolTipText = "Remove operation from parts".Localize()
};
removeButton.Click += (s, e) =>
{
scene.SelectedItem = null;
this.item.Remove();
};
scene.SelectionChanged += (s, e) => removeButton.Enabled = scene.SelectedItem?.CanRemove == true;
toolbar.AddChild(removeButton);
// Add container used to host the current specialized editor for the selection
editorColumn.AddChild(editorPanel = new FlowLayoutWidget(FlowDirection.TopToBottom)
{
HAnchor = HAnchor.Stretch,
VAnchor = VAnchor.Fit,
Padding = new BorderDouble(top: 10)
});
editorSection = new SectionWidget("Editor", editorColumn, theme);
scrollableContent.AddChild(editorSection);
var colorSection = new SectionWidget("Color".Localize(), new ColorSwatchSelector(scene, theme, buttonSize: 16, buttonSpacing: new BorderDouble(1, 1, 0, 0))
{
Margin = new BorderDouble(left: 10)
}, theme, expanded: false)
{
Name = "Color Panel",
};
scrollableContent.AddChild(colorSection);
var mirrorSection = new SectionWidget("Mirror".Localize(), new MirrorControls(scene, theme), theme, expanded: false)
{
Name = "Mirror Panel",
};
scrollableContent.AddChild(mirrorSection);
var scaleSection = new SectionWidget("Scale".Localize(), new ScaleControls(scene, theme), theme, expanded: false)
{
Name = "Scale Panel",
};
scrollableContent.AddChild(scaleSection);
var materialsSection = new SectionWidget("Materials".Localize(), new MaterialControls(scene, theme), theme, expanded: false)
{
Name = "Materials Panel",
};
scrollableContent.AddChild(materialsSection);
// Enforce panel padding in sidebar
foreach(var sectionWidget in scrollableContent.Children<SectionWidget>())
{
sectionWidget.ContentPanel.Padding = new BorderDouble(10, 10, 10, 0);
}
HashSet<IObject3DEditor> mappedEditors;
objectEditorsByType = new Dictionary<Type, HashSet<IObject3DEditor>>();
// TODO: Consider only loading once into a static
var objectEditors = PluginFinder.CreateInstancesOf<IObject3DEditor>();
foreach (IObject3DEditor editor in objectEditors)
{
foreach (Type type in editor.SupportedTypes())
{
if (!objectEditorsByType.TryGetValue(type, out mappedEditors))
{
mappedEditors = new HashSet<IObject3DEditor>();
objectEditorsByType.Add(type, mappedEditors);
}
mappedEditors.Add(editor);
}
}
}
private static Type componentAttribute = typeof(IObject3DComponentAttribute);
private static Type componentType = typeof(IObject3DComponent);
private static Type iobject3DType = typeof(IObject3D);
public void SetActiveItem(IObject3D selectedItem)
{
if (!scene.HasSelection)
{
this.Parent.Visible = false;
return;
}
var selectedItemType = selectedItem.GetType();
editButton.Enabled = (selectedItem.Children.Count > 0);
inlineTitleEdit.Text = selectedItem.Name ?? selectedItemType.Name;
this.item = selectedItem;
var viewMode = printer?.ViewState.ViewMode;
this.Parent.Visible = viewMode == null || viewMode == PartViewMode.Model;
HashSet<IObject3DEditor> mappedEditors = GetEditorsForType(selectedItemType);
var activeEditors = new List<(IObject3DEditor, IObject3D)>();
// If item is IObject3DComponent
if (componentType.IsAssignableFrom(selectedItemType))
{
// Get all public, instance properties where property type is IObject3D
var members = from item in selectedItemType.GetProperties(PublicPropertyEditor.OwnedPropertiesOnly)
let propertyType = item.PropertyType
where iobject3DType.IsAssignableFrom(propertyType)
select new
{
Type = propertyType,
Value = item.GetValue(selectedItem, null) as IObject3D
};
// Shown known editors for any matching properties
foreach (var member in members)
{
if (this.GetEditorsForType(member.Type)?.FirstOrDefault() is IObject3DEditor editor)
{
activeEditors.Add((editor, member.Value));
}
}
}
else
{
// Get all public, instance properties where property type is marked with IObject3DComponentAttribute
var members = from item in selectedItemType.GetProperties(PublicPropertyEditor.OwnedPropertiesOnly)
let propertyType = item.PropertyType
where Attribute.IsDefined(item, componentAttribute)
select new
{
Type = propertyType,
Value = item.GetValue(selectedItem, null) as IObject3D
};
// Shown known editors for any matching properties
foreach (var member in members)
{
if (this.GetEditorsForType(member.Type)?.FirstOrDefault() is IObject3DEditor editor)
{
activeEditors.Add((editor, member.Value));
}
}
}
if (mappedEditors?.Any() == true)
{
// Use first filtered or fall back to unfiltered first
activeEditors.Add((mappedEditors.First(), selectedItem));
}
ShowObjectEditor(activeEditors);
}
private HashSet<IObject3DEditor> GetEditorsForType(Type selectedItemType)
{
HashSet<IObject3DEditor> mappedEditors;
objectEditorsByType.TryGetValue(selectedItemType, out mappedEditors);
if (mappedEditors == null)
{
foreach (var kvp in objectEditorsByType)
{
var editorType = kvp.Key;
if (editorType.IsAssignableFrom(selectedItemType))
{
mappedEditors = kvp.Value;
break;
}
}
}
return mappedEditors;
}
private class OperationButton :TextButton
{
private GraphOperation graphOperation;
private IObject3D sceneItem;
public OperationButton(GraphOperation graphOperation, IObject3D sceneItem, ThemeConfig theme)
: base(graphOperation.Title, theme)
{
this.graphOperation = graphOperation;
this.sceneItem = sceneItem;
}
public void EnsureAvailablity()
{
this.Enabled = graphOperation.IsEnabled(sceneItem);
}
}
private void ShowObjectEditor(IEnumerable<(IObject3DEditor editor, IObject3D item)> scope)
{
editorPanel.CloseAllChildren();
if (scope == null)
{
return;
}
foreach (var scopeItem in scope)
{
var selectedItem = scopeItem.item;
var selectedItemType = selectedItem.GetType();
var editorWidget = scopeItem.editor.Create(selectedItem, view3DWidget, theme);
editorWidget.HAnchor = HAnchor.Stretch;
editorWidget.VAnchor = VAnchor.Fit;
editorWidget.Padding = 0;
editorPanel.AddChild(editorWidget);
// Only add Operation buttons if the item is rooted in the scene
if (selectedItem.Parent.Parent == null)
{
var buttons = new List<OperationButton>();
foreach (var graphOperation in ApplicationController.Instance.Graph.Operations)
{
foreach (var type in graphOperation.MappedTypes)
{
if (type.IsAssignableFrom(selectedItemType))
{
var button = new OperationButton(graphOperation, selectedItem, theme)
{
BackgroundColor = theme.MinimalShade,
Margin = theme.ButtonSpacing
};
button.EnsureAvailablity();
button.Click += (s, e) =>
{
graphOperation.Operation(selectedItem, scene).ConfigureAwait(false);
};
buttons.Add(button);
}
}
}
if (buttons.Any())
{
var toolbar = new Toolbar()
{
HAnchor = HAnchor.Stretch,
VAnchor = VAnchor.Fit,
Padding = theme.ToolbarPadding,
Margin = new BorderDouble(0, 8)
};
editorPanel.AddChild(toolbar);
foreach (var button in buttons)
{
toolbar.AddChild(button);
}
// TODO: Fix likely leak
selectedItem.Invalidated += (s, e) =>
{
foreach (var button in toolbar.ActionArea.Children.OfType<OperationButton>())
{
button.EnsureAvailablity();
}
};
}
}
else
{
// If the button toolbar isn't added, ensure panel has bottom margin
editorWidget.Margin = editorWidget.Margin.Clone(bottom: 15);
}
}
}
public void Save(ILibraryItem item, IObject3D content)
{
this.item.Parent.Children.Modify(children =>
{
children.Remove(this.item);
children.Add(content);
});
}
}
public enum AxisAlignment { Min, Center, Max, SourceCoordinateSystem };
}