Skip to content

Commit

Permalink
[+] Window Magic!
Browse files Browse the repository at this point in the history
  • Loading branch information
clansty committed Sep 27, 2024
1 parent c1c7788 commit 74e39c4
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 1 deletion.
1 change: 1 addition & 0 deletions AquaMai/AquaMai.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@
<Compile Include="TouchSensitivity\Enable.cs" />
<Compile Include="Utils\JudgeAdjust.cs" />
<Compile Include="Utils\LogUserId.cs" />
<Compile Include="Utils\WindowState.cs" />
<Compile Include="UX\CustomPlaceName.cs" />
<Compile Include="UX\CustomVersionString.cs" />
<Compile Include="UX\DemoMaster.cs" />
Expand Down
6 changes: 6 additions & 0 deletions AquaMai/AquaMai.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ JudgeAdjustA=0.0
JudgeAdjustB=0.0
# Touch screen delay, unit is milliseconds, one second = 1000 milliseconds. Must be an integer
TouchDelay=0
# Window the game
Windowed=false
# Width and height for windowed mode, rendering resolution for fullscreen mode
# If set to 0, windowed mode will remember the user-set size, fullscreen mode will use the current display resolution
Width=0
Height=0

# ===================================
# Save some potentially unnecessary time
Expand Down
6 changes: 6 additions & 0 deletions AquaMai/AquaMai.zh.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ JudgeAdjustA=0.0
JudgeAdjustB=0.0
# 触摸屏延迟,单位为毫秒,一秒 = 1000 毫秒。必须是整数
TouchDelay=0
# 窗口化游戏
Windowed=false
# 宽度和高度窗口化时为游戏窗口大小,全屏时为渲染分辨率
# 如果设为 0,窗口化将记住用户设定的大小,全屏时将使用当前显示器分辨率
Width=0
Height=0

# ===================================
# 节省一些不知道有用没用的时间
Expand Down
3 changes: 3 additions & 0 deletions AquaMai/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public class UtilsConfig
public float JudgeAdjustA { get; set; }
public float JudgeAdjustB { get; set; }
public int TouchDelay { get; set; }
public bool Windowed { get; set; }
public int Width { get; set; }
public int Height { get; set; }
}

public class TimeSavingConfig
Expand Down
2 changes: 1 addition & 1 deletion AquaMai/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ private void ApplyPatches()
{
Patch(directiveType);
}
else MelonLogger.Error($"Type not found for {categoryProp.Name}.{settingProp.Name}");
}
}
}
Expand Down Expand Up @@ -119,6 +118,7 @@ public override void OnInitializeMelon()
// Fixes that does not have side effects
// These don't need to be configurable

WindowState.Execute();
// Helpers
Patch(typeof(MessageHelper));
Patch(typeof(MusicDirHelper));
Expand Down
82 changes: 82 additions & 0 deletions AquaMai/Utils/WindowState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using UnityEngine;

namespace AquaMai.Utils;

public class WindowState
{
private const int GWL_STYLE = -16;
private const int WS_WHATEVER = 0x14CF0000;

private static IntPtr hwnd = IntPtr.Zero;

public static void Execute()
{
if (AquaMai.AppConfig.Utils.Windowed)
{
var alreadyWindowed = Screen.fullScreenMode == FullScreenMode.Windowed;
if (AquaMai.AppConfig.Utils.Width == 0 || AquaMai.AppConfig.Utils.Height == 0)
{
Screen.fullScreenMode = FullScreenMode.Windowed;
}
else
{
alreadyWindowed = false;
Screen.SetResolution(AquaMai.AppConfig.Utils.Width, AquaMai.AppConfig.Utils.Height, FullScreenMode.Windowed);
}

hwnd = GetWindowHandle();
if(alreadyWindowed)
{
SetResizeable();
}
else
{
Task.Run(async () =>
{
await Task.Delay(3000);
// Screen.SetResolution has delay
SetResizeable();
});
}
}
else
{
var width = AquaMai.AppConfig.Utils.Width == 0 ? Display.main.systemWidth : AquaMai.AppConfig.Utils.Width;
var height = AquaMai.AppConfig.Utils.Height == 0 ? Display.main.systemHeight : AquaMai.AppConfig.Utils.Height;
Screen.SetResolution(width, height, FullScreenMode.FullScreenWindow);
}
}

public static void SetResizeable()
{
if (hwnd == IntPtr.Zero) return;
SetWindowLongPtr(hwnd, GWL_STYLE, WS_WHATEVER);
}

private delegate bool EnumThreadDelegate(IntPtr hwnd, IntPtr lParam);

[DllImport("user32.dll")]
static extern bool EnumThreadWindows(int dwThreadId, EnumThreadDelegate lpfn, IntPtr lParam);

[DllImport("Kernel32.dll")]
static extern int GetCurrentThreadId();

static IntPtr GetWindowHandle()
{
IntPtr returnHwnd = IntPtr.Zero;
var threadId = GetCurrentThreadId();
EnumThreadWindows(threadId,
(hWnd, lParam) =>
{
if (returnHwnd == IntPtr.Zero) returnHwnd = hWnd;
return true;
}, IntPtr.Zero);
return returnHwnd;
}

[DllImport("user32.dll")]
static extern IntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, int dwNewLong);
}

0 comments on commit 74e39c4

Please sign in to comment.