Skip to content

Commit

Permalink
Merge pull request #861 from WildernessLabs/feature/progress-bar
Browse files Browse the repository at this point in the history
Feature/progress bar
  • Loading branch information
adrianstevens authored Dec 11, 2023
2 parents dc6978f + 8d9cf2c commit 4c081b4
Show file tree
Hide file tree
Showing 12 changed files with 238 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public class LineChart : ThemedControl
private int ChartAreaLeft { get; set; }
private int ChartAreaTop { get; set; }
private int ChartAreaBottom { get; set; }
private int ParentOffsetX => (Parent?.Left ?? 0);
private int ParentOffsetY => (Parent?.Top ?? 0);

/// <summary>
/// Creates a DisplayLineChart instance
Expand Down Expand Up @@ -158,25 +160,25 @@ private void DrawAxisLabels(MicroGraphics graphics)
if (XAxisYIntercept != YMinimumValue)
{
graphics.DrawText(
x: Left + DefaultMargin,
y: XAxisScaledPosition - (font.Height / 2), // centered on tick
x: Left + DefaultMargin + ParentOffsetX,
y: XAxisScaledPosition - (font.Height / 2) + +ParentOffsetY, // centered on tick
color: AxisLabelColor,
text: XAxisYIntercept.ToString("0.0"),
font: font);
}

// max label
graphics.DrawText(
x: Left + DefaultMargin,
y: ChartAreaTop + font.Height,
x: Left + DefaultMargin + ParentOffsetX,
y: ChartAreaTop + font.Height + ParentOffsetY,
color: AxisLabelColor,
text: YMaximumValue.ToString("0.0"),
font: font);

// min label
graphics.DrawText(
x: Left + DefaultMargin,
y: ChartAreaBottom - font.Height,
x: Left + DefaultMargin + ParentOffsetX,
y: ChartAreaBottom - font.Height + ParentOffsetY,
color: AxisLabelColor,
text: YMinimumValue.ToString("0.0"),
font: font);
Expand All @@ -203,8 +205,8 @@ private void DrawXAxis(MicroGraphics graphics, double minY, double maxY)
// for now it's a fixed line at the bottom
graphics.Stroke = DefaultAxisStroke;
graphics.DrawLine(
ChartAreaLeft,
XAxisScaledPosition,
ChartAreaLeft + ParentOffsetX,
XAxisScaledPosition + ParentOffsetY,
Right - DefaultMargin,
XAxisScaledPosition,
AxisColor);
Expand Down Expand Up @@ -242,8 +244,8 @@ private void DrawYAxis(MicroGraphics graphics)
// for now it's a fixed line at the left
graphics.Stroke = DefaultAxisStroke;
graphics.DrawLine(
ChartAreaLeft,
Top + DefaultMargin,
ChartAreaLeft + ParentOffsetX,
Top + DefaultMargin + ParentOffsetY,
ChartAreaLeft,
Bottom - DefaultMargin,
AxisColor);
Expand All @@ -261,8 +263,6 @@ private void DrawSeries(MicroGraphics graphics, LineChartSeries series)

graphics.Stroke = series.LineStroke;

//graphics.DrawRectangle(ChartAreaLeft + DefaultAxisStroke * 2 + DefaultMargin, ChartAreaTop - DefaultAxisStroke, ChartAreaWidth, ChartAreaHeight, Color.Red, true);

