Skip to content

Commit

Permalink
Address @aristurtle comments:
Browse files Browse the repository at this point in the history
 - Remove System.Drawing
 - Handle mouse up/down for clicks
 - Fix Exit on desktop targets
  • Loading branch information
Mindfulplays committed Jan 8, 2024
1 parent e5c24e9 commit d916436
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 84 deletions.
11 changes: 3 additions & 8 deletions Tests/Interactive/Common/TestGame.cs
Expand Up @@ -4,7 +4,6 @@

using System;
using System.Diagnostics;
using System.Drawing;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
Expand Down Expand Up @@ -70,10 +69,6 @@ protected void OnExit(Universe universe)
{
universe.Stop();
OnExiting(this, EventArgs.Empty);
#if IOS || ANDROID
#else
Exit();
#endif
}

/// <summary>
Expand Down Expand Up @@ -173,7 +168,7 @@ protected virtual void InitializeGui()
Text = $"Exit",
TextColor = Color.White
},
Location = PointF.Empty
Location = new Point(0, 0)
};

exitButton.Content.SizeToFit();
Expand All @@ -190,7 +185,7 @@ protected virtual void InitializeGui()
{
BackgroundColor = Color.Indigo,
Font = _font,
Location = new PointF(100, 60),
Location = new Point(100, 60),
Text = $"(Running on: {PlatformInfo.MonoGamePlatform} {PlatformInfo.GraphicsBackend})\n",
TextColor = Color.White
};
Expand All @@ -200,7 +195,7 @@ protected virtual void InitializeGui()
{
BackgroundColor = Color.Indigo,
Font = _font,
Location = new PointF(100, 0),
Location = new Point(100, 0),
Text = "",
TextColor = Color.White
};
Expand Down
20 changes: 9 additions & 11 deletions Tests/Interactive/Common/TestUI/Button.cs
Expand Up @@ -4,8 +4,6 @@

using System;
using Microsoft.Xna.Framework;
using System.Drawing;
using Color = System.Drawing.Color;

namespace MonoGame.InteractiveTests.TestUI
{
Expand Down Expand Up @@ -57,24 +55,24 @@ public override void LayoutSubviews()
view.LayoutIfNeeded();

var frame = view.Frame;
frame.X = Math.Max(frame.X, Padding.Left);
frame.Y = Math.Max(frame.Y, Padding.Top);
frame.X = Math.Max(frame.X, (int)Padding.Left);
frame.Y = Math.Max(frame.Y, (int)Padding.Top);
view.Frame = frame;
}
}

public override System.Drawing.SizeF SizeThatFits(SizeF size)
public override Point SizeThatFits(Point size)
{
var maxContentSize = size;
if (size != Size.Empty)
if (size != Point.Zero)
{
size.Width -= Padding.Horizontal;
size.Height -= Padding.Vertical;
size.X -= (int)Padding.Horizontal;
size.Y -= (int)Padding.Vertical;
}

var fitSize = Content.SizeThatFits(maxContentSize);
fitSize.Width += Padding.Horizontal;
fitSize.Height += Padding.Vertical;
fitSize.X += (int)Padding.Horizontal;
fitSize.Y += (int)Padding.Vertical;
return fitSize;
}

Expand All @@ -87,7 +85,7 @@ protected virtual void OnTapped(EventArgs e)
handler(this, e);
}

public override bool HandleGestureSample(PointF point, GameTime gameTime)
public override bool HandleGestureSample(Point point, GameTime gameTime)
{
OnTapped(EventArgs.Empty);
return true;
Expand Down
7 changes: 2 additions & 5 deletions Tests/Interactive/Common/TestUI/Label.cs
Expand Up @@ -4,8 +4,6 @@

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Drawing;

using Color = Microsoft.Xna.Framework.Color;

namespace MonoGame.InteractiveTests.TestUI
Expand All @@ -24,13 +22,13 @@ public Label()
public Color TextColor { get; set; }
public PaddingF Padding { get; set; }

public override SizeF SizeThatFits(SizeF size)
public override Point SizeThatFits(Point size)
{
if (Font == null || string.IsNullOrWhiteSpace(Text))
return Frame.Size;

var sizeVector = Font.MeasureString(Text);
return new SizeF(sizeVector.X + Padding.Horizontal, sizeVector.Y + Padding.Vertical);
return new Point((int)(sizeVector.X + Padding.Horizontal), (int)(sizeVector.Y + Padding.Vertical));
}

protected override void DrawForeground(DrawContext context, GameTime gameTime)
Expand All @@ -43,4 +41,3 @@ protected override void DrawForeground(DrawContext context, GameTime gameTime)
}
}
}

