This commit is contained in:
AnloGames 2023-08-14 18:20:45 +03:00
commit 94b2c440d6
30 changed files with 447 additions and 195 deletions

View file

@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\DangerousD\DangerousD.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,56 @@
using DangerousD.GameCore.Graphics;
using Microsoft.Xna.Framework;
using Newtonsoft.Json;
using System;
using System.Windows.Forms;
using System.IO;
using System.Linq;
namespace AnimationsFileCreator
{
class Program
{
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("Добро пожаловать в костыльную программу по созданию файлов анимации для игры DungerousD");
Console.Write("Введите название текстуры (нажмите enter, чтобы выбрать файл во всплывающем окошке): ");
string textureName = Console.ReadLine();
if (textureName == "")
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.ShowDialog();
textureName = dialog.FileName.Split('\\').Last();
}
Console.WriteLine("Введите количество кадров анимации: ");
int framesCount = int.Parse(Console.ReadLine());
Console.WriteLine("Введите длительность кадра в анимации: ");
int interval = int.Parse(Console.ReadLine());
Console.WriteLine("Введите начальную позицию X ректенгла анимации: ");
Rectangle rectangle = new Rectangle();
rectangle.X = int.Parse(Console.ReadLine());
Console.WriteLine("Введите начальную позицию Y ректенгла анимации: ");
rectangle.Y = int.Parse(Console.ReadLine());
Console.WriteLine("Введите начальную позицию Width ректенгла анимации: ");
rectangle.Width = int.Parse(Console.ReadLine());
Console.WriteLine("Введите начальную позицию Height ректенгла анимации: ");
rectangle.Height = int.Parse(Console.ReadLine());
Console.WriteLine("Введите название для этого файла - id анимации");
string id = Console.ReadLine();
AnimationContainer container = new AnimationContainer();
container.FramesCount = framesCount;
container.FrameTime = new System.Collections.Generic.List<Tuple<int, int>>();
container.FrameTime.Add(new Tuple<int, int>(0, interval));
container.StartSpriteRectangle = rectangle;
container.TextureName = textureName;
container.TextureFrameInterval = 1;
container.Id = id;
string json = JsonConvert.SerializeObject(container);
StreamWriter writer = new StreamWriter(id);
writer.WriteLine(json);
writer.Close();
}
}
}

View file

