Refactor temperature widgets

This commit is contained in:
John Lewin 2017-01-23 12:38:34 -08:00
parent 24b1756139
commit cbe60da89a
3 changed files with 75 additions and 100 deletions

View file

@ -1,5 +1,5 @@
/*
Copyright (c) 2014, Kevin Pope
Copyright (c) 2017, Kevin Pope, John Lewin
All rights reserved.
Redistribution and use in source and binary forms, with or without
@ -27,24 +27,30 @@ 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.Agg.UI;
using MatterHackers.Agg.VertexSource;
using MatterHackers.Localizations;
using MatterHackers.MatterControl.PrinterCommunication;
using System;
namespace MatterHackers.MatterControl.ActionBar
{
internal class TemperatureWidgetBase : GuiWidget
{
protected TextWidget currentTempIndicator;
private TextWidget currentTempIndicator;
protected TextWidget temperatureTypeName;
protected Button preheatButton;
protected TextImageButtonFactory whiteButtonFactory = new TextImageButtonFactory();
protected TextImageButtonFactory whiteButtonFactory = new TextImageButtonFactory()
{
FixedHeight = 18 * GuiWidget.DeviceScale,
fontSize = 7,
normalFillColor = RGBA_Bytes.White,
normalTextColor = RGBA_Bytes.DarkGray,
};
private RGBA_Bytes borderColor = new RGBA_Bytes(255, 255, 255);
private static RGBA_Bytes borderColor = new RGBA_Bytes(255, 255, 255);
private int borderWidth = 2;
public string IndicatorValue
@ -62,99 +68,73 @@ namespace MatterHackers.MatterControl.ActionBar
}
}
private EventHandler unregisterEvents;
public TemperatureWidgetBase(string textValue)
: base(52 * GuiWidget.DeviceScale, 52 * GuiWidget.DeviceScale)
{
whiteButtonFactory.FixedHeight = 18 * GuiWidget.DeviceScale;
whiteButtonFactory.fontSize = 7;
whiteButtonFactory.normalFillColor = RGBA_Bytes.White;
whiteButtonFactory.normalTextColor = RGBA_Bytes.DarkGray;
this.BackgroundColor = new RGBA_Bytes(255, 255, 255, 200);
this.Margin = new BorderDouble(0, 2) * GuiWidget.DeviceScale;
temperatureTypeName = new TextWidget("", pointSize: 8);
temperatureTypeName.AutoExpandBoundsToText = true;
temperatureTypeName.HAnchor = HAnchor.ParentCenter;
temperatureTypeName.VAnchor = VAnchor.ParentTop;
temperatureTypeName.Margin = new BorderDouble(0, 3);
temperatureTypeName.TextColor = ActiveTheme.Instance.SecondaryAccentColor;
temperatureTypeName.Visible = false;
temperatureTypeName = new TextWidget("", pointSize: 8)
{
AutoExpandBoundsToText = true,
HAnchor = HAnchor.ParentCenter,
VAnchor = VAnchor.ParentTop,
Margin = new BorderDouble(0, 3),
TextColor = ActiveTheme.Instance.SecondaryAccentColor,
Visible = false
};
this.AddChild(temperatureTypeName);
currentTempIndicator = new TextWidget(textValue, pointSize: 11);
currentTempIndicator.TextColor = ActiveTheme.Instance.PrimaryAccentColor;
currentTempIndicator.HAnchor = HAnchor.ParentCenter;
currentTempIndicator.VAnchor = VAnchor.ParentCenter;
currentTempIndicator.AutoExpandBoundsToText = true;
currentTempIndicator = new TextWidget(textValue, pointSize: 11)
{
TextColor = ActiveTheme.Instance.PrimaryAccentColor,
HAnchor = HAnchor.ParentCenter,
VAnchor = VAnchor.ParentCenter,
AutoExpandBoundsToText = true
};
this.AddChild(currentTempIndicator);
GuiWidget buttonContainer = new GuiWidget();
buttonContainer.HAnchor = Agg.UI.HAnchor.ParentLeftRight;
buttonContainer.Height = 18 * GuiWidget.DeviceScale;
var buttonContainer = new GuiWidget()
{
HAnchor = Agg.UI.HAnchor.ParentLeftRight,
Height = 18 * GuiWidget.DeviceScale
};
this.AddChild(buttonContainer);
preheatButton = whiteButtonFactory.Generate("Preheat".Localize().ToUpper());
preheatButton.Cursor = Cursors.Hand;
preheatButton.Visible = false;
preheatButton.Click += (s, e) => SetTargetTemperature();
buttonContainer.AddChild(preheatButton);
this.AddChild(temperatureTypeName);
this.AddChild(currentTempIndicator);
this.AddChild(buttonContainer);
this.MouseEnterBounds += onEnterBounds;
this.MouseLeaveBounds += onLeaveBounds;
this.preheatButton.Click += onPreheatButtonClick;
}
public override void OnClosed(EventArgs e)
{
if (unregisterEvents != null)
{
unregisterEvents(this, null);
}
base.OnClosed(e);
}
public void ThemeChanged(object sender, EventArgs e)
{
this.currentTempIndicator.TextColor = ActiveTheme.Instance.PrimaryAccentColor;
this.Invalidate();
}
private void onEnterBounds(Object sender, EventArgs e)
public override void OnMouseEnterBounds(MouseEventArgs mouseEvent)
{
temperatureTypeName.Visible = true;
if (PrinterConnectionAndCommunication.Instance.PrinterIsConnected && !PrinterConnectionAndCommunication.Instance.PrinterIsPrinting)
{
preheatButton.Visible = true;
}
base.OnMouseEnterBounds(mouseEvent);
}
private void onLeaveBounds(Object sender, EventArgs e)
public override void OnMouseLeaveBounds(MouseEventArgs mouseEvent)
{
temperatureTypeName.Visible = false;
preheatButton.Visible = false;
base.OnMouseLeaveBounds(mouseEvent);
}
public override void OnDraw(Graphics2D graphics2D)
{
base.OnDraw(graphics2D);
RectangleDouble Bounds = LocalBounds;
RoundedRect borderRect = new RoundedRect(this.LocalBounds, 0);
Stroke strokeRect = new Stroke(borderRect, borderWidth);
graphics2D.Render(strokeRect, borderColor);
var borderRect = new RoundedRect(this.LocalBounds, 0);
graphics2D.Render(new Stroke(borderRect, borderWidth), borderColor);
}
private void onPreheatButtonClick(object sender, EventArgs e)
{
SetTargetTemperature();
}
protected virtual void SetTargetTemperature()
{
}
protected virtual void SetTargetTemperature() { }
}
}

View file

@ -1,5 +1,5 @@
/*
Copyright (c) 2014, Kevin Pope
Copyright (c) 2017, Kevin Pope, John Lewin
All rights reserved.
Redistribution and use in source and binary forms, with or without
@ -27,40 +27,37 @@ of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
*/
using System;
using MatterHackers.Localizations;
using MatterHackers.MatterControl.PrinterCommunication;
using MatterHackers.MatterControl.SlicerConfiguration;
using System;
namespace MatterHackers.MatterControl.ActionBar
{
internal class TemperatureWidgetBed : TemperatureWidgetBase
{
private EventHandler unregisterEvents;
private string sliceSettingsNote = "Note: Slice Settings are applied before the print actually starts. Changes while printing will not effect the active print.".Localize();
private string waitingForBedToHeatMessage = "The bed is currently heating and its target temperature cannot be changed until it reaches {0}°C.\n\nYou can set the starting bed temperature in SETTINGS -> Filament -> Temperatures.\n\n{1}".Localize();
private string waitingForBedToHeatTitle = "Waiting For Bed To Heat".Localize();
//Not currently hooked up to anything
public TemperatureWidgetBed()
: base("150.3°")
{
temperatureTypeName.Text = "Print Bed";
setToCurrentTemperature();
DisplayCurrentTemperature();
ToolTipText = "Current bed temperature".Localize();
preheatButton.ToolTipText = "Preheat the Bed".Localize();
PrinterConnectionAndCommunication.Instance.BedTemperatureRead.RegisterEvent(onTemperatureRead, ref unregisterEvents);
PrinterConnectionAndCommunication.Instance.BedTemperatureRead.RegisterEvent((s, e) => DisplayCurrentTemperature(), ref unregisterEvents);
}
private EventHandler unregisterEvents;
public override void OnClosed(EventArgs e)
{
if (unregisterEvents != null)
{
unregisterEvents(this, null);
}
base.OnClosed(e);
}
private void setToCurrentTemperature()
private void DisplayCurrentTemperature()
{
string tempDirectionIndicator = "";
if (PrinterConnectionAndCommunication.Instance.TargetBedTemperature > 0)
{
if ((int)(PrinterConnectionAndCommunication.Instance.TargetBedTemperature + 0.5) < (int)(PrinterConnectionAndCommunication.Instance.ActualBedTemperature + 0.5))
@ -72,18 +69,10 @@ namespace MatterHackers.MatterControl.ActionBar
tempDirectionIndicator = "↑";
}
}
this.IndicatorValue = string.Format(" {0:0.#}°{1}", PrinterConnectionAndCommunication.Instance.ActualBedTemperature, tempDirectionIndicator);
}
private void onTemperatureRead(Object sender, EventArgs e)
{
setToCurrentTemperature();
}
private string sliceSettingsNote = "Note: Slice Settings are applied before the print actually starts. Changes while printing will not effect the active print.".Localize();
private string waitingForBedToHeatMessage = "The bed is currently heating and its target temperature cannot be changed until it reaches {0}°C.\n\nYou can set the starting bed temperature in SETTINGS -> Filament -> Temperatures.\n\n{1}".Localize();
private string waitingForBedToHeatTitle = "Waiting For Bed To Heat".Localize();
protected override void SetTargetTemperature()
{
double targetTemp = ActiveSliceSettings.Instance.GetValue<double>(SettingsKey.bed_temperature);
@ -103,5 +92,11 @@ namespace MatterHackers.MatterControl.ActionBar
}
}
}
public override void OnClosed(EventArgs e)
{
unregisterEvents?.Invoke(this, null);
base.OnClosed(e);
}
}
}

