From cad444e05df1a4388fbc0e19e7ad5ed64038b159 Mon Sep 17 00:00:00 2001 From: 0xdeadbeer <64986162+0xdeadbeer@users.noreply.github.com> Date: Wed, 6 Dec 2023 22:15:20 +0100 Subject: [PATCH] 06 GUI: refactor button code and add basic scenes --- 06-monogame/GUI/Content/Content.mgcb | 12 ++ .../GUI/Content/button-default-texture.png | Bin 0 -> 545 bytes 06-monogame/GUI/Game1.cs | 36 +++-- 06-monogame/GUI/UI/Button.cs | 136 ++++++++++++++++++ 06-monogame/GUI/UI/Drawable.cs | 40 ++++++ 06-monogame/GUI/UI/Scene.cs | 80 +++++++++++ 06-monogame/GUI/UIElements/Button.cs | 81 ----------- 7 files changed, 296 insertions(+), 89 deletions(-) create mode 100644 06-monogame/GUI/Content/button-default-texture.png create mode 100644 06-monogame/GUI/UI/Button.cs create mode 100644 06-monogame/GUI/UI/Drawable.cs create mode 100644 06-monogame/GUI/UI/Scene.cs delete mode 100644 06-monogame/GUI/UIElements/Button.cs diff --git a/06-monogame/GUI/Content/Content.mgcb b/06-monogame/GUI/Content/Content.mgcb index ddc4c36..8a4a692 100644 --- a/06-monogame/GUI/Content/Content.mgcb +++ b/06-monogame/GUI/Content/Content.mgcb @@ -13,3 +13,15 @@ #---------------------------------- Content ---------------------------------# +#begin button-default-texture.png +/importer:TextureImporter +/processor:TextureProcessor +/processorParam:ColorKeyColor=255,0,255,255 +/processorParam:ColorKeyEnabled=True +/processorParam:GenerateMipmaps=False +/processorParam:PremultiplyAlpha=True +/processorParam:ResizeToPowerOfTwo=False +/processorParam:MakeSquare=False +/processorParam:TextureFormat=Color +/build:button-default-texture.png + diff --git a/06-monogame/GUI/Content/button-default-texture.png b/06-monogame/GUI/Content/button-default-texture.png new file mode 100644 index 0000000000000000000000000000000000000000..b347aa30d2262f79bb3a0ca2db154fcf4ce242c4 GIT binary patch literal 545 zcmV++0^a?JP)EX>4Tx04R}tkv&MmKpe$iTeTt;2Q!E`WT;Md(V~u8g(6f4wL+^7CYOFelZGV4 z#ZhoAIQX$xb#QUk)xlK|1V2DrU7QqMq{ROvg%&X$9QWhhy~o`2P(}>H=GUg;H1>f;?j{slqVm!CFQX~O%zc|jvC=lKSnsvwdK6aeu2@rY)uJpFQ+5l!gNw2rH z=n*im4P0EeHDwRD+yRE34B3=jDMT|^C;;zg^i4Tn@D}J>^LlITh16gUtz36yjI000?uMObu0Z*6U5Zgc=ca%Ew3Wn>_CX>@2HM@dakSAh-}0000C jNklu3 literal 0 HcmV?d00001 diff --git a/06-monogame/GUI/Game1.cs b/06-monogame/GUI/Game1.cs index 00581e0..6129ad4 100644 --- a/06-monogame/GUI/Game1.cs +++ b/06-monogame/GUI/Game1.cs @@ -1,4 +1,6 @@ -using Microsoft.Xna.Framework; +using System; +using System.Linq; +using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using GUI.UIElements; @@ -9,7 +11,7 @@ public class Game1 : Game { private GraphicsDeviceManager _graphics; private SpriteBatch _spriteBatch; - private Button mainButton; + private Scene mainScene; public Game1() { @@ -20,14 +22,34 @@ public class Game1 : Game protected override void Initialize() { - mainButton = new Button(new Vector2(10.0f, 10.0f), new Vector2(80.0f, 40.0f), null, _graphics); + _spriteBatch = new SpriteBatch(GraphicsDevice); + + Scene.graphics = _graphics; + Scene.spriteBatch = _spriteBatch; + Scene.content = Content; + + mainScene = new Scene(); + DrawableData drawableData = new DrawableData(null); + drawableData.scale = new Vector2(80.0f, 40.0f); + + void buttonOnClick(Button obj) + { + Console.WriteLine("ACTION CALLED SUCCESSFULLY!"); + } + + ButtonData buttonData = new ButtonData(buttonOnClick); + buttonData.bgColor = Color.Red; + buttonData.fgColor = Color.White; + + Button mainButton = new Button(drawableData, buttonData); + + mainScene.drawables.Add(mainButton); base.Initialize(); } protected override void LoadContent() { - _spriteBatch = new SpriteBatch(GraphicsDevice); } protected override void Update(GameTime gameTime) @@ -35,7 +57,7 @@ public class Game1 : Game if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) Exit(); - mainButton.Update(gameTime); + mainScene.Update(gameTime); base.Update(gameTime); } @@ -44,9 +66,7 @@ public class Game1 : Game { GraphicsDevice.Clear(Color.Black); - _spriteBatch.Begin(); - mainButton.Draw(_spriteBatch, gameTime); - _spriteBatch.End(); + mainScene.Draw(gameTime); base.Draw(gameTime); } diff --git a/06-monogame/GUI/UI/Button.cs b/06-monogame/GUI/UI/Button.cs new file mode 100644 index 0000000..b98e250 --- /dev/null +++ b/06-monogame/GUI/UI/Button.cs @@ -0,0 +1,136 @@ +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 ButtonData +{ + public Color bgColor; + public Color fgColor; + + public Color hoverBgColor; + public Color hoverFgColor; + + public Color pressedBgColor; + public Color pressedFgColor; + + public bool isPressed = false; + public bool isReleased = false; + public bool isHovered = false; + public bool isNone = false; + + public Action