Extract IObject3D editor code from View3DWidget

This commit is contained in:
John Lewin 2017-10-18 18:18:10 -07:00
parent c00c33ed9f
commit f4fdb2c20f
2 changed files with 142 additions and 116 deletions

View file

@ -27,10 +27,14 @@ 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.UI;
using MatterHackers.DataConverters3D;
using MatterHackers.Localizations;
using MatterHackers.MeshVisualizer;
namespace MatterHackers.MatterControl.PartPreviewWindow
{
@ -38,16 +42,25 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
{
private IObject3D item = new Object3D();
private GuiWidget editorPanel;
private FlowLayoutWidget editorPanel;
private TextWidget itemName;
private ThemeConfig theme;
private View3DWidget view3DWidget;
private InteractiveScene scene;
public SelectedObjectPanel(View3DWidget view3DWidget, ThemeConfig theme)
private Dictionary<Type, HashSet<IObject3DEditor>> objectEditorsByType;
public SelectedObjectPanel(View3DWidget view3DWidget, InteractiveScene scene, ThemeConfig theme)
: base(FlowDirection.TopToBottom)
{
this.HAnchor |= HAnchor.Right;
this.VAnchor = VAnchor.Top | VAnchor.Fit;
this.Padding = new BorderDouble(8, 10);
this.MinimumSize = new VectorMath.Vector2(200, 0);
this.MinimumSize = new VectorMath.Vector2(220, 0);
this.view3DWidget = view3DWidget;
this.theme = theme;
this.scene = scene;
this.AddChild(itemName = new TextWidget("", textColor: ActiveTheme.Instance.PrimaryTextColor)
{
@ -98,25 +111,144 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
this.AddChild(objectActionList);
this.AddChild(editorPanel = new GuiWidget()
this.AddChild(editorPanel = new FlowLayoutWidget(FlowDirection.TopToBottom)
{
Name = "editorPanel",
HAnchor = HAnchor.Stretch,
VAnchor = VAnchor.Fit,
Margin = new BorderDouble(top: 10)
Margin = new BorderDouble(top: 10),
});
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);
}
}
}
public void SetActiveItem(IObject3D selectedItem, GuiWidget editorWidget)
public void SetActiveItem(IObject3D selectedItem)
{
if (!scene.HasSelection)
{
this.Visible = false;
return;
}
this.itemName.Text = selectedItem.Name ?? selectedItem.GetType().Name;
this.item = selectedItem;
this.editorPanel.RemoveAllChildren();
this.editorPanel.AddChild(editorWidget);
this.Visible = true;
HashSet<IObject3DEditor> mappedEditors;
objectEditorsByType.TryGetValue(selectedItem.GetType(), out mappedEditors);
if (mappedEditors == null)
{
foreach (var editor in objectEditorsByType)
{
if (selectedItem.GetType().IsSubclassOf(editor.Key))
{
mappedEditors = editor.Value;
break;
}
}
}
// Add any editor mapped to Object3D to the list
if (objectEditorsByType.TryGetValue(typeof(Object3D), out HashSet<IObject3DEditor> globalEditors))
{
foreach (var editor in globalEditors)
{
mappedEditors.Add(editor);
}
}
if (mappedEditors != null)
{
var dropDownList = new DropDownList("", maxHeight: 300)
{
HAnchor = HAnchor.Stretch
};
//dropDownList.SelectionChanged += (s, e) =>
//{
// ShowObjectEditor(
// mappedEditors.Where(m => m.Name == dropDownList.SelectedLabel).FirstOrDefault());
//};
foreach (IObject3DEditor editor in mappedEditors)
{
MenuItem menuItem = dropDownList.AddItem(editor.Name);
menuItem.Selected += (s, e2) =>
{
ShowObjectEditor(editor);
};
}
editorPanel.AddChild(dropDownList);
// Select the active editor or fall back to the first if not found
var firstEditor = (from editor in mappedEditors
let type = editor.GetType()
where type.Name == selectedItem.ActiveEditor
select editor).FirstOrDefault();
// Fall back to default editor?
if (firstEditor == null)
{
firstEditor = mappedEditors.First();
}
int selectedIndex = 0;
for (int i = 0; i < dropDownList.MenuItems.Count; i++)
{
if (dropDownList.MenuItems[i].Text == firstEditor.Name)
{
selectedIndex = i;
break;
}
}
dropDownList.SelectedIndex = selectedIndex;
ShowObjectEditor(firstEditor);
}
}
private GuiWidget activeEditorWidget;
private void ShowObjectEditor(IObject3DEditor editor)
{
if (editor == null)
{
return;
}
activeEditorWidget?.Close();
var newEditor = editor.Create(scene.SelectedItem, view3DWidget, theme);
newEditor.HAnchor = HAnchor.Stretch;
newEditor.VAnchor = VAnchor.Fit;
editorPanel.AddChild(newEditor);
activeEditorWidget = newEditor;
}
}
}

