diff --git a/06-monogame/GUI/.config/dotnet-tools.json b/06-monogame/GUI/.config/dotnet-tools.json new file mode 100644 index 0000000..efabe22 --- /dev/null +++ b/06-monogame/GUI/.config/dotnet-tools.json @@ -0,0 +1,36 @@ +{ + "version": 1, + "isRoot": true, + "tools": { + "dotnet-mgcb": { + "version": "3.8.1.303", + "commands": [ + "mgcb" + ] + }, + "dotnet-mgcb-editor": { + "version": "3.8.1.303", + "commands": [ + "mgcb-editor" + ] + }, + "dotnet-mgcb-editor-linux": { + "version": "3.8.1.303", + "commands": [ + "mgcb-editor-linux" + ] + }, + "dotnet-mgcb-editor-windows": { + "version": "3.8.1.303", + "commands": [ + "mgcb-editor-windows" + ] + }, + "dotnet-mgcb-editor-mac": { + "version": "3.8.1.303", + "commands": [ + "mgcb-editor-mac" + ] + } + } +} \ No newline at end of file diff --git a/06-monogame/GUI/Content/Content.mgcb b/06-monogame/GUI/Content/Content.mgcb new file mode 100644 index 0000000..ddc4c36 --- /dev/null +++ b/06-monogame/GUI/Content/Content.mgcb @@ -0,0 +1,15 @@ + +#----------------------------- Global Properties ----------------------------# + +/outputDir:bin/$(Platform) +/intermediateDir:obj/$(Platform) +/platform:DesktopGL +/config: +/profile:Reach +/compress:False + +#-------------------------------- References --------------------------------# + + +#---------------------------------- Content ---------------------------------# + diff --git a/06-monogame/GUI/GUI.csproj b/06-monogame/GUI/GUI.csproj new file mode 100644 index 0000000..a7fb71d --- /dev/null +++ b/06-monogame/GUI/GUI.csproj @@ -0,0 +1,29 @@ + + + WinExe + net6.0 + Major + false + false + + + app.manifest + Icon.ico + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/06-monogame/GUI/Game1.cs b/06-monogame/GUI/Game1.cs new file mode 100644 index 0000000..00581e0 --- /dev/null +++ b/06-monogame/GUI/Game1.cs @@ -0,0 +1,53 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using Microsoft.Xna.Framework.Input; +using GUI.UIElements; + +namespace GUI; + +public class Game1 : Game +{ + private GraphicsDeviceManager _graphics; + private SpriteBatch _spriteBatch; + private Button mainButton; + + public Game1() + { + _graphics = new GraphicsDeviceManager(this); + Content.RootDirectory = "Content"; + IsMouseVisible = true; + } + + protected override void Initialize() + { + mainButton = new Button(new Vector2(10.0f, 10.0f), new Vector2(80.0f, 40.0f), null, _graphics); + + base.Initialize(); + } + + protected override void LoadContent() + { + _spriteBatch = new SpriteBatch(GraphicsDevice); + } + + protected override void Update(GameTime gameTime) + { + if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) + Exit(); + + mainButton.Update(gameTime); + + base.Update(gameTime); + } + + protected override void Draw(GameTime gameTime) + { + GraphicsDevice.Clear(Color.Black); + + _spriteBatch.Begin(); + mainButton.Draw(_spriteBatch, gameTime); + _spriteBatch.End(); + + base.Draw(gameTime); + } +} diff --git a/06-monogame/GUI/Icon.bmp b/06-monogame/GUI/Icon.bmp new file mode 100644 index 0000000..2b48165 Binary files /dev/null and b/06-monogame/GUI/Icon.bmp differ diff --git a/06-monogame/GUI/Icon.ico b/06-monogame/GUI/Icon.ico new file mode 100644 index 0000000..7d9dec1 Binary files /dev/null and b/06-monogame/GUI/Icon.ico differ diff --git a/06-monogame/GUI/Program.cs b/06-monogame/GUI/Program.cs new file mode 100644 index 0000000..0cda301 --- /dev/null +++ b/06-monogame/GUI/Program.cs @@ -0,0 +1,3 @@ + +using var game = new GUI.Game1(); +game.Run(); diff --git a/06-monogame/GUI/UIElements/Button.cs b/06-monogame/GUI/UIElements/Button.cs new file mode 100644 index 0000000..3b92f7e --- /dev/null +++ b/06-monogame/GUI/UIElements/Button.cs @@ -0,0 +1,81 @@ +using System; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using Microsoft.Xna.Framework.Input; +using Vector2 = Microsoft.Xna.Framework.Vector2; + +namespace GUI.UIElements; + +public class Button +{ + private Texture2D texture; + private Vector2 pos; + private Vector2 size; + private float rot; + + private Color _bgColor; + public Color bgColor + { + get { return _bgColor; } + set + { + _bgColor = value; + texture.SetData(new Color[] { _bgColor }); + } + } + + public Button(Vector2? pos, Vector2? size, float? rot, GraphicsDeviceManager graphics) + { + this.pos = pos ?? Vector2.Zero; + this.size = size ?? Vector2.One; + this.rot = rot ?? 0.0f; + + texture = new Texture2D(graphics.GraphicsDevice, 1, 1); + bgColor = Color.Green; + } + + public void OnClick(GameTime gameTime) + { + Console.WriteLine("You pressed a button!"); + this.bgColor = Color.Yellow; + } + + public void OnHover(GameTime gameTime) + { + Console.WriteLine("You're hovering over the button!"); + this.bgColor = Color.Red; + } + + public void NoEvent(GameTime gameTime) + { + this.bgColor = Color.Green; + } + + public void Update(GameTime gameTime) + { + var mstate = Mouse.GetState(); + Vector2 mousePosition = mstate.Position.ToVector2(); + + bool insideHorizontalBoundary = (mousePosition.X > pos.X) && (mousePosition.X < (pos.X + size.X)); + bool insideVerticalBoundary = (mousePosition.Y > pos.Y) && (mousePosition.Y < (pos.Y + size.Y)); + + if (!insideHorizontalBoundary || !insideVerticalBoundary) + { + this.NoEvent(gameTime); + return; + } + + if (Mouse.GetState().LeftButton == ButtonState.Pressed) + { + this.OnClick(gameTime); + return; + } + + this.OnHover(gameTime); + } + + public void Draw(SpriteBatch spriteBatch, GameTime gameTime) + { + spriteBatch.Draw(texture, new Rectangle((int) pos.X, (int) pos.Y, (int) size.X, (int) size.Y), Color.White); + } +} \ No newline at end of file diff --git a/06-monogame/GUI/app.manifest b/06-monogame/GUI/app.manifest new file mode 100644 index 0000000..d75e70a --- /dev/null +++ b/06-monogame/GUI/app.manifest @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true/pm + permonitorv2,permonitor + + + + diff --git a/README.md b/README.md index 8796f44..e2221d0 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,10 @@

+
+
+

# Graphics