Jelajahi Sumber

Refactor for .net8

Jean-David Moisan 1 tahun lalu
induk
melakukan
78094307d2

+ 7 - 18
Source/Button.cs

@@ -1,12 +1,9 @@
 using System.Runtime.CompilerServices;
 using System.Runtime.CompilerServices;
-using Apos.Input;
 using Microsoft.Xna.Framework;
 using Microsoft.Xna.Framework;
 using MonoGame.Extended;
 using MonoGame.Extended;
 
 
 namespace Apos.Gui {
 namespace Apos.Gui {
-    public class Button : Component, IParent {
-        public Button(int id) : base(id) { }
-
+    public class Button(int id) : Component(id), IParent {
         public bool Clicked { get; set; } = false;
         public bool Clicked { get; set; } = false;
         public IComponent? Child { get; set; }
         public IComponent? Child { get; set; }
         public override bool IsFocusable { get; set; } = true;
         public override bool IsFocusable { get; set; } = true;
@@ -27,9 +24,7 @@ namespace Apos.Gui {
                 Clicked = false;
                 Clicked = false;
             }
             }
 
 
-            if (Child != null) {
-                Child.UpdateSetup(gameTime);
-            }
+            Child?.UpdateSetup(gameTime);
         }
         }
         public override void UpdateInput(GameTime gameTime) {
         public override void UpdateInput(GameTime gameTime) {
             if (Clip.Contains(GuiHelper.Mouse) && Default.MouseInteraction.Pressed()) {
             if (Clip.Contains(GuiHelper.Mouse) && Default.MouseInteraction.Pressed()) {
@@ -61,14 +56,10 @@ namespace Apos.Gui {
                 }
                 }
             }
             }
 
 
-            if (Child != null) {
-                Child.UpdateInput(gameTime);
-            }
+            Child?.UpdateInput(gameTime);
         }
         }
         public override void Update(GameTime gameTime) {
         public override void Update(GameTime gameTime) {
-            if (Child != null) {
-                Child.Update(gameTime);
-            }
+            Child?.Update(gameTime);
         }
         }
 
 
         public override void UpdatePrefSize(GameTime gameTime) {
         public override void UpdatePrefSize(GameTime gameTime) {
@@ -109,9 +100,7 @@ namespace Apos.Gui {
                 GuiHelper.SpriteBatch.DrawRectangle(Bounds, new Color(76, 76, 76), 2f);
                 GuiHelper.SpriteBatch.DrawRectangle(Bounds, new Color(76, 76, 76), 2f);
             }
             }
 
 
-            if (Child != null) {
-                Child.Draw(gameTime);
-            }
+            Child?.Draw(gameTime);
 
 
             GuiHelper.PopScissor();
             GuiHelper.PopScissor();
         }
         }
