move UI library to a separate project, refactor GUI system, remove TextureManager

This commit is contained in:
Ivan Filipenkov 2023-08-14 17:58:08 +03:00
parent 6f20650386
commit 42023d71cc
21 changed files with 194 additions and 172 deletions

View file

@ -5,6 +5,8 @@ VisualStudioVersion = 16.0.30907.101
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DangerousD", "DangerousD\DangerousD.csproj", "{1FC12F81-0E55-4142-83BD-23496EF29DC6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MonogameLibrary", "MonogameLibrary\MonogameLibrary.csproj", "{52ED99EF-9769-4587-8BD8-54D6B98E3E7E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -15,6 +17,10 @@ Global
{1FC12F81-0E55-4142-83BD-23496EF29DC6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1FC12F81-0E55-4142-83BD-23496EF29DC6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1FC12F81-0E55-4142-83BD-23496EF29DC6}.Release|Any CPU.Build.0 = Release|Any CPU
{52ED99EF-9769-4587-8BD8-54D6B98E3E7E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{52ED99EF-9769-4587-8BD8-54D6B98E3E7E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{52ED99EF-9769-4587-8BD8-54D6B98E3E7E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{52ED99EF-9769-4587-8BD8-54D6B98E3E7E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View file

@ -22,6 +22,9 @@
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.1.303" />
<PackageReference Include="MonoGame.Content.Builder.Task" Version="3.8.1.303" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MonogameLibrary\MonogameLibrary.csproj" />
</ItemGroup>
<Target Name="RestoreDotnetTools" BeforeTargets="Restore">
<Message Text="Restoring dotnet tools" Importance="High" />
<Exec Command="dotnet tool restore" />

View file

@ -0,0 +1,39 @@
using System.Collections.Generic;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using MonogameLibrary.UI.Base;
namespace DangerousD.GameCore.GUI;
public abstract class AbstractGui : IGui
{
protected UIManager Manager = new();
protected List<DrawableUIElement> Elements = new();
public AbstractGui()
{
}
protected abstract void CreateUI();
public virtual void Initialize(GraphicsDevice graphicsDevice)
{
Manager.Initialize("", graphicsDevice);
CreateUI();
}
public virtual void LoadContent(ContentManager content)
{
Manager.LoadContent(content);
}
public virtual void Update()
{
Manager.Update();
}
public virtual void Draw(SpriteBatch spriteBatch)
{
Manager.Draw(spriteBatch);
}
}

View file

@ -0,0 +1,13 @@
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
namespace DangerousD.GameCore.GUI
{
interface IGui
{
void Initialize(GraphicsDevice graphicsDevice);
void LoadContent(ContentManager content);
void Update();
void Draw(SpriteBatch spriteBatch);
}
}

View file

@ -0,0 +1,12 @@
using Microsoft.Xna.Framework;
using MonogameLibrary.UI.Elements;
namespace DangerousD.GameCore.GUI;
internal class MenuGUI : AbstractGui
{
protected override void CreateUI()
{
Elements.Add(new CheckBox(Manager) { rectangle = new Rectangle(10, 10, 50, 50) });
}
}

View file

@ -1,13 +0,0 @@
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Text;
namespace DangerousD.GameCore.HUD
{
interface IHUD
{
void Update();
void Draw(SpriteBatch _spriteBatch);
}
}

View file

@ -1,27 +0,0 @@
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Text;
namespace DangerousD.GameCore.HUD
{
class MenuHUD : IHUD
{
MonogameLibrary.UI.Base.MonoClassManagerUI managerUI = new MonogameLibrary.UI.Base.MonoClassManagerUI();
public MenuHUD()
{
managerUI.InitManager("");
var lab = new MonogameLibrary.UI.Elements.CheckBox(managerUI) { rectangle = new Microsoft.Xna.Framework.Rectangle(10, 10, 50, 50)};
lab.LoadTexture();
}
public void Draw(SpriteBatch _spriteBatch)
{
managerUI.Draw(_spriteBatch);
}
public void Update()
{
managerUI.Update(null);
}
}
}

View file

@ -4,8 +4,8 @@ using Microsoft.Xna.Framework.Content;
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Input;
using DangerousD.GameCore.HUD;
using DangerousD.GameCore.GUI;
using Microsoft.Xna.Framework.Input;
namespace DangerousD.GameCore
{
@ -16,31 +16,29 @@ namespace DangerousD.GameCore
private SpriteBatch _spriteBatch;
GameState gameState;
IHUD MenuGUI;
IHUD OptionsGUI;
IHUD LobbyGUI;
IGui MenuGUI;
IGui OptionsGUI;
IGui LobbyGUI;
public AppManager()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
gameState = GameState.Menu;
MenuGUI = new MenuGUI();
}
protected override void Initialize()
{
MenuGUI.Initialize(GraphicsDevice);
base.Initialize();
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
TextureManager.contentManager = Content;
TextureManager.graphicsDevice = GraphicsDevice;
MenuGUI = new HUD.MenuHUD();
MenuGUI.LoadContent(Content);
}
protected override void Update(GameTime gameTime)

View file

@ -1,42 +0,0 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Text;
namespace DangerousD.GameCore
{
static class TextureManager
{
public static ContentManager contentManager;
public static GraphicsDevice graphicsDevice;
public static SpriteBatch spriteBatch;
private static Dictionary<string, Texture2D> textures = new Dictionary<string, Texture2D>();
public static Texture2D nullTexture;
public static void Init(GraphicsDevice _graphicsDevice, ContentManager _contentManager, SpriteBatch _spriteBatch)
{
graphicsDevice = _graphicsDevice;
contentManager = _contentManager;
spriteBatch = _spriteBatch;
nullTexture = new Texture2D(graphicsDevice, 1, 1);
nullTexture.SetData(new Color[] { Color.Purple , Color.Black, Color.Purple, Color.Black });
}
public static Texture2D GetTexture(string textureName)
{
if (textures.ContainsKey(textureName))
return textures[textureName];
try
{
Texture2D loadedTexture = contentManager.Load<Texture2D>(textureName);
textures.Add(textureName, loadedTexture);
}
catch
{
}
return nullTexture;
}
}
}

View file

@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.1.303" />
</ItemGroup>
</Project>

View file

@ -6,35 +6,41 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework.Content;
namespace MonogameLibrary.UI.Base
{
public class MonoDrawableTextedUI : MonoDrawableUI
public class DrawableTextedUiElement : DrawableUIElement
{
protected SpriteFont spriteFont;
protected string fontName;
public string text = "";
public float scale = 0.5f;
public Color fontColor = Color.Black;
public TextAligment textAligment = TextAligment.Center;
public MonoDrawableTextedUI(MonoClassManagerUI MyUIManager = null, int layerIndex = 0) : base(MyUIManager,layerIndex)
public DrawableTextedUiElement(UIManager manager, int layerIndex = 0, string textureName = "", string fontName = "")
: base(manager, layerIndex, textureName)
{
this.fontName = fontName;
}
public virtual void LoadTexture(string textureName = "", string font = "")
public override void LoadTexture(ContentManager content)
{
base.LoadTexture(textureName);
if (font != "")
base.LoadTexture(content);
if (fontName != "")
{
try
{
spriteFont = MonoClassManagerUI.MainContent.Load<SpriteFont>(font);
spriteFont = content.Load<SpriteFont>(fontName);
}
catch
{
}
}
}
public virtual void DrawText(SpriteBatch _spriteBatch)
public virtual void DrawText(SpriteBatch spriteBatch)
{
if (text == "")
return;
@ -48,26 +54,29 @@ namespace MonogameLibrary.UI.Base
Vector2 pos = rectangle.Location.ToVector2();
pos.Y += (int)((rectangle.Height - measured.Y) / 2);
pos.X += (int)((rectangle.Width - measured.X) / 2);
_spriteBatch.DrawString(spriteFont, text, pos, fontColor, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
spriteBatch.DrawString(spriteFont, text, pos, fontColor, 0, Vector2.Zero, scale,
SpriteEffects.None, 0);
}
else if (textAligment == TextAligment.Center)
{
Vector2 pos = rectangle.Location.ToVector2();
pos.Y += (int)((rectangle.Height - measured.Y) / 2);
pos.X += (int)(2 * scale);
_spriteBatch.DrawString(spriteFont, text, pos, fontColor, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
spriteBatch.DrawString(spriteFont, text, pos, fontColor, 0, Vector2.Zero, scale,
SpriteEffects.None, 0);
}
else
{
Vector2 pos = rectangle.Location.ToVector2();
pos.Y += (int)((rectangle.Height - measured.Y) / 2);
pos.X += (int)(rectangle.Width - measured.X - 2 * scale);
_spriteBatch.DrawString(spriteFont, text, pos, fontColor, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
spriteBatch.DrawString(spriteFont, text, pos, fontColor, 0, Vector2.Zero, scale,
SpriteEffects.None, 0);
}
}
else
{
var measured = MonoClassManagerUI.MainBaseFont.MeasureString(text) * scale;
{
var measured = Manager.BaseFont.MeasureString(text) * scale;
measured.X -= measured.X % 10;
//measured.Y *= -1;
if (textAligment == TextAligment.Center)
@ -75,7 +84,8 @@ namespace MonogameLibrary.UI.Base
Vector2 pos = rectangle.Location.ToVector2();
pos.Y += (int)((rectangle.Height - measured.Y) / 2);
pos.X += (int)((rectangle.Width - measured.X) / 2);
_spriteBatch.DrawString(MonoClassManagerUI.MainBaseFont, text, pos, fontColor, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
spriteBatch.DrawString(Manager.BaseFont, text, pos, fontColor, 0, Vector2.Zero, scale,
SpriteEffects.None, 0);
}
else if (textAligment == TextAligment.Left)
{
@ -89,21 +99,21 @@ namespace MonogameLibrary.UI.Base
//_spriteBatch.DrawString(MonoClassManagerUI.MainBaseFont, text, rct.Location.ToVector2(), fontColor, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
Vector2 pos = rectangle.Location.ToVector2();
pos.Y += (int)((rectangle.Height - measured.Y) / 2);
pos.X += (int)(2 * scale);
_spriteBatch.DrawString(MonoClassManagerUI.MainBaseFont, text, pos, fontColor, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
spriteBatch.DrawString(Manager.BaseFont, text, pos, fontColor, 0, Vector2.Zero, scale,
SpriteEffects.None, 0);
}
else
{
Vector2 pos = rectangle.Location.ToVector2();
pos.Y += (int)((rectangle.Height - measured.Y) / 2);
pos.X += (int)(rectangle.Width - measured.X - 2 * scale);
_spriteBatch.DrawString(MonoClassManagerUI.MainBaseFont, text, pos, fontColor, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
spriteBatch.DrawString(Manager.BaseFont, text, pos, fontColor, 0, Vector2.Zero, scale,
SpriteEffects.None, 0);
}
}
}
}
}
}

View file

@ -6,35 +6,41 @@ using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using Microsoft.Xna.Framework.Content;
namespace MonogameLibrary.UI.Base
{
public class MonoDrawableUI
public class DrawableUIElement
{
protected Texture2D texture;
protected int layerIndex;
protected UIManager Manager;
protected string textureName;
public Rectangle rectangle = new Rectangle(0, 0, 10, 10);
public Color mainColor = Color.White;
public MonoDrawableUI(MonoClassManagerUI MyUIManager = null, int layerIndex = 0)
public DrawableUIElement(UIManager manager, int layerIndex = 0, string textureName = "")
{
MyUIManager.Register(this, layerIndex);
Manager = manager;
this.textureName = textureName;
manager.Register(this, layerIndex);
}
public void LoadTexture(string textureName)
public virtual void LoadTexture(ContentManager content)
{
if (textureName == "")
{
texture = new Texture2D(MonoClassManagerUI.MainGraphicsDevice, 1, 1);
texture = new Texture2D(Manager.GraphicsDevice, 1, 1);
texture.SetData<Color>(new Color[] { Color.White });
}
else
{
try
{
texture = MonoClassManagerUI.MainContent.Load<Texture2D>(textureName);
texture = content.Load<Texture2D>(textureName);
}
catch
{
texture = new Texture2D(MonoClassManagerUI.MainGraphicsDevice, 1, 1);
texture = new Texture2D(Manager.GraphicsDevice, 1, 1);
texture.SetData<Color>(new Color[] { Color.White });
}
}

View file

@ -1,5 +1,4 @@
using DangerousD.GameCore;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
@ -12,36 +11,41 @@ using static System.Net.Mime.MediaTypeNames;
namespace MonogameLibrary.UI.Base
{
public class MonoClassManagerUI
public class UIManager
{
static Dictionary<int, List<MonoDrawableUI>> layerCollection;
public static GraphicsDevice MainGraphicsDevice { get { return _graphicsDevice; } }
public static ContentManager MainContent { get { return _content; } }
public static SpriteFont MainBaseFont { get { return _baseFont; } }
static GraphicsDevice _graphicsDevice;
static ContentManager _content;
static SpriteFont _baseFont;
public void InitManager(string font)
Dictionary<int, List<DrawableUIElement>> layerCollection = new();
public GraphicsDevice GraphicsDevice { get; private set; }
public SpriteFont BaseFont { get; private set; }
public void Initialize(string font, GraphicsDevice graphicsDevice)
{
_graphicsDevice = TextureManager.graphicsDevice;
_content = TextureManager.contentManager;
GraphicsDevice = graphicsDevice;
try
{
//_baseFont = _content.Load<SpriteFont>(font);
//BaseFont = _content.Load<SpriteFont>(font);
}
catch
{
}
layerCollection = new Dictionary<int, List<MonoDrawableUI>>();
for (int i = -10; i < 11; i++)
{
layerCollection.Add(i, new List<MonoDrawableUI>());
layerCollection.Add(i, new List<DrawableUIElement>());
}
}
public KeyboardState GetKeyboardState { get { return keyboardState; } }
static MouseState mouseState, prevmouseState;
static KeyboardState keyboardState;
public void Update(GameTime gameTime)
public void LoadContent(ContentManager content)
{
foreach (var collection in layerCollection)
{
foreach (var item in collection.Value)
{
item.LoadTexture(content);
}
}
}
public void Update()
{
try
{
@ -80,12 +84,12 @@ namespace MonogameLibrary.UI.Base
}
spriteBatch.End();
}
public void Register(MonoDrawableUI monoDrawableUI, int layerIndex)
public void Register(DrawableUIElement drawableUiElement, int layerIndex)
{
if (!layerCollection.ContainsKey(layerIndex))
layerCollection.Add(layerIndex, new List<MonoDrawableUI>());
layerCollection.Add(layerIndex, new List<DrawableUIElement>());
layerCollection[layerIndex].Add(monoDrawableUI);
layerCollection[layerIndex].Add(drawableUiElement);
}
}
}

View file

@ -11,25 +11,23 @@ using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Xml.Linq;
using Microsoft.Xna.Framework.Content;
namespace MonogameLibrary.UI.Compounds
{
public enum BasicDrawableCompound_Type { Vertical, Horizontal };
public class BasicDrawableCompound : MonoDrawableTextedUI
public class BasicDrawableCompound : DrawableTextedUiElement
{
public BasicDrawableCompound(MonoClassManagerUI MyUIManager = null, int layerIndex = 0) : base(MyUIManager, layerIndex)
{
}
Dictionary<string, MonoDrawableTextedUI> drawables = new Dictionary<string, MonoDrawableTextedUI>();
public Vector2 lastPos;
Vector2 offset = new Vector2(10, 10);
public BasicDrawableCompound()
public BasicDrawableCompound(UIManager manager, int layerIndex = 0) : base(manager, layerIndex)
{
rectangle = new Rectangle(0, 0, 20, 20);
lastPos = new Vector2(0, offset.Y);
}
Dictionary<string, DrawableTextedUiElement> drawables = new Dictionary<string, DrawableTextedUiElement>();
public Vector2 lastPos;
Vector2 offset = new Vector2(10, 10);
int mainWidth = 40;
public void Add(string name, MonoDrawableTextedUI element)
public void Add(string name, DrawableTextedUiElement element)
{
//var a = Assembly.GetExecutingAssembly().GetTypes();
//var b = a.Where(t => t.Get().Contains(type));
@ -64,7 +62,7 @@ namespace MonogameLibrary.UI.Compounds
}
drawables.Add(name, element);
}
public Vector2 GetPositionOfElement(MonoDrawableTextedUI element)
public Vector2 GetPositionOfElement(DrawableTextedUiElement element)
{
Vector2 pos = lastPos + new Vector2(offset.X, 0);
lastPos = pos + new Vector2(element.rectangle.Width, 0);
@ -77,14 +75,14 @@ namespace MonogameLibrary.UI.Compounds
return rectangle.Location.ToVector2() + pos;
}
public override void LoadTexture(string textureName = "", string font = "")
public override void LoadTexture(ContentManager content)
{
base.LoadTexture(textureName);
if (font != "")
base.LoadTexture(content);
if (fontName != "")
{
try
{
spriteFont = MonoClassManagerUI.MainContent.Load<SpriteFont>(font);
spriteFont = content.Load<SpriteFont>(fontName);
}
catch
{
@ -92,16 +90,16 @@ namespace MonogameLibrary.UI.Compounds
}
foreach (var d in drawables)
{
d.Value.LoadTexture(font: font);
d.Value.LoadTexture(content);
}
}
public override void Draw(SpriteBatch _spriteBatch)
public override void Draw(SpriteBatch spriteBatch)
{
_spriteBatch.Draw(texture, rectangle, mainColor);
spriteBatch.Draw(texture, rectangle, mainColor);
foreach (var d in drawables)
{
d.Value.Draw(_spriteBatch);
d.Value.Draw(spriteBatch);
}
}
}

View file

@ -11,12 +11,17 @@ using static MonogameLibrary.UI.Elements.Button;
namespace MonogameLibrary.UI.Elements
{
public class Button : MonoDrawableTextedUI, IInteractable
public class Button : DrawableTextedUiElement, IInteractable
{
public delegate void OnButtonPressed();
public event OnButtonPressed? RightButtonPressed;
public event OnButtonPressed? LeftButtonPressed;
protected HoverState hoverState = HoverState.None;
public Button(UIManager manager, int layerIndex = 0) : base(manager, layerIndex)
{
}
public bool InteractUpdate(MouseState mouseState, MouseState prevmouseState)
{
if (rectangle.Intersects(new Rectangle(mouseState.Position, Point.Zero)))

View file

@ -10,9 +10,9 @@ using System.Text;
namespace MonogameLibrary.UI.Elements
{
public class CheckBox : MonoDrawableTextedUI, IInteractable
public class CheckBox : DrawableTextedUiElement, IInteractable
{
public CheckBox(MonoClassManagerUI MyUIManager = null, int layerIndex = 0) : base(MyUIManager, layerIndex)
public CheckBox(UIManager manager, int layerIndex = 0) : base(manager, layerIndex)
{
}
public delegate void OnCheck(bool checkState);

View file

@ -11,10 +11,10 @@ using System.Threading.Tasks;
namespace MonogameLibrary.UI.Elements
{
public class Label : MonoDrawableTextedUI
public class Label : DrawableTextedUiElement
{
public Label(MonoClassManagerUI MyUIManager = null, int layerIndex = 0) : base(MyUIManager, layerIndex)
public Label(UIManager manager, int layerIndex = 0) : base(manager, layerIndex)
{
}
protected HoverState hoverState = HoverState.None;

View file

@ -10,9 +10,9 @@ using System.Text;
namespace MonogameLibrary.UI.Elements
{
public class Slider : MonoDrawableTextedUI, IInteractable
public class Slider : DrawableTextedUiElement, IInteractable
{
public Slider(MonoClassManagerUI MyUIManager = null, int layerIndex = 0) : base(MyUIManager, layerIndex)
public Slider(UIManager manager, int layerIndex = 0) : base(manager, layerIndex)
{
}
public delegate void OnSliderChanges(float value);

View file

@ -12,10 +12,14 @@ using static System.Net.Mime.MediaTypeNames;
namespace MonogameLibrary.UI.Elements
{
public class TextBox : MonoDrawableTextedUI, IInteractable
public class TextBox : DrawableTextedUiElement, IInteractable
{
public TextBox(MonoClassManagerUI MyUIManager = null, int layerIndex = 0) : base(MyUIManager, layerIndex)
public TextBox(UIManager manager, int layerIndex = 0) : base(manager, layerIndex)
{
OnEnter += (txt) => {
isSelected = IsSelected.NotSelected;
StopChanging?.Invoke(text);
};
}
public delegate void OnTextChange(string text);
public event OnTextChange? TextChanged;
@ -26,13 +30,6 @@ namespace MonogameLibrary.UI.Elements
protected IsSelected isSelected = IsSelected.NotSelected;
public bool shouldEndOnEnter;
public TextBox() : base()
{
OnEnter += (txt) => {
isSelected = IsSelected.NotSelected;
StopChanging?.Invoke(text);
};
}
public virtual bool InteractUpdate(MouseState mouseState, MouseState prevmouseState)
{
if (isSelected == IsSelected.Selected)