using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Input.Touch; using Penguloon.Levels; using Penguloon.Objects; using Penguloon.Scenes; using System.Collections.Generic; using System; namespace Penguloon.Controls { public enum State { Selected, Idle, } public class ObjectSelector : ControlBase { public State State { get; set; } public int SelectedObjectIndex { get; set; } = -1; public Map Map { get; set; } public List> Objects { get; set; } = new List>(); public ObjectSelector(Map map, SceneBase parentScene, Vector2 position, Vector2 size) : base(parentScene, position, size) { this.Map = map; this.BackgroundIdle = ContentManager.GetTexture("UI/objectSelectionBackground"); this.BackgroundPressed = ContentManager.GetTexture("UI/objectSelectionBackground"); this.BackgroundDisabled = ContentManager.GetTexture("UI/objectSelectionBackground"); this.OnClick += ObjectSelector_OnClick; LoadObjects(); } private void ObjectSelector_OnClick(object sender, ClickArgs e) { int rows = 4; if (Objects.Count > 8) { rows = (int)Math.Ceiling((double)Objects.Count / 2); } int width = (int)Size.X / 2; int height = (int)Size.Y / rows; int oddX = (int)Position.X; int evenX = (int)Position.X + width; Rectangle fingerRec = new Rectangle((int)e.ClickPosition.X, (int)e.ClickPosition.Y, 1, 1); for (int i = 0; i < Objects.Count; i++) { if (Objects[i].Item2 > Map.Level.Money) continue; int posX = ((i + 1) % 2 != 0) ? oddX : evenX; int posY = (i / 2) * height; Rectangle rec = new Rectangle(posX, (int)Position.Y + posY, width, height); if (fingerRec.Intersects(rec)) { if (SelectedObjectIndex != i) { SelectedObjectIndex = i; State = State.Selected; } else { SelectedObjectIndex = -1; State = State.Idle; } return; } } } private void LoadObjects() { Objects.Add(new Tuple(new PenguinObject(Map), 250)); Objects.Add(new Tuple(new GoldPenguinObject(Map), 360)); Objects.Add(new Tuple(new CannonObject(Map), 650)); } public override void Update(float deltaTime, TouchLocation[] touchLocations) { base.Update(deltaTime, touchLocations); } public override void Draw(float deltaTime) { base.Draw(deltaTime); int rows = 4; if(Objects.Count > 8) { rows = (int)Math.Ceiling((double)Objects.Count / 2); } int width = (int)Size.X / 2; int height = (int)Size.Y / rows; int oddX = (int)Position.X; int evenX = (int)Position.X + width; int padding = 15; for (int i = 0; i < Objects.Count; i++) { int posX = ((i+1) % 2 != 0) ? oddX : evenX; int posY = (i / 2) * height; if(State == State.Selected && i == SelectedObjectIndex) { ParentScene.Main.SpriteBatch.Draw(ContentManager.GetTexture("UI/red"), destinationRectangle: new Rectangle(posX, (int)Position.Y + posY, width, height)); } int widthToDraw = height - (padding * 2); ParentScene.Main.SpriteBatch.Draw(Objects[i].Item1.Texture, destinationRectangle: new Rectangle(posX + (width - widthToDraw) / 2, (int)Position.Y + posY + padding, widthToDraw, height - (padding * 2))); ParentScene.DrawText(ContentManager.GetFont("Fonts/GWENT/36"), Objects[i].Item2.ToString(), new Vector2(posX, (int)Position.Y + posY), new Vector2(width, height), TextAllignment.CenterBottom, Color.White, Color.Black, 2); } // Draw borders ParentScene.Main.SpriteBatch.Draw(ContentManager.GetTexture("UI/objectSelectionBorder"), destinationRectangle: new Rectangle(evenX - 2, (int)Position.Y, 4, (int)Size.Y)); for(int x = 1; x < rows; x++) { // Draw borders ParentScene.Main.SpriteBatch.Draw(ContentManager.GetTexture("UI/objectSelectionBorder"), destinationRectangle: new Rectangle(oddX, (int)Position.Y + (x * height), (int)Size.X, 4)); } } } }