|
| 1 | +using Microsoft.Xna.Framework; |
| 2 | +using Microsoft.Xna.Framework.Graphics; |
| 3 | +using Microsoft.Xna.Framework.Input; |
| 4 | +using Nuklear.Net.Examples.Common; |
| 5 | +using Nuklear.Net.Rendering; |
| 6 | +using NkKey = Nuklear.Net.Key; |
| 7 | +using NkMouseButton = Nuklear.Net.MouseButton; |
| 8 | +using XnaColor = Microsoft.Xna.Framework.Color; |
| 9 | + |
| 10 | +namespace Nuklear.Net.Examples.MonoGame; |
| 11 | + |
| 12 | +public class Game1 : Game |
| 13 | +{ |
| 14 | + readonly GraphicsDeviceManager _graphics; |
| 15 | + Context? _context; |
| 16 | + MonoGameNuklearDevice? _device; |
| 17 | + float _sliderValue = 0.5f; |
| 18 | + int _clickCount; |
| 19 | + int _previousScrollWheelValue; |
| 20 | + |
| 21 | + public Game1() |
| 22 | + { |
| 23 | + _graphics = new GraphicsDeviceManager(this) |
| 24 | + { |
| 25 | + PreferredBackBufferWidth = 960, |
| 26 | + PreferredBackBufferHeight = 540, |
| 27 | + }; |
| 28 | + Content.RootDirectory = "Content"; |
| 29 | + IsMouseVisible = true; |
| 30 | + Window.Title = "Nuklear.Net - MonoGame"; |
| 31 | + IsFixedTimeStep = false; |
| 32 | + } |
| 33 | + |
| 34 | + protected override void Initialize() |
| 35 | + { |
| 36 | + base.Initialize(); |
| 37 | + } |
| 38 | + |
| 39 | + protected override void LoadContent() |
| 40 | + { |
| 41 | + _context = new Context(); |
| 42 | + _device = new MonoGameNuklearDevice(GraphicsDevice); |
| 43 | + var metrics = DisplayMetrics.From( |
| 44 | + Window.ClientBounds.Width, |
| 45 | + Window.ClientBounds.Height, |
| 46 | + GraphicsDevice.PresentationParameters.BackBufferWidth, |
| 47 | + GraphicsDevice.PresentationParameters.BackBufferHeight); |
| 48 | + _context.Initialize(_device, NuklearRenderingOptions.CreatePixelPerfect(), metrics); |
| 49 | + ApplyDisplayMetrics(); |
| 50 | + } |
| 51 | + |
| 52 | + void ApplyDisplayMetrics() |
| 53 | + { |
| 54 | + if (_context is null) |
| 55 | + return; |
| 56 | + |
| 57 | + _context.SetDisplayMetrics(DisplayMetrics.From( |
| 58 | + Window.ClientBounds.Width, |
| 59 | + Window.ClientBounds.Height, |
| 60 | + GraphicsDevice.PresentationParameters.BackBufferWidth, |
| 61 | + GraphicsDevice.PresentationParameters.BackBufferHeight)); |
| 62 | + } |
| 63 | + |
| 64 | + protected override void Update(GameTime gameTime) |
| 65 | + { |
| 66 | + if (_context is null) |
| 67 | + return; |
| 68 | + |
| 69 | + _context.DeltaTimeSeconds = (float)gameTime.ElapsedGameTime.TotalSeconds; |
| 70 | + |
| 71 | + if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) |
| 72 | + Exit(); |
| 73 | + |
| 74 | + base.Update(gameTime); |
| 75 | + } |
| 76 | + |
| 77 | + protected override void Draw(GameTime gameTime) |
| 78 | + { |
| 79 | + if (_context is null || _device is null) |
| 80 | + return; |
| 81 | + |
| 82 | + GraphicsDevice.Clear(new XnaColor(31, 36, 46)); |
| 83 | + ApplyDisplayMetrics(); |
| 84 | + _context.RunFrame(_device, MapInput, () => DemoUi.Build(_context, ref _sliderValue, ref _clickCount)); |
| 85 | + |
| 86 | + base.Draw(gameTime); |
| 87 | + } |
| 88 | + |
| 89 | + protected override void Dispose(bool disposing) |
| 90 | + { |
| 91 | + if (disposing) |
| 92 | + { |
| 93 | + _device?.Dispose(); |
| 94 | + _context?.Dispose(); |
| 95 | + } |
| 96 | + |
| 97 | + base.Dispose(disposing); |
| 98 | + } |
| 99 | + |
| 100 | + void MapInput(InputFrame frame) |
| 101 | + { |
| 102 | + var mouse = Mouse.GetState(); |
| 103 | + frame.MousePosition = new Vec2(mouse.X, mouse.Y); |
| 104 | + frame.SetMouseButton(NkMouseButton.Left, mouse.LeftButton == ButtonState.Pressed); |
| 105 | + frame.SetMouseButton(NkMouseButton.Right, mouse.RightButton == ButtonState.Pressed); |
| 106 | + frame.SetMouseButton(NkMouseButton.Middle, mouse.MiddleButton == ButtonState.Pressed); |
| 107 | + var scrollDelta = mouse.ScrollWheelValue - _previousScrollWheelValue; |
| 108 | + _previousScrollWheelValue = mouse.ScrollWheelValue; |
| 109 | + frame.ScrollDelta = new Vec2(0, scrollDelta / 120f); |
| 110 | + |
| 111 | + var keyboard = Keyboard.GetState(); |
| 112 | + frame.SetKey(NkKey.Shift, keyboard.IsKeyDown(Keys.LeftShift) || keyboard.IsKeyDown(Keys.RightShift)); |
| 113 | + frame.SetKey(NkKey.Ctrl, keyboard.IsKeyDown(Keys.LeftControl) || keyboard.IsKeyDown(Keys.RightControl)); |
| 114 | + frame.SetKey(NkKey.Alt, keyboard.IsKeyDown(Keys.LeftAlt) || keyboard.IsKeyDown(Keys.RightAlt)); |
| 115 | + frame.SetKey(NkKey.Enter, keyboard.IsKeyDown(Keys.Enter)); |
| 116 | + frame.SetKey(NkKey.Backspace, keyboard.IsKeyDown(Keys.Back)); |
| 117 | + frame.SetKey(NkKey.Tab, keyboard.IsKeyDown(Keys.Tab)); |
| 118 | + frame.SetKey(NkKey.Up, keyboard.IsKeyDown(Keys.Up)); |
| 119 | + frame.SetKey(NkKey.Down, keyboard.IsKeyDown(Keys.Down)); |
| 120 | + frame.SetKey(NkKey.Left, keyboard.IsKeyDown(Keys.Left)); |
| 121 | + frame.SetKey(NkKey.Right, keyboard.IsKeyDown(Keys.Right)); |
| 122 | + } |
| 123 | +} |
0 commit comments