16 changes: 9 additions & 7 deletions Tests/Interactive/Common/TestUI/Universe.cs
Expand Up @@ -3,7 +3,6 @@
// file 'LICENSE.txt', which is part of this source code package.

using System;
using System.Drawing;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
Expand All @@ -29,6 +28,7 @@ public Universe(ContentManager content)
}

private readonly ContentManager _content;
private bool _wasPressed;
public ContentManager Content { get { return _content; } }

public bool AutoHandleInput { get; set; }
Expand All @@ -38,7 +38,7 @@ public void Add(View view)
_views.Add(view);
}

public bool HandleGestureSample(PointF position, GameTime gameTime)
public bool HandleGestureSample(Point position, GameTime gameTime)
{
if (!_isActive) return false;
bool handled = false;
Expand All @@ -59,23 +59,25 @@ public void Update(GameTime gameTime)
if (!_isActive) return;
if (AutoHandleInput)
{
#if IOS || ANDROID
if (TouchPanel.GetCapabilities().IsConnected)
{
while (_isActive && TouchPanel.IsGestureAvailable)
{
var gestureSample = TouchPanel.ReadGesture();
HandleGestureSample(new(gestureSample.Position.X, gestureSample.Position.Y), gameTime);
HandleGestureSample(new((int)gestureSample.Position.X, (int)gestureSample.Position.Y),
gameTime);
}
}
else
{
#else
var state = Mouse.GetState();
var position = state.Position;
if (state.LeftButton == ButtonState.Pressed)
if (state.LeftButton == ButtonState.Released && _wasPressed)
{
HandleGestureSample(new(position.X, position.Y), gameTime);
}
}
_wasPressed = state.LeftButton == ButtonState.Pressed;
#endif
}
}

Expand Down
25 changes: 13 additions & 12 deletions Tests/Interactive/Common/TestUI/View.cs
Expand Up @@ -5,10 +5,11 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Color = Microsoft.Xna.Framework.Color;
using Point = Microsoft.Xna.Framework.Point;
using Rectangle = Microsoft.Xna.Framework.Rectangle;

namespace MonoGame.InteractiveTests.TestUI
{
Expand All @@ -30,9 +31,9 @@ public View()

public Color BackgroundColor { get; set; }

private RectangleF _frame;
private Rectangle _frame;

public RectangleF Frame
public Rectangle Frame
{
get { return _frame; }
set
Expand All @@ -43,7 +44,7 @@ public RectangleF Frame

public bool IsVisible { get; set; }

public PointF Location
public Point Location
{
get { return _frame.Location; }
set
Expand Down Expand Up @@ -79,7 +80,7 @@ public void Add(View subview)
SetNeedsLayout();
}

public IEnumerable<View> HitTest(PointF p)
public IEnumerable<View> HitTest(Point p)
{
foreach (var view in _subviews.HitTest(p))
yield return view;
Expand All @@ -88,10 +89,10 @@ public IEnumerable<View> HitTest(PointF p)
yield return this;
}

public bool HitTestSelf(PointF p)
public bool HitTestSelf(Point p)
{
var rect = Frame;
rect.Location = PointF.Empty;
rect.Location = Point.Zero;

return rect.Contains(p);
}
Expand All @@ -109,12 +110,12 @@ public virtual void LayoutSubviews()
{
}

protected virtual RectangleF SetFrameCore(RectangleF frame)
protected virtual Rectangle SetFrameCore(Rectangle frame)
{
return frame;
}

public virtual bool HandleGestureSample(PointF point, GameTime gameTime)
public virtual bool HandleGestureSample(Point point, GameTime gameTime)
{
return false;
}
Expand All @@ -133,7 +134,7 @@ public void SetNeedsLayout()
_needsLayout = true;
}

public virtual SizeF SizeThatFits(SizeF size)
public virtual Point SizeThatFits(Point size)
{
return Frame.Size;
}
Expand All @@ -144,12 +145,12 @@ public void SizeToFit()
// View's Origin (which doesn't exist yet).

if (Superview == null)
Frame = new RectangleF(Frame.Location, SizeThatFits(SizeF.Empty));
Frame = new Rectangle(Frame.Location, SizeThatFits(new Point(0, 0)));
else
// TODO: Calculate the available size based on
// this View's location and Origin and
// the Superview Frame.
Frame = new RectangleF(Frame.Location, SizeThatFits(Superview.Frame.Size));
Frame = new Rectangle(Frame.Location, SizeThatFits(Superview.Frame.Size));
}

