Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add resize window with resize UI: See #252 #267

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 44 additions & 16 deletions Chess-Challenge/src/Framework/Application/Core/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Raylib_cs;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices;

Expand All @@ -26,13 +27,15 @@ public static void Main()

Raylib.InitWindow(screenWidth, screenHeight, "Chess Coding Challenge");
Raylib.SetTargetFPS(60);
Raylib.SetWindowState(ConfigFlags.FLAG_WINDOW_RESIZABLE);

UpdateCamera(screenWidth, screenHeight);

ChallengeController controller = new();

while (!Raylib.WindowShouldClose())
{
UpdateWindowSize();
Raylib.BeginDrawing();
Raylib.ClearBackground(new Color(22, 22, 22, 255));
Raylib.BeginMode2D(cam);
Expand All @@ -53,28 +56,42 @@ 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);
UpdateCamera((int)size.X, (int)size.Y);
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()
{
Expand All @@ -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);
}


Expand Down
7 changes: 5 additions & 2 deletions Chess-Challenge/src/Framework/Application/Helpers/UIHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Raylib_cs;
using System;
using System.IO;
using System.Linq;
using System.Numerics;

namespace ChessChallenge.Application
Expand Down Expand Up @@ -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)
Expand Down