mattercontrol/PrinterControls/ControlWidgets/MovementControls.cs

371 lines
12 KiB
C#
Raw Normal View History

2014-10-15 16:57:26 -07:00
/*
Copyright (c) 2017, Kevin Pope, John Lewin
2014-10-15 16:57:26 -07:00
All rights reserved.
Redistribution and use in source and binary forms, with or without
2015-04-08 15:20:10 -07:00
modification, are permitted provided that the following conditions are met:
2014-10-15 16:57:26 -07:00
1. Redistributions of source code must retain the above copyright notice, this
2015-04-08 15:20:10 -07:00
list of conditions and the following disclaimer.
2014-10-15 16:57:26 -07:00
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
2015-04-08 15:20:10 -07:00
and/or other materials provided with the distribution.
2014-10-15 16:57:26 -07:00
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
2015-04-08 15:20:10 -07:00
of the authors and should not be interpreted as representing official policies,
2014-10-15 16:57:26 -07:00
either expressed or implied, of the FreeBSD Project.
*/
using System;
using System.Collections.Generic;
2014-10-15 16:57:26 -07:00
using MatterHackers.Agg;
using MatterHackers.Agg.Platform;
2014-10-15 16:57:26 -07:00
using MatterHackers.Agg.UI;
using MatterHackers.Localizations;
using MatterHackers.MatterControl.CustomWidgets;
2014-10-15 16:57:26 -07:00
using MatterHackers.MatterControl.PrinterCommunication;
2016-04-18 11:31:31 -07:00
using MatterHackers.MatterControl.SlicerConfiguration;
using MatterHackers.MatterControl.Utilities;
2015-04-08 15:20:10 -07:00
using MatterHackers.VectorMath;
2014-10-15 16:57:26 -07:00
namespace MatterHackers.MatterControl.PrinterControls
{
2018-01-10 16:34:24 -08:00
public class MovementControls : FlowLayoutWidget
2015-04-08 15:20:10 -07:00
{
private PrinterConfig printer;
2018-01-10 15:00:59 -08:00
private ThemeConfig theme;
2015-11-23 10:49:08 -08:00
public FlowLayoutWidget manualControlsLayout;
2015-04-08 15:20:10 -07:00
private Button disableMotors;
2015-11-23 10:49:08 -08:00
private EditManualMovementSpeedsWindow editManualMovementSettingsWindow;
2015-04-08 15:20:10 -07:00
private Button homeAllButton;
private Button homeXButton;
private Button homeYButton;
private Button homeZButton;
internal JogControls jogControls;
// Provides a list of DisableableWidgets controls that can be toggled on/off at runtime
internal List<DisableableWidget> DisableableWidgets = new List<DisableableWidget>();
// Displays the current baby step offset stream values
private TextWidget offsetStreamLabel;
private LimitCallingFrequency reportDestinationChanged = null;
private EventHandler unregisterEvents;
private MovementControls(PrinterConfig printer, ThemeConfig theme)
: base (FlowDirection.TopToBottom)
{
this.printer = printer;
this.theme = theme;
jogControls = new JogControls(printer, new XYZColors())
{
HAnchor = HAnchor.Left | HAnchor.Stretch,
Margin = 0
};
this.AddChild(CreateDisableableContainer(GetHomeButtonBar()));
// Separator line
this.AddChild(new HorizontalLine(alpha: 50)
{
Margin = new BorderDouble(0, 5)
});
this.AddChild(jogControls);
this.AddChild(CreateDisableableContainer(GetHWDestinationBar()));
}
public static SectionWidget CreateSection(PrinterConfig printer, ThemeConfig theme)
{
var widget = new MovementControls(printer, theme);
var editButton = new IconButton(AggContext.StaticData.LoadIcon("icon_edit.png", 16, 16, IconColor.Theme), theme);
editButton.Click += (s, e) => widget.EditOptions();
return new SectionWidget(
"Movement".Localize(),
widget,
theme,
editButton);
}
public override void OnClosed(ClosedEventArgs e)
{
2016-04-18 11:31:31 -07:00
unregisterEvents?.Invoke(this, null);
base.OnClosed(e);
}
private void EditOptions()
{
if (editManualMovementSettingsWindow == null)
{
editManualMovementSettingsWindow = new EditManualMovementSpeedsWindow("Movement Speeds".Localize(), printer.Settings.Helpers.GetMovementSpeedsString(), SetMovementSpeeds);
editManualMovementSettingsWindow.Closed += (s, e) =>
{
editManualMovementSettingsWindow = null;
};
}
else
{
editManualMovementSettingsWindow.BringToFront();
}
}
/// <summary>
/// Helper method to create DisableableWidget containers and populate the DisableableWidgets local property.
/// </summary>
/// <param name="widget">The widget to wrap.</param>
private DisableableWidget CreateDisableableContainer(GuiWidget widget)
{
var container = new DisableableWidget()
{
HAnchor = HAnchor.Left | HAnchor.Stretch
};
container.AddChild(widget);
DisableableWidgets.Add(container);
return container;
}
private void SetMovementSpeeds(string speedString)
2015-11-23 10:49:08 -08:00
{
if (!string.IsNullOrEmpty(speedString))
2015-11-23 10:49:08 -08:00
{
printer.Settings.SetValue(SettingsKey.manual_movement_speeds, speedString);
printer.Bed.GCodeRenderer?.Clear3DGCode();
2015-11-23 10:49:08 -08:00
}
}
2015-04-08 15:20:10 -07:00
private FlowLayoutWidget GetHomeButtonBar()
{
FlowLayoutWidget homeButtonBar = new FlowLayoutWidget();
homeButtonBar.HAnchor = HAnchor.Stretch;
homeButtonBar.Margin = new BorderDouble(0);
2015-04-08 15:20:10 -07:00
homeButtonBar.Padding = new BorderDouble(0);
2018-01-10 15:00:59 -08:00
var homingButtonFactory = theme.HomingButtons;
var commonButtonFactory = theme.ButtonFactory;
2015-04-08 15:20:10 -07:00
var homeIconImageWidget = new ImageWidget(AggContext.StaticData.LoadIcon("icon_home_white_24x24.png", 24, 24, IconColor.Theme));
homeIconImageWidget.Margin = new BorderDouble(0, 0, 6, 0);
homeIconImageWidget.OriginRelativeParent += new Vector2(0, 2) * GuiWidget.DeviceScale;
homeAllButton = homingButtonFactory.Generate("ALL".Localize());
homeAllButton.ToolTipText = "Home X, Y and Z".Localize();
homeAllButton.Margin = new BorderDouble(0, 0, 6, 0);
homeAllButton.Click += homeAll_Click;
2014-10-15 16:57:26 -07:00
double fixedWidth = (int)homeAllButton.Width * GuiWidget.DeviceScale;
2017-08-13 14:17:34 -07:00
homeXButton = homingButtonFactory.Generate("X", fixedWidth: fixedWidth);
homeXButton.ToolTipText = "Home X".Localize();
homeXButton.Margin = new BorderDouble(0, 0, 6, 0);
homeXButton.Click += homeXButton_Click;
2015-04-08 15:20:10 -07:00
2017-08-13 14:17:34 -07:00
homeYButton = homingButtonFactory.Generate("Y", fixedWidth: fixedWidth);
homeYButton.ToolTipText = "Home Y".Localize();
homeYButton.Margin = new BorderDouble(0, 0, 6, 0);
homeYButton.Click += homeYButton_Click;
2015-04-08 15:20:10 -07:00
2017-08-13 14:17:34 -07:00
homeZButton = homingButtonFactory.Generate("Z", fixedWidth: fixedWidth);
homeZButton.ToolTipText = "Home Z".Localize();
homeZButton.Margin = new BorderDouble(0, 0, 6, 0);
homeZButton.Click += homeZButton_Click;
2015-04-08 15:20:10 -07:00
// Create 'Release' button, clearing fixedWidth needed on sibling 'Home' controls
2017-12-10 21:44:08 -08:00
disableMotors = commonButtonFactory.Generate("Release".Localize(), fixedWidth: 0);
2015-04-08 15:20:10 -07:00
disableMotors.Margin = new BorderDouble(0);
disableMotors.Click += (s, e) =>
{
printer.Connection.ReleaseMotors(true);
};
2015-04-08 15:20:10 -07:00
homeButtonBar.AddChild(homeIconImageWidget);
homeButtonBar.AddChild(homeAllButton);
homeButtonBar.AddChild(homeXButton);
homeButtonBar.AddChild(homeYButton);
homeButtonBar.AddChild(homeZButton);
offsetStreamLabel = new TextWidget("Z Offset".Localize() + ":", pointSize: 8)
{
TextColor = ActiveTheme.Instance.PrimaryTextColor,
Margin = new BorderDouble(left: 10),
AutoExpandBoundsToText = true,
VAnchor = VAnchor.Center
};
homeButtonBar.AddChild(offsetStreamLabel);
2018-01-10 15:00:59 -08:00
var ztuningWidget = new ZTuningWidget(printer.Settings, theme);
homeButtonBar.AddChild(ztuningWidget);
2018-01-10 15:00:59 -08:00
homeButtonBar.AddChild(new HorizontalSpacer());
2015-04-08 15:20:10 -07:00
homeButtonBar.AddChild(disableMotors);
return homeButtonBar;
}
private FlowLayoutWidget GetHWDestinationBar()
{
FlowLayoutWidget hwDestinationBar = new FlowLayoutWidget();
hwDestinationBar.HAnchor = HAnchor.Stretch;
hwDestinationBar.Margin = new BorderDouble(top: 8);
hwDestinationBar.Padding = 0;
TextWidget xPosition = new TextWidget("X: 0.0 ", pointSize: 12, textColor: ActiveTheme.Instance.PrimaryTextColor);
TextWidget yPosition = new TextWidget("Y: 0.0 ", pointSize: 12, textColor: ActiveTheme.Instance.PrimaryTextColor);
TextWidget zPosition = new TextWidget("Z: 0.0 ", pointSize: 12, textColor: ActiveTheme.Instance.PrimaryTextColor);
hwDestinationBar.AddChild(xPosition);
hwDestinationBar.AddChild(yPosition);
hwDestinationBar.AddChild(zPosition);
SetDestinationPositionText(xPosition, yPosition, zPosition);
reportDestinationChanged = new LimitCallingFrequency(1, () =>
{
UiThread.RunOnIdle(() =>
{
SetDestinationPositionText(xPosition, yPosition, zPosition);
});
});
printer.Connection.DestinationChanged.RegisterEvent((object sender, EventArgs e) =>
{
reportDestinationChanged.CallEvent();
}, ref unregisterEvents);
return hwDestinationBar;
}
private void SetDestinationPositionText(TextWidget xPosition, TextWidget yPosition, TextWidget zPosition)
{
Vector3 destinationPosition = printer.Connection.CurrentDestination;
xPosition.Text = "X: {0:0.00}".FormatWith(destinationPosition.X);
yPosition.Text = "Y: {0:0.00}".FormatWith(destinationPosition.Y);
zPosition.Text = "Z: {0:0.00}".FormatWith(destinationPosition.Z);
}
2015-11-23 10:49:08 -08:00
private void homeAll_Click(object sender, EventArgs mouseEvent)
2015-04-08 15:20:10 -07:00
{
printer.Connection.HomeAxis(PrinterConnection.Axis.XYZ);
2015-04-08 15:20:10 -07:00
}
private void homeXButton_Click(object sender, EventArgs mouseEvent)
{
printer.Connection.HomeAxis(PrinterConnection.Axis.X);
2015-04-08 15:20:10 -07:00
}
private void homeYButton_Click(object sender, EventArgs mouseEvent)
{
printer.Connection.HomeAxis(PrinterConnection.Axis.Y);
2015-04-08 15:20:10 -07:00
}
private void homeZButton_Click(object sender, EventArgs mouseEvent)
{
printer.Connection.HomeAxis(PrinterConnection.Axis.Z);
2015-04-08 15:20:10 -07:00
}
2015-11-23 10:49:08 -08:00
}
2015-04-08 15:20:10 -07:00
2015-11-23 10:49:08 -08:00
public class XYZColors
{
2017-10-31 11:43:25 -07:00
public static Color eColor = new Color(180, 180, 180);
public static Color xColor = new Color(180, 180, 180);
public static Color yColor = new Color(255, 255, 255);
public static Color zColor = new Color(255, 255, 255);
2015-11-23 10:49:08 -08:00
public XYZColors()
2015-04-08 15:20:10 -07:00
{
}
}
public class ZTuningWidget : GuiWidget
{
private TextWidget zOffsetStreamDisplay;
private Button clearZOffsetButton;
private FlowLayoutWidget zOffsetStreamContainer;
private EventHandler unregisterEvents;
private bool allowRemoveButton;
PrinterSettings printerSettings;
2018-01-10 15:00:59 -08:00
public ZTuningWidget(PrinterSettings printerSettings, ThemeConfig theme, bool allowRemoveButton = true)
{
this.printerSettings = printerSettings;
this.allowRemoveButton = allowRemoveButton;
this.HAnchor = HAnchor.Fit;
this.VAnchor = VAnchor.Fit | VAnchor.Center;
ActiveSliceSettings.SettingChanged.RegisterEvent((s, e) =>
{
2017-02-01 15:46:11 -08:00
if ((e as StringEventArgs)?.Data == SettingsKey.baby_step_z_offset)
{
2017-02-01 15:46:11 -08:00
OffsetStreamChanged(null, null);
}
}, ref unregisterEvents);
zOffsetStreamContainer = new FlowLayoutWidget(FlowDirection.LeftToRight)
{
Margin = new BorderDouble(3, 0),
Padding = new BorderDouble(3),
HAnchor = HAnchor.Fit,
VAnchor = VAnchor.Center,
BackgroundColor = ActiveTheme.Instance.SecondaryBackgroundColor,
Height = 20
};
this.AddChild(zOffsetStreamContainer);
double zoffset = printerSettings.GetValue<double>(SettingsKey.baby_step_z_offset);
zOffsetStreamDisplay = new TextWidget(zoffset.ToString("0.##"))
{
AutoExpandBoundsToText = true,
TextColor = ActiveTheme.Instance.PrimaryTextColor,
Margin = new BorderDouble(5, 0, 8, 0),
VAnchor = VAnchor.Center
};
zOffsetStreamContainer.AddChild(zOffsetStreamDisplay);
2018-01-10 15:00:59 -08:00
clearZOffsetButton = theme.CreateSmallResetButton();
clearZOffsetButton.Name = "Clear ZOffset button";
clearZOffsetButton.ToolTipText = "Clear ZOffset".Localize();
clearZOffsetButton.Visible = allowRemoveButton && zoffset != 0;
clearZOffsetButton.Click += (sender, e) =>
{
printerSettings.SetValue(SettingsKey.baby_step_z_offset, "0");
};
zOffsetStreamContainer.AddChild(clearZOffsetButton);
}
internal void OffsetStreamChanged(object sender, EventArgs e)
{
double zoffset = printerSettings.GetValue<double>(SettingsKey.baby_step_z_offset);
bool hasOverriddenZOffset = (zoffset != 0);
2017-09-13 21:56:13 -07:00
zOffsetStreamContainer.BackgroundColor = (allowRemoveButton && hasOverriddenZOffset) ? SliceSettingsRow.userSettingBackgroundColor : ActiveTheme.Instance.SecondaryBackgroundColor;
clearZOffsetButton.Visible = allowRemoveButton && hasOverriddenZOffset;
zOffsetStreamDisplay.Text = zoffset.ToString("0.##");
}
public override void OnClosed(ClosedEventArgs e)
{
unregisterEvents?.Invoke(null, null);
base.OnClosed(e);
}
}
}