From 7e6e36745353f35fb21ae4f946a7a6ea0ccd151a Mon Sep 17 00:00:00 2001 From: Paul Spooner Date: Sun, 23 Jul 2023 16:20:31 -0700 Subject: [PATCH] Add resize window with resize UI: see SebLague#252 Same as SebLague#252 but with UI scaling properly and no crash for Linux users. --- .../src/Framework/Application/Core/Program.cs | 60 ++++++++++++++----- .../Framework/Application/Helpers/UIHelper.cs | 7 ++- 2 files changed, 49 insertions(+), 18 deletions(-) diff --git a/Chess-Challenge/src/Framework/Application/Core/Program.cs b/Chess-Challenge/src/Framework/Application/Core/Program.cs index 134db7645..0c98e8639 100644 --- a/Chess-Challenge/src/Framework/Application/Core/Program.cs +++ b/Chess-Challenge/src/Framework/Application/Core/Program.cs @@ -1,5 +1,6 @@ using Raylib_cs; using System.IO; +using System.Linq; using System.Numerics; using System.Runtime.InteropServices; @@ -26,6 +27,7 @@ public static void Main() Raylib.InitWindow(screenWidth, screenHeight, "Chess Coding Challenge"); Raylib.SetTargetFPS(60); + Raylib.SetWindowState(ConfigFlags.FLAG_WINDOW_RESIZABLE); UpdateCamera(screenWidth, screenHeight); @@ -33,6 +35,7 @@ public static void Main() while (!Raylib.WindowShouldClose()) { + UpdateWindowSize(); Raylib.BeginDrawing(); Raylib.ClearBackground(new Color(22, 22, 22, 255)); Raylib.BeginMode2D(cam); @@ -53,6 +56,19 @@ public static void Main() UIHelper.Release(); } + static void UpdateWindowSize() + { + if (Raylib.IsWindowResized()) + { + int width = Raylib.GetScreenWidth(); + int height = Raylib.GetScreenHeight(); + Vector2 size = new Vector2(width, height); + SetWindowSize(size); + } + + // Rest of update logic + } + public static void SetWindowSize(Vector2 size) { Raylib.SetWindowSize((int)size.X, (int)size.Y); @@ -60,21 +76,22 @@ public static void SetWindowSize(Vector2 size) SaveWindowSize(); } - public static Vector2 ScreenToWorldPos(Vector2 screenPos) => Raylib.GetScreenToWorld2D(screenPos, cam); + public static Vector2 ScreenToWorldPos(Vector2 screenPos) => + Raylib.GetScreenToWorld2D(screenPos, cam); static void UpdateCamera(int screenWidth, int screenHeight) { cam = new Camera2D(); cam.target = new Vector2(0, 15); cam.offset = new Vector2(screenWidth / 2f, screenHeight / 2f); - cam.zoom = screenWidth / 1280f * 0.7f; + int[] normalizedScreenDimensions = {(int)(Raylib.GetScreenHeight() * 1.777), Raylib.GetScreenWidth()}; + cam.zoom = normalizedScreenDimensions.Min() / 1280f * 0.7f; } - - [UnmanagedCallersOnly(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })] - private static unsafe void LogCustom(int logLevel, sbyte* text, sbyte* args) - { - } + [UnmanagedCallersOnly( + CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) } + )] + private static unsafe void LogCustom(int logLevel, sbyte* text, sbyte* args) { } static Vector2 GetSavedWindowSize() { @@ -83,24 +100,35 @@ static Vector2 GetSavedWindowSize() string prefs = File.ReadAllText(FileHelper.PrefsFilePath); if (!string.IsNullOrEmpty(prefs)) { - if (prefs[0] == '0') + string[] parts = prefs.Split('x'); + if (parts.Length == 2) { - return Settings.ScreenSizeSmall; - } - else if (prefs[0] == '1') - { - return Settings.ScreenSizeBig; + int width = int.Parse(parts[0]); + int height = int.Parse(parts[1]); + return new Vector2(width, height); } } + // Default sizes + if (prefs[0] == '0') + { + return Settings.ScreenSizeSmall; + } + else if (prefs[0] == '1') + { + return Settings.ScreenSizeBig; + } } return Settings.ScreenSizeSmall; } static void SaveWindowSize() { - Directory.CreateDirectory(FileHelper.AppDataPath); - bool isBigWindow = Raylib.GetScreenWidth() > Settings.ScreenSizeSmall.X; - File.WriteAllText(FileHelper.PrefsFilePath, isBigWindow ? "1" : "0"); + int width = (int)Raylib.GetScreenWidth(); + int height = (int)Raylib.GetScreenHeight(); + + string prefs = $"{width}x{height}"; + + File.WriteAllText(FileHelper.PrefsFilePath, prefs); } diff --git a/Chess-Challenge/src/Framework/Application/Helpers/UIHelper.cs b/Chess-Challenge/src/Framework/Application/Helpers/UIHelper.cs index 950f2475d..e27b8045e 100644 --- a/Chess-Challenge/src/Framework/Application/Helpers/UIHelper.cs +++ b/Chess-Challenge/src/Framework/Application/Helpers/UIHelper.cs @@ -1,6 +1,7 @@ using Raylib_cs; using System; using System.IO; +using System.Linq; using System.Numerics; namespace ChessChallenge.Application @@ -111,12 +112,14 @@ public static string GetResourcePath(params string[] localPath) public static float Scale(float val, int referenceResolution = referenceResolution) { - return Raylib.GetScreenWidth() / (float)referenceResolution * val; + int[] normalizedScreenDimensions = {(int)(Raylib.GetScreenHeight() * 1.777), Raylib.GetScreenWidth()}; + return normalizedScreenDimensions.Min() / (float)referenceResolution * val; } public static int ScaleInt(int val, int referenceResolution = referenceResolution) { - return (int)Math.Round(Raylib.GetScreenWidth() / (float)referenceResolution * val); + int[] normalizedScreenDimensions = {(int)(Raylib.GetScreenHeight() * 1.777), Raylib.GetScreenWidth()}; + return (int)Math.Round(normalizedScreenDimensions.Min() / (float)referenceResolution * val); } public static Vector2 Scale(Vector2 vec, int referenceResolution = referenceResolution)