From dffa58159672008f13703242046940c37e46809d Mon Sep 17 00:00:00 2001 From: Michel Fedde Date: Fri, 21 May 2021 16:05:52 +0200 Subject: [PATCH] Renderer: + Font.BaselineAdjust Utils: + GameControllerState.AnyInteractions --- SMRendererV3.sln | 4 +-- ...csproj => SMRenderer.Intergrations.csproj} | 0 .../SM.Utils/Controls/GameController.cs | 2 +- .../SM.Utils/Controls/GameControllerState.cs | 4 +-- .../Controls/GameControllerStateButtons.cs | 12 ++++++- .../Controls/GameControllerStateDPad.cs | 6 +++- .../Controls/GameControllerStateThumbs.cs | 4 ++- .../Controls/GameControllerStateTriggers.cs | 5 ++- .../SM.Utils/Controls/GameKeybind.cs | 2 +- .../SM.Utils/Controls/GameKeybindActor.cs | 2 +- .../SM.Utils/Controls/GameKeybindContext.cs | 2 +- .../SM.Utils/Controls/GameKeybindHost.cs | 2 +- .../SM.Utils/Controls/GameKeybindList.cs | 2 +- ...M.Utils.csproj => SMRenderer.Utils.csproj} | 0 .../SM.Base/Drawing/GenericTransformation.cs | 9 ++++- src/renderer/SM.Base/Drawing/Text/Font.cs | 13 ++++++-- tests/SM_TEST/Program.cs | 33 ++++++++----------- tests/SM_TEST/SM_TEST.csproj | 8 +++-- 18 files changed, 70 insertions(+), 40 deletions(-) rename src/optionals/SM.Intergrations/{SM.Intergrations.csproj => SMRenderer.Intergrations.csproj} (100%) rename src/optionals/SM.Utils/{SM.Utils.csproj => SMRenderer.Utils.csproj} (100%) diff --git a/SMRendererV3.sln b/SMRendererV3.sln index 753bfc7..fecf2a6 100644 --- a/SMRendererV3.sln +++ b/SMRendererV3.sln @@ -11,9 +11,9 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SM2D", "src\renderer\SM2D\S EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SM_TEST", "tests\SM_TEST\SM_TEST.csproj", "{6D4FB8E6-4D0B-4928-8F9E-EF5C2FBF44E8}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SM.Utils", "src\optionals\SM.Utils\SM.Utils.csproj", "{079BAB31-3DC4-40DA-90C7-EFAA8517C647}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SMRenderer.Utils", "src\optionals\SM.Utils\SMRenderer.Utils.csproj", "{079BAB31-3DC4-40DA-90C7-EFAA8517C647}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SM.Intergrations", "src\optionals\SM.Intergrations\SM.Intergrations.csproj", "{4CB351F4-B3F2-4F77-ACC2-02F21DBF5EC2}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SMRenderer.Intergrations", "src\optionals\SM.Intergrations\SMRenderer.Intergrations.csproj", "{4CB351F4-B3F2-4F77-ACC2-02F21DBF5EC2}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Renderer", "Renderer", "{62ED6240-4DEC-4535-95EB-AE3D90A3FDF5}" EndProject diff --git a/src/optionals/SM.Intergrations/SM.Intergrations.csproj b/src/optionals/SM.Intergrations/SMRenderer.Intergrations.csproj similarity index 100% rename from src/optionals/SM.Intergrations/SM.Intergrations.csproj rename to src/optionals/SM.Intergrations/SMRenderer.Intergrations.csproj diff --git a/src/optionals/SM.Utils/Controls/GameController.cs b/src/optionals/SM.Utils/Controls/GameController.cs index af6d989..abac97a 100644 --- a/src/optionals/SM.Utils/Controls/GameController.cs +++ b/src/optionals/SM.Utils/Controls/GameController.cs @@ -1,6 +1,6 @@ using SharpDX.XInput; -namespace SM.Optionals.Controls +namespace SM.Utils.Controls { public struct GameController { diff --git a/src/optionals/SM.Utils/Controls/GameControllerState.cs b/src/optionals/SM.Utils/Controls/GameControllerState.cs index 4b661a2..d080ec3 100644 --- a/src/optionals/SM.Utils/Controls/GameControllerState.cs +++ b/src/optionals/SM.Utils/Controls/GameControllerState.cs @@ -2,7 +2,7 @@ using OpenTK; using SharpDX.XInput; -namespace SM.Optionals.Controls +namespace SM.Utils.Controls { public struct GameControllerState { @@ -12,7 +12,7 @@ namespace SM.Optionals.Controls public GameControllerStateButtons Buttons; public bool FromConnected { get; } - + public bool AnyInteraction => Buttons.AnyInteraction || DPad.AnyInteraction || Triggers.AnyInteraction || Thumbs.AnyInteraction; internal GameControllerState(bool empty) { FromConnected = false; diff --git a/src/optionals/SM.Utils/Controls/GameControllerStateButtons.cs b/src/optionals/SM.Utils/Controls/GameControllerStateButtons.cs index 7ee7763..7feeb05 100644 --- a/src/optionals/SM.Utils/Controls/GameControllerStateButtons.cs +++ b/src/optionals/SM.Utils/Controls/GameControllerStateButtons.cs @@ -1,6 +1,6 @@ using SharpDX.XInput; -namespace SM.Optionals.Controls +namespace SM.Utils.Controls { public struct GameControllerStateButtons { @@ -19,8 +19,13 @@ namespace SM.Optionals.Controls public bool LeftThumb; public bool RightThumb; + public bool Start; + public bool Back; + public bool this[GamepadButtonFlags flags] => _buttonFlags.HasFlag(flags); + public bool AnyInteraction { get; } + internal GameControllerStateButtons(GamepadButtonFlags flags) { _buttonFlags = flags; @@ -35,6 +40,11 @@ namespace SM.Optionals.Controls LeftThumb = flags.HasFlag(GamepadButtonFlags.LeftThumb); RightThumb = flags.HasFlag(GamepadButtonFlags.RightThumb); + + Start = flags.HasFlag(GamepadButtonFlags.Start); + Back = flags.HasFlag(GamepadButtonFlags.Back); + + AnyInteraction = (int) flags >= 16; } public override string ToString() diff --git a/src/optionals/SM.Utils/Controls/GameControllerStateDPad.cs b/src/optionals/SM.Utils/Controls/GameControllerStateDPad.cs index e5308a6..ff3b30e 100644 --- a/src/optionals/SM.Utils/Controls/GameControllerStateDPad.cs +++ b/src/optionals/SM.Utils/Controls/GameControllerStateDPad.cs @@ -1,6 +1,6 @@ using SharpDX.XInput; -namespace SM.Optionals.Controls +namespace SM.Utils.Controls { public struct GameControllerStateDPad { @@ -11,12 +11,16 @@ namespace SM.Optionals.Controls public bool Left; public bool Right; + public bool AnyInteraction { get; } + internal GameControllerStateDPad(GamepadButtonFlags flags) { Up = flags.HasFlag(GamepadButtonFlags.DPadUp); Down = flags.HasFlag(GamepadButtonFlags.DPadDown); Left = flags.HasFlag(GamepadButtonFlags.DPadLeft); Right = flags.HasFlag(GamepadButtonFlags.DPadRight); + + AnyInteraction = (int)flags > 0 && (int) flags < 16; } public override string ToString() diff --git a/src/optionals/SM.Utils/Controls/GameControllerStateThumbs.cs b/src/optionals/SM.Utils/Controls/GameControllerStateThumbs.cs index eba90e6..5a63e45 100644 --- a/src/optionals/SM.Utils/Controls/GameControllerStateThumbs.cs +++ b/src/optionals/SM.Utils/Controls/GameControllerStateThumbs.cs @@ -1,6 +1,6 @@ using OpenTK; -namespace SM.Optionals.Controls +namespace SM.Utils.Controls { public struct GameControllerStateThumbs { @@ -13,6 +13,8 @@ namespace SM.Optionals.Controls public bool PressedLeft; public bool PressedRight; + public bool AnyInteraction => Left != Vector2.Zero || Right != Vector2.Zero || PressedLeft || PressedRight; + public override string ToString() { return $"Left: ({Left.X}; {Left.Y}){(PressedLeft ? " Pressed" : "")}; Right: ({Right.X}; {Right.Y}){(PressedRight ? " Pressed" : "")}"; diff --git a/src/optionals/SM.Utils/Controls/GameControllerStateTriggers.cs b/src/optionals/SM.Utils/Controls/GameControllerStateTriggers.cs index 13ee4cb..3d3f41c 100644 --- a/src/optionals/SM.Utils/Controls/GameControllerStateTriggers.cs +++ b/src/optionals/SM.Utils/Controls/GameControllerStateTriggers.cs @@ -1,4 +1,4 @@ -namespace SM.Optionals.Controls +namespace SM.Utils.Controls { public struct GameControllerStateTriggers { @@ -6,6 +6,9 @@ public float Left; public float Right; + + public bool AnyInteraction => Left != 0f || Right != 0f; + public override string ToString() { return $"Left: {Left}; Right: {Right}"; diff --git a/src/optionals/SM.Utils/Controls/GameKeybind.cs b/src/optionals/SM.Utils/Controls/GameKeybind.cs index 1f1e823..b8a683d 100644 --- a/src/optionals/SM.Utils/Controls/GameKeybind.cs +++ b/src/optionals/SM.Utils/Controls/GameKeybind.cs @@ -1,6 +1,6 @@ using System; -namespace SM.Optionals.Controls +namespace SM.Utils.Controls { public class GameKeybind { diff --git a/src/optionals/SM.Utils/Controls/GameKeybindActor.cs b/src/optionals/SM.Utils/Controls/GameKeybindActor.cs index 66a583f..1bbc8a5 100644 --- a/src/optionals/SM.Utils/Controls/GameKeybindActor.cs +++ b/src/optionals/SM.Utils/Controls/GameKeybindActor.cs @@ -1,6 +1,6 @@ using OpenTK.Input; -namespace SM.Optionals.Controls +namespace SM.Utils.Controls { public enum GameKeybindActorType { diff --git a/src/optionals/SM.Utils/Controls/GameKeybindContext.cs b/src/optionals/SM.Utils/Controls/GameKeybindContext.cs index f3c6d9b..189ec44 100644 --- a/src/optionals/SM.Utils/Controls/GameKeybindContext.cs +++ b/src/optionals/SM.Utils/Controls/GameKeybindContext.cs @@ -1,6 +1,6 @@ using OpenTK.Input; -namespace SM.Optionals.Controls +namespace SM.Utils.Controls { public struct GameKeybindContext { diff --git a/src/optionals/SM.Utils/Controls/GameKeybindHost.cs b/src/optionals/SM.Utils/Controls/GameKeybindHost.cs index 9c35ed1..30c9832 100644 --- a/src/optionals/SM.Utils/Controls/GameKeybindHost.cs +++ b/src/optionals/SM.Utils/Controls/GameKeybindHost.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; -namespace SM.Optionals.Controls +namespace SM.Utils.Controls { public class GameKeybindHost { diff --git a/src/optionals/SM.Utils/Controls/GameKeybindList.cs b/src/optionals/SM.Utils/Controls/GameKeybindList.cs index 2e37d86..b5db7ed 100644 --- a/src/optionals/SM.Utils/Controls/GameKeybindList.cs +++ b/src/optionals/SM.Utils/Controls/GameKeybindList.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; -namespace SM.Optionals.Controls +namespace SM.Utils.Controls { public class GameKeybindList : List> { diff --git a/src/optionals/SM.Utils/SM.Utils.csproj b/src/optionals/SM.Utils/SMRenderer.Utils.csproj similarity index 100% rename from src/optionals/SM.Utils/SM.Utils.csproj rename to src/optionals/SM.Utils/SMRenderer.Utils.csproj diff --git a/src/renderer/SM.Base/Drawing/GenericTransformation.cs b/src/renderer/SM.Base/Drawing/GenericTransformation.cs index 3d3325a..8a3bcdf 100644 --- a/src/renderer/SM.Base/Drawing/GenericTransformation.cs +++ b/src/renderer/SM.Base/Drawing/GenericTransformation.cs @@ -39,9 +39,16 @@ namespace SM.Base.Drawing /// /// Returns the current model matrix. /// + /// If set to true, it will always (re-)calculate the model matrix. /// - public Matrix4 GetMatrix() + public Matrix4 GetMatrix(bool force = false) { + if (force) + { + _lastFrame = SMRenderer.CurrentFrame; + return _modelMatrix = RequestMatrix(); + } + if (Ignore) return Matrix4.Identity; if (_lastFrame != SMRenderer.CurrentFrame) diff --git a/src/renderer/SM.Base/Drawing/Text/Font.cs b/src/renderer/SM.Base/Drawing/Text/Font.cs index f9c9667..102046a 100644 --- a/src/renderer/SM.Base/Drawing/Text/Font.cs +++ b/src/renderer/SM.Base/Drawing/Text/Font.cs @@ -40,6 +40,12 @@ namespace SM.Base.Drawing.Text /// public float FontSize { get; set; } = 12; + /// + /// Allows to adjust the baseline to fix clipping issues. + /// Due to some issues with the calculations, this is a temporary fix. + /// + public float BaselineAdjust { get; set; } = 1f; + /// /// The character positions. /// @@ -64,6 +70,8 @@ namespace SM.Base.Drawing.Text public void RegenerateTexture() { Width = Height = 0; + + //Height = Math.Abs(_fontFace.BBox.Bottom) + _fontFace.BBox.Top; Positions = new Dictionary(); _fontFace.SetCharSize(0, FontSize, 0, 96); @@ -83,7 +91,7 @@ namespace SM.Base.Drawing.Text float bBoxHeight = (Math.Abs(_fontFace.BBox.Bottom) + _fontFace.BBox.Top); float bBoxTopScale = _fontFace.BBox.Top / bBoxHeight; - float baseline = Height * bBoxTopScale + 1; + float baseline = (Height * bBoxTopScale) + BaselineAdjust; Map = new Bitmap(Width, Height); using (Graphics g = Graphics.FromImage(Map)) @@ -95,8 +103,7 @@ namespace SM.Base.Drawing.Text { _fontFace.LoadChar(keyvalue.Key, LoadFlags.Render, LoadTarget.Normal); - int y = ((int)baseline - (int)_fontFace.Glyph.Metrics.HorizontalBearingY); - + int y = ((int)baseline - (int) _fontFace.Glyph.Metrics.HorizontalBearingY); g.DrawImageUnscaled(_fontFace.Glyph.Bitmap.ToGdipBitmap(Color.White), (int)keyvalue.Value[1], y); Vector2 offset = new Vector2(keyvalue.Value[1] / Width, 0); diff --git a/tests/SM_TEST/Program.cs b/tests/SM_TEST/Program.cs index a13626d..70bd0b0 100644 --- a/tests/SM_TEST/Program.cs +++ b/tests/SM_TEST/Program.cs @@ -14,6 +14,7 @@ using SM.Base.Drawing; using SM.Base.Time; using SM.Base.Window; using SM.Intergrations.ShaderTool; +using SM.Utils.Controls; using SM2D; using SM2D.Controls; using SM2D.Drawing; @@ -37,9 +38,13 @@ namespace SM_TEST public static STPProject portal; static void Main(string[] args) { - Font font = new Font(@"C:\Windows\Fonts\Arial.ttf") + Font font = new Font(@".\GapSansBold.ttf") { - FontSize = 30, + FontSize = 51, + CharSet = new char[] + { + 'I', 'A','M','T','W','O' + } }; font.RegenerateTexture(); @@ -58,18 +63,10 @@ namespace SM_TEST { }; - DrawObject2D obj = new DrawObject2D() - { - Material = new STMaterial(portal.DrawNodes.First(a => a.Variables.ContainsKey("_MATColor"))) - { - ShaderArguments = { - { "RingLoc", .33f }, - - } - }, - Mesh = Polygon.GenerateCircle() - }; - obj.Transform.Size.Set(200); + + DrawText obj = new DrawText(font, "I AM\n\tTWO") + {}; + scene.Objects.Add(obj); window.UpdateFrame += WindowOnUpdateFrame; @@ -86,12 +83,8 @@ namespace SM_TEST private static void WindowOnUpdateFrame(object sender, FrameEventArgs e) { - /* - if (Mouse.LeftClick) - particles.Trigger(); - if (Mouse.RightClick) - particles.ContinuousInterval = .05f;*/ - + bool interactions = new GameController(0).GetState().AnyInteraction; + Console.WriteLine(); } } } \ No newline at end of file diff --git a/tests/SM_TEST/SM_TEST.csproj b/tests/SM_TEST/SM_TEST.csproj index 54f83b0..a520d91 100644 --- a/tests/SM_TEST/SM_TEST.csproj +++ b/tests/SM_TEST/SM_TEST.csproj @@ -73,9 +73,13 @@ - + {4cb351f4-b3f2-4f77-acc2-02f21dbf5ec2} - SM.Intergrations + SMRenderer.Intergrations + + + {079BAB31-3DC4-40DA-90C7-EFAA8517C647} + SMRenderer.Utils {8e733844-4204-43e7-b3dc-3913cddabb0d}