foreach (var point in series.Points)
{
var scaledX = ChartAreaLeft + DefaultAxisStroke * 2 + DefaultMargin + (int)(point.X / xRange * ChartAreaWidth);
Expand All @@ -277,8 +277,8 @@ private void DrawSeries(MicroGraphics graphics, LineChartSeries series)
else
{
graphics.DrawLine(
(int)lastPoint.X,
(int)lastPoint.Y,
(int)lastPoint.X + ParentOffsetX,
(int)lastPoint.Y + ParentOffsetY,
scaledX,
scaledY,
series.LineColor);
Expand All @@ -290,7 +290,7 @@ private void DrawSeries(MicroGraphics graphics, LineChartSeries series)

if (series.ShowPoints)
{
graphics.DrawCircle(scaledX, scaledY, series.PointSize, series.PointColor, true);
graphics.DrawCircle(scaledX + ParentOffsetX, scaledY + ParentOffsetY, series.PointSize, series.PointColor, true);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected override void OnDraw(MicroGraphics graphics)
{
if (ForeColor != Color.Transparent)
{
graphics.DrawRectangle(Left, Top, Width, Height, ForeColor, Filled);
graphics.DrawRectangle(Left + (Parent?.Left ?? 0), Top + (Parent?.Top ?? 0), Width, Height, ForeColor, Filled);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,25 +137,31 @@ protected override void OnDraw(MicroGraphics graphics)
{
graphics.Stroke = ButtonDepth;

var parentOffsetX = Parent?.Left ?? 0;
var parentOffsetY = Parent?.Top ?? 0;

if (Pressed)
{
graphics.DrawRectangle(Left, Top, Width, Height, PressedColor, true);
graphics.DrawRectangle(Left + parentOffsetX, Top + parentOffsetY, Width, Height, PressedColor, true);

graphics.DrawHorizontalLine(Left, Top, Width, ShadowColor);
graphics.DrawVerticalLine(Left, Top, Height, ShadowColor);
graphics.DrawHorizontalLine(Left + parentOffsetX, Top + parentOffsetY, Width, ShadowColor);
graphics.DrawVerticalLine(Left + parentOffsetX, Top + parentOffsetY, Height, ShadowColor);

graphics.DrawHorizontalLine(Left, Bottom, Width, HighlightColor);
graphics.DrawVerticalLine(Right, Top, Height, HighlightColor);
graphics.DrawHorizontalLine(Left + parentOffsetX, Bottom + parentOffsetY, Width, HighlightColor);
graphics.DrawVerticalLine(Right + parentOffsetX, Top + parentOffsetY, Height, HighlightColor);

if (Image != null) // image always wins over text
{
graphics.DrawImage(Left + ((this.Width - Image.Width) / 2) + ButtonDepth, Top + ((this.Height - Image.Height) / 2) + ButtonDepth, Image);
graphics.DrawImage(
Left + ((this.Width - Image.Width) / 2) + ButtonDepth + parentOffsetX,
Top + ((this.Height - Image.Height) / 2) + ButtonDepth + parentOffsetY,
Image);
}
else if (!string.IsNullOrEmpty(Text))
{
graphics.DrawText(
Left + ButtonDepth + (this.Width / 2),
Top + ButtonDepth + (this.Height / 2),
Left + ButtonDepth + (this.Width / 2) + parentOffsetX,
Top + ButtonDepth + (this.Height / 2) + parentOffsetY,
Text,
TextColor,
scaleFactor: ScaleFactor,
Expand All @@ -166,26 +172,26 @@ protected override void OnDraw(MicroGraphics graphics)
}
else
{
graphics.DrawRectangle(Left, Top, Width, Height, ForeColor, true);
graphics.DrawRectangle(Left + parentOffsetX, Top + parentOffsetY, Width, Height, ForeColor, true);

graphics.DrawHorizontalLine(Left, Top, Width, HighlightColor);
graphics.DrawVerticalLine(Left, Top, Height, HighlightColor);
graphics.DrawHorizontalLine(Left + parentOffsetX, Top + parentOffsetY, Width, HighlightColor);
graphics.DrawVerticalLine(Left + parentOffsetX, Top + parentOffsetY, Height, HighlightColor);

graphics.DrawHorizontalLine(Left, Bottom, Width, ShadowColor);
graphics.DrawVerticalLine(Right, Top, Height, ShadowColor);
graphics.DrawHorizontalLine(Left + parentOffsetX, Bottom + parentOffsetY, Width, ShadowColor);
graphics.DrawVerticalLine(Right + parentOffsetX, Top + parentOffsetY, Height, ShadowColor);

if (Image != null) // image always wins over text
{
graphics.DrawImage(
Left + ((this.Width - Image.Width) / 2),
Top + ((this.Height - Image.Height) / 2),
Left + ((this.Width - Image.Width) / 2) + parentOffsetX,
Top + ((this.Height - Image.Height) / 2) + parentOffsetY,
Image);
}
else if (!string.IsNullOrEmpty(Text))
{
graphics.DrawText(
Left + (this.Width / 2),
Top + (this.Height / 2),
Left + (this.Width / 2) + parentOffsetX,
Top + (this.Height / 2) + parentOffsetY,
Text,
TextColor,
scaleFactor: ScaleFactor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public abstract class Control : IControl
/// </summary>
public object? Context { get; set; }

/// <inheritdoc/>
public IControl? Parent { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="Control"/> class with the specified dimensions.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ protected override void OnDraw(MicroGraphics graphics)
{
if (BackColor != Color.Transparent)
{
graphics.DrawRectangle(Left, Top, Width, Height, BackColor, true);
graphics.DrawRectangle(Left + (Parent?.Left ?? 0), Top + (Parent?.Top ?? 0), Width, Height, BackColor, true);
}

int x, y;
Expand Down Expand Up @@ -147,6 +147,9 @@ protected override void OnDraw(MicroGraphics graphics)
break;
}

x += Parent?.Left ?? 0;
y += Parent?.Top ?? 0;

graphics.DrawText(
Left + x,
Top + y,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,13 @@ protected override void OnDraw(MicroGraphics graphics)
{
if (BackColor != Color.Transparent)
{
graphics.DrawRectangle(Left, Top, Width, Height, BackColor, true);
graphics.DrawRectangle(
Left + (Parent?.Left ?? 0),
Top + (Parent?.Top ?? 0),
Width,
Height,
BackColor,
true);
}

int x, y;
Expand Down Expand Up @@ -113,6 +119,9 @@ protected override void OnDraw(MicroGraphics graphics)
y = Top;
}

x += Parent?.Left ?? 0;
y += Parent?.Top ?? 0;

graphics.DrawImage(x, y, Image);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
using System;

namespace Meadow.Foundation.Graphics.MicroLayout;

/// <summary>
/// Represents a progress bar control in the user interface.
/// </summary>
public class ProgressBar : ThemedControl
{
private Color _valueColor = Color.DarkBlue;
private Color _borderColor = Color.Transparent;
private Color _backColor = Color.DarkGray;
private int _value = 0;
private int _minimum = 0;
private int _maximum = 100;

/// <summary>
/// Represents a rectangular, horizontal progress bar in the user interface.
/// </summary>
///
public ProgressBar(int left, int top, int width, int height)
: base(left, top, width, height)
{
}

/// <inheritdoc/>
public override void ApplyTheme(DisplayTheme theme)
{
if (theme != null)
{
if (theme.ForegroundColor != null) this.ValueColor = theme.ForegroundColor.Value;
if (theme.BackgroundColor != null) this.BackColor = theme.BackgroundColor.Value;
if (theme.HighlightColor != null) this.BorderColor = theme.HighlightColor.Value;
}
}

/// <summary>
/// Gets or set the Value for the ProgressBar
/// </summary>
public int Value
{
get => _value;
set
{
if (value > Maximum || value < Minimum) throw new ArgumentOutOfRangeException();
SetInvalidatingProperty(ref _value, value);
}
}

/// <summary>
/// Gets or set the minimum Value for the ProgressBar
/// </summary>
public int Minimum
{
get => _minimum;
set
{
if (value >= Maximum) throw new ArgumentOutOfRangeException();
SetInvalidatingProperty(ref _minimum, value);
}
}

/// <summary>
/// Gets or set the maximum Value for the ProgressBar
/// </summary>
public int Maximum
{
get => _maximum;
set
{
if (value <= Minimum) throw new ArgumentOutOfRangeException();
SetInvalidatingProperty(ref _maximum, value);
}
}

/// <summary>
/// Gets or sets the foreground (value) color to fill on the ProgressBar
/// </summary>
public Color ValueColor
{
get => _valueColor;
set => SetInvalidatingProperty(ref _valueColor, value);
}

/// <summary>
/// Gets or sets the background (non-value) color to fill on the ProgressBar
/// </summary>
public Color BackColor
{
get => _backColor;
set => SetInvalidatingProperty(ref _backColor, value);
}

/// <summary>
/// Gets or sets the border color to around the ProgressBar
/// </summary>
public Color BorderColor
{
get => _borderColor;
set => SetInvalidatingProperty(ref _borderColor, value);
}

/// <inheritdoc/>
protected override void OnDraw(MicroGraphics graphics)
{
var valueWidth = (int)((Value / (float)(Maximum - Minimum)) * Width);
var emptyWidth = Width - valueWidth;

if (ValueColor != Color.Transparent)
{
graphics.DrawRectangle(
Left + (Parent?.Left ?? 0),
Top + (Parent?.Top ?? 0),
valueWidth,
Height,
ValueColor,
true);
}
if (BackColor != Color.Transparent)
{
Resolver.Log.Info($"Drawing back color of {BackColor}");

graphics.DrawRectangle(
Left + valueWidth + (Parent?.Left ?? 0),
Top + (Parent?.Top ?? 0),
emptyWidth,
Height,
BackColor,
true);
}
if (BorderColor != Color.Transparent)
{
graphics.DrawRectangle(
Left + (Parent?.Left ?? 0),
Top + (Parent?.Top ?? 0),
Width,
Height,
BorderColor,
false);
}
}
}
Loading

0 comments on commit 4c081b4

Please sign in to comment.