@ -1,9 +1,13 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30907.101
# Visual Studio Version 17
VisualStudioVersion = 17.3.32611.2
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DangerousD", "DangerousD\DangerousD.csproj", "{1FC12F81-0E55-4142-83BD-23496EF29DC6}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DangerousD", "DangerousD\DangerousD.csproj", "{1FC12F81-0E55-4142-83BD-23496EF29DC6}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AnimationsFileCreator", "AnimationsFileCreator\AnimationsFileCreator.csproj", "{CE9485FC-F4C6-4E0D-8B8E-1843AA20CEDE}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MonogameLibrary", "MonogameLibrary\MonogameLibrary.csproj", "{52ED99EF-9769-4587-8BD8-54D6B98E3E7E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -15,6 +19,14 @@ 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
{CE9485FC-F4C6-4E0D-8B8E-1843AA20CEDE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CE9485FC-F4C6-4E0D-8B8E-1843AA20CEDE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CE9485FC-F4C6-4E0D-8B8E-1843AA20CEDE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CE9485FC-F4C6-4E0D-8B8E-1843AA20CEDE}.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

@ -21,9 +21,13 @@
<ItemGroup>
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.1.303" />
<PackageReference Include="MonoGame.Content.Builder.Task" Version="3.8.1.303" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</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" />
</Target>
</Project>
</Project>

View file

@ -0,0 +1,40 @@
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using MonogameLibrary.UI.Base;
namespace DangerousD.GameCore.GUI;
public abstract class AbstractGui : IGameObject
{
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(GameTime gameTime)
{
Manager.Update();
}
public virtual void Draw(SpriteBatch spriteBatch)
{
Manager.Draw(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,4 +1,5 @@
using System;
using DangerousD.GameCore.Graphics;
using System;
using System.Collections.Generic;
using System.Text;

View file

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

View file

@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Newtonsoft.Json;
namespace DangerousD.GameCore.Graphics
{
public class AnimationBuilder
{
public List<AnimationContainer> animations;
void LoadAnimations(string nameOfMainFile)
{
animations = new List<AnimationContainer>();
List<string> animationFilesNames = new List<string>();
StreamReader reader = new StreamReader(nameOfMainFile);
while (reader.Peek() != -1)
{
animationFilesNames.Add(reader.ReadLine());
}
reader.Close();
foreach (var fileName in animationFilesNames)
{
reader = new StreamReader(fileName);
string json = reader.ReadToEnd();
AnimationContainer animation = JsonConvert.DeserializeObject<AnimationContainer>(json);
animations.Add(animation);
}
}
}
}

View file

@ -0,0 +1,26 @@
using Microsoft.Xna.Framework;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
namespace DangerousD.GameCore.Graphics
{
[Serializable]
public class AnimationContainer
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("textureName")]
public string TextureName { get; set; }
[JsonProperty("startSpriteRectangle")]
public Rectangle StartSpriteRectangle { get; set; }
[JsonProperty("frameSecond")]
public List<Tuple<int, int>> FrameTime { get; set; }
[JsonProperty("textureFrameInterval")]
public int TextureFrameInterval { get; set; }
[JsonProperty("framesCount")]
public int FramesCount { get; set; }
}
}

View file

@ -0,0 +1,105 @@
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.Graphics
{
class GraphicsComponent
{
private List<AnimationContainer> animations;
private List<Texture2D> textures;
private List<string> texturesNames;
private AnimationContainer currentAnimation;
private SpriteBatch _spriteBatch;
private string lastAnimationId;
private int currentFrame;
private int interval;
private int lastInterval;
private Rectangle sourceRectangle;
public GraphicsComponent(List<string> animationsId, SpriteBatch _spriteBatch)
{
this._spriteBatch = _spriteBatch;
currentFrame = 0;
lastInterval = 1;
lastAnimationId = null;
LoadAnimations(animationsId);
}
private void LoadAnimations(List<string> animationsId)
{
animations = new List<AnimationContainer>();
foreach (var id in animationsId)
{
animations.Add( GameManager.builder.animations.Find(x => x.Id == id));
}
}
public void LoadContent(ContentManager content)
{
textures = new List<Texture2D>();
texturesNames = new List<string>();
foreach (var animation in animations)
{
if (!texturesNames.Contains(animation.TextureName))
{
texturesNames.Add(animation.TextureName);
textures.Add(content.Load<Texture2D>(animation.TextureName));
}
}
}
public void DrawAnimation(Rectangle destinationRectangle, string animationId)
{
if (animationId != lastAnimationId)
{
currentFrame = 0;
currentAnimation = animations.Find(x => x.Id == animationId);
buildSourceRectangle();
SetInterval();
}
if (interval == 0)
{
currentFrame++;
if (currentAnimation.FramesCount - 1 <= currentFrame)
{
currentFrame = 0;
}
buildSourceRectangle();
SetInterval();
}
interval--;
_spriteBatch.Draw(textures[texturesNames.FindIndex(x => x == currentAnimation.TextureName)], destinationRectangle, sourceRectangle, Color.White);
}
private void buildSourceRectangle()
{
sourceRectangle = new Rectangle();
sourceRectangle.X = currentAnimation.StartSpriteRectangle.X + currentFrame * (currentAnimation.StartSpriteRectangle.Width + currentAnimation.TextureFrameInterval);
sourceRectangle.Y = currentAnimation.StartSpriteRectangle.Y;
sourceRectangle.Height = currentAnimation.StartSpriteRectangle.Height;
sourceRectangle.Width = currentAnimation.StartSpriteRectangle.Width;
}
private void SetInterval()
{
Tuple<int, int> i = currentAnimation.FrameTime.Find(x => x.Item1 == currentFrame);
if (i != null)
{
interval = i.Item2;
lastInterval = interval;
}
else
{
interval = lastInterval;
}
}
}
}

View file

@ -1,14 +0,0 @@
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Text;
namespace DangerousD.GameCore
{
class GraphicsComponent
{
public void Draw(SpriteBatch s
) { }
}
}

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
{
@ -14,33 +14,31 @@ namespace DangerousD.GameCore
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
GameState gameState;
IHUD MenuGUI;
IHUD OptionsGUI;
IHUD LobbyGUI;
IGameObject MenuGUI;
IGameObject OptionsGUI;
IGameObject LobbyGUI;
public AppManager()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
TargetElapsedTime = TimeSpan.FromMilliseconds(1000 / 30);
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)
@ -52,13 +50,13 @@ namespace DangerousD.GameCore
switch (gameState)
{
case GameState.Menu:
MenuGUI.Update();
MenuGUI.Update(gameTime);
break;
case GameState.Options:
OptionsGUI.Update();
OptionsGUI.Update(gameTime);
break;
case GameState.Lobby:
LobbyGUI.Update();
LobbyGUI.Update(gameTime);
break;
case GameState.Game:
GameManager.Update(gameTime);

View file

@ -1,4 +1,5 @@
using DangerousD.GameCore.GameObjects;
using DangerousD.GameCore.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
@ -11,6 +12,7 @@ namespace DangerousD.GameCore
{
static List<LivingEntity> livingEntities;
static List<MapObject> MapObjects;
public static AnimationBuilder builder;
internal static void Register(GameObject gameObject)
{
if (gameObject is LivingEntity)

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

@ -10,6 +10,7 @@ namespace DangerousD
{
using (var game = new AppManager())
game.Run();
}
}
}

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)