OneNightDuel/FNAF_Clone/GUI/UIElement.cs

88 lines
3.1 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGameLibrary;
using MonoGameLibrary.Graphics;
using MonoGameLibrary.Input;
namespace FNAF_Clone.GUI;
public class UIElement {
public bool Active { get; set; } = false;
public bool Pressable { get; set; } = false;
public bool Visible { get; set; } = true;
public (Point, Point) Bounds{ get; private set; } // TODO: Change this to support non-rectangular hitboxes
private (Point, Point) screenSpaceBounds;
private List<TextureRegion> textures = new();
private int currentTextureId = 0;
private float _scaleMultiplier = 1;
public float ScaleMultiplier{
get{
return _scaleMultiplier;
}
set{
_scaleMultiplier = value;
LoadPixelScaleMultiplier();
}
}
private int pixelScaleMultiplier = 1;
private void LoadPixelScaleMultiplier() {
pixelScaleMultiplier = (int)(UIManager.GlobalPixelMultiplier * _scaleMultiplier); // TODO: move GlobalPixelMultiplier somewhere where it would make sense
screenSpaceBounds = (Bounds.Item1.MultiplyByScalar(pixelScaleMultiplier), Bounds.Item2.MultiplyByScalar(pixelScaleMultiplier));
}
public UIElement(TextureRegion texture, Point position) {
textures.Add(texture);
Bounds = (position, position + new Point(texture.Width, texture.Height));
LoadPixelScaleMultiplier();
}
public UIElement(TextureRegion[] textures, Point position) {
this.textures.AddRange(textures);
Bounds = (position, position + new Point(textures[0].Width, textures[0].Height));
LoadPixelScaleMultiplier();
}
public UIElement(Point corner1, Point corner2) {
Bounds = (corner1, corner2);
Visible = false;
LoadPixelScaleMultiplier();
}
public void SetTexture(int textureId) {
if (textureId >= textures.Count){
Console.WriteLine($"WARNING: TEXTURE {textureId} OUT OF BOUNDS");
return;
}
currentTextureId = textureId;
}
public 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 OnMousePress() { }
public virtual void OnMouseRelease() { }
public virtual void OnMouseHold() { }
public void Draw(SpriteBatch spriteBatch) {
if (!Visible){
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);
}
}