View file

@ -167,25 +167,6 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
BackgroundColor = ActiveTheme.Instance.PrimaryBackgroundColor,
};
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);
}
}
Scene.SelectionChanged += Scene_SelectionChanged;
// add in the plater tools
@ -447,7 +428,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
this.TrackballTumbleWidget.TransformState = TrackBallController.MouseDownType.Rotation;
selectedObjectPanel = new SelectedObjectPanel(this, theme)
selectedObjectPanel = new SelectedObjectPanel(this, this.Scene, theme)
{
Margin = 5,
BackgroundColor = new RGBA_Bytes(0, 0, 0, ViewControlsBase.overlayAlpha),
@ -1755,12 +1736,6 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
}
}
public List<IObject3DEditor> objectEditors = new List<IObject3DEditor>();
public Dictionary<Type, HashSet<IObject3DEditor>> objectEditorsByType = new Dictionary<Type, HashSet<IObject3DEditor>>();
public IObject3DEditor ActiveSelectionEditor { get; set; }
private void Scene_SelectionChanged(object sender, EventArgs e)
{
if (!Scene.HasSelection)
@ -1776,87 +1751,6 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
var selectedItem = Scene.SelectedItem;
HashSet<IObject3DEditor> mappedEditors;
objectEditorsByType.TryGetValue(selectedItem.GetType(), out mappedEditors);
if (mappedEditors == null)
{
foreach (var editor in objectEditorsByType)
{
if (selectedItem.GetType().IsSubclassOf(editor.Key))
{
mappedEditors = editor.Value;
break;
}
}
}
// Add any editor mapped to Object3D to the list
if (objectEditorsByType.TryGetValue(typeof(Object3D), out HashSet<IObject3DEditor> globalEditors))
{
foreach(var editor in globalEditors)
{
mappedEditors.Add(editor);
}
}
var totalEditor = new FlowLayoutWidget(FlowDirection.TopToBottom)
{
HAnchor = HAnchor.Stretch,
VAnchor = VAnchor.Fit
};
if (mappedEditors != null)
{
editorPanel = new FlowLayoutWidget(FlowDirection.TopToBottom)
{
HAnchor = HAnchor.Stretch,
VAnchor = VAnchor.Fit
};
var dropDownList = new DropDownList("", maxHeight: 300)
{
HAnchor = HAnchor.Stretch
};
foreach (IObject3DEditor editor in mappedEditors)
{
MenuItem menuItem = dropDownList.AddItem(editor.Name);
menuItem.Selected += (s, e2) =>
{
ShowObjectEditor(editor);
};
}
totalEditor.AddChild(dropDownList);
totalEditor.AddChild(editorPanel);
// Select the active editor or fall back to the first if not found
this.ActiveSelectionEditor = (from editor in mappedEditors
let type = editor.GetType()
where type.Name == selectedItem.ActiveEditor
select editor).FirstOrDefault();
// Fall back to default editor?
if (this.ActiveSelectionEditor == null)
{
this.ActiveSelectionEditor = mappedEditors.First();
}
int selectedIndex = 0;
for (int i = 0; i < dropDownList.MenuItems.Count; i++)
{
if (dropDownList.MenuItems[i].Text == this.ActiveSelectionEditor.Name)
{
selectedIndex = i;
break;
}
}
dropDownList.SelectedIndex = selectedIndex;
ShowObjectEditor(this.ActiveSelectionEditor);
}
if (extruderButtons?.Count > 0)
{
@ -1877,7 +1771,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
}
}
selectedObjectPanel.SetActiveItem(selectedItem, totalEditor);
selectedObjectPanel.SetActiveItem(selectedItem);
}
private void ShowObjectEditor(IObject3DEditor editor)