Extract template rendering to reusable component

- Add conditional logic to render vertical/horizontal templates
- Add vertical and horizontal templates to printing page
This commit is contained in:
John Lewin 2019-02-17 20:30:19 -08:00 committed by jlewin
parent f64c4a33a9
commit dea002ab73
4 changed files with 207 additions and 88 deletions

View file

@ -42,7 +42,10 @@ namespace MatterHackers.MatterControl
private static int glyphSize = 8;
private bool mouseInBounds;
private bool vertical;
private ThemeConfig theme;
private IVertexSource glyph = null;
static CalibrationLine()
{
@ -50,17 +53,38 @@ namespace MatterHackers.MatterControl
CalibrationLine.CreateGlyphs(glyphCenter);
}
public CalibrationLine(ThemeConfig theme)
public CalibrationLine(FlowDirection direction, int glyphIndex, ThemeConfig theme)
{
if (direction == FlowDirection.LeftToRight)
{
this.Width = 8;
this.HAnchor = HAnchor.Absolute;
this.VAnchor = VAnchor.Stretch;
}
else
{
this.Height = 8;
this.HAnchor = HAnchor.Stretch;
this.VAnchor = VAnchor.Absolute;
}
vertical = direction == FlowDirection.LeftToRight;
if (Glyphs.TryGetValue(glyphIndex, out IVertexSource glyph))
{
if (!vertical)
{
glyph = new VertexSourceApplyTransform(glyph, Affine.NewRotation(MathHelper.DegreesToRadians(90)));
}
this.glyph = glyph;
}
this.theme = theme;
}
public int GlyphIndex { get; set; } = -1;
public bool IsNegative { get; internal set; }
public bool Vertical { get; set; } = true;
public override void OnMouseEnterBounds(MouseEventArgs mouseEvent)
{
mouseInBounds = true;
@ -87,49 +111,38 @@ namespace MatterHackers.MatterControl
public override void OnDraw(Graphics2D graphics2D)
{
if (this.Vertical)
var centerX = this.LocalBounds.XCenter + .5;
var centerY = this.LocalBounds.YCenter - .5;
var start = new Vector2(centerX, (glyph == null) ? 20 : 9);
var end = new Vector2(centerX, this.LocalBounds.Height);
if (!vertical)
{
int centerX = (int)this.LocalBounds.XCenter;
start = new Vector2(0, centerY);
end = new Vector2(this.LocalBounds.Width - ((glyph == null) ? 20 : 9), centerY);
}
if (this.GlyphIndex == -1)
{
// Draw primary line
graphics2D.Line(
new Vector2(centerX + .5, (this.GlyphIndex == -1) ? 20 : 9 /*+ .5*/),
new Vector2(centerX + .5, this.LocalBounds.Height /*+ .5*/),
theme.TextColor,
1);
}
else
{
// Draw primary line
graphics2D.Line(
new Vector2(centerX, (this.GlyphIndex == -1) ? 20 : 9 /*+ .5*/),
new Vector2(centerX, this.LocalBounds.Height /*+ .5*/),
theme.TextColor,
2);
}
graphics2D.Line(start, end, theme.TextColor, 1);
// Draw line end
if (this.GlyphIndex != -1
&& Glyphs.TryGetValue(this.GlyphIndex, out IVertexSource vertexSource))
{
graphics2D.Render(
vertexSource,
new Vector2((int)(this.LocalBounds.XCenter), 11),
theme.TextColor);
}
// Draw line end
if (glyph != null)
{
graphics2D.Render(
glyph,
vertical ? new Vector2(this.LocalBounds.XCenter, 11) : new Vector2(this.Width - 11, this.LocalBounds.YCenter),
theme.TextColor);
}
// Draw negative adornment below glyphs
if (this.GlyphIndex != -1
&& this.IsNegative)
{
graphics2D.Line(
new Vector2(this.LocalBounds.XCenter, 5),
new Vector2(this.LocalBounds.XCenter, 0),
theme.TextColor,
1);
}
// Draw negative adornment below glyphs
if (glyph != null
&& this.IsNegative)
{
graphics2D.Line(
vertical ? new Vector2(centerX, 0) : new Vector2(0, centerY),
vertical ? new Vector2(centerX, 5) : new Vector2(this.Width - 5, centerY),
theme.TextColor,
1);
}
base.OnDraw(graphics2D);

View file

@ -30,6 +30,7 @@ either expressed or implied, of the FreeBSD Project.
using System;
using System.Threading;
using System.Threading.Tasks;
using MatterHackers.Agg;
using MatterHackers.Agg.UI;
using MatterHackers.Localizations;
@ -38,6 +39,8 @@ namespace MatterHackers.MatterControl
public class NozzleOffsetCalibrationPrintPage : WizardPage
{
private NozzleOffsetTemplatePrinter templatePrinter;
private NozzleOffsetTemplateWidget xOffsetWidget;
private NozzleOffsetTemplateWidget yOffsetWidget;
public NozzleOffsetCalibrationPrintPage(ISetupWizard setupWizard, PrinterConfig printer)
: base(setupWizard)
@ -46,12 +49,19 @@ namespace MatterHackers.MatterControl
this.HeaderText = "Nozzle Offset Calibration".Localize() + ":";
this.Name = "Nozzle Offset Calibration Wizard";
this.ContentRow.AddChild(new TextWidget("Printing Calibration Guide".Localize(), pointSize: theme.DefaultFontSize, textColor: theme.TextColor));
this.ContentRow.AddChild(new TextWidget("Printing Guide...".Localize(), pointSize: theme.DefaultFontSize, textColor: theme.TextColor));
templatePrinter = new NozzleOffsetTemplatePrinter(printer);
contentRow.AddChild(new TextWidget("Printing Calibration Guide".Localize(), pointSize: theme.DefaultFontSize, textColor: theme.TextColor));
contentRow.AddChild(new TextWidget("Printing Guide...".Localize(), pointSize: theme.DefaultFontSize, textColor: theme.TextColor));
contentRow.AddChild(xOffsetWidget = new NozzleOffsetTemplateWidget(templatePrinter.ActiveOffsets, FlowDirection.LeftToRight, theme));
contentRow.AddChild(yOffsetWidget = new NozzleOffsetTemplateWidget(templatePrinter.ActiveOffsets, FlowDirection.TopToBottom, theme)
{
Margin = new BorderDouble(top: 15)
});
this.NextButton.Enabled = false;
}

View file

@ -30,7 +30,6 @@ either expressed or implied, of the FreeBSD Project.
using MatterHackers.Agg;
using MatterHackers.Agg.UI;
using MatterHackers.Localizations;
using MatterHackers.MatterControl.CustomWidgets;
namespace MatterHackers.MatterControl
{
@ -57,47 +56,8 @@ namespace MatterHackers.MatterControl
};
contentRow.AddChild(row);
for(var i = 0; i <= 40; i++)
{
var calibrationLine = new CalibrationLine(theme)
{
Width = 8,
Margin = 1,
HAnchor = HAnchor.Absolute,
VAnchor = VAnchor.Stretch,
GlyphIndex = (i % 5 == 0) ? i : -1,
IsNegative = i < 20,
OffsetIndex = i
};
calibrationLine.Click += (s, e) =>
{
activeOffset.Text = (activeOffsets[calibrationLine.OffsetIndex] * -1).ToString("0.####");
};
row.AddChild(calibrationLine);
// Add spacers to stretch to size
if (i < 40)
{
row.AddChild(new HorizontalSpacer());
}
}
contentRow.AddChild(activeOffset = new TextWidget("", pointSize: theme.DefaultFontSize, textColor: theme.TextColor));
row.AfterDraw += (s, e) =>
{
int strokeWidth = 3;
var rect = new RectangleDouble(0, 20, row.LocalBounds.Width, row.LocalBounds.Height);
rect.Inflate(-2);
var center = rect.Center;
e.Graphics2D.Rectangle(rect, theme.TextColor, strokeWidth);
e.Graphics2D.Line(rect.Left, center.Y, rect.Right, center.Y, theme.TextColor, strokeWidth);
};
var nextButton = theme.CreateDialogButton("Next".Localize());
nextButton.Name = "Begin calibration print";
nextButton.Click += (s, e) =>

View file

@ -0,0 +1,136 @@
/*
Copyright (c) 2019, Lars Brubaker, John Lewin
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.
*/
using System;
using MatterHackers.Agg;
using MatterHackers.Agg.UI;
using MatterHackers.MatterControl.CustomWidgets;
namespace MatterHackers.MatterControl
{
public class NozzleOffsetTemplateWidget : FlowLayoutWidget
{
private double _activeOffset;
private ThemeConfig theme;
public event EventHandler OffsetChanged;
public NozzleOffsetTemplateWidget(double[] activeOffsets, FlowDirection direction, ThemeConfig theme)
: base(direction)
{
this.theme = theme;
if (direction == FlowDirection.LeftToRight)
{
this.HAnchor = HAnchor.Stretch;
this.VAnchor = VAnchor.Absolute;
this.Height = 110;
}
else
{
this.HAnchor = HAnchor.Absolute;
this.VAnchor = VAnchor.Stretch;
this.Width = 110;
}
for (var i = 0; i <= 40; i++)
{
var calibrationLine = new CalibrationLine(direction, (i % 5 == 0) ? i : -1, theme)
{
//Margin = 1,
IsNegative = i < 20,
OffsetIndex = i,
};
calibrationLine.Click += (s, e) =>
{
this.ActiveOffset = activeOffsets[calibrationLine.OffsetIndex] * -1;
};
this.AddChild(calibrationLine);
// Add spacers to stretch to size
if (i < 40)
{
if (this.FlowDirection == FlowDirection.LeftToRight)
{
this.AddChild(new HorizontalSpacer());
}
else
{
this.AddChild(new VerticalSpacer());
}
}
}
}
public double ActiveOffset
{
get => _activeOffset;
set
{
if (value != _activeOffset)
{
this.OffsetChanged?.Invoke(this, null);
}
}
}
public override void OnDraw(Graphics2D graphics2D)
{
int strokeWidth = 3;
RectangleDouble rect;
if (this.FlowDirection == FlowDirection.LeftToRight)
{
rect = new RectangleDouble(0, 20, this.LocalBounds.Width, this.LocalBounds.Height);
}
else
{
rect = new RectangleDouble(0, 0, this.LocalBounds.Width - 20, this.LocalBounds.Height);
}
rect.Inflate(-2);
var center = rect.Center;
graphics2D.Rectangle(rect, theme.TextColor, strokeWidth);
if (this.FlowDirection == FlowDirection.LeftToRight)
{
graphics2D.Line(rect.Left, center.Y, rect.Right, center.Y, theme.TextColor, strokeWidth);
}
else
{
graphics2D.Line(rect.XCenter, rect.Top, rect.XCenter, rect.Bottom, theme.TextColor, strokeWidth);
}
base.OnDraw(graphics2D);
}
}
}