diff options
| author | aldrikboy <aldrikboy@gmail.com> | 2017-12-11 22:02:13 +0100 |
|---|---|---|
| committer | aldrikboy <aldrikboy@gmail.com> | 2017-12-11 22:02:13 +0100 |
| commit | fd6fa4e5cebbe3edb65d50c78dcc8a97ce98ce64 (patch) | |
| tree | 8950f6b9023e0b47e22e1cd4869ab76de0803f4c /Penguloon/Controls | |
| parent | c4c0f3c887d627b6432551e96009c7aeecd4cdd8 (diff) | |
First commit
Diffstat (limited to 'Penguloon/Controls')
| -rw-r--r-- | Penguloon/Controls/Button.cs | 28 | ||||
| -rw-r--r-- | Penguloon/Controls/ControlBase.cs | 134 | ||||
| -rw-r--r-- | Penguloon/Controls/LevelSelector.cs | 101 |
3 files changed, 263 insertions, 0 deletions
diff --git a/Penguloon/Controls/Button.cs b/Penguloon/Controls/Button.cs new file mode 100644 index 0000000..121321c --- /dev/null +++ b/Penguloon/Controls/Button.cs @@ -0,0 +1,28 @@ +using Microsoft.Xna.Framework; +using Penguloon.Scenes; + +namespace Penguloon.Controls +{ + class Button : ControlBase + { + public Button(SceneBase parentScene, Vector2 position, Vector2 size, string text) : base(parentScene, position, size) + { + this.BackgroundIdle = ContentManager.GetTexture("UI/btnIdle"); + this.BackgroundPressed = ContentManager.GetTexture("UI/btnPressed"); + this.BackgroundDisabled = ContentManager.GetTexture("UI/btnDisabled"); + this.Text = text; + + this.ForeColor = Color.Gray; + this.BorderColor = Color.Gray; + this.BorderWidth = 0; + this.Font = ContentManager.GetFont(StaticUIValues.MenuFont); + + OnClick += Button_OnClick; + } + + private void Button_OnClick(object sender, ClickArgs e) + { + SoundManager.PlayClickSound(); + } + } +}
\ No newline at end of file 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 diff --git a/Penguloon/Controls/LevelSelector.cs b/Penguloon/Controls/LevelSelector.cs new file mode 100644 index 0000000..1e26fcc --- /dev/null +++ b/Penguloon/Controls/LevelSelector.cs @@ -0,0 +1,101 @@ +using Penguloon.Scenes; +using Microsoft.Xna.Framework; +using System; +using System.Collections.Generic; +using Penguloon.Levels; +using Microsoft.Xna.Framework.Input.Touch; + +namespace Penguloon.Controls +{ + class LevelSelector : ControlBase + { + public int PanelWidth { get; set; } + public const int PanelMarginX = 30; + public const int MaxPanels = 3; + + int selectedMap = 0; + + public List<LevelBase> Levels { get; set; } = new List<LevelBase>(); + + public Rectangle Panel1 { get; set; } + public Rectangle Panel2 { get; set; } + public Rectangle Panel3 { get; set; } + + public LevelSelector(SceneBase parentScene, Vector2 position, Vector2 size) : base(parentScene, position, size) + { + this.BackgroundIdle = ContentManager.GetTexture("UI/btnIdle"); + this.BackgroundPressed = ContentManager.GetTexture("UI/btnIdle"); + this.BackgroundDisabled = ContentManager.GetTexture("UI/btnIdle"); + + PanelWidth = (int)(StaticUIValues.ScreenViewport.X - ((MaxPanels + 1) * PanelMarginX)) / MaxPanels; + + int panel1posX = PanelMarginX + (PanelMarginX * 0) + (PanelWidth * 0); + int panel2posX = PanelMarginX + (PanelMarginX * 1) + (PanelWidth * 1); + int panel3posX = PanelMarginX + (PanelMarginX * 2) + (PanelWidth * 2); + + int sidePanelMarginY = 25; + + Panel1 = new Rectangle(panel1posX, (int)position.Y + (sidePanelMarginY / 2), PanelWidth, (int)size.Y - sidePanelMarginY); + Panel2 = new Rectangle(panel2posX, (int)position.Y, PanelWidth, (int)size.Y); + Panel3 = new Rectangle(panel3posX, (int)position.Y + (sidePanelMarginY / 2), PanelWidth, (int)size.Y - sidePanelMarginY); + + this.OnClick += LevelSelector_OnClick; + + CreateLevels(); + } + + private void LevelSelector_OnClick(object sender, ClickArgs e) + { + Rectangle fingerRec = new Rectangle(e.ClickPosition.ToPoint(), new Point(1, 1)); + + if (Panel1.Intersects(fingerRec) && selectedMap > 0) + { + selectedMap--; + } + + if (Panel2.Intersects(fingerRec)) + { + SceneManager.GameScene = new GameScene(ParentScene.Main, Levels[selectedMap]); + SceneManager.SelectedScene = SelectedScene.Ingame; + } + + if (Panel3.Intersects(fingerRec) && selectedMap < Levels.Count - 1) + { + selectedMap++; + } + } + + private void CreateLevels() + { + Levels.Add(new IceLevel()); + //Levels.Add(new IceLevel()); + //Levels.Add(new IceLevel()); + } + + public override void Draw(float deltaTime) + { + // We dont need to draw text or a background + //base.Draw(deltaTime); + + DrawPanels(); + } + + private void DrawPanels() + { + if (selectedMap - 1 >= 0) + { + ParentScene.Main.SpriteBatch.Draw(Levels[selectedMap - 1].SplashArt, + destinationRectangle: Panel1); + } + + ParentScene.Main.SpriteBatch.Draw(Levels[selectedMap].SplashArt, + destinationRectangle: Panel2); + + if (selectedMap + 1 < Levels.Count) + { + ParentScene.Main.SpriteBatch.Draw(Levels[selectedMap + 1].SplashArt, + destinationRectangle: Panel3); + } + } + } +}
\ No newline at end of file |
