Making the editor have local function to reduce complexity

This commit is contained in:
Lars Brubaker 2023-10-27 16:54:35 -07:00
parent 9450bdbcf9
commit 7b65b02d92
8 changed files with 1325 additions and 1231 deletions

View file

@ -146,7 +146,7 @@ namespace MatterHackers.MatterControl
public IEnumerable<PrinterConfig> ActivePrinters => this.Workspaces.Where(w => w.Printer != null).Select(w => w.Printer);
public ExtensionsConfig Extensions { get; }
public EditorExtensionsConfig EditorExtensions { get; }
public PopupMenu GetActionMenuForSceneItem(bool addInSubmenu, View3DWidget view3DWidget)
{
@ -1166,9 +1166,9 @@ namespace MatterHackers.MatterControl
// _activePrinters = new List<PrinterConfig>();
};
this.Extensions = new ExtensionsConfig(this.Library);
this.Extensions.Register(new SheetEditor());
this.Extensions.Register(new PropertyEditor());
this.EditorExtensions = new EditorExtensionsConfig(this.Library);
this.EditorExtensions.RegisterFactory((theme, undoBuffer) => new SheetEditor());
this.EditorExtensions.RegisterFactory((theme, undoBuffer) => new PropertyEditor(theme, undoBuffer));
HelpArticle helpArticle = null;

View file

