mattercontrol/MatterControlLib/PartPreviewWindow/ViewControls3D.cs

779 lines
24 KiB
C#
Raw Normal View History

2015-08-15 16:38:07 -07:00
/*
Copyright (c) 2018, 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;
2015-04-08 15:20:10 -07:00
using System.IO;
using System.Linq;
using MatterHackers.Agg;
using MatterHackers.Agg.Image;
using MatterHackers.Agg.Platform;
using MatterHackers.Agg.UI;
using MatterHackers.DataConverters3D;
2021-05-21 15:23:25 -07:00
using MatterHackers.ImageProcessing;
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.MatterControl.PrintLibrary;
using MatterHackers.MatterControl.SlicerConfiguration;
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 PartViewMode PreviousMode { get; set; }
2017-10-19 09:01:07 -07:00
}
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;
private View3DWidget view3DWidget;
2020-09-12 13:09:17 -07:00
private readonly ISceneContext sceneContext;
private readonly PartWorkspace workspace;
private ViewControls3DButtons activeTransformState = ViewControls3DButtons.PartSelect;
2020-09-24 13:53:49 -07:00
private readonly Dictionary<GuiWidget, SceneOperation> operationButtons;
private MainViewWidget mainViewWidget = null;
2020-09-12 13:09:17 -07:00
private readonly PopupMenuButton bedMenuButton;
private readonly UndoBuffer undoBuffer;
private readonly IconButton undoButton;
private readonly IconButton redoButton;
2018-08-08 07:08:08 -07:00
public ViewControls3D(PartWorkspace workspace, ThemeConfig theme, UndoBuffer undoBuffer, bool isPrinterType, bool showPrintButton)
: base(theme)
2015-04-08 15:20:10 -07:00
{
2018-11-16 08:44:56 -08:00
this.undoBuffer = undoBuffer;
this.ActionArea.Click += (s, e) =>
{
2020-09-11 19:59:14 -07:00
view3DWidget.Object3DControlLayer.Focus();
};
this.OverflowButton.ToolTipText = "Tool Bar Overflow".Localize();
2019-06-18 18:29:10 -07:00
this.OverflowButton.DynamicPopupContent = () =>
{
2020-09-26 10:59:31 -07:00
bool IncludeInMenu(SceneOperation operation)
2019-06-18 18:29:10 -07:00
{
2020-09-26 10:59:31 -07:00
foreach (var widget in this.ActionArea.Children.Where(c => !c.Visible && !ignoredInMenuTypes.Contains(c.GetType())))
2019-06-18 18:29:10 -07:00
{
2020-09-26 10:59:31 -07:00
if (operationButtons.TryGetValue(widget, out SceneOperation buttonOperation)
&& buttonOperation == operation)
2019-06-18 18:29:10 -07:00
{
2020-09-27 08:17:47 -07:00
return true;
2019-06-18 18:29:10 -07:00
}
}
2020-09-26 10:59:31 -07:00
2020-09-27 08:17:47 -07:00
return false;
2019-06-18 18:29:10 -07:00
}
return SceneOperations.GetToolbarOverflowMenu(AppContext.MenuTheme, sceneContext, IncludeInMenu);
2019-06-18 18:29:10 -07:00
};
this.IsPrinterMode = isPrinterType;
this.sceneContext = workspace.SceneContext;
this.workspace = workspace;
this.AddChild(CreateOpenButton(sceneContext, theme));
2015-04-08 15:20:10 -07:00
this.AddChild(CreateOpenFileButton(theme));
this.AddChild(CreateSaveButton(theme));
2020-10-19 18:08:15 -07:00
this.AddChild(new ToolbarSeparator(theme.GetBorderColor(50), theme.SeparatorMargin));
bedMenuButton = new PopupMenuButton("Bed".Localize(), StaticData.Instance.LoadIcon("bed.png", 16, 16).SetToColor(theme.TextColor), theme)
{
Name = "Bed Options Menu",
Enabled = true,
Margin = theme.ButtonSpacing,
2018-10-19 18:17:27 -07:00
VAnchor = VAnchor.Center,
DrawArrow = true
};
this.AddChild(bedMenuButton);
2020-10-19 18:08:15 -07:00
this.AddChild(new ToolbarSeparator(theme.GetBorderColor(50), theme.SeparatorMargin));
2021-05-25 16:48:59 -07:00
undoButton = new IconButton(StaticData.Instance.LoadIcon("undo.png", 16, 16).SetToColor(theme.TextColor), theme)
{
Name = "3D View Undo",
ToolTipText = "Undo".Localize(),
Enabled = false,
2018-06-25 08:39:57 -07:00
Margin = theme.ButtonSpacing,
VAnchor = VAnchor.Center
};
2017-08-14 12:34:44 -07:00
undoButton.Click += (sender, e) =>
{
sceneContext.Scene.Undo();
2020-09-11 19:59:14 -07:00
view3DWidget.Object3DControlLayer.Focus();
2017-08-14 12:34:44 -07:00
};
this.AddChild(undoButton);
2021-05-25 16:48:59 -07:00
redoButton = new IconButton(StaticData.Instance.LoadIcon("redo.png", 16, 16).SetToColor(theme.TextColor), theme)
{
Name = "3D View Redo",
2018-06-25 08:39:57 -07:00
Margin = theme.ButtonSpacing,
ToolTipText = "Redo".Localize(),
Enabled = false,
VAnchor = VAnchor.Center
};
2017-08-14 12:34:44 -07:00
redoButton.Click += (sender, e) =>
{
sceneContext.Scene.Redo();
2020-09-11 19:59:14 -07:00
view3DWidget.Object3DControlLayer.Focus();
2017-08-14 12:34:44 -07:00
};
this.AddChild(redoButton);
2018-10-15 15:13:40 -07:00
if (showPrintButton)
{
2018-10-17 14:42:01 -07:00
var printButton = new TextButton("Print", theme)
{
2018-10-17 17:38:20 -07:00
Name = "Print Button",
2018-10-17 14:42:01 -07:00
BackgroundColor = theme.AccentMimimalOverlay
};
printButton.Click += (s, e) =>
{
view3DWidget.PushToPrinterAndPrint();
};
2018-10-15 15:13:40 -07:00
this.AddChild(printButton);
}
2020-10-19 18:08:15 -07:00
this.AddChild(new ToolbarSeparator(theme.GetBorderColor(50), theme.SeparatorMargin));
2017-08-14 12:34:44 -07:00
undoButton.Enabled = undoBuffer.UndoCount > 0;
redoButton.Enabled = undoBuffer.RedoCount > 0;
2020-09-24 13:53:49 -07:00
operationButtons = new Dictionary<GuiWidget, SceneOperation>();
// Add Selected IObject3D -> Operations to toolbar
2020-09-26 10:59:31 -07:00
foreach (var namedAction in SceneOperations.All)
{
if (namedAction is SceneSelectionSeparator)
{
2020-10-19 18:08:15 -07:00
this.AddChild(new ToolbarSeparator(theme.GetBorderColor(50), theme.SeparatorMargin));
continue;
}
// add the create support before the align
if (namedAction is OperationGroup group
&& group.Id == "Transform")
{
this.AddChild(CreateSupportButton(theme));
2020-10-19 18:08:15 -07:00
this.AddChild(new ToolbarSeparator(theme.GetBorderColor(50), theme.SeparatorMargin));
}
GuiWidget button = null;
if (namedAction is OperationGroup operationGroup)
{
if (operationGroup.Collapse)
{
var defaultOperation = operationGroup.GetDefaultOperation();
PopupMenuButton groupButton = null;
groupButton = theme.CreateSplitButton(
new SplitButtonParams()
{
2021-05-21 14:24:45 -07:00
Icon = defaultOperation.Icon(theme),
ButtonAction = (menuButton) =>
{
defaultOperation.Action.Invoke(sceneContext);
},
ButtonTooltip = defaultOperation.HelpText ?? defaultOperation.Title,
ButtonName = defaultOperation.Title,
ExtendPopupMenu = (PopupMenu popupMenu) =>
{
foreach (var operation in operationGroup.Operations)
{
2021-05-21 14:24:45 -07:00
var operationMenu = popupMenu.CreateMenuItem(operation.Title, operation.Icon?.Invoke(theme));
2020-06-26 08:07:53 -07:00
operationMenu.Enabled = operation.IsEnabled(sceneContext);
operationMenu.ToolTipText = operation.Title;
if (!operationMenu.Enabled
&& !string.IsNullOrEmpty(operation.HelpText))
{
operationMenu.ToolTipText += "\n\n" + operation.HelpText;
}
operationMenu.Click += (s, e) => UiThread.RunOnIdle(() =>
{
if (defaultOperation != operation)
{
// Update button
var iconButton = groupButton.Children.OfType<IconButton>().First();
2021-05-21 14:24:45 -07:00
iconButton.SetIcon(operation.Icon(theme));
iconButton.ToolTipText = operation.HelpText ?? operation.Title;
UserSettings.Instance.set(operationGroup.GroupRecordId, operationGroup.Operations.IndexOf(operation).ToString());
defaultOperation = operation;
iconButton.Invalidate();
}
operation.Action?.Invoke(sceneContext);
});
}
}
},
operationGroup);
button = groupButton;
}
else
{
if (!(this.ActionArea.Children.LastOrDefault() is ToolbarSeparator))
{
2020-10-19 18:08:15 -07:00
this.AddChild(new ToolbarSeparator(theme.GetBorderColor(50), theme.SeparatorMargin));
}
foreach (var operation in operationGroup.Operations)
{
var operationButton = new OperationIconButton(operation, sceneContext, theme);
operationButtons.Add(operationButton, operation);
this.AddChild(operationButton);
}
2020-10-19 18:08:15 -07:00
this.AddChild(new ToolbarSeparator(theme.GetBorderColor(50), theme.SeparatorMargin));
}
}
else if (namedAction.Icon != null)
{
2021-05-21 14:24:45 -07:00
button = new IconButton(namedAction.Icon(theme), theme)
{
Name = namedAction.Title + " Button",
ToolTipText = namedAction.Title,
Margin = theme.ButtonSpacing,
BackgroundColor = theme.ToolbarButtonBackground,
HoverColor = theme.ToolbarButtonHover,
MouseDownColor = theme.ToolbarButtonDown,
};
}
else
{
button = new TextButton(namedAction.Title, theme)
{
Name = namedAction.Title + " Button",
Margin = theme.ButtonSpacing,
BackgroundColor = theme.ToolbarButtonBackground,
HoverColor = theme.ToolbarButtonHover,
MouseDownColor = theme.ToolbarButtonDown,
};
}
if (button != null)
{
operationButtons.Add(button, namedAction);
// Only bind Click event if not a SplitButton
if (!(button is PopupMenuButton))
{
button.Click += (s, e) => UiThread.RunOnIdle(() =>
{
namedAction.Action.Invoke(sceneContext);
var partTab = button.Parents<PartTabPage>().FirstOrDefault();
var view3D = partTab.Descendants<View3DWidget>().FirstOrDefault();
2020-09-11 19:59:14 -07:00
view3D.Object3DControlLayer.Focus();
});
}
this.AddChild(button);
}
}
2018-11-16 08:44:56 -08:00
// Register listeners
undoBuffer.Changed += UndoBuffer_Changed;
sceneContext.Scene.SelectionChanged += UpdateToolbarButtons;
sceneContext.Scene.ItemsModified += UpdateToolbarButtons;
// Run on load
UpdateToolbarButtons(null, null);
}
2021-05-25 17:25:44 -07:00
public void NotifyResetView()
2019-06-18 18:04:03 -07:00
{
this.ResetView.Invoke(this, null);
}
public bool IsPrinterMode { get; }
public ViewControls3DButtons ActiveButton
{
get => activeTransformState;
set
{
this.activeTransformState = value;
2021-05-17 07:25:37 -07:00
view3DWidget?.UpdateControlButtons(activeTransformState);
2019-06-18 18:04:03 -07:00
TransformStateChanged?.Invoke(this, new TransformStateChangedEventArgs()
{
TransformMode = activeTransformState
});
}
}
internal void SetView3DWidget(View3DWidget view3DWidget)
{
this.view3DWidget = view3DWidget;
bedMenuButton.DynamicPopupContent = () =>
{
var workspaceActions = ApplicationController.Instance.GetWorkspaceActions(view3DWidget);
var menuTheme = ApplicationController.Instance.MenuTheme;
var popupMenu = new PopupMenu(menuTheme);
int thumbWidth = 45;
var gutterWidth = thumbWidth + 7;
popupMenu.CreateSubMenu("Open Recent".Localize(), menuTheme, (subMenu) =>
{
int maxItemWidth = 0;
var recentFiles = new DirectoryInfo(ApplicationDataStorage.Instance.PlatingDirectory).GetFiles("*.mcx").OrderByDescending(f => f.LastWriteTime);
foreach (var item in recentFiles.Where(f => f.Length > 215).Select(f => new SceneReplacementFileItem(f.FullName)).Take(12))
{
var imageBuffer = new ImageBuffer(thumbWidth, thumbWidth);
var title = new FileInfo(item.Path).LastWriteTime.ToString("MMMM d h:mm tt");
var bedHistory = subMenu.CreateMenuItem(title, imageBuffer);
bedHistory.GutterWidth = gutterWidth;
bedHistory.HAnchor = HAnchor.Fit;
bedHistory.VAnchor = VAnchor.Absolute;
bedHistory.Padding = new BorderDouble(gutterWidth + 3, 2, 12, 2);
bedHistory.Height = thumbWidth + 3;
bedHistory.Click += (s, e) =>
{
UiThread.RunOnIdle(async () =>
{
await ApplicationController.Instance.Tasks.Execute("Saving changes".Localize() + "...", sceneContext.Printer, sceneContext.SaveChanges);
await sceneContext.LoadLibraryContent(item);
if (sceneContext.Printer != null)
{
sceneContext.Printer.ViewState.ViewMode = PartViewMode.Model;
}
});
};
maxItemWidth = (int)Math.Max(maxItemWidth, bedHistory.Width);
void UpdateImageBuffer(ImageBuffer thumbnail)
{
// Copy updated thumbnail into original image
imageBuffer.CopyFrom(thumbnail);
bedHistory.Invalidate();
}
ApplicationController.Instance.Library.LoadItemThumbnail(
UpdateImageBuffer,
(contentProvider) =>
{
if (contentProvider is MeshContentProvider meshContentProvider)
{
ApplicationController.Instance.Thumbnails.QueueForGeneration(async () =>
{
// Ask the MeshContentProvider to RayTrace the image
var thumbnail = await meshContentProvider.GetThumbnail(item, thumbWidth, thumbWidth);
if (thumbnail != null)
{
UpdateImageBuffer(thumbnail);
}
});
}
},
item,
ApplicationController.Instance.Library.PlatingHistory,
thumbWidth,
thumbWidth,
menuTheme).ConfigureAwait(false);
}
// Resize menu items to max item width
foreach (var menuItem in subMenu.Children)
{
menuItem.HAnchor = HAnchor.Left | HAnchor.Absolute;
menuItem.Width = maxItemWidth;
}
});
2020-09-12 13:09:17 -07:00
var actions = new NamedAction[]
{
2019-06-18 18:04:03 -07:00
new ActionSeparator(),
workspaceActions["Edit"],
2019-06-18 18:04:03 -07:00
new ActionSeparator(),
workspaceActions["Print"],
new ActionSeparator(),
new NamedAction()
{
ID = "Export",
Title = "Export".Localize(),
2021-05-21 15:23:25 -07:00
Icon = StaticData.Instance.LoadIcon("cube_export.png", 16, 16).SetToColor(menuTheme.TextColor),
2019-06-18 18:04:03 -07:00
Action = () =>
{
ApplicationController.Instance.ExportLibraryItems(
2020-09-12 13:09:17 -07:00
new[] { new InMemoryLibraryItem(sceneContext.Scene) },
2019-06-18 18:04:03 -07:00
centerOnBed: false,
printer: view3DWidget.Printer);
},
IsEnabled = () => sceneContext.EditableScene
|| (sceneContext.EditContext.SourceItem is ILibraryAsset libraryAsset
2020-09-12 13:09:17 -07:00
&& string.Equals(Path.GetExtension(libraryAsset.FileName), ".gcode", StringComparison.OrdinalIgnoreCase))
2019-06-18 18:04:03 -07:00
},
new ActionSeparator(),
new NamedAction()
{
ID = "ClearBed",
Title = "Clear Bed".Localize(),
Action = () =>
{
UiThread.RunOnIdle(() =>
{
view3DWidget.ClearPlate();
});
}
}
};
menuTheme.CreateMenuItems(popupMenu, actions);
return popupMenu;
};
}
2018-11-16 08:44:56 -08:00
private void UndoBuffer_Changed(object sender, EventArgs e)
{
undoButton.Enabled = undoBuffer.UndoCount > 0;
redoButton.Enabled = undoBuffer.RedoCount > 0;
}
private GuiWidget CreateOpenFileButton(ThemeConfig theme)
2018-10-29 17:31:43 -07:00
{
var popupMenu = new PopupMenuButton("", theme)
2018-10-29 17:31:43 -07:00
{
2018-11-27 18:15:17 -08:00
Name = "Open File Button"
2018-10-29 17:31:43 -07:00
};
if (popupMenu.Children<SimpleButton>().FirstOrDefault() is SimpleButton simpleButton)
{
simpleButton.Padding = 0;
};
var openMenuItems = new PopupMenu(ApplicationController.Instance.MenuTheme);
popupMenu.PopupContent = openMenuItems;
var openButton = openMenuItems.CreateMenuItem("Open File".Localize(), StaticData.Instance.LoadIcon("fa-folder-open_16.png", 16, 16).SetToColor(theme.TextColor));
2018-10-29 17:31:43 -07:00
openButton.Click += (s, e) =>
{
var extensionsWithoutPeriod = new HashSet<string>(ApplicationSettings.OpenDesignFileParams.Split('|').First().Split(',').Select(t => t.Trim().Trim('.')));
foreach (var extension in ApplicationController.Instance.Library.ContentProviders.Keys)
{
extensionsWithoutPeriod.Add(extension.ToUpper());
}
var extensionsArray = extensionsWithoutPeriod.OrderBy(t => t).ToArray();
string filter = string.Format(
"{0}|{1}",
string.Join(",", extensionsArray),
string.Join("", extensionsArray.Select(t => $"*.{t.ToLower()};").ToArray()));
UiThread.RunOnIdle(() =>
{
AggContext.FileDialogs.OpenFileDialog(
new OpenFileDialogParams(filter, multiSelect: true),
(openParams) =>
{
sceneContext.AddToPlate(openParams.FileNames);
2018-10-29 17:31:43 -07:00
});
}, .1);
};
return popupMenu;
2018-10-29 17:31:43 -07:00
}
private void UpdateToolbarButtons(object sender, EventArgs e)
{
// Set enabled level based on operation rules
2019-06-18 18:29:10 -07:00
foreach (var (button, operation) in operationButtons.Select(kvp => (kvp.Key, kvp.Value)))
{
2020-06-26 08:07:53 -07:00
button.Enabled = operation.IsEnabled?.Invoke(sceneContext) ?? false;
button.ToolTipText = operation.Title;
if (!button.Enabled
&& !string.IsNullOrEmpty(operation.HelpText))
{
button.ToolTipText += "\n\n" + operation.HelpText;
}
2019-06-18 18:29:10 -07:00
if (operation is OperationGroup operationGroup
&& button is PopupMenuButton splitButton
&& button.Descendants<IconButton>().FirstOrDefault() is IconButton iconButton)
{
2020-06-26 08:07:53 -07:00
var defaultOperation = operationGroup.GetDefaultOperation();
iconButton.Enabled = defaultOperation.IsEnabled(sceneContext);
iconButton.ToolTipText = defaultOperation.Title;
if (!iconButton.Enabled
&& !string.IsNullOrEmpty(defaultOperation.HelpText))
{
iconButton.ToolTipText += "\n\n" + defaultOperation.HelpText;
}
}
}
}
private GuiWidget CreateOpenButton(ISceneContext sceneContext, ThemeConfig theme)
{
var openColor = theme.ResolveColor(theme.BackgroundColor, theme.SlightShade);
PopupMenuButton libraryPopup = null;
libraryPopup = new PopupMenuButton("Open".Localize(), StaticData.Instance.LoadIcon("fa-folder-open_16.png", 16, 16).SetToColor(theme.TextColor), theme)
{
MakeScrollable = false,
2018-10-10 13:33:16 -07:00
Name = "Add Content Menu",
AlwaysKeepOpen = true,
DynamicPopupContent = () =>
{
if (mainViewWidget == null)
{
mainViewWidget = this.Parents<MainViewWidget>().FirstOrDefault();
}
var verticalResizeContainer = new VerticalResizeContainer(theme, GrabBarSide.Right)
{
BackgroundColor = openColor,
MinimumSize = new Vector2(120, 50),
Height = libraryPopup.TransformToScreenSpace(libraryPopup.Position).Y,
SplitterBarColor = theme.SlightShade,
};
double.TryParse(UserSettings.Instance.get(UserSettingsKey.PopupLibraryWidth), out double controlWidth);
if (controlWidth == 0)
{
controlWidth = 400;
}
2020-08-08 07:12:56 -07:00
verticalResizeContainer.Width = controlWidth;
verticalResizeContainer.BoundsChanged += (s2, e2) =>
{
UserSettings.Instance.set(UserSettingsKey.PopupLibraryWidth, verticalResizeContainer.Width.ToString());
};
var systemWindow = this.Parents<SystemWindow>().FirstOrDefault();
// Compute slight highlight of openColor for use as listView background color
var slightHighlight = theme.ResolveColor(openColor, Color.White.WithAlpha(theme.IsDarkTheme ? 10 : 50));
var printLibraryWidget = new PrintLibraryWidget(mainViewWidget, workspace, theme, slightHighlight, libraryPopup)
{
HAnchor = HAnchor.Stretch,
VAnchor = VAnchor.Absolute,
Height = libraryPopup.TransformToScreenSpace(libraryPopup.Position).Y,
Margin = new BorderDouble(left: verticalResizeContainer.SplitterWidth)
};
systemWindow.SizeChanged += (s, e) =>
{
printLibraryWidget.Height = libraryPopup.TransformToScreenSpace(libraryPopup.Position).Y;
};
verticalResizeContainer.AddChild(printLibraryWidget);
2020-09-12 13:09:17 -07:00
systemWindow.MouseDown += SystemWindownMouseDown;
2020-09-12 13:09:17 -07:00
void SystemWindownMouseDown(object s2, MouseEventArgs mouseEvent)
{
if (verticalResizeContainer.Parent != null)
{
// MouseUp on our SystemWindow outside of our bounds should call close
var resizeContainerMousePosition = verticalResizeContainer.TransformFromScreenSpace(mouseEvent.Position);
bool mouseUpOnWidget = resizeContainerMousePosition.X >= 0 && resizeContainerMousePosition.X <= verticalResizeContainer.Width
&& resizeContainerMousePosition.Y >= 0 && resizeContainerMousePosition.Y <= verticalResizeContainer.Height;
if (!mouseUpOnWidget)
{
libraryPopup.CloseMenu();
2020-09-12 13:09:17 -07:00
systemWindow.MouseDown -= SystemWindownMouseDown;
}
}
else
{
2020-09-12 13:09:17 -07:00
systemWindow.MouseDown -= SystemWindownMouseDown;
}
2020-09-12 13:09:17 -07:00
}
return verticalResizeContainer;
},
BackgroundColor = theme.ToolbarButtonBackground,
HoverColor = theme.ToolbarButtonHover,
MouseDownColor = theme.ToolbarButtonDown,
OpenColor = openColor,
// DrawArrow = true,
Margin = 0,
2019-02-24 10:02:53 -08:00
PopupBorderColor = Color.Transparent,
PopupHAnchor = HAnchor.Fit,
PopupVAnchor = VAnchor.Fit
};
return libraryPopup;
}
2018-12-31 09:12:49 -08:00
private GuiWidget CreateSupportButton(ThemeConfig theme)
{
PopupMenuButton toggleSupportButton = null;
var minimumSupportHeight = .05;
if (sceneContext.Printer != null)
{
minimumSupportHeight = sceneContext.Printer.Settings.GetValue<double>(SettingsKey.layer_height) / 2;
}
2021-05-21 15:23:25 -07:00
toggleSupportButton = new PopupMenuButton(StaticData.Instance.LoadIcon("support.png", 16, 16).SetToColor(theme.TextColor), theme)
2018-12-31 09:12:49 -08:00
{
Name = "Support SplitButton",
ToolTipText = "Generate Support".Localize(),
DynamicPopupContent = () => new GenerateSupportPanel(AppContext.MenuTheme, sceneContext.Scene, minimumSupportHeight),
2018-12-31 12:12:05 -08:00
PopupHAnchor = HAnchor.Fit,
PopupVAnchor = VAnchor.Fit,
MakeScrollable = false,
2018-12-31 09:12:49 -08:00
BackgroundColor = theme.ToolbarButtonBackground,
HoverColor = theme.ToolbarButtonHover,
MouseDownColor = theme.ToolbarButtonDown,
DrawArrow = true,
Margin = theme.ButtonSpacing,
};
return toggleSupportButton;
}
private GuiWidget CreateSaveButton(ThemeConfig theme)
{
return theme.CreateSplitButton(new SplitButtonParams()
{
ButtonText = "Save".Localize(),
ButtonName = "Save",
2021-05-21 15:23:25 -07:00
Icon = StaticData.Instance.LoadIcon("save_grey_16x.png", 16, 16).SetToColor(theme.TextColor),
ButtonAction = (menuButton) =>
{
ApplicationController.Instance.Tasks.Execute("Saving".Localize(), sceneContext.Printer, async (progress, cancellationToken) =>
{
menuButton.Enabled = false;
try
{
await sceneContext.SaveChanges(progress, cancellationToken);
}
catch (Exception ex)
{
ApplicationController.Instance.LogError("Error saving file".Localize() + ": " + ex.Message);
}
menuButton.Enabled = true;
}).ConfigureAwait(false);
},
ExtendPopupMenu = (PopupMenu popupMenu) =>
{
var saveAs = popupMenu.CreateMenuItem("Save As".Localize());
saveAs.Click += (s, e) => UiThread.RunOnIdle(() =>
{
DialogWindow.Show(
new SaveAsPage(
(newName, destinationContainer) =>
{
// Save to the destination provider
if (destinationContainer is ILibraryWritableContainer writableContainer)
{
// Wrap stream with ReadOnlyStream library item and add to container
writableContainer.Add(new[]
{
new InMemoryLibraryItem(sceneContext.Scene)
{
Name = newName
}
});
destinationContainer.Dispose();
}
}));
});
2021-05-21 15:23:25 -07:00
var export = popupMenu.CreateMenuItem("Export".Localize(), StaticData.Instance.LoadIcon("cube_export.png", 16, 16).SetToColor(theme.TextColor));
export.Click += (s, e) => UiThread.RunOnIdle(() =>
{
ApplicationController.Instance.ExportLibraryItems(
new[] { new InMemoryLibraryItem(sceneContext.Scene) },
centerOnBed: false,
printer: view3DWidget.Printer);
});
}
});
}
public override void OnClosed(EventArgs e)
2015-04-08 15:20:10 -07:00
{
2018-11-16 08:44:56 -08:00
// Unregister listeners
undoBuffer.Changed -= UndoBuffer_Changed;
sceneContext.Scene.SelectionChanged -= UpdateToolbarButtons;
sceneContext.Scene.Children.ItemsModified -= UpdateToolbarButtons;
2018-11-16 08:44:56 -08:00
2015-04-08 15:20:10 -07:00
base.OnClosed(e);
}
}
}