From fd6fa4e5cebbe3edb65d50c78dcc8a97ce98ce64 Mon Sep 17 00:00:00 2001 From: aldrikboy Date: Mon, 11 Dec 2017 22:02:13 +0100 Subject: First commit --- Penguloon/Controls/ControlBase.cs | 134 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 Penguloon/Controls/ControlBase.cs (limited to 'Penguloon/Controls/ControlBase.cs') 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 OnClick; + public event EventHandler OnMove; + + /// + /// Base constructor. + /// + /// Active Main object. + /// Control position. + /// Control size. + protected ControlBase(SceneBase parentScene, Vector2 position, Vector2 size) + { + this.ParentScene = parentScene; + this.Position = position; + this.Size = size; + } + + /// + /// Draw controls. + /// + /// Delta time. + 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); + } + + /// + /// Update control. + /// + /// Delta time. + /// Finger touch points. + 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 -- cgit v1.2.3-70-g09d2