@@ -160,8 +149,8 @@ namespace Apos.Gui {
             id = GuiHelper.CurrentIMGUI.TryCreateId(id, isAbsoluteId, out IComponent c);
             id = GuiHelper.CurrentIMGUI.TryCreateId(id, isAbsoluteId, out IComponent c);
 
 
             Button a;
             Button a;
-            if (c is Button) {
-                a = (Button)c;
+            if (c is Button d) {
+                a = d;
             } else {
             } else {
                 a = new Button(id);
                 a = new Button(id);
             }
             }

+ 4 - 9
Source/Checkbox.cs

@@ -1,16 +1,11 @@
 using System.Runtime.CompilerServices;
 using System.Runtime.CompilerServices;
-using Apos.Input;
 using Microsoft.Xna.Framework;
 using Microsoft.Xna.Framework;
 using MonoGame.Extended;
 using MonoGame.Extended;
 
 
 namespace Apos.Gui {
 namespace Apos.Gui {
-    public class Checkbox : Component {
-        public Checkbox(int id, bool isChecked) : base(id) {
-            IsChecked = isChecked;
-        }
-
+    public class Checkbox(int id, bool isChecked) : Component(id) {
         public bool Clicked { get; set; } = false;
         public bool Clicked { get; set; } = false;
-        public bool IsChecked { get; set; } = false;
+        public bool IsChecked { get; set; } = isChecked;
         public override bool IsFocusable { get; set; } = true;
         public override bool IsFocusable { get; set; } = true;
         public override bool IsFocused {
         public override bool IsFocused {
             get => base.IsFocused;
             get => base.IsFocused;
@@ -101,8 +96,8 @@ namespace Apos.Gui {
             id = GuiHelper.CurrentIMGUI.TryCreateId(id, isAbsoluteId, out IComponent c);
             id = GuiHelper.CurrentIMGUI.TryCreateId(id, isAbsoluteId, out IComponent c);
 
 
             Checkbox a;
             Checkbox a;
-            if (c is Checkbox) {
-                a = (Checkbox)c;
+            if (c is Checkbox d) {
+                a = d;
                 if (a.IsFocused) {
                 if (a.IsFocused) {
                     isChecked = a.IsChecked;
                     isChecked = a.IsChecked;
                 } else {
                 } else {

+ 6 - 10
Source/Component.cs

@@ -7,13 +7,9 @@ namespace Apos.Gui {
     /// An empty component that doesn't really do anything.
     /// An empty component that doesn't really do anything.
     /// Used as a base class for other components.
     /// Used as a base class for other components.
     /// </summary>
     /// </summary>
-    public class Component : IComponent {
-        public Component(int id) {
-            Id = id;
-        }
-
+    public class Component(int id) : IComponent {
         public virtual uint LastPing { get; set; } = 0;
         public virtual uint LastPing { get; set; } = 0;
-        public virtual int Id { get; set; }
+        public virtual int Id { get; set; } = id;
         public virtual int Index { get; set; } = 0;
         public virtual int Index { get; set; } = 0;
 
 
         public virtual float X { get; set; } = 0;
         public virtual float X { get; set; } = 0;
@@ -67,28 +63,28 @@ namespace Apos.Gui {
         }
         }
 
 
         public virtual Vector2 XY {
         public virtual Vector2 XY {
-            get => new Vector2(X, Y);
+            get => new(X, Y);
             set {
             set {
                 X = value.X;
                 X = value.X;
                 Y = value.Y;
                 Y = value.Y;
             }
             }
         }
         }
         public virtual Vector2 Size {
         public virtual Vector2 Size {
-            get => new Vector2(Width, Height);
+            get => new(Width, Height);
             set {
             set {
                 Width = value.X;
                 Width = value.X;
                 Height = value.Y;
                 Height = value.Y;
             }
             }
         }
         }
         public virtual Vector2 PrefSize {
         public virtual Vector2 PrefSize {
-            get => new Vector2(PrefWidth, PrefHeight);
+            get => new(PrefWidth, PrefHeight);
             set {
             set {
                 PrefWidth = value.X;
                 PrefWidth = value.X;
                 PrefHeight = value.Y;
                 PrefHeight = value.Y;
             }
             }
         }
         }
         public virtual RectangleF Bounds {
         public virtual RectangleF Bounds {
-            get => new RectangleF(XY, Size);
+            get => new(XY, Size);
             set {
             set {
                 XY = value.Position;
                 XY = value.Position;
                 Size = value.Size;
                 Size = value.Size;

+ 11 - 27
Source/Dock.cs

@@ -1,37 +1,23 @@
 using System.Runtime.CompilerServices;
 using System.Runtime.CompilerServices;
-using Apos.Input;
 using Microsoft.Xna.Framework;
 using Microsoft.Xna.Framework;
 
 
 namespace Apos.Gui {
 namespace Apos.Gui {
-    public class Dock : Component, IParent {
-        public Dock(int id, float left, float top, float right, float bottom) : base(id) {
-            DockLeft = left;
-            DockTop = top;
-            DockRight = right;
-            DockBottom = bottom;
-        }
-
-        public float DockLeft { get; set; }
-        public float DockTop { get; set; }
-        public float DockRight { get; set; }
-        public float DockBottom { get; set; }
+    public class Dock(int id, float left, float top, float right, float bottom) : Component(id), IParent {
+        public float DockLeft { get; set; } = left;
+        public float DockTop { get; set; } = top;
+        public float DockRight { get; set; } = right;
+        public float DockBottom { get; set; } = bottom;
 
 
         public IComponent? Child { get; set; }
         public IComponent? Child { get; set; }
 
 
         public override void UpdateSetup(GameTime gameTime) {
         public override void UpdateSetup(GameTime gameTime) {
-            if (Child != null) {
-                Child.UpdateSetup(gameTime);
-            }
+            Child?.UpdateSetup(gameTime);
         }
         }
         public override void UpdateInput(GameTime gameTime) {
         public override void UpdateInput(GameTime gameTime) {
-            if (Child != null) {
-                Child.UpdateInput(gameTime);
-            }
+            Child?.UpdateInput(gameTime);
         }
         }
         public override void Update(GameTime gameTime) {
         public override void Update(GameTime gameTime) {
-            if (Child != null) {
-                Child.Update(gameTime);
-            }
+            Child?.Update(gameTime);
         }
         }
 
 
         public override void UpdatePrefSize(GameTime gameTime) {
         public override void UpdatePrefSize(GameTime gameTime) {
@@ -60,9 +46,7 @@ namespace Apos.Gui {
         }
         }
 
 
         public override void Draw(GameTime gameTime) {
         public override void Draw(GameTime gameTime) {
-            if (Child != null) {
-                Child.Draw(gameTime);
-            }
+            Child?.Draw(gameTime);
         }
         }
 
 
         public void Add(IComponent c) {
         public void Add(IComponent c) {
@@ -108,8 +92,8 @@ namespace Apos.Gui {
             id = GuiHelper.CurrentIMGUI.TryCreateId(id, isAbsoluteId, out IComponent c);
             id = GuiHelper.CurrentIMGUI.TryCreateId(id, isAbsoluteId, out IComponent c);
 
 
             Dock a;
             Dock a;
-            if (c is Dock) {
-                a = (Dock)c;
+            if (c is Dock d) {
+                a = d;
                 a.DockLeft = left;
                 a.DockLeft = left;
                 a.DockTop = top;
                 a.DockTop = top;
                 a.DockRight = right;
                 a.DockRight = right;

+ 3 - 6
Source/FloatingWindow.cs

@@ -1,12 +1,9 @@
 using System.Runtime.CompilerServices;
 using System.Runtime.CompilerServices;
-using Apos.Input;
 using Microsoft.Xna.Framework;
 using Microsoft.Xna.Framework;
 using MonoGame.Extended;
 using MonoGame.Extended;
 
 
 namespace Apos.Gui {
 namespace Apos.Gui {
-    public class FloatingWindow : Vertical {
-        public FloatingWindow(int id) : base(id) { }
-
+    public class FloatingWindow(int id) : Vertical(id) {
         public override bool IsFocusable { get; set; } = true;
         public override bool IsFocusable { get; set; } = true;
         public override bool IsFloatable { get; set; } = true;
         public override bool IsFloatable { get; set; } = true;
 
 
@@ -52,8 +49,8 @@ namespace Apos.Gui {
             id = GuiHelper.CurrentIMGUI.TryCreateId(id, isAbsoluteId, out IComponent c);
             id = GuiHelper.CurrentIMGUI.TryCreateId(id, isAbsoluteId, out IComponent c);
 
 
             FloatingWindow a;
             FloatingWindow a;
-            if (c is FloatingWindow) {
-                a = (FloatingWindow)c;
+            if (c is FloatingWindow d) {
+                a = d;
             } else {
             } else {
                 a = new FloatingWindow(id);
                 a = new FloatingWindow(id);
             }
             }

+ 9 - 11
Source/Horizontal.cs

@@ -8,9 +8,7 @@ using System;
 using Apos.Tweens;
 using Apos.Tweens;
 
 
 namespace Apos.Gui {
 namespace Apos.Gui {
-    public class Horizontal : Component, IParent {
-        public Horizontal(int id) : base(id) { }
-
+    public class Horizontal(int id) : Component(id), IParent {
         public float OffsetX {
         public float OffsetX {
             get => _offsetX;
             get => _offsetX;
             set {
             set {
@@ -27,14 +25,14 @@ namespace Apos.Gui {
         public float FullHeight { get; set; } = 100;
         public float FullHeight { get; set; } = 100;
 
 
         public Vector2 OffsetXY {
         public Vector2 OffsetXY {
-            get => new Vector2(OffsetX, OffsetY);
+            get => new(OffsetX, OffsetY);
             set {
             set {
                 OffsetX = value.X;
                 OffsetX = value.X;
                 OffsetY = value.Y;
                 OffsetY = value.Y;
             }
             }
         }
         }
         public Vector2 FullSize {
         public Vector2 FullSize {
-            get => new Vector2(FullWidth, FullHeight);
+            get => new(FullWidth, FullHeight);
             set {
             set {
                 FullWidth = value.X;
                 FullWidth = value.X;
                 FullHeight = value.Y;
                 FullHeight = value.Y;
@@ -232,11 +230,11 @@ namespace Apos.Gui {
         }
         }
 
 
         protected int _nextChildIndex = 0;
         protected int _nextChildIndex = 0;
-        protected List<IComponent> _children = new List<IComponent>();
-        protected List<IComponent> _childrenRenderOrder = new List<IComponent>();
+        protected List<IComponent> _children = [];
+        protected List<IComponent> _childrenRenderOrder = [];
 
 
-        protected FloatTween _offsetXTween = new FloatTween(0f, 0f, 0, Easing.ExpoOut);
-        protected FloatTween _offsetYTween = new FloatTween(0f, 0f, 0, Easing.ExpoOut);
+        protected FloatTween _offsetXTween = new(0f, 0f, 0, Easing.ExpoOut);
+        protected FloatTween _offsetYTween = new(0f, 0f, 0, Easing.ExpoOut);
 
 
         protected float _offsetX = 0;
         protected float _offsetX = 0;
         protected float _offsetY = 0;
         protected float _offsetY = 0;
@@ -245,8 +243,8 @@ namespace Apos.Gui {
             id = GuiHelper.CurrentIMGUI.TryCreateId(id, isAbsoluteId, out IComponent c);
             id = GuiHelper.CurrentIMGUI.TryCreateId(id, isAbsoluteId, out IComponent c);
 
 
             Horizontal a;
             Horizontal a;
-            if (c is Horizontal) {
-                a = (Horizontal)c;
+            if (c is Horizontal d) {
+                a = d;
             } else {
             } else {
                 a = new Horizontal(id);
                 a = new Horizontal(id);
             }
             }

+ 5 - 10
Source/Icon.cs

@@ -1,15 +1,10 @@
 using System.Runtime.CompilerServices;
 using System.Runtime.CompilerServices;
-using Apos.Input;
 using Microsoft.Xna.Framework;
 using Microsoft.Xna.Framework;
 using MonoGame.Extended.TextureAtlases;
 using MonoGame.Extended.TextureAtlases;
 
 
 namespace Apos.Gui {
 namespace Apos.Gui {
-    public class Icon : Component {
-        public Icon(int id, TextureRegion2D region) : base(id) {
-            Region = region;
-        }
-
-        public TextureRegion2D Region { get; set; }
+    public class Icon(int id, TextureRegion2D region) : Component(id) {
+        public TextureRegion2D Region { get; set; } = region;
 
 
         public override void UpdatePrefSize(GameTime gametime) {
         public override void UpdatePrefSize(GameTime gametime) {
             PrefWidth = Region.Width;
             PrefWidth = Region.Width;
@@ -24,7 +19,7 @@ namespace Apos.Gui {
             int halfHeight = (int)(Height / 2);
             int halfHeight = (int)(Height / 2);
             int iconHalfHeight = (int)(PrefHeight / 2);
             int iconHalfHeight = (int)(PrefHeight / 2);
 
 
-            Vector2 pos = new Vector2(Left + halfWidth - iconHalfWidth, Top + halfHeight - iconHalfHeight);
+            Vector2 pos = new(Left + halfWidth - iconHalfWidth, Top + halfHeight - iconHalfHeight);
 
 
             GuiHelper.SpriteBatch.Draw(Region, pos, Color.White);
             GuiHelper.SpriteBatch.Draw(Region, pos, Color.White);
 
 
@@ -35,8 +30,8 @@ namespace Apos.Gui {
             id = GuiHelper.CurrentIMGUI.TryCreateId(id, isAbsoluteId, out IComponent c);
             id = GuiHelper.CurrentIMGUI.TryCreateId(id, isAbsoluteId, out IComponent c);
 
 
             Icon a;
             Icon a;
-            if (c is Icon) {
-                a = (Icon)c;
+            if (c is Icon d) {
+                a = d;
                 a.Region = region;
                 a.Region = region;
             } else {
             } else {
                 a = new Icon(id, region);
                 a = new Icon(id, region);

+ 2 - 3
Source/Label.cs

@@ -1,5 +1,4 @@
 using System.Runtime.CompilerServices;
 using System.Runtime.CompilerServices;
-using Apos.Input;
 using FontStashSharp;
 using FontStashSharp;
 using Microsoft.Xna.Framework;
 using Microsoft.Xna.Framework;
 
 
@@ -65,8 +64,8 @@ namespace Apos.Gui {
             color ??= new Color(200, 200, 200);
             color ??= new Color(200, 200, 200);
 
 
             Label a;
             Label a;
-            if (c is Label) {
-                a = (Label)c;
+            if (c is Label d) {
+                a = d;
                 a.Text = text;
                 a.Text = text;
                 a.Color = color.Value;
                 a.Color = color.Value;
                 a.FontSize = fontSize;
                 a.FontSize = fontSize;

+ 3 - 6
Source/MenuPanel.cs

@@ -1,12 +1,9 @@
 using System.Runtime.CompilerServices;
 using System.Runtime.CompilerServices;
-using Apos.Input;
 using Apos.Tweens;
 using Apos.Tweens;
 using Microsoft.Xna.Framework;
 using Microsoft.Xna.Framework;
 
 
 namespace Apos.Gui {
 namespace Apos.Gui {
-    public class MenuPanel : Vertical {
-        public MenuPanel(int id) : base(id) { }
-
+    public class MenuPanel(int id) : Vertical(id) {
         public override void UpdatePrefSize(GameTime gameTime) {
         public override void UpdatePrefSize(GameTime gameTime) {
             base.UpdatePrefSize(gameTime);
             base.UpdatePrefSize(gameTime);
 
 
@@ -72,8 +69,8 @@ namespace Apos.Gui {
             id = GuiHelper.CurrentIMGUI.TryCreateId(id, isAbsoluteId, out IComponent c);
             id = GuiHelper.CurrentIMGUI.TryCreateId(id, isAbsoluteId, out IComponent c);
 
 
             MenuPanel a;
             MenuPanel a;
-            if (c is MenuPanel) {
-                a = (MenuPanel)c;
+            if (c is MenuPanel d) {
+                a = d;
             } else {
             } else {
                 a = new MenuPanel(id);
                 a = new MenuPanel(id);
             }
             }

+ 2 - 2
Source/Slider.cs

@@ -99,8 +99,8 @@ namespace Apos.Gui {
             id = GuiHelper.CurrentIMGUI.TryCreateId(id, isAbsoluteId, out IComponent c);
             id = GuiHelper.CurrentIMGUI.TryCreateId(id, isAbsoluteId, out IComponent c);
 
 
             Slider a;
             Slider a;
-            if (c is Slider) {
-                a = (Slider)c;
+            if (c is Slider d) {
+                a = d;
                 if (a.IsFocused) {
                 if (a.IsFocused) {
                     value = a.Value;
                     value = a.Value;
                 } else {
                 } else {

+ 4 - 4
Source/Textbox.cs

@@ -193,8 +193,8 @@ namespace Apos.Gui {
 
 
         protected int _inputDelay = 50;
         protected int _inputDelay = 50;
         protected int _inputInitialDelay = 400;
         protected int _inputInitialDelay = 400;
-        protected FloatTween _input = new FloatTween(0f, 1f, 50, Easing.Linear);
-        protected FloatTween _blink = new FloatTween(0f, 1f, 1500, Easing.Linear);
+        protected FloatTween _input = new(0f, 1f, 50, Easing.Linear);
+        protected FloatTween _blink = new(0f, 1f, 1500, Easing.Linear);
 
 
         protected bool _pressed = false;
         protected bool _pressed = false;
 
 
@@ -204,8 +204,8 @@ namespace Apos.Gui {
             color ??= new Color(200, 200, 200);
             color ??= new Color(200, 200, 200);
 
 
             Textbox a;
             Textbox a;
-            if (c is Textbox) {
-                a = (Textbox)c;
+            if (c is Textbox d) {
+                a = d;
                 if (a.IsFocused) {
                 if (a.IsFocused) {
                     text = a.Text;
                     text = a.Text;
                 } else {
                 } else {

+ 2 - 2
Source/Vertical.cs

@@ -244,8 +244,8 @@ namespace Apos.Gui {
             id = GuiHelper.CurrentIMGUI.TryCreateId(id, isAbsoluteId, out IComponent c);
             id = GuiHelper.CurrentIMGUI.TryCreateId(id, isAbsoluteId, out IComponent c);
 
 
             Vertical a;
             Vertical a;
-            if (c is Vertical) {
-                a = (Vertical)c;
+            if (c is Vertical d) {
+                a = d;
             } else {
             } else {
                 a = new Vertical(id);
                 a = new Vertical(id);
             }
             }