@ -30,6 +30,7 @@ either expressed or implied, of the FreeBSD Project.
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using MatterHackers.Agg.UI;
using MatterHackers.DataConverters3D;
using MatterHackers.MatterControl.Library;
using MatterHackers.MatterControl.PartPreviewWindow;
@ -41,7 +42,7 @@ using MatterHackers.MeshVisualizer;
namespace MatterHackers.MatterControl
{
public class ExtensionsConfig
public class EditorExtensionsConfig
{
private LibraryConfig libraryConfig;
@ -52,37 +53,39 @@ namespace MatterHackers.MatterControl
// new SubtractAndReplace()
// };
public ExtensionsConfig(LibraryConfig libraryConfig)
public EditorExtensionsConfig(LibraryConfig libraryConfig)
{
this.libraryConfig = libraryConfig;
objectEditorsByType = new Dictionary<Type, HashSet<IObjectEditor>>();
objectEditorsByType = new Dictionary<Type, HashSet<Func<ThemeConfig, UndoBuffer, IObjectEditor>>>();
}
private void MapTypesToEditor(IObjectEditor editor)
private void MapTypesToEditorFactory(Func<ThemeConfig, UndoBuffer, IObjectEditor> object3DEditorFactory)
{
foreach (Type type in editor.SupportedTypes())
var editor = object3DEditorFactory.Invoke(null, null);
foreach (Type type in editor.SupportedTypes())
{
if (!objectEditorsByType.TryGetValue(type, out HashSet<IObjectEditor> mappedEditors))
if (!objectEditorsByType.TryGetValue(type, out HashSet<Func<ThemeConfig, UndoBuffer, IObjectEditor>> mappedEditorsFactories))
{
mappedEditors = new HashSet<IObjectEditor>();
objectEditorsByType.Add(type, mappedEditors);
mappedEditorsFactories = new HashSet<Func<ThemeConfig, UndoBuffer, IObjectEditor>>();
objectEditorsByType.Add(type, mappedEditorsFactories);
}
mappedEditors.Add(editor);
mappedEditorsFactories.Add(object3DEditorFactory);
}
}
public void Register(IObjectEditor object3DEditor)
public void RegisterFactory(Func<ThemeConfig, UndoBuffer, IObjectEditor> object3DEditorFactory)
{
this.MapTypesToEditor(object3DEditor);
this.MapTypesToEditorFactory(object3DEditorFactory);
}
private Dictionary<Type, HashSet<IObjectEditor>> objectEditorsByType;
private Dictionary<Type, HashSet<Func<ThemeConfig, UndoBuffer, IObjectEditor>>> objectEditorsByType;
public HashSet<IObjectEditor> GetEditorsForType(Type selectedItemType)
public HashSet<Func<ThemeConfig, UndoBuffer, IObjectEditor>> GetEditorsForType(Type selectedItemType)
{
HashSet<IObjectEditor> mappedEditors;
HashSet<Func<ThemeConfig, UndoBuffer, IObjectEditor>> mappedEditors;
objectEditorsByType.TryGetValue(selectedItemType, out mappedEditors);
if (mappedEditors == null)

View file

@ -48,7 +48,7 @@ namespace MatterHackers.Plugins.EditorTools
applicationController.Library.RegisterCreator(item);
}
applicationController.Extensions.Register(new OpenSCADBuilder());
applicationController.EditorExtensions.RegisterFactory((theme, undoBuffer) => new OpenSCADBuilder());
// applicationController.Extensions.Register(new PrimitivesEditor());
}

File diff suppressed because it is too large Load diff

View file

@ -37,12 +37,13 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using static MatterHackers.Agg.UI.OnScreenKeyboard;
namespace MatterHackers.MatterControl.DesignTools
{
public static class SelectedChildrenEditor
public class SelectedChildrenPropertyEditor : IPropertyEditorFactory
{
public static GuiWidget CreateEditor(EditableProperty property, EditorContext context, UndoBuffer undoBuffer, ThemeConfig theme)
public GuiWidget CreateEditor(PropertyEditor propertyEditor, EditableProperty property, EditorContext context)
{
var childSelector = property.Source as SelectedChildren;
if (childSelector != null)
@ -51,6 +52,8 @@ namespace MatterHackers.MatterControl.DesignTools
throw new Exception("Passed the wrong type to the SelectedChildrenEditor");
}
var theme = propertyEditor.Theme;
var undoBuffer = propertyEditor.UndoBuffer;
var contextItem = context.Item;
var contextObject3D = contextItem as IObject3D;
var propertyIObject3D = property.Source as IObject3D;
@ -76,7 +79,7 @@ namespace MatterHackers.MatterControl.DesignTools
return childrenSelector;
});
rowContainer = PropertyEditor.CreateSettingsRow(property, field.Content, theme);
rowContainer = propertyEditor.CreateSettingsRow(property, field.Content, theme);
}
else // show the subtract editor for boolean subtract and subtract and replace
{
@ -187,7 +190,7 @@ namespace MatterHackers.MatterControl.DesignTools
public static void Register()
{
PropertyEditor.RegisterEditor(typeof(SelectedChildren), CreateEditor);
PropertyEditor.RegisterEditor(typeof(SelectedChildren), new SelectedChildrenPropertyEditor());
}
private static GuiWidget CreateSelector(SelectedChildren childSelector, IObject3D parent, ThemeConfig theme)

View file

@ -0,0 +1,264 @@
/*
Copyright (c) 2018, 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 MatterHackers.Agg;
using MatterHackers.Agg.Platform;
using MatterHackers.ImageProcessing;
using MatterHackers.Agg.UI;
using MatterHackers.DataConverters3D;
using MatterHackers.Localizations;
using MatterHackers.MatterControl.CustomWidgets;
using MatterHackers.MatterControl.SlicerConfiguration;
using System;
using System.Linq;
using System.Web;
using System.ComponentModel;
using MatterHackers.VectorMath;
using System.IO;
namespace MatterHackers.MatterControl.DesignTools
{
public class StringPropertyEditor : IPropertyEditorFactory
{
public GuiWidget CreateEditor(PropertyEditor propertyEditor, EditableProperty property, EditorContext context)
{
var stringValue = property.Source as string;
if (stringValue != null)
{
// we passed the wrong type
throw new Exception("Passed the wrong type to the SelectedChildrenEditor");
}
var theme = propertyEditor.Theme;
var undoBuffer = propertyEditor.UndoBuffer;
var contextItem = context.Item;
var contextObject3D = contextItem as IObject3D;
var propertyIObject3D = property.Source as IObject3D;
var propertyGridModifier = property.Source as IPropertyGridModifier;
GuiWidget rowContainer = null;
if (property.PropertyInfo.GetCustomAttributes(true).OfType<GoogleSearchAttribute>().FirstOrDefault() != null)
{
rowContainer = NewImageSearchWidget(theme);
}
else if (propertyIObject3D is AssetObject3D assetObject
&& property.PropertyInfo.Name == "AssetPath")
{
// This is the AssetPath property of an asset object, add a button to set the AssetPath from a file
// Change button
var changeButton = new ThemedTextButton(property.Description, theme)
{
BackgroundColor = theme.MinimalShade,
Margin = 3
};
rowContainer = new SettingsRow(property.DisplayName,
null,
changeButton,
theme);
changeButton.Click += (sender, e) =>
{
UiThread.RunOnIdle(() =>
{
ImageObject3D.ShowOpenDialog(assetObject);
});
};
}
else
{
var readOnly = property.PropertyInfo.GetCustomAttributes(true).OfType<ReadOnlyAttribute>().FirstOrDefault() != null;
if (readOnly)
{
WrappedTextWidget wrappedTextWidget = null;
if (!string.IsNullOrEmpty(property.DisplayName))
{
rowContainer = new GuiWidget()
{
HAnchor = HAnchor.Stretch,
VAnchor = VAnchor.Fit,
Margin = 9
};
var displayName = rowContainer.AddChild(new TextWidget(property.DisplayName,
textColor: theme.TextColor,
pointSize: 10)
{
VAnchor = VAnchor.Center,
});
var wrapContainer = new GuiWidget()
{
Margin = new BorderDouble(displayName.Width + displayName.Margin.Width + 15, 3, 3, 3),
HAnchor = HAnchor.Stretch,
VAnchor = VAnchor.Fit
};
wrappedTextWidget = new WrappedTextWidget(stringValue, textColor: theme.TextColor, pointSize: 10)
{
HAnchor = HAnchor.Stretch
};
wrappedTextWidget.TextWidget.HAnchor = HAnchor.Right;
wrapContainer.AddChild(wrappedTextWidget);
rowContainer.AddChild(wrapContainer);
}
else
{
rowContainer = wrappedTextWidget = new WrappedTextWidget(stringValue,
textColor: theme.TextColor,
pointSize: 10)
{
Margin = 9
};
}
void RefreshField(object s, InvalidateArgs e)
{
if (e.InvalidateType.HasFlag(InvalidateType.DisplayValues))
{
wrappedTextWidget.Text = property.Value.ToString();
}
}
if (propertyIObject3D != null)
{
propertyIObject3D.Invalidated += RefreshField;
wrappedTextWidget.Closed += (s, e) => propertyIObject3D.Invalidated -= RefreshField;
}
}
else // normal edit row
{
if (property.PropertyInfo.GetCustomAttributes(true).OfType<MultiLineEditAttribute>().FirstOrDefault() != null)
{
// create a a multi-line string editor
var field = new MultilineStringField(theme, property.PropertyInfo.GetCustomAttributes(true).OfType<UpdateOnEveryKeystrokeAttribute>().FirstOrDefault() != null);
field.Initialize(0);
field.SetValue(stringValue, false);
field.ClearUndoHistory();
field.Content.HAnchor = HAnchor.Stretch;
field.Content.Descendants<ScrollableWidget>().FirstOrDefault().MaximumSize = new Vector2(double.MaxValue, 200);
field.Content.Descendants<ScrollingArea>().FirstOrDefault().Parent.VAnchor = VAnchor.Top;
field.Content.MinimumSize = new Vector2(0, 100 * GuiWidget.DeviceScale);
field.Content.Margin = new BorderDouble(0, 0, 0, 5);
PropertyEditor.RegisterValueChanged(property, undoBuffer, context, field, (valueString) => valueString);
rowContainer = PropertyEditor.CreateSettingsColumn(property, field, fullWidth: true);
}
else
{
// create a string editor
var field = new TextField(theme);
field.Initialize(0);
field.SetValue(stringValue, false);
field.ClearUndoHistory();
field.Content.HAnchor = HAnchor.Stretch;
PropertyEditor.RegisterValueChanged(property, undoBuffer, context, field, (valueString) => valueString);
rowContainer = propertyEditor.CreateSettingsRow(property, field.Content, theme, true);
// check for DirectoryPathAttribute
var directoryPathAttribute = property.PropertyInfo.GetCustomAttributes(true).OfType<DirectoryPathAttribute>().FirstOrDefault();
if (directoryPathAttribute != null)
{
// add a browse button
var browseButton = new ThemedIconButton(StaticData.Instance.LoadIcon(Path.Combine("Library", "folder.png"), 16, 16).GrayToColor(theme.TextColor), theme)
{
ToolTipText = "Select Folder".Localize(),
};
browseButton.Click += (s, e) =>
{
UiThread.RunOnIdle(() =>
{
AggContext.FileDialogs.SelectFolderDialog(
new SelectFolderDialogParams(directoryPathAttribute.Message)
{
ActionButtonLabel = directoryPathAttribute.ActionLabel,
Title = ApplicationController.Instance.ProductName + " - " + "Select A Folder".Localize(),
RootFolder = SelectFolderDialogParams.RootFolderTypes.Specify,
FolderPath = stringValue
},
(openParams) =>
{
if (!string.IsNullOrEmpty(openParams.FolderPath))
{
field.SetValue(openParams.FolderPath, true);
}
});
});
};
rowContainer.AddChild(browseButton);
}
}
}
}
return rowContainer;
}
public static GuiWidget NewImageSearchWidget(ThemeConfig theme, string postPend = "silhouette")
{
var searchRow = new FlowLayoutWidget()
{
HAnchor = HAnchor.Stretch,
Margin = new BorderDouble(5, 0)
};
var searchField = new ThemedTextEditWidget("", theme, messageWhenEmptyAndNotSelected: "Search Google for images")
{
HAnchor = HAnchor.Stretch,
VAnchor = VAnchor.Center
};
searchRow.AddChild(searchField);
var searchButton = new ThemedIconButton(StaticData.Instance.LoadIcon("icon_search_24x24.png", 16, 16).GrayToColor(theme.TextColor), theme)
{
ToolTipText = "Search".Localize(),
};
searchRow.AddChild(searchButton);
void DoSearch(object s, EventArgs e)
{
var search = HttpUtility.UrlEncode(searchField.Text);
if (!string.IsNullOrEmpty(search))
{
ApplicationController.LaunchBrowser($"http://www.google.com/search?q={search} {postPend}&tbm=isch");
}
};
searchField.ActualTextEditWidget.EditComplete += DoSearch;
searchButton.Click += DoSearch;
return searchRow;
}
public static void Register()
{
PropertyEditor.RegisterEditor(typeof(String), new StringPropertyEditor());
}
}
}

View file

@ -60,8 +60,9 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
var rows = new SafeList<SettingsRow>();
var editor = PropertyEditor.CreatePropertyEditor(
rows,
var propertyEditor = new PropertyEditor(theme, new UndoBuffer());
var editor = propertyEditor.CreatePropertyEditor(
new EditableProperty(propertyInfo, supportGenerator),
null,
new EditorContext(),

View file

@ -250,7 +250,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
editorSectionWidget.Text = selectedItem.Name ?? selectedItemType.Name;
HashSet<IObjectEditor> mappedEditors = ApplicationController.Instance.Extensions.GetEditorsForType(selectedItemType);
HashSet<Func<ThemeConfig, UndoBuffer, IObjectEditor>> mappedEditors = ApplicationController.Instance.EditorExtensions.GetEditorsForType(selectedItemType);
var undoBuffer = sceneContext.Scene.UndoBuffer;
@ -310,9 +310,9 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
else
{
if (item != null
&& ApplicationController.Instance.Extensions.GetEditorsForType(item.GetType())?.FirstOrDefault() is IObjectEditor editor)
&& ApplicationController.Instance.EditorExtensions.GetEditorsForType(item.GetType())?.FirstOrDefault() is Func<ThemeConfig, UndoBuffer, IObjectEditor> editorFactory)
{
ShowObjectEditor((editor, item, item.Name), selectedItem);
ShowObjectEditor((editorFactory.Invoke(theme, undoBuffer), item, item.Name), selectedItem);
}
}
@ -559,9 +559,9 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
{
if (instance is IObject3D object3D)
{
if (ApplicationController.Instance.Extensions.GetEditorsForType(object3D.GetType())?.FirstOrDefault() is IObjectEditor editor)
if (ApplicationController.Instance.EditorExtensions.GetEditorsForType(object3D.GetType())?.FirstOrDefault() is Func<ThemeConfig, UndoBuffer, IObjectEditor> editorFactory)
{
ShowObjectEditor((editor, object3D, object3D.Name), selectedItem);
ShowObjectEditor((editorFactory.Invoke(theme, undoBuffer), object3D, object3D.Name), selectedItem);
}
}
else if (JsonPathContext.ReflectionValueSystem.LastMemberValue is ReflectionTarget reflectionTarget)
@ -577,7 +577,8 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
var editableProperty = new EditableProperty(reflectionTarget.PropertyInfo, reflectionTarget.Source);
var editor = PropertyEditor.CreatePropertyEditor(rows, editableProperty, undoBuffer, context, theme);
var propertyEditor = new PropertyEditor(theme, undoBuffer);
var editor = propertyEditor.CreatePropertyEditor(editableProperty, undoBuffer, context, theme);
if (editor != null)
{
editorPanel.AddChild(editor);