View file

@ -1,5 +1,5 @@
/*
Copyright (c) 2016, Kevin Pope, John Lewin
Copyright (c) 2017, Kevin Pope, John Lewin
All rights reserved.
Redistribution and use in source and binary forms, with or without
@ -46,21 +46,15 @@ namespace MatterHackers.MatterControl.ActionBar
: base("150.3°")
{
temperatureTypeName.Text = "Extruder";
setToCurrentTemperature();
DisplayCurrentTemperature();
ToolTipText = "Current extruder temperature".Localize();
preheatButton.ToolTipText = "Preheat the Extruder".Localize();
PrinterConnectionAndCommunication.Instance.ExtruderTemperatureRead.RegisterEvent((s, e) => setToCurrentTemperature(), ref unregisterEvents);
PrinterConnectionAndCommunication.Instance.ExtruderTemperatureRead.RegisterEvent((s, e) => DisplayCurrentTemperature(), ref unregisterEvents);
}
public override void OnClosed(EventArgs e)
{
unregisterEvents?.Invoke(this, null);
base.OnClosed(e);
}
private void setToCurrentTemperature()
private void DisplayCurrentTemperature()
{
string tempDirectionIndicator = "";
if (PrinterConnectionAndCommunication.Instance.GetTargetExtruderTemperature(extruderNumber - 1) > 0)
@ -96,5 +90,11 @@ namespace MatterHackers.MatterControl.ActionBar
}
}
}
public override void OnClosed(EventArgs e)
{
unregisterEvents?.Invoke(this, null);
base.OnClosed(e);
}
}
}