From d91643615886c878be78375a9ccd3301b4e9e7ad Mon Sep 17 00:00:00 2001 From: Peru S Date: Mon, 8 Jan 2024 15:50:22 -0800 Subject: [PATCH] Address @Aristurtle comments: - Remove System.Drawing - Handle mouse up/down for clicks - Fix Exit on desktop targets --- Tests/Interactive/Common/TestGame.cs | 11 +++----- Tests/Interactive/Common/TestUI/Button.cs | 20 +++++++-------- Tests/Interactive/Common/TestUI/Label.cs | 7 ++---- Tests/Interactive/Common/TestUI/Universe.cs | 16 ++++++------ Tests/Interactive/Common/TestUI/View.cs | 25 ++++++++++--------- .../Common/TestUI/ViewCollection.cs | 5 ++-- .../TestRunners/DesktopGL/Program.cs | 19 +++++++------- .../TestRunners/iOS/RootViewController.cs | 6 ++--- .../MultipleRenderTargetsTest.cs | 4 --- .../RenderTargetTest/RenderTargetTest.cs | 4 --- .../Tests/TestUI/TestUITestGame.cs | 9 +++---- .../Tests/TextureTest/TextureBasicTest.cs | 3 --- .../Tests/TextureTest/TextureUpdateTest.cs | 4 --- .../Tests/TouchTest/TouchTestGame.cs | 4 +-- 14 files changed, 53 insertions(+), 84 deletions(-) diff --git a/Tests/Interactive/Common/TestGame.cs b/Tests/Interactive/Common/TestGame.cs index 2e2c6cff556..0d0a8702a1b 100644 --- a/Tests/Interactive/Common/TestGame.cs +++ b/Tests/Interactive/Common/TestGame.cs @@ -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; @@ -70,10 +69,6 @@ protected void OnExit(Universe universe) { universe.Stop(); OnExiting(this, EventArgs.Empty); -#if IOS || ANDROID -#else - Exit(); -#endif } /// @@ -173,7 +168,7 @@ protected virtual void InitializeGui() Text = $"Exit", TextColor = Color.White }, - Location = PointF.Empty + Location = new Point(0, 0) }; exitButton.Content.SizeToFit(); @@ -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 }; @@ -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 }; diff --git a/Tests/Interactive/Common/TestUI/Button.cs b/Tests/Interactive/Common/TestUI/Button.cs index 9c4a40c517c..1e9d25a87bf 100644 --- a/Tests/Interactive/Common/TestUI/Button.cs +++ b/Tests/Interactive/Common/TestUI/Button.cs @@ -4,8 +4,6 @@ using System; using Microsoft.Xna.Framework; -using System.Drawing; -using Color = System.Drawing.Color; namespace MonoGame.InteractiveTests.TestUI { @@ -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; } @@ -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; diff --git a/Tests/Interactive/Common/TestUI/Label.cs b/Tests/Interactive/Common/TestUI/Label.cs index f8d0b0f938a..51f0486e716 100644 --- a/Tests/Interactive/Common/TestUI/Label.cs +++ b/Tests/Interactive/Common/TestUI/Label.cs @@ -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 @@ -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) @@ -43,4 +41,3 @@ protected override void DrawForeground(DrawContext context, GameTime gameTime) } } } - diff --git a/Tests/Interactive/Common/TestUI/Universe.cs b/Tests/Interactive/Common/TestUI/Universe.cs index 814bfbbb4d7..6163811079d 100644 --- a/Tests/Interactive/Common/TestUI/Universe.cs +++ b/Tests/Interactive/Common/TestUI/Universe.cs @@ -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; @@ -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; } @@ -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; @@ -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 } } diff --git a/Tests/Interactive/Common/TestUI/View.cs b/Tests/Interactive/Common/TestUI/View.cs index 5a75b3f1ea4..e01fe91eafc 100644 --- a/Tests/Interactive/Common/TestUI/View.cs +++ b/Tests/Interactive/Common/TestUI/View.cs @@ -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 { @@ -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 @@ -43,7 +44,7 @@ public RectangleF Frame public bool IsVisible { get; set; } - public PointF Location + public Point Location { get { return _frame.Location; } set @@ -79,7 +80,7 @@ public void Add(View subview) SetNeedsLayout(); } - public IEnumerable HitTest(PointF p) + public IEnumerable HitTest(Point p) { foreach (var view in _subviews.HitTest(p)) yield return view; @@ -88,10 +89,10 @@ public IEnumerable 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); } @@ -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; } @@ -133,7 +134,7 @@ public void SetNeedsLayout() _needsLayout = true; } - public virtual SizeF SizeThatFits(SizeF size) + public virtual Point SizeThatFits(Point size) { return Frame.Size; } @@ -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) diff --git a/Tests/Interactive/Common/TestUI/ViewCollection.cs b/Tests/Interactive/Common/TestUI/ViewCollection.cs index 64a8422a940..db61f1792c9 100644 --- a/Tests/Interactive/Common/TestUI/ViewCollection.cs +++ b/Tests/Interactive/Common/TestUI/ViewCollection.cs @@ -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 @@ -35,7 +34,7 @@ public Universe Universe } } - public IEnumerable HitTest(PointF p) + public IEnumerable HitTest(Point p) { foreach (var view in Items) { @@ -48,7 +47,7 @@ public IEnumerable HitTest(PointF p) } } - public IEnumerable HitTest(RectangleF rect) + public IEnumerable HitTest(Rectangle rect) { throw new NotImplementedException(); } diff --git a/Tests/Interactive/TestRunners/DesktopGL/Program.cs b/Tests/Interactive/TestRunners/DesktopGL/Program.cs index 0b110cbd9de..81b1238c945 100644 --- a/Tests/Interactive/TestRunners/DesktopGL/Program.cs +++ b/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 { @@ -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. } diff --git a/Tests/Interactive/TestRunners/iOS/RootViewController.cs b/Tests/Interactive/TestRunners/iOS/RootViewController.cs index fedcc750405..e14ce1c0fc3 100644 --- a/Tests/Interactive/TestRunners/iOS/RootViewController.cs +++ b/Tests/Interactive/TestRunners/iOS/RootViewController.cs @@ -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 { @@ -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 | diff --git a/Tests/Interactive/Tests/RenderTargetTest/MultipleRenderTargetsTest.cs b/Tests/Interactive/Tests/RenderTargetTest/MultipleRenderTargetsTest.cs index 4ad19fbccc9..490872b48e1 100644 --- a/Tests/Interactive/Tests/RenderTargetTest/MultipleRenderTargetsTest.cs +++ b/Tests/Interactive/Tests/RenderTargetTest/MultipleRenderTargetsTest.cs @@ -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; diff --git a/Tests/Interactive/Tests/RenderTargetTest/RenderTargetTest.cs b/Tests/Interactive/Tests/RenderTargetTest/RenderTargetTest.cs index ec4c70f39e4..aed3c6fa836 100644 --- a/Tests/Interactive/Tests/RenderTargetTest/RenderTargetTest.cs +++ b/Tests/Interactive/Tests/RenderTargetTest/RenderTargetTest.cs @@ -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; diff --git a/Tests/Interactive/Tests/TestUI/TestUITestGame.cs b/Tests/Interactive/Tests/TestUI/TestUITestGame.cs index 74bdfd131c8..7e85eddaadf 100644 --- a/Tests/Interactive/Tests/TestUI/TestUITestGame.cs +++ b/Tests/Interactive/Tests/TestUI/TestUITestGame.cs @@ -2,10 +2,7 @@ // This file is subject to the terms and conditions defined in // file 'LICENSE.txt', which is part of this source code package. -using System.Drawing; using Microsoft.Xna.Framework; -using Microsoft.Xna.Framework.Graphics; -using Microsoft.Xna.Framework.Input.Touch; using Color = Microsoft.Xna.Framework.Color; namespace MonoGame.InteractiveTests.TestUI @@ -25,7 +22,7 @@ protected override void InitializeGui() { BackgroundColor = Color.Indigo, Font = _font, - Location = new PointF(20, 200), + Location = new Point(20, 200), Text = "fantastic", TextColor = Color.White }; @@ -41,7 +38,7 @@ protected override void InitializeGui() Text = "Button 1", TextColor = Color.White }, - Location = new PointF(label.Frame.Left, 60) + Location = new Point(label.Frame.Left, 60) }; button1.Content.SizeToFit(); @@ -58,7 +55,7 @@ protected override void InitializeGui() Text = "Button 2", TextColor = Color.White }, - Location = new PointF(button1.Frame.Left, button1.Frame.Bottom) + Location = new Point(button1.Frame.Left, button1.Frame.Bottom) }; button2.Content.SizeToFit(); diff --git a/Tests/Interactive/Tests/TextureTest/TextureBasicTest.cs b/Tests/Interactive/Tests/TextureTest/TextureBasicTest.cs index 2f95f0fe01b..bbd2433de3e 100644 --- a/Tests/Interactive/Tests/TextureTest/TextureBasicTest.cs +++ b/Tests/Interactive/Tests/TextureTest/TextureBasicTest.cs @@ -5,9 +5,6 @@ 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; diff --git a/Tests/Interactive/Tests/TextureTest/TextureUpdateTest.cs b/Tests/Interactive/Tests/TextureTest/TextureUpdateTest.cs index b657aa4a134..7e48dfd3d62 100644 --- a/Tests/Interactive/Tests/TextureTest/TextureUpdateTest.cs +++ b/Tests/Interactive/Tests/TextureTest/TextureUpdateTest.cs @@ -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; diff --git a/Tests/Interactive/Tests/TouchTest/TouchTestGame.cs b/Tests/Interactive/Tests/TouchTest/TouchTestGame.cs index d35afc32408..4420a31c279 100644 --- a/Tests/Interactive/Tests/TouchTest/TouchTestGame.cs +++ b/Tests/Interactive/Tests/TouchTest/TouchTestGame.cs @@ -3,10 +3,8 @@ // file 'LICENSE.txt', which is part of this source code package. using Microsoft.Xna.Framework; -using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input.Touch; using MonoGame.InteractiveTests.TestUI; -using System.Drawing; using MonoGame.Framework.Utilities; using Color = Microsoft.Xna.Framework.Color; @@ -46,7 +44,7 @@ protected override void InitializeGui() base.InitializeGui(); _labelTouchInfo = new Label { - Frame = new RectangleF(20, 150, 320, 20), + Frame = new Rectangle(20, 150, 320, 20), Font = _font, TextColor = Color.White };