mattercontrol/PartPreviewWindow/ViewControls3D.cs

568 lines
16 KiB
C#
Raw Normal View History

2015-08-15 16:38:07 -07:00
/*
Copyright (c) 2017, Lars Brubaker, John Lewin
2015-08-15 16:38:07 -07:00
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.
*/
2015-04-08 15:20:10 -07:00
using System;
using System.Collections.Generic;
2017-07-07 12:25:31 -07:00
using System.Collections.ObjectModel;
2015-04-08 15:20:10 -07:00
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using MatterHackers.Agg;
using MatterHackers.Agg.ImageProcessing;
using MatterHackers.Agg.Platform;
using MatterHackers.Agg.UI;
using MatterHackers.DataConverters3D;
using MatterHackers.Localizations;
2017-08-14 12:34:44 -07:00
using MatterHackers.MatterControl.CustomWidgets;
using MatterHackers.MatterControl.DataStorage;
using MatterHackers.MatterControl.Library;
using MatterHackers.MeshVisualizer;
2017-08-14 12:34:44 -07:00
using MatterHackers.VectorMath;
namespace MatterHackers.MatterControl.PartPreviewWindow
{
public enum ViewControls3DButtons
{
Rotate,
Scale,
Translate,
PartSelect
}
2017-10-19 09:01:07 -07:00
public enum PartViewMode
{
Layers2D,
Layers3D,
Model
}
public class ViewModeChangedEventArgs : EventArgs
{
public PartViewMode ViewMode { get; set; }
}
public class TransformStateChangedEventArgs : EventArgs
{
public ViewControls3DButtons TransformMode { get; set; }
}
public class ViewControls3D : OverflowBar
2015-04-08 15:20:10 -07:00
{
public event EventHandler ResetView;
2017-10-19 09:01:07 -07:00
public event EventHandler<TransformStateChangedEventArgs> TransformStateChanged;
2015-04-08 15:20:10 -07:00
private GuiWidget partSelectSeparator;
private RadioIconButton translateButton;
private RadioIconButton rotateButton;
private RadioIconButton scaleButton;
private RadioIconButton partSelectButton;
2017-11-29 14:41:55 -08:00
private RadioIconButton layers2DButton;
2017-11-29 13:45:01 -08:00
internal RadioIconButton modelViewButton;
private RadioIconButton layers3DButton;
2017-03-15 16:17:06 -07:00
private EventHandler unregisterEvents;
private PrinterConfig printer;
private View3DWidget view3DWidget;
private ViewControls3DButtons activeTransformState = ViewControls3DButtons.Rotate;
public bool IsPrinterMode { get; set; }
public ViewControls3DButtons ActiveButton
{
get
{
return activeTransformState;
}
set
{
this.activeTransformState = value;
switch (this.activeTransformState)
{
case ViewControls3DButtons.Rotate:
2017-07-05 13:55:38 -07:00
if (rotateButton != null)
{
rotateButton.Checked = true;
}
break;
case ViewControls3DButtons.Translate:
2017-07-05 13:55:38 -07:00
if (translateButton != null)
{
translateButton.Checked = true;
}
break;
case ViewControls3DButtons.Scale:
2017-07-05 13:55:38 -07:00
if (scaleButton != null)
{
scaleButton.Checked = true;
}
break;
case ViewControls3DButtons.PartSelect:
if (partSelectButton != null)
{
partSelectButton.Checked = true;
}
break;
}
TransformStateChanged?.Invoke(this, new TransformStateChangedEventArgs()
{
TransformMode = activeTransformState
});
}
}
internal void SetView3DWidget(View3DWidget view3DWidget)
{
this.view3DWidget = view3DWidget;
}
public ViewControls3D(BedConfig sceneContext, ThemeConfig theme, UndoBuffer undoBuffer)
: base (theme)
2015-04-08 15:20:10 -07:00
{
this.printer = sceneContext.Printer;
2017-03-15 16:17:06 -07:00
string iconPath;
2015-04-08 15:20:10 -07:00
2017-08-14 12:34:44 -07:00
var commonMargin = theme.ButtonSpacing;
2017-08-10 05:13:45 -07:00
var buttonFactory = theme.RadioButtons;
2017-08-10 05:13:45 -07:00
double height = theme.ButtonFactory.Options.FixedHeight;
2017-08-14 12:34:44 -07:00
2017-11-13 16:08:06 -08:00
var homeButton = new IconButton(AggContext.StaticData.LoadIcon("fa-home_16.png", IconColor.Theme), theme)
{
ToolTipText = "Reset View".Localize(),
Margin = commonMargin
};
homeButton.Click += (s, e) => ResetView?.Invoke(this, null);
AddChild(homeButton);
var undoButton = new IconButton(AggContext.StaticData.LoadIcon("Undo_grey_16x.png", 16, 16, IconColor.Theme), theme)
{
Name = "3D View Undo",
ToolTipText = "Undo",
Enabled = false,
MinimumSize = new Vector2(height, height),
Margin = commonMargin,
VAnchor = VAnchor.Center
};
2017-08-14 12:34:44 -07:00
undoButton.Click += (sender, e) =>
{
undoBuffer.Undo();
};
this.AddChild(undoButton);
var redoButton = new IconButton(AggContext.StaticData.LoadIcon("Redo_grey_16x.png", 16, 16, IconColor.Theme), theme)
{
Name = "3D View Redo",
Margin = commonMargin,
MinimumSize = new Vector2(height, height),
ToolTipText = "Redo",
Enabled = false,
VAnchor = VAnchor.Center
};
2017-08-14 12:34:44 -07:00
redoButton.Click += (sender, e) =>
{
undoBuffer.Redo();
};
this.AddChild(redoButton);
this.AddChild(new VerticalLine(50)
{
Margin = 4
});
undoBuffer.Changed += (sender, e) =>
{
undoButton.Enabled = undoBuffer.UndoCount > 0;
redoButton.Enabled = undoBuffer.RedoCount > 0;
};
2017-07-07 12:25:31 -07:00
var buttonGroupA = new ObservableCollection<GuiWidget>();
2017-07-05 13:55:38 -07:00
if (UserSettings.Instance.IsTouchScreen)
{
iconPath = Path.Combine("ViewTransformControls", "rotate.png");
rotateButton = new RadioIconButton(AggContext.StaticData.LoadIcon(iconPath, 32, 32, IconColor.Theme), theme)
{
SiblingRadioButtonList = buttonGroupA,
ToolTipText = "Rotate (Alt + Left Mouse)".Localize(),
Margin = commonMargin
};
2017-07-05 13:55:38 -07:00
rotateButton.Click += (s, e) => this.ActiveButton = ViewControls3DButtons.Rotate;
2017-07-07 12:25:31 -07:00
buttonGroupA.Add(rotateButton);
2017-07-05 13:55:38 -07:00
AddChild(rotateButton);
iconPath = Path.Combine("ViewTransformControls", "translate.png");
translateButton = new RadioIconButton(AggContext.StaticData.LoadIcon(iconPath, 32, 32, IconColor.Theme), theme)
{
SiblingRadioButtonList = buttonGroupA,
ToolTipText = "Move (Shift + Left Mouse)".Localize(),
Margin = commonMargin
};
2017-07-05 13:55:38 -07:00
translateButton.Click += (s, e) => this.ActiveButton = ViewControls3DButtons.Translate;
2017-07-07 12:25:31 -07:00
buttonGroupA.Add(translateButton);
2017-07-05 13:55:38 -07:00
AddChild(translateButton);
iconPath = Path.Combine("ViewTransformControls", "scale.png");
scaleButton = new RadioIconButton(AggContext.StaticData.LoadIcon(iconPath, 32, 32, IconColor.Theme), theme)
{
SiblingRadioButtonList = buttonGroupA,
ToolTipText = "Zoom (Ctrl + Left Mouse)".Localize(),
Margin = commonMargin
};
2017-07-05 13:55:38 -07:00
scaleButton.Click += (s, e) => this.ActiveButton = ViewControls3DButtons.Scale;
2017-07-07 12:25:31 -07:00
buttonGroupA.Add(scaleButton);
2017-07-05 13:55:38 -07:00
AddChild(scaleButton);
rotateButton.Checked = true;
2015-04-08 15:20:10 -07:00
partSelectSeparator = new VerticalLine(50)
{
Margin = 3
};
this.AddChild(partSelectSeparator);
2015-04-08 15:20:10 -07:00
iconPath = Path.Combine("ViewTransformControls", "partSelect.png");
partSelectButton = new RadioIconButton(AggContext.StaticData.LoadIcon(iconPath, 32, 32, IconColor.Theme), theme)
{
SiblingRadioButtonList = buttonGroupA,
ToolTipText = "Select Part".Localize(),
Margin = commonMargin
};
partSelectButton.Click += (s, e) => this.ActiveButton = ViewControls3DButtons.PartSelect;
buttonGroupA.Add(partSelectButton);
AddChild(partSelectButton);
}
2015-04-08 15:20:10 -07:00
2017-07-07 12:25:31 -07:00
var buttonGroupB = new ObservableCollection<GuiWidget>();
iconPath = Path.Combine("ViewTransformControls", "model.png");
modelViewButton = new RadioIconButton(AggContext.StaticData.LoadIcon(iconPath, IconColor.Theme), theme)
{
SiblingRadioButtonList = buttonGroupB,
Name = "Model View Button",
Checked = printer?.ViewState.ViewMode == PartViewMode.Model || printer == null,
ToolTipText = "Model".Localize(),
Margin = commonMargin
};
modelViewButton.Click += SwitchModes_Click;
2017-07-07 12:25:31 -07:00
buttonGroupB.Add(modelViewButton);
AddChild(modelViewButton);
2017-07-07 17:58:23 -07:00
iconPath = Path.Combine("ViewTransformControls", "3d.png");
layers3DButton = new RadioIconButton(AggContext.StaticData.LoadIcon(iconPath, IconColor.Theme), theme)
{
SiblingRadioButtonList = buttonGroupB,
Name = "Layers3D Button",
Checked = printer?.ViewState.ViewMode == PartViewMode.Layers3D,
ToolTipText = "3D Layers".Localize(),
Margin = commonMargin
};
layers3DButton.Click += SwitchModes_Click;
2017-07-07 12:25:31 -07:00
buttonGroupB.Add(layers3DButton);
if (!UserSettings.Instance.IsTouchScreen)
{
this.AddChild(layers3DButton);
}
2017-07-07 17:58:23 -07:00
iconPath = Path.Combine("ViewTransformControls", "2d.png");
2017-11-29 14:41:55 -08:00
layers2DButton = new RadioIconButton(AggContext.StaticData.LoadIcon(iconPath, IconColor.Theme), theme)
{
SiblingRadioButtonList = buttonGroupB,
Name = "Layers2D Button",
Checked = printer?.ViewState.ViewMode == PartViewMode.Layers2D,
ToolTipText = "2D Layers".Localize(),
Margin = commonMargin
};
2017-11-29 14:41:55 -08:00
layers2DButton.Click += SwitchModes_Click;
buttonGroupB.Add(layers2DButton);
this.AddChild(layers2DButton);
var buttonView = new FlowLayoutWidget();
buttonView.AddChild(new ImageWidget(AggContext.StaticData.LoadIcon((IsPrinterMode) ? "bed.png" : "cube.png", IconColor.Theme))
{
VAnchor = VAnchor.Center,
2018-01-09 17:50:12 -08:00
Margin = theme.ButtonSpacing,
});
var buttonText = (IsPrinterMode) ? "Bed".Localize() : "Part".Localize();
buttonView.AddChild(new TextButton(buttonText, theme)
{
Padding = new BorderDouble(8, 4, 0, 4)
});
var overflowMenu = new OverflowMenu(buttonView)
{
Name = "Bed Options Menu",
DynamicPopupContent = () => theme.CreatePopupMenu(this.BedMenuActions(sceneContext)),
DrawArrow = true
};
overflowMenu.Load += (s, e) =>
{
var firstBackgroundColor = this.Parents<GuiWidget>().Where(p => p.BackgroundColor.Alpha0To1 == 1).FirstOrDefault()?.BackgroundColor;
overflowMenu.BackgroundColor = firstBackgroundColor ?? Color.Transparent;
};
this.AddChild(overflowMenu);
2017-11-27 18:09:29 -08:00
if (printer != null)
{
2017-11-27 18:09:29 -08:00
printer.ViewState.ViewModeChanged += (s, e) =>
{
2017-11-27 18:09:29 -08:00
if (e.ViewMode == PartViewMode.Layers2D)
{
2017-11-29 14:41:55 -08:00
this.layers2DButton.Checked = true;
2017-11-27 18:09:29 -08:00
}
else if (e.ViewMode == PartViewMode.Layers3D)
{
layers3DButton.Checked = true;
}
else
{
modelViewButton.Checked = true;
}
};
}
}
private void SwitchModes_Click(object sender, MouseEventArgs e)
{
if (sender is GuiWidget widget)
{
if (widget.Name == "Layers2D Button")
{
printer.ViewState.ViewMode = PartViewMode.Layers2D;
printer.Bed.EnsureGCodeLoaded();
}
else if (widget.Name == "Layers3D Button")
{
printer.ViewState.ViewMode = PartViewMode.Layers3D;
printer.Bed.EnsureGCodeLoaded();
}
else
{
printer.ViewState.ViewMode = PartViewMode.Model;
}
}
}
private async void LoadAndAddPartsToPlate(string[] filesToLoad, InteractiveScene scene)
{
if (filesToLoad != null && filesToLoad.Length > 0)
{
await Task.Run(() => loadAndAddPartsToPlate(filesToLoad, scene));
if (HasBeenClosed)
{
return;
}
bool addingOnlyOneItem = scene.Children.Count == scene.Children.Count + 1;
if (scene.HasChildren())
{
if (addingOnlyOneItem)
{
// if we are only adding one part to the plate set the selection to it
scene.SelectLastChild();
}
}
scene.Invalidate();
this.Invalidate();
}
}
private async Task loadAndAddPartsToPlate(string[] filesToLoadIncludingZips, InteractiveScene scene)
{
if (filesToLoadIncludingZips?.Any() == true)
{
List<string> filesToLoad = new List<string>();
foreach (string loadedFileName in filesToLoadIncludingZips)
{
string extension = Path.GetExtension(loadedFileName).ToUpper();
if ((extension != "" && MeshFileIo.ValidFileExtensions().Contains(extension)))
{
filesToLoad.Add(loadedFileName);
}
else if (extension == ".ZIP")
{
List<PrintItem> partFiles = ProjectFileHandler.ImportFromProjectArchive(loadedFileName);
if (partFiles != null)
{
foreach (PrintItem part in partFiles)
{
filesToLoad.Add(part.FileLocation);
}
}
}
}
string progressMessage = "Loading Parts...".Localize();
var itemCache = new Dictionary<string, IObject3D>();
foreach (string filePath in filesToLoad)
{
var libraryItem = new FileSystemFileItem(filePath);
IObject3D object3D = null;
await ApplicationController.Instance.Tasks.Execute(async (progressReporter, cancelationToken) =>
{
var progressStatus = new ProgressStatus()
{
Status = "Loading ".Localize() + Path.GetFileName(filePath),
};
progressReporter.Report(progressStatus);
object3D = await libraryItem.CreateContent((double progress0To1, string processingState) =>
{
progressStatus.Progress0To1 = progress0To1;
progressStatus.Status = processingState;
progressReporter.Report(progressStatus);
});
});
if (object3D != null)
{
scene.Children.Modify(list => list.Add(object3D));
PlatingHelper.MoveToOpenPositionRelativeGroup(object3D, scene.Children);
}
}
}
}
private NamedAction[] BedMenuActions(BedConfig sceneContext)
{
// Bed menu
return new[]
{
2018-01-09 11:03:13 -08:00
new NamedAction()
{
Title = "Insert".Localize(),
2018-01-09 17:50:12 -08:00
Icon = AggContext.StaticData.LoadIcon("cube.png", 16, 16, IconColor.Raw),
2018-01-09 11:03:13 -08:00
Action = () =>
{
UiThread.RunOnIdle(() =>
{
AggContext.FileDialogs.OpenFileDialog(
new OpenFileDialogParams(ApplicationSettings.OpenDesignFileParams, multiSelect: true),
(openParams) =>
{
this.LoadAndAddPartsToPlate(openParams.FileNames, sceneContext.Scene);
});
});
}
},
new NamedAction()
{
Title = "Save".Localize(),
Action = async () =>
{
await ApplicationController.Instance.Tasks.Execute(view3DWidget.SaveChanges);
},
IsEnabled = () => sceneContext.EditableScene
},
new NamedAction()
{
Title = "Save As".Localize(),
Action = () => UiThread.RunOnIdle(view3DWidget.OpenSaveAsWindow),
IsEnabled = () => sceneContext.EditableScene
},
new NamedAction()
{
Title = "Export".Localize(),
Action = () =>
{
UiThread.RunOnIdle(() =>
{
DialogWindow.Show(
new ExportPrintItemPage(new[]
{
new FileSystemFileItem(sceneContext.EditContext.PartFilePath)
}));
});
},
IsEnabled = () => sceneContext.EditableScene
},
new NamedAction()
{
Title = "Publish".Localize(),
Action = () =>
{
UiThread.RunOnIdle(() => DialogWindow.Show<PublishPartToMatterHackers>());
},
IsEnabled = () => sceneContext.EditableScene
},
new NamedAction()
{
Title = "Arrange All Parts".Localize(),
Action = () =>
{
sceneContext.Scene.AutoArrangeChildren(view3DWidget);
},
IsEnabled = () => sceneContext.EditableScene
},
new NamedAction() { Title = "----" },
new NamedAction()
{
Title = "Clear Bed".Localize(),
Action = () =>
{
UiThread.RunOnIdle(() =>
{
sceneContext.ClearPlate().ConfigureAwait(false);
});
}
}
};
}
public override void OnClosed(ClosedEventArgs e)
2015-04-08 15:20:10 -07:00
{
2017-03-15 16:17:06 -07:00
unregisterEvents?.Invoke(this, null);
2015-04-08 15:20:10 -07:00
base.OnClosed(e);
}
}
}