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; using Microsoft.Xna.Framework.Graphics; namespace Penguloon.Controls { public enum State { Selected, Idle, } public class ObjectSelector : ControlBase { public State State { get; set; } = State.Idle; public int SelectedObjectIndex { get; set; } = -1; public Map Map { get; set; } public List> Objects { get; set; } = new List>(); public bool DrawInfoPanel { get; set; } = false; public int TouchedObjectIndex { get; set; } 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; this.OnFingerDown += ObjectSelector_OnFingerDown; LoadObjects(); } private void ObjectSelector_OnFingerDown(object sender, ClickArgs e) { DrawInfoPanel = true; 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++) { 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)) { TouchedObjectIndex = i; return; } } } 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++) { 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)) { TouchedObjectIndex = i; if (Objects[i].Item2 > Map.Level.Money) continue; if (SelectedObjectIndex != i) { SoundManager.PlayClickSound2(); SelectedObjectIndex = i; State = State.Selected; } else { SoundManager.PlayClickSound2(); 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)); Objects.Add(new Tuple(new HealthGeneratorObject(Map), 800)); Objects.Add(new Tuple(new MortarObject(Map), 150)); } public override void Update(float deltaTime, TouchLocation[] touchLocations) { DrawInfoPanel = false; 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/lightred"), destinationRectangle: new Rectangle(posX, (int)Position.Y + posY, width, height)); } //int widthToDraw = (height / Objects[i].Item1.TileSpanY) - (padding * (3 - Objects[i].Item1.TileSpanY)); //int heightToDraw = (height / Objects[i].Item1.TileSpanX) - (padding * (3 - Objects[i].Item1.TileSpanX)); int spanY = 1; int spanX = 1; int widthToDraw = (width / spanY) - (padding * 2); int heightToDraw = (height / spanX) - (padding * 2); if (Objects[i].Item1.TextureBase != null) ParentScene.Main.SpriteBatch.Draw(Objects[i].Item1.TextureBase, destinationRectangle: new Rectangle(posX + (width - widthToDraw) / 2, (int)Position.Y + posY + (height / 2) - (heightToDraw / 2), widthToDraw, heightToDraw)); ParentScene.Main.SpriteBatch.Draw(Objects[i].Item1.Texture, destinationRectangle: new Rectangle(posX + (width - widthToDraw) / 2, (int)Position.Y + posY + (height / 2) - (heightToDraw / 2), widthToDraw, heightToDraw)); 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)); } if(DrawInfoPanel) DrawSelectedInfoPanel(); } private void DrawSelectedInfoPanel() { Texture2D panel = ContentManager.GetTexture("UI/textPanel"); int padding = 15; Vector2 panelPos = new Vector2(padding, (int)StaticUIValues.ScreenViewport.Y - (int)StaticUIValues.IngameInfoPanelSize.Y + padding); string[] Lines; Lines = new string[Objects[TouchedObjectIndex].Item1.infoText.Split(new string[] { "\n" }, StringSplitOptions.None).Length]; Lines = Objects[TouchedObjectIndex].Item1.infoText.Split(new string[] { "\n" }, StringSplitOptions.None); if (panel != null) { ParentScene.Main.SpriteBatch.Draw(panel, destinationRectangle: new Rectangle(0, (int)StaticUIValues.ScreenViewport.Y - (int)StaticUIValues.IngameInfoPanelSize.Y, (int)StaticUIValues.IngameInfoPanelSize.X, (int)StaticUIValues.IngameInfoPanelSize.Y)); } int lineHeight = (int)ContentManager.GetFont(StaticUIValues.IngameFont).MeasureString("zulul").Y; for (int i = 0; i < Lines.Length; i++) { ParentScene.DrawText(ContentManager.GetFont(StaticUIValues.IngameFont), Lines[i].Trim(), new Vector2(panelPos.X, panelPos.Y + (i * lineHeight)), new Vector2(), TextAllignment.LeftTop, Color.White, Color.Black, 2); } } } }