summaryrefslogtreecommitdiff
path: root/Penguloon/Controls/ControlBase.cs
diff options
context:
space:
mode:
authoraldrikboy <aldrikboy@gmail.com>2017-12-11 22:02:13 +0100
committeraldrikboy <aldrikboy@gmail.com>2017-12-11 22:02:13 +0100
commitfd6fa4e5cebbe3edb65d50c78dcc8a97ce98ce64 (patch)
tree8950f6b9023e0b47e22e1cd4869ab76de0803f4c /Penguloon/Controls/ControlBase.cs
parentc4c0f3c887d627b6432551e96009c7aeecd4cdd8 (diff)
First commit
Diffstat (limited to 'Penguloon/Controls/ControlBase.cs')
-rw-r--r--Penguloon/Controls/ControlBase.cs134
1 files changed, 134 insertions, 0 deletions
diff --git a/Penguloon/Controls/ControlBase.cs b/Penguloon/Controls/ControlBase.cs
new file mode 100644
index 0000000..dea0bd7
--- /dev/null
+++ b/Penguloon/Controls/ControlBase.cs
@@ -0,0 +1,134 @@
+using System;
+using Microsoft.Xna.Framework;
+using Microsoft.Xna.Framework.Graphics;
+using Microsoft.Xna.Framework.Input.Touch;
+using Penguloon.Scenes;
+
+namespace Penguloon.Controls
+{
+ public abstract class ControlBase
+ {
+ public SceneBase ParentScene { get; set; }
+
+ public ControlState ControlState { get; set; } = ControlState.Idle;
+
+ public string Text { get; set; }
+ public SpriteFont Font { get; set; }
+ public Vector2 Position { get; set; }
+ public Vector2 Size { get; set; }
+ public Color ForeColor { get; set; }
+ public Color BorderColor { get; set; }
+ public int BorderWidth { get; set; }
+
+ public Texture2D BackgroundIdle { get; set; }
+ public Texture2D BackgroundPressed { get; set; }
+ public Texture2D BackgroundDisabled { get; set; }
+
+ public event EventHandler<ClickArgs> OnClick;
+ public event EventHandler<MoveArgs> OnMove;
+
+ /// <summary>
+ /// Base constructor.
+ /// </summary>
+ /// <param name="main">Active Main object.</param>
+ /// <param name="position">Control position.</param>
+ /// <param name="size">Control size.</param>
+ protected ControlBase(SceneBase parentScene, Vector2 position, Vector2 size)
+ {
+ this.ParentScene = parentScene;
+ this.Position = position;
+ this.Size = size;
+ }
+
+ /// <summary>
+ /// Draw controls.
+ /// </summary>
+ /// <param name="deltaTime">Delta time.</param>
+ public virtual void Draw(float deltaTime)
+ {
+ switch (ControlState)
+ {
+ case ControlState.Idle:
+ if(BackgroundIdle != null)
+ ParentScene.Main.SpriteBatch.Draw(BackgroundIdle,
+ destinationRectangle: new Rectangle(Position.ToPoint(), Size.ToPoint())); break;
+ case ControlState.Pressed:
+ if (BackgroundPressed != null)
+ ParentScene.Main.SpriteBatch.Draw(BackgroundPressed,
+ destinationRectangle: new Rectangle(Position.ToPoint(), Size.ToPoint())); break;
+ case ControlState.Disabled:
+ if (BackgroundDisabled != null)
+ ParentScene.Main.SpriteBatch.Draw(BackgroundDisabled,
+ destinationRectangle: new Rectangle(Position.ToPoint(), Size.ToPoint())); break;
+ }
+
+ if (Font == null) return;
+ if (string.IsNullOrWhiteSpace(Text)) return;
+
+ ParentScene.DrawText(Font, Text, Position, Size, TextAllignment.CenterMiddle, ForeColor, BorderColor, BorderWidth);
+ }
+
+ /// <summary>
+ /// Update control.
+ /// </summary>
+ /// <param name="deltaTime">Delta time.</param>
+ /// <param name="touchLocations">Finger touch points.</param>
+ public virtual void Update(float deltaTime, TouchLocation[] touchLocations)
+ {
+ if (ControlState == ControlState.Disabled) return;
+
+ Rectangle controlRec = new Rectangle(Position.ToPoint(), Size.ToPoint());
+
+ for(int i = 0; i < touchLocations.Length; i++)
+ {
+ Rectangle touchRec = new Rectangle(touchLocations[i].Position.ToPoint(), new Point(1, 1));
+
+ if (touchRec.Intersects(controlRec))
+ {
+ if (touchLocations[i].State == TouchLocationState.Released)
+ {
+ this.ControlState = ControlState.Idle;
+ OnClick?.Invoke(this, new ClickArgs(touchLocations[i].Position));
+ }
+
+ if (touchLocations[i].State == TouchLocationState.Pressed)
+ {
+ this.ControlState = ControlState.Pressed;
+ }
+
+ if (touchLocations[i].State == TouchLocationState.Moved)
+ {
+ touchLocations[i].TryGetPreviousLocation(out TouchLocation touch);
+ OnMove?.Invoke(this, new MoveArgs(touchLocations[i].Position, touch.Position));
+ }
+
+ return;
+ }
+ else
+ this.ControlState = ControlState.Idle;
+ }
+ }
+ }
+
+ public class ClickArgs : EventArgs
+ {
+ public Vector2 ClickPosition { get; set; }
+
+ public ClickArgs(Vector2 clickPosition)
+ {
+ this.ClickPosition = clickPosition;
+ }
+ }
+
+ public class MoveArgs : EventArgs
+ {
+ public Vector2 TouchPosition { get; set; }
+ public Vector2 PrevTouchPosition { get; set; }
+
+ public MoveArgs(Vector2 touchPosition, Vector2 prevTouchPosition)
+ {
+ TouchPosition = touchPosition;
+ PrevTouchPosition = prevTouchPosition;
+ }
+ }
+} \ No newline at end of file