From 257fa1963893d2aafd0fb3b12456ec3ccb9a9232 Mon Sep 17 00:00:00 2001 From: LarsBrubaker Date: Fri, 29 Jan 2021 08:22:00 -0800 Subject: [PATCH] Now we can edit material colors --- .../Settings/SliceSettingsFields.cs | 8 +++--- .../ApplicationView/Config/BedConfig.cs | 2 +- .../ApplicationView/Config/PrinterConfig.cs | 14 +++++------ .../PartPreviewWindow/ItemColorButton.cs | 2 +- .../PartPreviewWindow/ItemMaterialButton.cs | 8 +++--- .../PartPreviewWindow/MaterialControls.cs | 4 +-- .../PartPreviewWindow/SelectedObjectPanel.cs | 2 +- .../View3D/MaterialRendering.cs | 25 ++++++++++++++++--- .../View3D/Object3DControlsLayer.cs | 4 +-- .../UIFields/ColorField.cs | 2 +- .../UIFields/MaterialIndexField.cs | 6 +++-- 11 files changed, 47 insertions(+), 30 deletions(-) diff --git a/MatterControl.Printing/Settings/SliceSettingsFields.cs b/MatterControl.Printing/Settings/SliceSettingsFields.cs index 55ff04330..7efc015af 100644 --- a/MatterControl.Printing/Settings/SliceSettingsFields.cs +++ b/MatterControl.Printing/Settings/SliceSettingsFields.cs @@ -2099,7 +2099,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration HelpText = "The color of the first material.".Localize(), DataEditType = DataEditTypes.COLOR, RequiredDisplayDetail = DisplayDetailRequired.Simple, - DefaultValue = "#FF0000" + DefaultValue = ColorF.FromHSL(0 / 10.0, .99, .49).ToColor().Html, }, new SliceSettingData() { @@ -2109,7 +2109,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration DataEditType = DataEditTypes.COLOR, RequiredDisplayDetail = DisplayDetailRequired.Simple, ShowIfSet = "!sla_printer&extruder_count>1", - DefaultValue = "#00FF00" + DefaultValue = ColorF.FromHSL(1 / 10.0, .99, .49).ToColor().Html, }, new SliceSettingData() { @@ -2119,7 +2119,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration DataEditType = DataEditTypes.COLOR, RequiredDisplayDetail = DisplayDetailRequired.Simple, ShowIfSet = "!sla_printer&extruder_count>2", - DefaultValue = "#0000FF" + DefaultValue = ColorF.FromHSL(2 / 10.0, .99, .49).ToColor().Html, }, new SliceSettingData() { @@ -2129,7 +2129,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration DataEditType = DataEditTypes.COLOR, RequiredDisplayDetail = DisplayDetailRequired.Simple, ShowIfSet = "!sla_printer&extruder_count>3", - DefaultValue = "#FFFF00" + DefaultValue = ColorF.FromHSL(3 / 10.0, .99, .49).ToColor().Html, }, new SliceSettingData() { diff --git a/MatterControlLib/ApplicationView/Config/BedConfig.cs b/MatterControlLib/ApplicationView/Config/BedConfig.cs index e389bf62b..244493d33 100644 --- a/MatterControlLib/ApplicationView/Config/BedConfig.cs +++ b/MatterControlLib/ApplicationView/Config/BedConfig.cs @@ -626,7 +626,7 @@ namespace MatterHackers.MatterControl 0, 1, this.GetRenderType, - MaterialRendering.Color); + (index) => MaterialRendering.Color(this.Printer, index)); GCodeRenderer.ExtruderWidth = this.Printer.Settings.GetValue(SettingsKey.nozzle_diameter); diff --git a/MatterControlLib/ApplicationView/Config/PrinterConfig.cs b/MatterControlLib/ApplicationView/Config/PrinterConfig.cs index 326b527d0..0e36a452c 100644 --- a/MatterControlLib/ApplicationView/Config/PrinterConfig.cs +++ b/MatterControlLib/ApplicationView/Config/PrinterConfig.cs @@ -31,17 +31,15 @@ using System; using System.Threading.Tasks; using MatterHackers.Agg.UI; using MatterHackers.MatterControl.SlicerConfiguration; +using System.Threading; +using MatterHackers.Agg; +using MatterHackers.Localizations; +using MatterHackers.MatterControl.PrinterCommunication; +using MatterHackers.VectorMath; +using Newtonsoft.Json; namespace MatterHackers.MatterControl { - using System.Threading; - using MatterHackers.Agg; - using MatterHackers.Localizations; - using MatterHackers.MatterControl.PrinterCommunication; - using MatterHackers.MatterControl.SlicerConfiguration.MappingClasses; - using MatterHackers.VectorMath; - using Newtonsoft.Json; - public class PrinterConfig : IDisposable { public event EventHandler Disposed; diff --git a/MatterControlLib/PartPreviewWindow/ItemColorButton.cs b/MatterControlLib/PartPreviewWindow/ItemColorButton.cs index 99943e988..995c81f88 100644 --- a/MatterControlLib/PartPreviewWindow/ItemColorButton.cs +++ b/MatterControlLib/PartPreviewWindow/ItemColorButton.cs @@ -125,8 +125,8 @@ namespace MatterHackers.MatterControl.PartPreviewWindow }) as RadialColorPicker; picker.SelectedColorChanged += (s, newColor) => { - update?.Invoke(picker.SelectedColor); htmlField.SetValue(picker.SelectedColor.Html.Substring(1, 6), false); + update?.Invoke(picker.SelectedColor); }; var rightContent = content.AddChild(new FlowLayoutWidget(FlowDirection.TopToBottom) diff --git a/MatterControlLib/PartPreviewWindow/ItemMaterialButton.cs b/MatterControlLib/PartPreviewWindow/ItemMaterialButton.cs index 3cac75d70..3add8be04 100644 --- a/MatterControlLib/PartPreviewWindow/ItemMaterialButton.cs +++ b/MatterControlLib/PartPreviewWindow/ItemMaterialButton.cs @@ -46,7 +46,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow private int currentIndex; - public ItemMaterialButton(ThemeConfig theme, int initialMaterialIndex) + public ItemMaterialButton(PrinterConfig printer, ThemeConfig theme, int initialMaterialIndex) { this.currentIndex = initialMaterialIndex; this.ToolTipText = "Material".Localize(); @@ -61,7 +61,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow this.DynamicPopupContent = () => { - var materialControl = new MaterialControls(menuTheme, currentIndex) + var materialControl = new MaterialControls(printer, menuTheme, currentIndex) { Padding = theme.DefaultContainerPadding, BackgroundColor = menuTheme.BackgroundColor, @@ -73,13 +73,13 @@ namespace MatterHackers.MatterControl.PartPreviewWindow { currentIndex = e; MaterialChanged?.Invoke(this, e); - materialColorButton.BackgroundColor = MaterialRendering.Color(currentIndex, theme.BorderColor); + materialColorButton.BackgroundColor = MaterialRendering.Color(printer, currentIndex, theme.BorderColor); }; return materialControl; }; - materialColorButton = new ColorButton(MaterialRendering.Color(currentIndex, theme.BorderColor)) + materialColorButton = new ColorButton(MaterialRendering.Color(printer, currentIndex, theme.BorderColor)) { Width = scaledButtonSize, Height = scaledButtonSize, diff --git a/MatterControlLib/PartPreviewWindow/MaterialControls.cs b/MatterControlLib/PartPreviewWindow/MaterialControls.cs index dc249c6e8..9ab227913 100644 --- a/MatterControlLib/PartPreviewWindow/MaterialControls.cs +++ b/MatterControlLib/PartPreviewWindow/MaterialControls.cs @@ -43,7 +43,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow private ThemeConfig theme; public event EventHandler IndexChanged; - public MaterialControls(ThemeConfig theme, int initialMaterialIndex) + public MaterialControls(PrinterConfig printer, ThemeConfig theme, int initialMaterialIndex) : base(FlowDirection.TopToBottom) { this.theme = theme; @@ -68,7 +68,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow var scaledButtonSize = 16 * GuiWidget.DeviceScale; - buttonView.AddChild(new ColorButton(MaterialRendering.Color(extruderIndex, theme.BorderColor)) + buttonView.AddChild(new ColorButton(MaterialRendering.Color(printer, extruderIndex, theme.BorderColor)) { Width = scaledButtonSize, Height = scaledButtonSize, diff --git a/MatterControlLib/PartPreviewWindow/SelectedObjectPanel.cs b/MatterControlLib/PartPreviewWindow/SelectedObjectPanel.cs index c19dc1588..ba36c3cfd 100644 --- a/MatterControlLib/PartPreviewWindow/SelectedObjectPanel.cs +++ b/MatterControlLib/PartPreviewWindow/SelectedObjectPanel.cs @@ -272,7 +272,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow editorPanel.AddChild(row); // put in a material edit field - var materialField = new MaterialIndexField(theme, selectedItem.MaterialIndex); + var materialField = new MaterialIndexField(sceneContext.Printer, theme, selectedItem.MaterialIndex); materialField.Initialize(0); materialField.ValueChanged += (s, e) => { diff --git a/MatterControlLib/PartPreviewWindow/View3D/MaterialRendering.cs b/MatterControlLib/PartPreviewWindow/View3D/MaterialRendering.cs index 066c0059e..6b4462354 100644 --- a/MatterControlLib/PartPreviewWindow/View3D/MaterialRendering.cs +++ b/MatterControlLib/PartPreviewWindow/View3D/MaterialRendering.cs @@ -27,8 +27,10 @@ of the authors and should not be interpreted as representing official policies, either expressed or implied, of the FreeBSD Project. */ -using System; using MatterHackers.Agg; +using MatterHackers.MatterControl; +using MatterHackers.MatterControl.SlicerConfiguration; +using System; namespace MatterHackers.MeshVisualizer { @@ -39,9 +41,9 @@ namespace MatterHackers.MeshVisualizer /// /// The extruder/material index to resolve /// The color for the given extruder - public static Color Color(int materialIndex) + public static Color Color(PrinterConfig printer, int materialIndex) { - return ColorF.FromHSL(Math.Max(materialIndex, 0) / 10.0, .99, .49).ToColor(); + return Color(printer, Math.Min(4, Math.Max(0, materialIndex)), Agg.Color.Cyan); } /// @@ -50,8 +52,23 @@ namespace MatterHackers.MeshVisualizer /// The extruder/material index to resolve /// The color to use when the extruder/material has not been assigned /// The color for the given extruder - public static Color Color(int materialIndex, Color unassignedColor) + public static Color Color(PrinterConfig printer, int materialIndex, Color unassignedColor) { + if (printer?.Settings != null) + { + switch (materialIndex) + { + case 0: + return new Color(printer.Settings.GetValue(SettingsKey.material_color)); + case 1: + return new Color(printer.Settings.GetValue(SettingsKey.material_color_1)); + case 2: + return new Color(printer.Settings.GetValue(SettingsKey.material_color_2)); + case 3: + return new Color(printer.Settings.GetValue(SettingsKey.material_color_3)); + } + } + return (materialIndex == -1) ? unassignedColor : ColorF.FromHSL(materialIndex / 10.0, .99, .49).ToColor(); } } diff --git a/MatterControlLib/PartPreviewWindow/View3D/Object3DControlsLayer.cs b/MatterControlLib/PartPreviewWindow/View3D/Object3DControlsLayer.cs index b28b10c4f..aacc325eb 100644 --- a/MatterControlLib/PartPreviewWindow/View3D/Object3DControlsLayer.cs +++ b/MatterControlLib/PartPreviewWindow/View3D/Object3DControlsLayer.cs @@ -801,7 +801,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow else if (sceneContext.ViewState.RenderType == RenderTypes.Materials) { // check if we should be rendering materials (this overrides the other colors) - drawColor = MaterialRendering.Color(item.WorldMaterialIndex()); + drawColor = MaterialRendering.Color(sceneContext.Printer, item.WorldMaterialIndex()); } if (sceneContext.Printer is PrinterConfig printer) @@ -983,7 +983,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow var matrix = Matrix4X4.CreateTranslation(emulator.CurrentPosition + new Vector3(.5, .5, 5)); GLHelper.Render(emulatorNozzleMesh, - MaterialRendering.Color(emulator.ExtruderIndex), + MaterialRendering.Color(sceneContext.Printer, emulator.ExtruderIndex), matrix, RenderTypes.Shaded, matrix * World.ModelviewMatrix); diff --git a/MatterControlLib/SlicerConfiguration/UIFields/ColorField.cs b/MatterControlLib/SlicerConfiguration/UIFields/ColorField.cs index 66f832292..185c2ded5 100644 --- a/MatterControlLib/SlicerConfiguration/UIFields/ColorField.cs +++ b/MatterControlLib/SlicerConfiguration/UIFields/ColorField.cs @@ -63,7 +63,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration colorWidget = new ItemColorButton(theme, initialColor); colorWidget.ColorChanged += (s, e) => { - base.OnValueChanged(new FieldChangedEventArgs(true)); + this.SetValue(Color.Html, true); }; container.AddChild(colorWidget); diff --git a/MatterControlLib/SlicerConfiguration/UIFields/MaterialIndexField.cs b/MatterControlLib/SlicerConfiguration/UIFields/MaterialIndexField.cs index 6196b7cbe..39964b5db 100644 --- a/MatterControlLib/SlicerConfiguration/UIFields/MaterialIndexField.cs +++ b/MatterControlLib/SlicerConfiguration/UIFields/MaterialIndexField.cs @@ -36,10 +36,12 @@ namespace MatterHackers.MatterControl.SlicerConfiguration public class MaterialIndexField : UIField { private ItemMaterialButton materialWidget; + private PrinterConfig printer; private ThemeConfig theme; - public MaterialIndexField(ThemeConfig theme, int materialIndex) + public MaterialIndexField(PrinterConfig printer, ThemeConfig theme, int materialIndex) { + this.printer = printer; this.theme = theme; this.MaterialIndex = materialIndex; } @@ -50,7 +52,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration { var container = new FlowLayoutWidget(); - materialWidget = new ItemMaterialButton(theme, MaterialIndex); + materialWidget = new ItemMaterialButton(printer, theme, MaterialIndex); materialWidget.MaterialChanged += (s, e) => { MaterialIndex = e;