06: initialize basic GUI with button functionality
This commit is contained in:
parent
727d3535ba
commit
ae2c68bb24
36
06-monogame/GUI/.config/dotnet-tools.json
Normal file
36
06-monogame/GUI/.config/dotnet-tools.json
Normal file
|
@ -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"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
15
06-monogame/GUI/Content/Content.mgcb
Normal file
15
06-monogame/GUI/Content/Content.mgcb
Normal file
|
@ -0,0 +1,15 @@
|
|||
|
||||
#----------------------------- Global Properties ----------------------------#
|
||||
|
||||
/outputDir:bin/$(Platform)
|
||||
/intermediateDir:obj/$(Platform)
|
||||
/platform:DesktopGL
|
||||
/config:
|
||||
/profile:Reach
|
||||
/compress:False
|
||||
|
||||
#-------------------------------- References --------------------------------#
|
||||
|
||||
|
||||
#---------------------------------- Content ---------------------------------#
|
||||
|
29
06-monogame/GUI/GUI.csproj
Normal file
29
06-monogame/GUI/GUI.csproj
Normal file
|
@ -0,0 +1,29 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<RollForward>Major</RollForward>
|
||||
<PublishReadyToRun>false</PublishReadyToRun>
|
||||
<TieredCompilation>false</TieredCompilation>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<ApplicationManifest>app.manifest</ApplicationManifest>
|
||||
<ApplicationIcon>Icon.ico</ApplicationIcon>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<None Remove="Icon.ico" />
|
||||
<None Remove="Icon.bmp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="Icon.ico" />
|
||||
<EmbeddedResource Include="Icon.bmp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.1.303" />
|
||||
<PackageReference Include="MonoGame.Content.Builder.Task" Version="3.8.1.303" />
|
||||
</ItemGroup>
|
||||
<Target Name="RestoreDotnetTools" BeforeTargets="Restore">
|
||||
<Message Text="Restoring dotnet tools" Importance="High" />
|
||||
<Exec Command="dotnet tool restore" />
|
||||
</Target>
|
||||
</Project>
|
53
06-monogame/GUI/Game1.cs
Normal file
53
06-monogame/GUI/Game1.cs
Normal file
|
@ -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);
|
||||
}
|
||||
}
|
BIN
06-monogame/GUI/Icon.bmp
Normal file
BIN
06-monogame/GUI/Icon.bmp
Normal file
Binary file not shown.
After Width: | Height: | Size: 256 KiB |
BIN
06-monogame/GUI/Icon.ico
Normal file
BIN
06-monogame/GUI/Icon.ico
Normal file
Binary file not shown.
After Width: | Height: | Size: 144 KiB |
3
06-monogame/GUI/Program.cs
Normal file
3
06-monogame/GUI/Program.cs
Normal file
|
@ -0,0 +1,3 @@
|
|||
|
||||
using var game = new GUI.Game1();
|
||||
game.Run();
|
81
06-monogame/GUI/UIElements/Button.cs
Normal file
81
06-monogame/GUI/UIElements/Button.cs
Normal file
|
@ -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);
|
||||
}
|
||||
}
|
43
06-monogame/GUI/app.manifest
Normal file
43
06-monogame/GUI/app.manifest
Normal file
|
@ -0,0 +1,43 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
|
||||
<assemblyIdentity version="1.0.0.0" name="GUI"/>
|
||||
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
|
||||
<security>
|
||||
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
|
||||
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
|
||||
</requestedPrivileges>
|
||||
</security>
|
||||
</trustInfo>
|
||||
|
||||
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
|
||||
<application>
|
||||
<!-- A list of the Windows versions that this application has been tested on and is
|
||||
is designed to work with. Uncomment the appropriate elements and Windows will
|
||||
automatically selected the most compatible environment. -->
|
||||
|
||||
<!-- Windows Vista -->
|
||||
<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />
|
||||
|
||||
<!-- Windows 7 -->
|
||||
<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />
|
||||
|
||||
<!-- Windows 8 -->
|
||||
<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />
|
||||
|
||||
<!-- Windows 8.1 -->
|
||||
<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />
|
||||
|
||||
<!-- Windows 10 -->
|
||||
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
|
||||
|
||||
</application>
|
||||
</compatibility>
|
||||
|
||||
<application xmlns="urn:schemas-microsoft-com:asm.v3">
|
||||
<windowsSettings>
|
||||
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true/pm</dpiAware>
|
||||
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">permonitorv2,permonitor</dpiAwareness>
|
||||
</windowsSettings>
|
||||
</application>
|
||||
|
||||
</assembly>
|
Loading…
Reference in New Issue
Block a user