public virtual void Update(GameTime gameTime)
Expand Down
5 changes: 2 additions & 3 deletions Tests/Interactive/Common/TestUI/ViewCollection.cs
Expand Up @@ -5,7 +5,6 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using Microsoft.Xna.Framework;

namespace MonoGame.InteractiveTests.TestUI
Expand Down Expand Up @@ -35,7 +34,7 @@ public Universe Universe
}
}

public IEnumerable<View> HitTest(PointF p)
public IEnumerable<View> HitTest(Point p)
{
foreach (var view in Items)
{
Expand All @@ -48,7 +47,7 @@ public IEnumerable<View> HitTest(PointF p)
}
}

public IEnumerable<View> HitTest(RectangleF rect)
public IEnumerable<View> HitTest(Rectangle rect)
{
throw new NotImplementedException();
}
Expand Down
19 changes: 9 additions & 10 deletions Tests/Interactive/TestRunners/DesktopGL/Program.cs
@@ -1,11 +1,4 @@
using System;
using System.Linq;
using System.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using MonoGame.InteractiveTests;
using MonoGame.InteractiveTests.TestUI;

namespace MonoGame.InteractiveTests
{
Expand All @@ -24,9 +17,15 @@ static void Main(string[] args)
if (filteredTests.Count > 0)
{
var test = filteredTests[0];
Console.WriteLine($"--Running {test.Name}");
test.Create().Run();
Console.WriteLine($"--Finished {test.Name}");
var testGame = test.Create() as TestGame;
if (testGame != null)
{
Console.WriteLine($"--Running {test.Name}");
testGame.Exiting += (o, e) => { testGame.Exit(); };
testGame.Run();
Console.WriteLine($"--Finished {test.Name}");
}

// We currently support running exactly one test per run.
// This is due to MonoGame limitation of creating a single SDL thread/Window.
}
Expand Down
6 changes: 2 additions & 4 deletions Tests/Interactive/TestRunners/iOS/RootViewController.cs
Expand Up @@ -4,12 +4,11 @@

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using CoreGraphics;
using Foundation;
using UIKit;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace MonoGame.InteractiveTests.iOS
{
Expand Down Expand Up @@ -39,8 +38,7 @@ public override void LoadView()
{
View = new UIView();

var rect = new RectangleF(PointF.Empty, new SizeF((float)View.Frame.Size.Width,
(float)View.Frame.Size.Height));
var rect = new CGRect(0, 0, (int)View.Frame.Size.Width, (int)View.Frame.Size.Height);
_tableView = new UITableView(rect);
_tableView.AutoresizingMask =
UIViewAutoresizing.FlexibleHeight |
Expand Down
Expand Up @@ -2,12 +2,8 @@
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using MonoGame.InteractiveTests.TestUI;
using System.Drawing;
using Color = Microsoft.Xna.Framework.Color;
using GD = MonoGame.InteractiveTests.GameDebug;

Expand Down
4 changes: 0 additions & 4 deletions Tests/Interactive/Tests/RenderTargetTest/RenderTargetTest.cs
Expand Up @@ -2,12 +2,8 @@
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using MonoGame.InteractiveTests.TestUI;
using System.Drawing;
using Color = Microsoft.Xna.Framework.Color;
using GD = MonoGame.InteractiveTests.GameDebug;

Expand Down

0 comments on commit d916436

Please sign in to comment.