Skip to content

Commit

Permalink
Merge pull request #916 from WildernessLabs/feature/xpt2046
Browse files Browse the repository at this point in the history
Feature/xpt2046
  • Loading branch information
adrianstevens authored Mar 5, 2024
2 parents 5b2cdb9 + 0c37c90 commit 117c46c
Show file tree
Hide file tree
Showing 20 changed files with 537 additions and 124 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
namespace Meadow.Foundation.Graphics.MicroLayout;

public struct Coordinate2D
{
public int X { get; set; }
public int Y { get; set; }
}

/// <summary>
/// Represents a circle in the user interface.
/// </summary>
public class Circle : ThemedControl
{
private Color _foreColor;
private Color foreColor;
private Point center;
private int radius;

/// <summary>
/// Gets or sets a value indicating whether the Circle is filled with the foreground color.
Expand All @@ -25,7 +21,7 @@ public class Circle : ThemedControl
/// <param name="centerY">The Y coordinate of the circles's center.</param>
/// <param name="radius">The radius of the circle.</param>
public Circle(int centerX, int centerY, int radius)
: base(centerX - radius, centerY - radius, radius * 2, radius * 2)
: this(new Point(centerX, centerY), radius)
{
}

Expand All @@ -34,9 +30,11 @@ public Circle(int centerX, int centerY, int radius)
/// </summary>
/// <param name="center">The coordinate of the circles's center.</param>
/// <param name="radius">The radius of the circle.</param>
public Circle(Coordinate2D center, int radius)
public Circle(Point center, int radius)
: base(center.X - radius, center.Y - radius, radius * 2, radius * 2)
{
this.center = center;
this.radius = radius;
}

/// <summary>
Expand All @@ -56,23 +54,23 @@ public override void ApplyTheme(DisplayTheme theme)
/// </summary>
public Color ForeColor
{
get => _foreColor;
set => SetInvalidatingProperty(ref _foreColor, value);
get => foreColor;
set => SetInvalidatingProperty(ref foreColor, value);
}

/// <summary>
/// Gets or sets the foreground color of the Circle.
/// </summary>
public int Radius
{
get => Width / 2;
get => radius;
set
{
// keep centered
var coeff = (value > Radius) ? -1 : 1;
var offset = value - Radius;

Width = value * 2;
radius = value;
Left = center.X - radius;
Width = radius * 2;
Top = center.Y - radius;
Height = radius * 2;
}
}

Expand All @@ -84,10 +82,7 @@ protected override void OnDraw(MicroGraphics graphics)
{
if (ForeColor != Color.Transparent)
{
var radius = (Right - Left) / 2;
var centerX = Left + radius;
var centerY = Top + radius;
graphics.DrawCircle(centerX, centerY, radius, ForeColor, IsFilled);
graphics.DrawCircle(center.X, center.Y, radius, ForeColor, IsFilled);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
namespace Meadow.Foundation.Graphics.MicroLayout;

/// <summary>
/// Represents a circle in the user interface.
/// </summary>
public class Crosshair : ThemedControl
{
private Color _foreColor = Color.Black;
private int _lineWidth;

/// <summary>
/// Initializes a new instance of the <see cref="Crosshair"/> class with the specified dimensions and center point.
/// </summary>
/// <param name="centerX">The X coordinate of the crosshair's center.</param>
/// <param name="centerY">The Y coordinate of the crosshair's center.</param>
/// <param name="size">The with and height of the crosshair.</param>
/// <param name="linewidth">The line width of the crosshair</param>
public Crosshair(int centerX, int centerY, int size = 20, int linewidth = 3)
: base(centerX, centerY, size, size)
{
_lineWidth = linewidth;
}

/// <inheritdoc/>
public override void ApplyTheme(DisplayTheme theme)
{
if (theme != null)
{
if (theme.ForegroundColor != null) this.ForeColor = theme.ForegroundColor.Value;
}
}

/// <summary>
/// Gets or sets the foreground color of the Crosshair.
/// </summary>
public Color ForeColor
{
get => _foreColor;
set => SetInvalidatingProperty(ref _foreColor, value);
}

/// <inheritdoc/>
protected override void OnDraw(MicroGraphics graphics)
{
if (ForeColor != Color.Transparent)
{
// position is the center of the crosshair
graphics.DrawRectangle(Left - Width / 2, Top - _lineWidth / 2, Width, _lineWidth, ForeColor, true);
graphics.DrawRectangle(Left - _lineWidth / 2, Top - Height / 2, _lineWidth, Height, ForeColor, true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ public class DisplayScreen
{
private readonly IPixelDisplay _display;
private readonly MicroGraphics _graphics;
private readonly ITouchScreen? _touchScreen;
private bool _updateInProgress = false;

/// <summary>
/// Gets the Touchscreen associated with the display screen
/// </summary>
public ITouchScreen? TouchScreen { get; }

/// <summary>
/// Gets the collection of controls on the display screen.
/// </summary>
Expand Down Expand Up @@ -56,12 +60,12 @@ public DisplayScreen(IPixelDisplay physicalDisplay, RotationType rotation = Rota

_graphics.Rotation = rotation;

_touchScreen = touchScreen;
TouchScreen = touchScreen;

if (_touchScreen != null)
if (TouchScreen != null)
{
_touchScreen.TouchDown += _touchScreen_TouchDown;
_touchScreen.TouchUp += _touchScreen_TouchUp;
TouchScreen.TouchDown += _touchScreen_TouchDown;
TouchScreen.TouchUp += _touchScreen_TouchUp;
}

if (theme?.Font != null)
Expand All @@ -81,32 +85,39 @@ public DisplayScreen(IPixelDisplay physicalDisplay, RotationType rotation = Rota
}
}

private void _touchScreen_TouchUp(int x, int y)
private void _touchScreen_TouchUp(ITouchScreen source, TouchPoint point)
{
lock (Controls.SyncRoot)
if (Monitor.TryEnter(Controls.SyncRoot, 100))
{
foreach (var control in Controls)
try
{
if (control is IClickableControl c)
foreach (var control in Controls)
{
if (control.Contains(x, y))
if (control is IClickableControl c)
{
c.Pressed = false;
if (control.Contains(point.ScreenX, point.ScreenY))
{
c.Pressed = false;
}
}
}
}
finally
{
Monitor.Exit(Controls.SyncRoot);
}
}
}

private void _touchScreen_TouchDown(int x, int y)
private void _touchScreen_TouchDown(ITouchScreen source, TouchPoint point)
{
lock (Controls.SyncRoot)
{
foreach (var control in Controls)
{
if (control is IClickableControl c)
{
if (control.Contains(x, y))
if (control.Contains(point.ScreenX, point.ScreenY))
{
c.Pressed = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,4 @@
<ProjectReference Include="..\..\Driver\Graphics.MicroLayout.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="libmpsse.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net7.0-windows</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand All @@ -15,10 +15,4 @@
<ProjectReference Include="..\..\Driver\Graphics.MicroLayout.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="libmpsse.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Binary file not shown.
28 changes: 16 additions & 12 deletions Source/Meadow.Foundation.Peripherals/Displays.Gtk/Driver/Gtk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,14 @@ namespace Meadow.Foundation.Displays;
/// </summary>
public class GtkDisplay : IPixelDisplay, ITouchScreen
{
/// <summary>
/// Event fired when the display gets a mouse down
/// </summary>
/// <inheritdoc/>
public event Hardware.TouchEventHandler TouchDown = default!;
/// <summary>
/// Event fired when the display gets a mouse up
/// </summary>
/// <inheritdoc/>
public event Hardware.TouchEventHandler TouchUp = default!;
/// <summary>
/// Event fired when the display gets a mouse click
/// </summary>
/// <inheritdoc/>
public event Hardware.TouchEventHandler TouchClick = default!;
/// <inheritdoc/>
public event Hardware.TouchEventHandler TouchMoved = default!;

private Window _window = default!;
private IPixelBuffer _pixelBuffer = default!;
Expand All @@ -34,6 +30,9 @@ public class GtkDisplay : IPixelDisplay, ITouchScreen

private EventWaitHandle ShowComplete { get; } = new EventWaitHandle(true, EventResetMode.ManualReset);

/// <inheritdoc/>
public RotationType Rotation => RotationType.Normal;

/// <inheritdoc/>
public IPixelBuffer PixelBuffer => _pixelBuffer;

Expand All @@ -49,6 +48,9 @@ public class GtkDisplay : IPixelDisplay, ITouchScreen
/// <inheritdoc/>
public ColorMode SupportedColorModes => ColorMode.Format24bppRgb888 | ColorMode.Format16bppRgb565 | ColorMode.Format32bppRgba8888;

/// <inheritdoc/>
public bool IsTouched { get; private set; }

static GtkDisplay()
{
Application.Init();
Expand Down Expand Up @@ -108,15 +110,15 @@ private void Initialize(int width, int height, ColorMode mode)
private void RaiseTouchDown(double x, double y)
{
_leftButtonState = true;
TouchDown?.Invoke((int)x, (int)y);
TouchDown?.Invoke(this, TouchPoint.FromScreenData((int)x, (int)y, 0, (int)x, (int)y, 0));
}

private void RaiseTouchUp(double x, double y)
{
TouchUp?.Invoke((int)x, (int)y);
TouchUp?.Invoke(this, TouchPoint.FromScreenData((int)x, (int)y, 0, (int)x, (int)y, 0));
if (_leftButtonState)
{
TouchClick?.Invoke((int)x, (int)y);
TouchClick?.Invoke(this, TouchPoint.FromScreenData((int)x, (int)y, 0, (int)x, (int)y, 0));
}
_leftButtonState = false;

Expand All @@ -133,9 +135,11 @@ private void WindowWidgetEvent(object o, WidgetEventArgs args)
{
case Gdk.ModifierType.None:
RaiseTouchDown(b.X, b.Y);
IsTouched = true;
break;
case Gdk.ModifierType.Button1Mask:
RaiseTouchUp(b.X, b.Y);
IsTouched = false;
break;
}
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,20 @@ namespace Meadow.Foundation.Displays;
/// </summary>
public class WinFormsDisplay : Form, IPixelDisplay, ITouchScreen
{
/// <summary>
/// Event fired when the display gets a mouse down
/// </summary>
public event TouchEventHandler TouchDown = default!;
/// <summary>
/// Event fired when the display gets a mouse up
/// </summary>
public event TouchEventHandler TouchUp = default!;
/// <summary>
/// Event fired when the display gets a mouse click
/// </summary>
public event TouchEventHandler TouchClick = default!;
/// <inheritdoc/>
public event TouchEventHandler? TouchDown = default!;
/// <inheritdoc/>
public event TouchEventHandler? TouchUp = default!;
/// <inheritdoc/>
public event TouchEventHandler? TouchClick = default!;
/// <inheritdoc/>
public event TouchEventHandler? TouchMoved = default!;

private readonly WinFormsPixelBuffer _buffer;

/// <inheritdoc/>
public RotationType Rotation => RotationType.Normal;

/// <inheritdoc/>
public ColorMode ColorMode => PixelBuffer.ColorMode;

Expand All @@ -32,6 +31,9 @@ public class WinFormsDisplay : Form, IPixelDisplay, ITouchScreen
/// <inheritdoc/>
public ColorMode SupportedColorModes => ColorMode.Format24bppRgb888;

/// <inheritdoc/>
public bool IsTouched { get; private set; }

/// <summary>
/// Create a new WinFormsDisplay
/// </summary>
Expand Down Expand Up @@ -64,21 +66,23 @@ protected override void Dispose(bool disposing)
///<inheritdoc/>
protected override void OnMouseDown(MouseEventArgs e)
{
TouchDown?.Invoke(e.X, e.Y);
TouchDown?.Invoke(this, TouchPoint.FromScreenData(e.X, e.Y, 0, e.X, e.Y, 0));
IsTouched = true;
base.OnMouseDown(e);
}

///<inheritdoc/>
protected override void OnMouseUp(MouseEventArgs e)
{
TouchUp?.Invoke(e.X, e.Y);
TouchUp?.Invoke(this, TouchPoint.FromScreenData(e.X, e.Y, 0, e.X, e.Y, 0));
IsTouched = false;
base.OnMouseUp(e);
}

///<inheritdoc/>
protected override void OnClick(EventArgs e)
{
TouchClick?.Invoke(-1, -1);
TouchClick?.Invoke(this, TouchPoint.FromScreenData(-1, -1, 0, -1, -1, 0));
base.OnClick(e);
}

Expand Down
Loading

0 comments on commit 117c46c

Please sign in to comment.