using System; using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using MonoGameLibrary.Graphics; namespace ONDClient.GUI; public class UIElement { public static int GlobalPixelMultiplier{ get; set; } public bool Active { get; set; } = true; public bool Pressable { get; set; } = false; public bool Visible { get; set; } = true; public int DrawPriority { get; } = 0; public (Point, Point) Bounds{ get => _bounds; protected set { _bounds = value; UpdateBounds(); } } protected (Point, Point) _bounds; protected (Point, Point) ScreenSpaceBounds; protected int CurrentTextureId = 0; private List textures = new(); private float _scaleMultiplier = 1; public float ScaleMultiplier{ get{ return _scaleMultiplier; } set{ _scaleMultiplier = value; LoadPixelScaleMultiplier(); } } protected int pixelScaleMultiplier = 1; private void LoadPixelScaleMultiplier() { pixelScaleMultiplier = (int)(GlobalPixelMultiplier * _scaleMultiplier); UpdateBounds(); } protected virtual void UpdateBounds() { _bounds = (Bounds.Item1, (Bounds.Item2 - Bounds.Item1).MultiplyByScalar(ScaleMultiplier) + Bounds.Item1); ScreenSpaceBounds = (Bounds.Item1.MultiplyByScalar(pixelScaleMultiplier), Bounds.Item2.MultiplyByScalar(pixelScaleMultiplier)); } public UIElement(TextureRegion texture, Point position, int drawPriority = 0) { textures.Add(texture); Bounds = (position, position + new Point(texture.Width, texture.Height)); DrawPriority = drawPriority; LoadPixelScaleMultiplier(); } public UIElement(TextureRegion[] textures, Point position, int drawPriority = 0) { this.textures.AddRange(textures); Bounds = (position, position + new Point(textures[0].Width, textures[0].Height)); DrawPriority = drawPriority; LoadPixelScaleMultiplier(); } public UIElement(Point corner1, Point corner2) { Bounds = (corner1, corner2); Visible = false; LoadPixelScaleMultiplier(); } public virtual void SetTexture(int textureId) { if (textureId >= textures.Count){ Console.WriteLine($"WARNING: TEXTURE {textureId} OUT OF BOUNDS"); return; } CurrentTextureId = textureId; } public virtual void Update() { } public bool IsWithinBounds(Point pos) { return pos.X >= Math.Min(ScreenSpaceBounds.Item1.X, ScreenSpaceBounds.Item2.X) && pos.X <= Math.Max(ScreenSpaceBounds.Item1.X, ScreenSpaceBounds.Item2.X) && pos.Y >= Math.Min(ScreenSpaceBounds.Item1.Y, ScreenSpaceBounds.Item2.Y) && pos.Y <= Math.Max(ScreenSpaceBounds.Item1.Y, ScreenSpaceBounds.Item2.Y); } public Action OnMousePress{ get; set; } // public virtual void OnMouseRelease() { } // public virtual void OnMouseHold() { } public virtual void Draw(SpriteBatch spriteBatch) { if (!Visible || !Active || textures.Count == 0){ return; } textures[CurrentTextureId].Draw(spriteBatch, ScreenSpaceBounds.Item1.ToVector2(), Color.White, 0, Vector2.Zero, pixelScaleMultiplier, SpriteEffects.None, 0); // texture.Draw(spriteBatch, bounds.Item1.ToVector2(), Color.White); } public void SetPosition(Point position) { Bounds = (position, position + new Point(textures[0].Width, textures[0].Height)); } public UIElement Clone() { return new UIElement(textures.ToArray(), Bounds.Item1); } public TextureRegion[] GetTextures() => textures.ToArray(); }