2026-01-15 19:56:50 +01:00
|
|
|
using System;
|
2026-01-26 09:39:17 +01:00
|
|
|
using System.Collections.Generic;
|
2026-03-25 16:37:18 +01:00
|
|
|
using System.Net.Mime;
|
2026-01-15 19:56:50 +01:00
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
2026-01-26 09:39:17 +01:00
|
|
|
using MonoGameLibrary;
|
2026-01-15 19:56:50 +01:00
|
|
|
using MonoGameLibrary.Graphics;
|
|
|
|
|
using MonoGameLibrary.Input;
|
|
|
|
|
|
2026-03-22 18:31:05 +01:00
|
|
|
namespace ONDClient.GUI;
|
2026-01-15 19:56:50 +01:00
|
|
|
|
|
|
|
|
public class UIElement {
|
2026-01-26 09:39:17 +01:00
|
|
|
|
2026-03-09 20:05:21 +01:00
|
|
|
public bool Active { get; set; } = true;
|
2026-01-15 19:56:50 +01:00
|
|
|
public bool Pressable { get; set; } = false;
|
2026-01-29 19:37:40 +01:00
|
|
|
public bool Visible { get; set; } = true;
|
2026-03-16 20:43:53 +01:00
|
|
|
public int DrawPriority { get; } = 0;
|
2026-01-15 19:56:50 +01:00
|
|
|
|
2026-03-11 22:35:30 +01:00
|
|
|
protected (Point, Point) _bounds;
|
2026-02-26 16:24:55 +01:00
|
|
|
public (Point, Point) Bounds{
|
2026-03-11 22:35:30 +01:00
|
|
|
get => _bounds;
|
|
|
|
|
protected set { _bounds = value; UpdateBounds(); }
|
|
|
|
|
}
|
2026-03-09 20:05:21 +01:00
|
|
|
|
|
|
|
|
protected (Point, Point) screenSpaceBounds;
|
|
|
|
|
public List<TextureRegion> Textures = new();
|
|
|
|
|
protected int currentTextureId = 0;
|
2026-01-15 19:56:50 +01:00
|
|
|
|
2026-01-26 09:39:17 +01:00
|
|
|
private float _scaleMultiplier = 1;
|
|
|
|
|
public float ScaleMultiplier{
|
|
|
|
|
get{
|
|
|
|
|
return _scaleMultiplier;
|
|
|
|
|
}
|
|
|
|
|
set{
|
|
|
|
|
_scaleMultiplier = value;
|
|
|
|
|
LoadPixelScaleMultiplier();
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-11 22:35:30 +01:00
|
|
|
protected int pixelScaleMultiplier = 1;
|
2026-01-26 09:39:17 +01:00
|
|
|
private void LoadPixelScaleMultiplier() {
|
2026-01-29 19:37:40 +01:00
|
|
|
pixelScaleMultiplier = (int)(UIManager.GlobalPixelMultiplier * _scaleMultiplier); // TODO: move GlobalPixelMultiplier somewhere where it would make sense
|
2026-02-26 16:24:55 +01:00
|
|
|
UpdateBounds();
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-11 22:35:30 +01:00
|
|
|
protected virtual void UpdateBounds() {
|
|
|
|
|
_bounds = (Bounds.Item1, (Bounds.Item2 - Bounds.Item1).MultiplyByScalar(ScaleMultiplier) + Bounds.Item1);
|
2026-02-16 21:48:59 +01:00
|
|
|
screenSpaceBounds = (Bounds.Item1.MultiplyByScalar(pixelScaleMultiplier), Bounds.Item2.MultiplyByScalar(pixelScaleMultiplier));
|
2026-01-26 09:39:17 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-16 20:43:53 +01:00
|
|
|
public UIElement(TextureRegion texture, Point position, int drawPriority = 0) {
|
2026-03-09 20:05:21 +01:00
|
|
|
Textures.Add(texture);
|
2026-02-16 21:48:59 +01:00
|
|
|
Bounds = (position, position + new Point(texture.Width, texture.Height));
|
2026-03-16 20:43:53 +01:00
|
|
|
DrawPriority = drawPriority;
|
2026-01-26 09:39:17 +01:00
|
|
|
LoadPixelScaleMultiplier();
|
|
|
|
|
}
|
2026-03-16 20:43:53 +01:00
|
|
|
public UIElement(TextureRegion[] textures, Point position, int drawPriority = 0) {
|
2026-03-09 20:05:21 +01:00
|
|
|
this.Textures.AddRange(textures);
|
2026-02-16 21:48:59 +01:00
|
|
|
Bounds = (position, position + new Point(textures[0].Width, textures[0].Height));
|
2026-03-16 20:43:53 +01:00
|
|
|
DrawPriority = drawPriority;
|
2026-01-26 09:39:17 +01:00
|
|
|
LoadPixelScaleMultiplier();
|
|
|
|
|
}
|
2026-01-29 19:37:40 +01:00
|
|
|
|
|
|
|
|
public UIElement(Point corner1, Point corner2) {
|
2026-02-16 21:48:59 +01:00
|
|
|
Bounds = (corner1, corner2);
|
2026-01-29 19:37:40 +01:00
|
|
|
Visible = false;
|
|
|
|
|
LoadPixelScaleMultiplier();
|
|
|
|
|
}
|
2026-01-26 09:39:17 +01:00
|
|
|
|
2026-03-16 20:43:53 +01:00
|
|
|
public virtual void SetTexture(int textureId) {
|
2026-03-09 20:05:21 +01:00
|
|
|
if (textureId >= Textures.Count){
|
2026-01-26 09:39:17 +01:00
|
|
|
Console.WriteLine($"WARNING: TEXTURE {textureId} OUT OF BOUNDS");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
currentTextureId = textureId;
|
2026-01-15 19:56:50 +01:00
|
|
|
}
|
2026-03-08 16:55:49 +01:00
|
|
|
public virtual void Update() { }
|
2026-01-15 19:56:50 +01:00
|
|
|
|
|
|
|
|
public bool IsWithinBounds(Point pos) {
|
2026-01-29 19:37:40 +01:00
|
|
|
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);
|
2026-01-15 19:56:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Action OnMousePress{ get; set; }
|
|
|
|
|
|
|
|
|
|
// public virtual void OnMousePress() { }
|
|
|
|
|
|
|
|
|
|
public virtual void OnMouseRelease() { }
|
|
|
|
|
|
|
|
|
|
public virtual void OnMouseHold() { }
|
|
|
|
|
|
2026-03-09 20:05:21 +01:00
|
|
|
public virtual void Draw(SpriteBatch spriteBatch) {
|
2026-03-25 16:37:18 +01:00
|
|
|
if (!Visible || !Active || Textures.Count == 0){
|
2026-01-29 19:37:40 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2026-03-25 16:37:18 +01:00
|
|
|
|
2026-03-09 20:05:21 +01:00
|
|
|
Textures[currentTextureId].Draw(spriteBatch, screenSpaceBounds.Item1.ToVector2(), Color.White, 0, Vector2.Zero, pixelScaleMultiplier, SpriteEffects.None, 0);
|
2026-01-26 09:39:17 +01:00
|
|
|
// texture.Draw(spriteBatch, bounds.Item1.ToVector2(), Color.White);
|
2026-01-15 19:56:50 +01:00
|
|
|
}
|
2026-02-26 16:24:55 +01:00
|
|
|
|
|
|
|
|
public void SetPosition(Point position) {
|
2026-03-09 20:05:21 +01:00
|
|
|
Bounds = (position, position + new Point(Textures[0].Width, Textures[0].Height));
|
2026-02-26 16:24:55 +01:00
|
|
|
}
|
2026-03-09 20:05:21 +01:00
|
|
|
|
|
|
|
|
public UIElement Clone() {
|
|
|
|
|
return new UIElement(Textures.ToArray(), Bounds.Item1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TextureRegion[] GetTextures() => Textures.ToArray();
|
2026-01-15 19:56:50 +01:00
|
|
|
}
|