using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Input.Touch; using Penguloon.Scenes; using System; namespace Penguloon.Controls { public class MessageBox : ControlBase { public IngameOptionsState State { get; set; } = IngameOptionsState.Hide; public DateTime ShowTime { get; set; } public Button BtnYes { get; set; } public Button BtnNo { get; set; } public event EventHandler OnYes; public event EventHandler OnNo; public string[] Lines { get; set; } public MessageBox(SceneBase parentScene, Vector2 position, Vector2 size, string text) : base(parentScene, position, size) { this.DrawText = false; this.BackgroundIdle = ContentManager.GetTexture("UI/msgBoxBackground"); this.BackgroundPressed = ContentManager.GetTexture("UI/msgBoxBackground"); this.BackgroundDisabled = ContentManager.GetTexture("UI/msgBoxBackground"); this.Text = text; this.Font = ContentManager.GetFont("Fonts/GWENT/52"); this.ForeColor = Color.Gray; this.BorderColor = Color.Gray; this.BorderWidth = 0; this.TextAllignment = TextAllignment.CenterTop; this.PaddingTop = 35; Vector2 BtnSize = new Vector2((Size.X - 135) / 2, StaticUIValues.IngameUIPlayButtonHeight); BtnYes = new Button(parentScene, new Vector2(position.X + 45, Position.Y + Size.Y - BtnSize.Y - 45), BtnSize, parentScene.Main.Resources.GetString(Resource.String.IngameYes)); BtnNo = new Button(parentScene, new Vector2(position.X + Size.X - BtnSize.X - 45, Position.Y + Size.Y - BtnSize.Y - 45), BtnSize, parentScene.Main.Resources.GetString(Resource.String.IngameNo)); OnMissClick += IngameOptions_OnMissClick; BtnYes.OnClick += delegate { OnYes?.Invoke(this, EventArgs.Empty); this.State = IngameOptionsState.Hide; }; BtnNo.OnClick += delegate { OnNo?.Invoke(this, EventArgs.Empty); this.State = IngameOptionsState.Hide; }; int charsPerLine = 25; int lines = (int)Math.Ceiling((double)text.Length / charsPerLine); Lines = new string[lines]; for (int i = 0; i < lines; i++) { if (i == lines - 1) { Lines[i] = Text.Substring(i * charsPerLine); } else { Lines[i] = Text.Substring(i * charsPerLine, charsPerLine); } } } private void IngameOptions_OnMissClick(object sender, System.EventArgs e) { if ((DateTime.Now - ShowTime).TotalMilliseconds > 200) { State = IngameOptionsState.Hide; } } public override void Draw(float deltaTime) { if (State == IngameOptionsState.Hide) return; base.Draw(deltaTime); BtnYes.Draw(deltaTime); BtnNo.Draw(deltaTime); if (Font == null) return; if (string.IsNullOrWhiteSpace(Text)) return; for (int i = 0; i < Lines.Length; i++) { ParentScene.DrawText(Font, Lines[i], new Vector2(Position.X + PaddingTop, Position.Y + PaddingTop + ((Font.MeasureString(Lines[i]).Y + 5) * i)), new Vector2(), TextAllignment.LeftTop, ForeColor, BorderColor, BorderWidth); } } public override void Update(float deltaTime, TouchLocation[] touchLocations) { if (State == IngameOptionsState.Hide) return; base.Update(deltaTime, touchLocations); BtnYes.Update(deltaTime, touchLocations); BtnNo.Update(deltaTime, touchLocations); } } }