From 8a60bc26b50bd6dac1db1bb0d7645c246fae6d50 Mon Sep 17 00:00:00 2001 From: KarimAkra Date: Thu, 31 Oct 2024 12:34:38 +0200 Subject: [PATCH] Experimental full screen scale mode for mobile --- source/Main.hx | 1 + .../system/scaleModes/FullScreenScaleMode.hx | 122 ++++++++++++++++++ source/funkin/ui/mainmenu/MainMenuState.hx | 2 +- 3 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 source/flixel/system/scaleModes/FullScreenScaleMode.hx diff --git a/source/Main.hx b/source/Main.hx index 3c7ea31a6..9dd961ebb 100644 --- a/source/Main.hx +++ b/source/Main.hx @@ -118,6 +118,7 @@ class Main extends Sprite #if mobile FlxG.signals.gameResized.add(resizeGame); + FlxG.scaleMode = new flixel.system.scaleModes.FullScreenScaleMode(); #end var game:FlxGame = new FlxGame(gameWidth, gameHeight, initialState, Preferences.framerate, Preferences.framerate, skipSplash, startFullscreen); diff --git a/source/flixel/system/scaleModes/FullScreenScaleMode.hx b/source/flixel/system/scaleModes/FullScreenScaleMode.hx new file mode 100644 index 000000000..7629ea0c1 --- /dev/null +++ b/source/flixel/system/scaleModes/FullScreenScaleMode.hx @@ -0,0 +1,122 @@ +package flixel.system.scaleModes; + +import flixel.system.scaleModes.BaseScaleMode; +import flixel.math.FlxPoint; +import flixel.util.FlxAxes; +import flixel.FlxG; +#if android +import android.os.Build; +import android.Tools; +#end + +class FullScreenScaleMode extends BaseScaleMode +{ + public static var windowScale:FlxPoint = FlxPoint.get(1, 1); + public static var cutoutSize:FlxPoint = FlxPoint.get(0, 0); + // x is game ratio, y is window ratio. + public static var ratio:FlxPoint = FlxPoint.get(-1, -1); + public static var ratioAxis:FlxAxes = X; + public static var enabled(default, set):Bool; + + public function new(enable:Bool = true) + { + super(); + enabled = enable; + } + + override public function onMeasure(Width:Int, Height:Int):Void + { + FlxG.width = FlxG.initialWidth; + FlxG.height = FlxG.initialHeight; + + updateGameSize(Width, Height); + updateDeviceSize(Width, Height); + updateCutoutSize(Width, Height); + updateScaleOffset(); + updateGamePosition(); + if ((cutoutSize.x > 0 || cutoutSize.y > 0) && enabled) + { + // trace('\nresized the game!\nDevice Size: ${deviceSize}\nGame Size: ${gameSize}\nCutout Size: ${cutoutSize}'); + fillScreen(); + } + } + + override function updateGameSize(Width:Int, Height:Int):Void + { + ratio.x = FlxG.width / FlxG.height; + ratio.y = Width / Height; + + ratioAxis = ratio.y < ratio.x ? Y : X; + + // trace('ratio axis: ' + ratioAxis); + + if (ratioAxis == Y) + { + gameSize.x = Width; + gameSize.y = Math.floor(gameSize.x / ratio.x); + } + else + { + gameSize.y = Height; + gameSize.x = Math.floor(gameSize.y * ratio.x); + } + } + + public function updateCutoutSize(Width:Float, Height:Float) + { + cutoutSize.set(0, 0); + + if (ratioAxis == Y) + { + cutoutSize.y = Height - gameSize.y; + } + else + { + cutoutSize.x = Width - gameSize.x; + } + } + + public function fillScreen() + { + if (ratioAxis == Y) + { + gameSize.y += cutoutSize.y; + FlxG.height = Math.floor(gameSize.y / scale.y); + } + else + { + gameSize.x += cutoutSize.x; + FlxG.width = Math.floor(gameSize.x / scale.x); + } + + windowScale.set(FlxG.width / FlxG.initialWidth, FlxG.height / FlxG.initialHeight); + // trace('resized game with size $gameSize and scale ${scale}'); + // trace('flixel width: ${FlxG.width} - flixel height: ${FlxG.height}'); + // trace('current window scale is $windowScale'); + } + + @:noCompletion + private static function set_enabled(Value:Bool):Bool + { + // No idea what to do about this for iOS + #if android + if (!(VERSION.SDK_INT >= VERSION_CODES.P || Tools.isTablet())) + { + Value = false; + } + #end + + if (Value == true) + { + FlxG.scaleMode.horizontalAlign = LEFT; + FlxG.scaleMode.verticalAlign = TOP; + } + else + { + FlxG.scaleMode.horizontalAlign = CENTER; + FlxG.scaleMode.verticalAlign = CENTER; + } + + return enabled = Value; + } +} diff --git a/source/funkin/ui/mainmenu/MainMenuState.hx b/source/funkin/ui/mainmenu/MainMenuState.hx index 4eb85b450..8b8d59c4f 100644 --- a/source/funkin/ui/mainmenu/MainMenuState.hx +++ b/source/funkin/ui/mainmenu/MainMenuState.hx @@ -67,7 +67,7 @@ class MainMenuState extends MusicBeatState var bg:FlxSprite = new FlxSprite(Paths.image('menuBG')); bg.scrollFactor.x = 0; bg.scrollFactor.y = 0.17; - bg.setGraphicSize(Std.int(bg.width * 1.2)); + bg.setGraphicSize(Std.int(FlxG.width * 1.2)); bg.updateHitbox(); bg.screenCenter(); add(bg);