Přidány základy uživatelského rozhraní a testovací textury. Aktualizovaná Monogame library pro podporu myši

This commit is contained in:
Perry 2026-01-15 19:56:50 +01:00
parent 4561e254d4
commit b968b12090
9 changed files with 184 additions and 11 deletions

91
FNAF_Clone/GUI/Screen.cs Normal file
View file

@ -0,0 +1,91 @@
using System.Collections.Generic;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using MonoGameLibrary.Input;
namespace FNAF_Clone.GUI;
public class Screen {
public static Dictionary<string, Screen> Screens = new();
public static Screen CurrentScreen{ get; private set; }
public static void AddScreens((string id, Screen screen)[] screens) {
foreach (var tuple in screens){
Screens.Add(tuple.id, tuple.screen);
}
}
public static void AddScreen(string id, Screen screen, bool activate = false) {
Screens.Add(id, screen);
if (activate) SetScreen(id);
}
public static void SetScreen(string id) {
CurrentScreen = Screens[id];
CurrentScreen.Active = true;
}
public static void RemoveScreen(string id) {
Screens.Remove(id);
}
public enum ScreenType {
MAIN_MENU,
OFFICE,
CAMERAS
}
public ScreenType Type{ get; private set; }
private Dictionary<string, UIElement> elements = new();
public bool Active { get; private set; } = false;
private InputListenerHook mouseInputHook = new(true);
public Screen(ScreenType type) {
Type = type;
InputManager.AddListener(InputManager.MouseButton.LEFT, () => ProcessMouseInput(InputManager.MouseState), InputTiming.PRESS, mouseInputHook);
}
public Screen(ScreenType type, Dictionary<string, UIElement> elements) {
this.elements = elements;
Type = type;
}
public void AddElement(string id, UIElement element) {
elements.Add(id, element);
}
public void SetActive(bool active) {
Active = active;
if (Active == active) return;
foreach (var keyValuePair in elements){
keyValuePair.Value.Active = Active;
}
}
private void ProcessMouseInput(MouseState mouseState) {
foreach (var element in elements.Values){
if (!element.Pressable) continue;
if (element.IsWithinBounds(mouseState.Position)){
element.OnMousePress(); // TODO: differentiate between press, hold and release events
}
}
}
public void Update() {
foreach (var keyValuePair in elements){
keyValuePair.Value.Update();
}
}
public void Draw(SpriteBatch spriteBatch) {
foreach (var val in elements.Values){
val.Draw(spriteBatch);
}
}
}

View file

@ -0,0 +1,40 @@
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
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;
private (Point, Point) bounds; // TODO: Change this to support non-rectangular hitboxes
private TextureRegion texture;
public UIElement(TextureRegion texture, Point position) {
this.texture = texture;
bounds = (position, position + new Point(texture.Width, texture.Height));
}
public void Update() {
}
public bool IsWithinBounds(Point pos) {
return pos.X >= Math.Min(bounds.Item1.X, bounds.Item2.X) && pos.X <= Math.Max(bounds.Item1.X, bounds.Item2.X) &&
pos.Y >= Math.Min(bounds.Item1.Y, bounds.Item2.Y) && pos.Y <= Math.Max(bounds.Item1.Y, bounds.Item2.Y);
}
public Action OnMousePress{ get; set; }
// public virtual void OnMousePress() { }
public virtual void OnMouseRelease() { }
public virtual void OnMouseHold() { }
public void Draw(SpriteBatch spriteBatch) {
texture.Draw(spriteBatch, bounds.Item1.ToVector2(), Color.White);
}
}