commit 613efe3be04d4aaf31f832602df45c4a5af5cb91 Author: SergoDobro Date: Sun Aug 13 19:05:51 2023 +0300 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f677870 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +bin +obj +.vs \ No newline at end of file diff --git a/DangerousD.sln b/DangerousD.sln new file mode 100644 index 0000000..336b535 --- /dev/null +++ b/DangerousD.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +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 +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {1FC12F81-0E55-4142-83BD-23496EF29DC6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {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 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {54250E62-AA1F-4858-BA7A-76F41EB64904} + EndGlobalSection +EndGlobal diff --git a/DangerousD/Content/Content.mgcb b/DangerousD/Content/Content.mgcb new file mode 100644 index 0000000..3c42606 --- /dev/null +++ b/DangerousD/Content/Content.mgcb @@ -0,0 +1,15 @@ + +#----------------------------- Global Properties ----------------------------# + +/outputDir:bin/$(Platform) +/intermediateDir:obj/$(Platform) +/platform:Windows +/config: +/profile:Reach +/compress:False + +#-------------------------------- References --------------------------------# + + +#---------------------------------- Content ---------------------------------# + diff --git a/DangerousD/DangerousD.csproj b/DangerousD/DangerousD.csproj new file mode 100644 index 0000000..dc5c07f --- /dev/null +++ b/DangerousD/DangerousD.csproj @@ -0,0 +1,27 @@ + + + WinExe + netcoreapp3.1 + false + false + true + + + app.manifest + Icon.ico + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/DangerousD/GameCore/AppManager.cs b/DangerousD/GameCore/AppManager.cs new file mode 100644 index 0000000..8e611a4 --- /dev/null +++ b/DangerousD/GameCore/AppManager.cs @@ -0,0 +1,100 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using Microsoft.Xna.Framework.Content; +using System; +using System.Collections.Generic; +using System.Text; +using Microsoft.Xna.Framework.Input; +using DangerousD.GameCore.HUD; + +namespace DangerousD.GameCore +{ + enum GameState { Menu, Options, Lobby, Game } + public class AppManager : Game + { + private GraphicsDeviceManager _graphics; + private SpriteBatch _spriteBatch; + + GameState gameState; + GameManager GameManager; + IHUD MenuGUI; + IHUD OptionsGUI; + IHUD LobbyGUI; + public AppManager() + { + _graphics = new GraphicsDeviceManager(this); + Content.RootDirectory = "Content"; + IsMouseVisible = true; + + + gameState = GameState.Menu; + } + + protected override void Initialize() + { + base.Initialize(); + } + + protected override void LoadContent() + { + _spriteBatch = new SpriteBatch(GraphicsDevice); + + // TODO: use this.Content to load your game content here + } + + protected override void Update(GameTime gameTime) + { + if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) + Exit(); + + + switch (gameState) + { + case GameState.Menu: + MenuGUI.Update(); + break; + case GameState.Options: + OptionsGUI.Update(); + break; + case GameState.Lobby: + LobbyGUI.Update(); + break; + case GameState.Game: + GameManager.Update(gameTime); + break; + default: + break; + } + + base.Update(gameTime); + } + + protected override void Draw(GameTime gameTime) + { + GraphicsDevice.Clear(Color.CornflowerBlue); + + switch (gameState) + { + case GameState.Menu: + MenuGUI.Draw(_spriteBatch); + break; + case GameState.Options: + OptionsGUI.Draw(_spriteBatch); + break; + case GameState.Lobby: + LobbyGUI.Draw(_spriteBatch); + break; + case GameState.Game: + GameManager.Draw(_spriteBatch); + break; + default: + break; + } + + base.Draw(gameTime); + } + + + + } +} diff --git a/DangerousD/GameCore/GameManager.cs b/DangerousD/GameCore/GameManager.cs new file mode 100644 index 0000000..59a7663 --- /dev/null +++ b/DangerousD/GameCore/GameManager.cs @@ -0,0 +1,20 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using System; +using System.Collections.Generic; +using System.Text; + +namespace DangerousD.GameCore +{ + class GameManager + { + public void Draw(SpriteBatch _spriteBatch) + { + + } + public void Update(GameTime gameTime) + { + + } + } +} diff --git a/DangerousD/GameCore/GameObjects/GameObject.cs b/DangerousD/GameCore/GameObjects/GameObject.cs new file mode 100644 index 0000000..6a62f36 --- /dev/null +++ b/DangerousD/GameCore/GameObjects/GameObject.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace DangerousD.GameCore +{ + class GameObject + { + } +} diff --git a/DangerousD/GameCore/GraphicsComponent.cs b/DangerousD/GameCore/GraphicsComponent.cs new file mode 100644 index 0000000..2ae348b --- /dev/null +++ b/DangerousD/GameCore/GraphicsComponent.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace DangerousD.GameCore +{ + class GraphicsComponent + { + } +} diff --git a/DangerousD/GameCore/HUD/IHUD.cs b/DangerousD/GameCore/HUD/IHUD.cs new file mode 100644 index 0000000..1047c56 --- /dev/null +++ b/DangerousD/GameCore/HUD/IHUD.cs @@ -0,0 +1,13 @@ +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); + } +} diff --git a/DangerousD/GameCore/NetworkManager.cs b/DangerousD/GameCore/NetworkManager.cs new file mode 100644 index 0000000..d9941f2 --- /dev/null +++ b/DangerousD/GameCore/NetworkManager.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace DangerousD.GameCore +{ + class NetworkManager + { + } +} diff --git a/DangerousD/GameCore/SoundManager.cs b/DangerousD/GameCore/SoundManager.cs new file mode 100644 index 0000000..668b67c --- /dev/null +++ b/DangerousD/GameCore/SoundManager.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace DangerousD.GameCore +{ + class SoundManager + { + } +} diff --git a/DangerousD/GameCore/UI/Base/MonoClassManagerUI.cs b/DangerousD/GameCore/UI/Base/MonoClassManagerUI.cs new file mode 100644 index 0000000..dc0931a --- /dev/null +++ b/DangerousD/GameCore/UI/Base/MonoClassManagerUI.cs @@ -0,0 +1,90 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Content; +using Microsoft.Xna.Framework.Graphics; +using Microsoft.Xna.Framework.Input; +using MonogameLibrary.UI.Elements; +using MonogameLibrary.UI.Interfaces; +using System.Collections.Generic; +using System.Linq; +using System.Security.Cryptography.X509Certificates; +using static System.Net.Mime.MediaTypeNames; + +namespace MonogameLibrary.UI.Base +{ + public class MonoClassManagerUI + { + static Dictionary> 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(GraphicsDevice graphicsDevice, ContentManager content, string font) + { + _graphicsDevice = graphicsDevice; + _content = content; + try + { + _baseFont = _content.Load(font); + } + catch + { + } + layerCollection = new Dictionary>(); + for (int i = -10; i < 11; i++) + { + layerCollection.Add(i, new List()); + } + } + public KeyboardState GetKeyboardState { get { return keyboardState; } } + static MouseState mouseState, prevmouseState; + static KeyboardState keyboardState; + public void Update(GameTime gameTime) + { + try + { + keyboardState = Keyboard.GetState(); + mouseState = Mouse.GetState(); + } + catch + { + + } + bool hasInteracted = false; + foreach (var collection in layerCollection) + { + foreach (var item in collection.Value) + { + if (item is IInteractable) + { + if (!hasInteracted) + { + hasInteracted = (item as IInteractable).InteractUpdate(mouseState, prevmouseState); + } + } + } + } + prevmouseState = mouseState; + } + public void Draw(SpriteBatch spriteBatch) + { + spriteBatch.Begin(); + foreach (var collection in layerCollection) + { + foreach (var item in collection.Value) + { + item.Draw(spriteBatch); + } + } + spriteBatch.End(); + } + public void Register(MonoDrawableUI monoDrawableUI, int layerIndex) + { + if (!layerCollection.ContainsKey(layerIndex)) + layerCollection.Add(layerIndex, new List()); + + layerCollection[layerIndex].Add(monoDrawableUI); + } + } +} diff --git a/DangerousD/GameCore/UI/Base/MonoDrawableTextedUI.cs b/DangerousD/GameCore/UI/Base/MonoDrawableTextedUI.cs new file mode 100644 index 0000000..f6432d3 --- /dev/null +++ b/DangerousD/GameCore/UI/Base/MonoDrawableTextedUI.cs @@ -0,0 +1,109 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using MonogameLibrary.UI.Enums; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MonogameLibrary.UI.Base +{ + public class MonoDrawableTextedUI : MonoDrawableUI + { + protected SpriteFont spriteFont; + 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 virtual void LoadTexture(string textureName = "", string font = "") + { + base.LoadTexture(textureName); + if (font != "") + { + try + { + spriteFont = MonoClassManagerUI.MainContent.Load(font); + } + catch + { + } + } + + } + public virtual void DrawText(SpriteBatch _spriteBatch) + { + if (text == "") + return; + + if (spriteFont != null) + { + var measured = spriteFont.MeasureString(text) * scale; + + if (textAligment == TextAligment.Center) + { + 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); + } + 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); + } + 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); + } + } + else + { + var measured = MonoClassManagerUI.MainBaseFont.MeasureString(text) * scale; + measured.X -= measured.X % 10; + //measured.Y *= -1; + if (textAligment == TextAligment.Center) + { + 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); + } + else if (textAligment == TextAligment.Left) + { + //var rct = new Rectangle(rectangle.Location, rectangle.Size); + //rct.Width = (int)measured.X; + //rct.Height = (int)measured.Y; + //rct.Y += (int)((rectangle.Height - measured.Y) / 2); + //rct.X += (int)((rectangle.Width - measured.X) / 2); + + //_spriteBatch.Draw(texture, rct, new Color(255, 0, 0, 125)); + //_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); + } + 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); + } + } + + } + } +} diff --git a/DangerousD/GameCore/UI/Base/MonoDrawableUI.cs b/DangerousD/GameCore/UI/Base/MonoDrawableUI.cs new file mode 100644 index 0000000..b826eb3 --- /dev/null +++ b/DangerousD/GameCore/UI/Base/MonoDrawableUI.cs @@ -0,0 +1,47 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using MonogameLibrary.UI.Enums; +using System; +using System.Collections.Generic; +using System.Security.Cryptography.X509Certificates; +using System.Text; +using System.Threading; + +namespace MonogameLibrary.UI.Base +{ + public class MonoDrawableUI + { + protected Texture2D texture; + protected int layerIndex; + public Rectangle rectangle = new Rectangle(0, 0, 10, 10); + public Color mainColor = Color.White; + public MonoDrawableUI(MonoClassManagerUI MyUIManager = null, int layerIndex = 0) + { + MyUIManager.Register(this, layerIndex); + } + public void LoadTexture(string textureName) + { + if (textureName == "") + { + texture = new Texture2D(MonoClassManagerUI.MainGraphicsDevice, 1, 1); + texture.SetData(new Color[] { Color.White }); + } + else + { + try + { + texture = MonoClassManagerUI.MainContent.Load(textureName); + } + catch + { + texture = new Texture2D(MonoClassManagerUI.MainGraphicsDevice, 1, 1); + texture.SetData(new Color[] { Color.White }); + } + } + } + public virtual void Draw(SpriteBatch _spriteBatch) + { + _spriteBatch.Draw(texture, rectangle, mainColor); + } + } +} diff --git a/DangerousD/GameCore/UI/Compounds/BasicDrawableCompound.cs b/DangerousD/GameCore/UI/Compounds/BasicDrawableCompound.cs new file mode 100644 index 0000000..338def8 --- /dev/null +++ b/DangerousD/GameCore/UI/Compounds/BasicDrawableCompound.cs @@ -0,0 +1,105 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using MonogameLibrary.UI.Base; +using MonogameLibrary.UI.Elements; +using MonogameLibrary.UI.Enums; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Input; +using System.Xml.Linq; + +namespace MonogameLibrary.UI.Compounds +{ + public enum BasicDrawableCompound_Type { Vertical, Horizontal }; + public class BasicDrawableCompound : MonoDrawableTextedUI + { + Dictionary drawables = new Dictionary(); + public Vector2 lastPos; + Vector2 offset = new Vector2(10, 10); + public BasicDrawableCompound() + { + rectangle = new Rectangle(0, 0, 20, 20); + lastPos = new Vector2(0, offset.Y); + } + int mainWidth = 40; + public void Add(string name, MonoDrawableTextedUI element) + { + //var a = Assembly.GetExecutingAssembly().GetTypes(); + //var b = a.Where(t => t.Get().Contains(type)); + //var element = ((MonoDrawableTextedUI)Assembly.GetExecutingAssembly().GetTypes() + // .Where(t => t.GetInterfaces().Contains(type)) + // .Select(x => Activator.CreateInstance(x) as ICommand).ToArray()[0]); + if (element is Slider) + { + element.rectangle = new Rectangle(0, 0, mainWidth * 3, 30); + } + if (element is Label) + { + element.rectangle = new Rectangle(0, 0, mainWidth * 2, 30); + } + if (element is TextBox) + { + element.rectangle = new Rectangle(0, 0, mainWidth * 2, 30); + } + if (element is Button) + { + element.rectangle = new Rectangle(0, 0, mainWidth, 30); + } + element.rectangle.Location = GetPositionOfElement(element).ToPoint(); + if (drawables.ContainsKey(name)) + { + int i = 1; + while (drawables.ContainsKey(name + $"({i})")) + { + i++; + } + name += $"({i})"; + } + drawables.Add(name, element); + } + public Vector2 GetPositionOfElement(MonoDrawableTextedUI element) + { + Vector2 pos = lastPos + new Vector2(offset.X, 0); + lastPos = pos + new Vector2(element.rectangle.Width, 0); + if (lastPos.X>= rectangle.Width) + { + pos.X = offset.X; + pos.Y += offset.Y + element.rectangle.Height; + lastPos = pos + new Vector2(element.rectangle.Width, 0); + } + return rectangle.Location.ToVector2() + pos; + } + + public override void LoadTexture(string textureName = "", string font = "") + { + base.LoadTexture(textureName); + if (font != "") + { + try + { + spriteFont = MonoClassManagerUI.MainContent.Load(font); + } + catch + { + } + } + foreach (var d in drawables) + { + d.Value.LoadTexture(font: font); + } + + } + public override void Draw(SpriteBatch _spriteBatch) + { + _spriteBatch.Draw(texture, rectangle, mainColor); + foreach (var d in drawables) + { + d.Value.Draw(_spriteBatch); + } + } + } +} diff --git a/DangerousD/GameCore/UI/Elements/Button.cs b/DangerousD/GameCore/UI/Elements/Button.cs new file mode 100644 index 0000000..c0ab1c2 --- /dev/null +++ b/DangerousD/GameCore/UI/Elements/Button.cs @@ -0,0 +1,67 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using Microsoft.Xna.Framework.Input; +using MonogameLibrary.UI.Base; +using MonogameLibrary.UI.Enums; +using MonogameLibrary.UI.Interfaces; +using System; +using System.Collections.Generic; +using System.Text; +using static MonogameLibrary.UI.Elements.Button; + +namespace MonogameLibrary.UI.Elements +{ + public class Button : MonoDrawableTextedUI, IInteractable + { + public delegate void OnButtonPressed(); + public event OnButtonPressed? RightButtonPressed; + public event OnButtonPressed? LeftButtonPressed; + protected HoverState hoverState = HoverState.None; + public bool InteractUpdate(MouseState mouseState, MouseState prevmouseState) + { + if (rectangle.Intersects(new Rectangle(mouseState.Position, Point.Zero))) + { + if (mouseState.LeftButton == ButtonState.Pressed || mouseState.RightButton == ButtonState.Pressed) + { + hoverState = HoverState.Pressing; + } + else + { + hoverState = HoverState.Hovering; + } + if (prevmouseState.LeftButton == ButtonState.Pressed) + { + if (mouseState.LeftButton != prevmouseState.LeftButton) + { + hoverState = HoverState.Pressing; + LeftButtonPressed?.Invoke(); + return true; + } + } + else if(prevmouseState.RightButton == ButtonState.Pressed) + { + if (mouseState.RightButton != prevmouseState.RightButton) + { + RightButtonPressed?.Invoke(); + return true; + } + } + } + else + { + hoverState = HoverState.None; + } + return false; + } + public override void Draw(SpriteBatch _spriteBatch) + { + if (hoverState == HoverState.None) + _spriteBatch.Draw(texture, rectangle, Color.White); + else if (hoverState == HoverState.Hovering) + _spriteBatch.Draw(texture, rectangle, new Color(211,211,211)); + else + _spriteBatch.Draw(texture, rectangle, new Color(112, 128, 144)); + DrawText(_spriteBatch); + } + } +} diff --git a/DangerousD/GameCore/UI/Elements/CheckBox.cs b/DangerousD/GameCore/UI/Elements/CheckBox.cs new file mode 100644 index 0000000..2fdf3bf --- /dev/null +++ b/DangerousD/GameCore/UI/Elements/CheckBox.cs @@ -0,0 +1,66 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using Microsoft.Xna.Framework.Input; +using MonogameLibrary.UI.Base; +using MonogameLibrary.UI.Enums; +using MonogameLibrary.UI.Interfaces; +using System; +using System.Collections.Generic; +using System.Text; + +namespace MonogameLibrary.UI.Elements +{ + public class CheckBox : MonoDrawableTextedUI, IInteractable + { + public delegate void OnCheck(bool checkState); + public event OnCheck? Checked; + private bool isChecked; + HoverState hoverState = HoverState.None; + public bool GetChecked { get { return isChecked; } } + public bool InteractUpdate(MouseState mouseState, MouseState prevmouseState) + { + if (rectangle.Intersects(new Rectangle(mouseState.Position, Point.Zero))) + { + hoverState = HoverState.Hovering; + if (prevmouseState.LeftButton == ButtonState.Pressed) + { + hoverState = HoverState.Pressing; + if (mouseState.LeftButton != prevmouseState.LeftButton) + { + isChecked = !isChecked; + Checked?.Invoke(isChecked); + return true; + } + } + } + else + { + hoverState = HoverState.None; + } + return false; + } + public override void Draw(SpriteBatch _spriteBatch) + { + if (isChecked) + { + if (hoverState == HoverState.None) + _spriteBatch.Draw(texture, rectangle, new Color(124, 255, 0)); + else if (hoverState == HoverState.Hovering) + _spriteBatch.Draw(texture, rectangle, new Color(124, 215, 0)); + else + _spriteBatch.Draw(texture, rectangle, new Color(124, 175, 0)); + } + else + { + if (hoverState == HoverState.None) + _spriteBatch.Draw(texture, rectangle, new Color(255, 20, 0)); + else if (hoverState == HoverState.Hovering) + _spriteBatch.Draw(texture, rectangle, new Color(215, 20, 0)); + else + _spriteBatch.Draw(texture, rectangle, new Color(175, 20, 0)); + } + DrawText(_spriteBatch); + } + + } +} diff --git a/DangerousD/GameCore/UI/Elements/Label.cs b/DangerousD/GameCore/UI/Elements/Label.cs new file mode 100644 index 0000000..4657bbc --- /dev/null +++ b/DangerousD/GameCore/UI/Elements/Label.cs @@ -0,0 +1,51 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using Microsoft.Xna.Framework.Input; +using MonogameLibrary.UI.Base; +using MonogameLibrary.UI.Enums; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MonogameLibrary.UI.Elements +{ + public class Label : MonoDrawableTextedUI + { + + protected HoverState hoverState = HoverState.None; + + public virtual bool InteractUpdate(MouseState mouseState, MouseState prevmouseState) + { + if (rectangle.Intersects(new Rectangle(mouseState.Position, Point.Zero))) + { + if (mouseState.LeftButton == ButtonState.Pressed) + { + hoverState = HoverState.Pressing; + } + else + { + hoverState = HoverState.Hovering; + } + } + else + { + hoverState = HoverState.None; + } + return false; + } + public override void Draw(SpriteBatch _spriteBatch) + { + if (hoverState == HoverState.None) + { + _spriteBatch.Draw(texture, rectangle, new Color(235, 235, 235)); + } + else if (hoverState == HoverState.Hovering) + _spriteBatch.Draw(texture, rectangle, new Color(211, 211, 211)); + else + _spriteBatch.Draw(texture, rectangle, new Color(112, 128, 144)); + DrawText(_spriteBatch); + } + } +} diff --git a/DangerousD/GameCore/UI/Elements/Slider.cs b/DangerousD/GameCore/UI/Elements/Slider.cs new file mode 100644 index 0000000..7656f64 --- /dev/null +++ b/DangerousD/GameCore/UI/Elements/Slider.cs @@ -0,0 +1,71 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using Microsoft.Xna.Framework.Input; +using MonogameLibrary.UI.Base; +using MonogameLibrary.UI.Enums; +using MonogameLibrary.UI.Interfaces; +using System; +using System.Collections.Generic; +using System.Text; + +namespace MonogameLibrary.UI.Elements +{ + public class Slider : MonoDrawableTextedUI, IInteractable + { + public delegate void OnSliderChanges(float value); + public event OnSliderChanges? SliderChanged; + + public Rectangle sliderRect = new Rectangle(0, 0, 30, 30); + private float sliderValue = 0; + private float minValue = 0, maxValue = 1; + SliderState sliderState = SliderState.None; + + public float GetValue { get { return minValue + sliderValue * (maxValue - minValue); } } + public float GetSliderValue { get { return sliderValue; } } + public float MinValue { get { return minValue; } set { minValue = value; } } + public float MaxValue { get { return maxValue; } set { maxValue = value; } } + + public bool InteractUpdate(MouseState mouseState, MouseState prevmouseState) + { + if (mouseState.LeftButton == ButtonState.Pressed) + { + if (sliderRect.Intersects(new Rectangle(mouseState.Position, Point.Zero)) || + rectangle.Intersects(new Rectangle(mouseState.Position, Point.Zero)) || + sliderState == SliderState.Moving) + { + sliderValue = Math.Clamp((mouseState.Position.X - rectangle.X - sliderRect.Width / 2f) / (rectangle.Width - sliderRect.Width), 0, 1); + sliderState = SliderState.Moving; + SliderChanged?.Invoke(GetValue); + return true; + } + } + else if (sliderRect.Intersects(new Rectangle(mouseState.Position, Point.Zero))) + { + sliderState = SliderState.HoveringOverSliderButton; + } + else + sliderState = SliderState.None; + return false; + } + public void SetValue(float setvalue) + { + sliderValue = setvalue; + SliderChanged?.Invoke(GetValue); + } + + public override void Draw(SpriteBatch _spriteBatch) + { + base.Draw(_spriteBatch); + sliderRect.Location = rectangle.Location; + sliderRect.X += (int)(sliderValue * (rectangle.Width - sliderRect.Width)); + sliderRect.Y -= sliderRect.Height / 2 - rectangle.Height / 2; + if (sliderState == SliderState.Moving) + _spriteBatch.Draw(texture, sliderRect, Color.DarkRed); + else if(sliderState == SliderState.HoveringOverSliderButton) + _spriteBatch.Draw(texture, sliderRect, new Color(200,0,0)); + else + _spriteBatch.Draw(texture, sliderRect, Color.Red); + DrawText(_spriteBatch); + } + } +} diff --git a/DangerousD/GameCore/UI/Elements/TextBox.cs b/DangerousD/GameCore/UI/Elements/TextBox.cs new file mode 100644 index 0000000..1661942 --- /dev/null +++ b/DangerousD/GameCore/UI/Elements/TextBox.cs @@ -0,0 +1,306 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using Microsoft.Xna.Framework.Input; +using MonogameLibrary.UI.Base; +using MonogameLibrary.UI.Enums; +using MonogameLibrary.UI.Interfaces; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using static System.Net.Mime.MediaTypeNames; + +namespace MonogameLibrary.UI.Elements +{ + public class TextBox : MonoDrawableTextedUI, IInteractable + { + public delegate void OnTextChange(string text); + public event OnTextChange? TextChanged; + public event OnTextChange? StopChanging; + public event OnTextChange? OnEnter; + + protected HoverState hoverState = HoverState.None; + 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) + { + if (MonoClassManagerUI.GetKeyboardState.IsKeyDown(Keys.Enter)) + { + OnEnter?.Invoke(text); + if (shouldEndOnEnter) + { + return false; + } + } + + InputManager.GetInput(ref text); + } + if (rectangle.Intersects(new Rectangle(mouseState.Position, Point.Zero))) + { + if (mouseState.LeftButton == ButtonState.Pressed) + { + hoverState = HoverState.Pressing; + } + else + { + hoverState = HoverState.Hovering; + } + if (prevmouseState.LeftButton == ButtonState.Pressed) + { + if (mouseState.LeftButton != prevmouseState.LeftButton) + { + hoverState = HoverState.Pressing; + isSelected = IsSelected.Selected; + TextChanged?.Invoke(text); + return true; + } + } + } + else + { + + + if (mouseState.LeftButton == ButtonState.Pressed) + { + isSelected = IsSelected.NotSelected; + StopChanging?.Invoke(text); + } + + hoverState = HoverState.None; + } + return false; + } + public override void Draw(SpriteBatch _spriteBatch) + { + if (hoverState == HoverState.None) + { + if (isSelected == IsSelected.Selected) + _spriteBatch.Draw(texture, rectangle, new Color(220, 220, 220)); + else + _spriteBatch.Draw(texture, rectangle, new Color(245, 245, 245)); + } + else if (hoverState == HoverState.Hovering) + _spriteBatch.Draw(texture, rectangle, new Color(211, 211, 211)); + else + _spriteBatch.Draw(texture, rectangle, new Color(112, 128, 144)); + DrawText(_spriteBatch); + } + } + + + //TODO: add translation + public static class InputManager + { + static List pressed = new List(); + static float del = 0; + static bool isShiftPressed = false; + static bool isCTRLPressed = false; + static bool isALTPressed = false; + static bool isCAPSLOCKOn = false; + + public static void GetInput(ref string text) + { + var state = MonoClassManagerUI.GetKeyboardState; + var keys = state.GetPressedKeys(); + isShiftPressed = state.IsKeyDown(Keys.LeftShift) || state.IsKeyDown(Keys.RightShift); + isCTRLPressed = state.IsKeyDown(Keys.LeftControl) || state.IsKeyDown(Keys.RightControl); + isALTPressed = state.IsKeyDown(Keys.LeftAlt) || state.IsKeyDown(Keys.RightAlt); + for (int i = 0; i < pressed.Count; i++) + { + if (!keys.Contains(pressed[i])) + { + pressed.RemoveAt(i); + i--; + } + } + foreach (var key in keys) + { + //System.Diagnostics.Debug.WriteLine(key.ToString()); + if (!pressed.Contains(key)) + { + pressed.Add(key); + + + + string getPressed = KeyDecode(key); + if (getPressed != null) + { + text += getPressed; + } + else + { + if (key == Keys.Back) + { + if (text.Length > 0) + { + text = text.Remove(text.Length - 1); + del = 0; + } + } + if (key == Keys.CapsLock) + { + isCAPSLOCKOn = !isCAPSLOCKOn; + } + } + + + } + if (IsKeySpecial(key)) + { + del++; + if (key == Keys.Back) + { + if (del>15) + { + if (text.Length > 0) + text = text.Remove(text.Length - 1); + del = 13; + } + } + } + } + } + public static string KeyDecode(Keys key) + { + string str = null; + if ((int)key >= 65 && (int)key <= 90) + { + str = key.ToString(); + } + else if ((int)key >= 48 && (int)key <= 57) + { + if (!isShiftPressed) + { + str = key.ToString().Trim('D'); + } + else + { + switch (key) + { + case Keys.D1: + return str = "!"; + case Keys.D2: + return str = "@"; + case Keys.D3: + return str = "#"; + case Keys.D4: + return str = "$"; + case Keys.D5: + return str = "%"; + case Keys.D6: + return str = "^"; + case Keys.D7: + return str = "&"; + case Keys.D8: + return str = "*"; + case Keys.D9: + return str = "("; + case Keys.D0: + return str = ")"; + } + } + } + else if ((int)key >= 96 && (int)key <= 105) + { + str = key.ToString().Remove(0, 6); + } + if (str != null) + { + if ((!isShiftPressed && !isCAPSLOCKOn) || (isShiftPressed && isCAPSLOCKOn)) + return str.ToLower(); + else + return str.ToUpper(); + } + + + if (!isShiftPressed) + { + switch (key) + { + case Keys.Space: + return " "; + case Keys.OemTilde: + return "`"; + case Keys.Enter: + return "\n"; + case Keys.OemPipe: + return "\\"; + case Keys.OemPlus: + return "="; + case Keys.OemMinus: + return "-"; + + + case Keys.OemComma: + return ","; + case Keys.OemQuestion: + return "."; + case Keys.OemPeriod: + return "/"; + case Keys.OemSemicolon: + return ";"; + case Keys.OemQuotes: + return "'"; + case Keys.OemOpenBrackets: + return "["; + case Keys.OemCloseBrackets: + return "]"; + } + } + else + { + switch (key) + { + case Keys.Space: + return " "; + case Keys.OemTilde: + return "~"; + case Keys.Enter: + return "\n"; + case Keys.OemPipe: + return "|"; + case Keys.OemPlus: + return "+"; + case Keys.OemMinus: + return "_"; + + + case Keys.OemComma: + return "<"; + case Keys.OemQuestion: + return ">"; + case Keys.OemPeriod: + return "?"; + case Keys.OemSemicolon: + return ":"; + case Keys.OemQuotes: + return "\""; + case Keys.OemOpenBrackets: + return "{"; + case Keys.OemCloseBrackets: + return "}"; + } + } + + //else if ((int)key >= 112 && (int)key <= 125) + //{ + // return key.ToString().Trim('F'); + //} + return null; + } + public static bool IsKeySpecial(Keys key) + { + return (key == Keys.Back) || (key == Keys.Space); + } + } +} diff --git a/DangerousD/GameCore/UI/Enums/Enums.cs b/DangerousD/GameCore/UI/Enums/Enums.cs new file mode 100644 index 0000000..3c7838a --- /dev/null +++ b/DangerousD/GameCore/UI/Enums/Enums.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace MonogameLibrary.UI.Enums +{ + public enum HoverState { None, Hovering, Pressing } + public enum SliderState { None, Moving, HoveringOverSliderButton } + public enum IsSelected { NotSelected, Selected } + public enum TextAligment { Center, Left, Right } +} diff --git a/DangerousD/GameCore/UI/Interfaces/IInteractable.cs b/DangerousD/GameCore/UI/Interfaces/IInteractable.cs new file mode 100644 index 0000000..6f48b9f --- /dev/null +++ b/DangerousD/GameCore/UI/Interfaces/IInteractable.cs @@ -0,0 +1,12 @@ +using Microsoft.Xna.Framework.Input; +using System; +using System.Collections.Generic; +using System.Text; + +namespace MonogameLibrary.UI.Interfaces +{ + interface IInteractable + { + bool InteractUpdate(MouseState mouseState, MouseState prevmouseState); + } +} diff --git a/DangerousD/Icon.ico b/DangerousD/Icon.ico new file mode 100644 index 0000000..7d9dec1 Binary files /dev/null and b/DangerousD/Icon.ico differ diff --git a/DangerousD/Program.cs b/DangerousD/Program.cs new file mode 100644 index 0000000..a36e5d1 --- /dev/null +++ b/DangerousD/Program.cs @@ -0,0 +1,14 @@ +using System; + +namespace DangerousD +{ + public static class Program + { + [STAThread] + static void Main() + { + using (var game = new AppManager()) + game.Run(); + } + } +} diff --git a/DangerousD/app.manifest b/DangerousD/app.manifest new file mode 100644 index 0000000..676d5d7 --- /dev/null +++ b/DangerousD/app.manifest @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true/pm + permonitorv2,permonitor + + + +