2022-03-27 02:18:26 +00:00
|
|
|
package funkin.util;
|
|
|
|
|
2023-01-02 22:40:53 +00:00
|
|
|
import flixel.util.FlxSignal.FlxTypedSignal;
|
|
|
|
|
2023-11-24 00:48:28 +00:00
|
|
|
using StringTools;
|
|
|
|
|
2023-11-07 09:04:22 +00:00
|
|
|
/**
|
|
|
|
* Utilities for operating on the current window, such as changing the title.
|
|
|
|
*/
|
2025-04-16 01:00:09 +00:00
|
|
|
@:nullSafety
|
2022-03-27 02:18:26 +00:00
|
|
|
class WindowUtil
|
|
|
|
{
|
2025-05-08 17:28:39 +00:00
|
|
|
/**
|
|
|
|
* A regex to match valid URLs.
|
|
|
|
*/
|
|
|
|
public static final URL_REGEX:EReg = ~/^https?:\/?\/?(?:www\.)?[-a-zA-Z0-9@:%_\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(?:[-a-zA-Z0-9()@:%_\+.~#?&\/=]*)$/;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sanitizes a URL via a regex.
|
|
|
|
*
|
|
|
|
* @param targetUrl The URL to sanitize.
|
|
|
|
* @return The sanitized URL, or an empty string if the URL is invalid.
|
|
|
|
*/
|
|
|
|
public static function sanitizeURL(targetUrl:String):String
|
|
|
|
{
|
|
|
|
targetUrl = (targetUrl ?? '').trim();
|
|
|
|
if (targetUrl == '')
|
|
|
|
{
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
final lowerUrl:String = targetUrl.toLowerCase();
|
|
|
|
if (!lowerUrl.startsWith('http:') && !lowerUrl.startsWith('https:'))
|
|
|
|
{
|
|
|
|
targetUrl = 'http://' + targetUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (URL_REGEX.match(targetUrl))
|
|
|
|
{
|
|
|
|
return URL_REGEX.matched(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2023-06-25 16:36:21 +00:00
|
|
|
/**
|
|
|
|
* Runs platform-specific code to open a URL in a web browser.
|
|
|
|
* @param targetUrl The URL to open.
|
|
|
|
*/
|
2023-11-23 00:17:35 +00:00
|
|
|
public static function openURL(targetUrl:String):Void
|
2023-01-23 00:55:30 +00:00
|
|
|
{
|
2025-04-17 00:17:35 +00:00
|
|
|
// Ensure you can't open protocols such as steam://, file://, etc
|
|
|
|
var protocol:Array<String> = targetUrl.split("://");
|
|
|
|
if (protocol.length == 1) targetUrl = 'https://${targetUrl}';
|
|
|
|
else if (protocol[0] != 'http' && protocol[0] != 'https') throw "openURL can only open http and https links.";
|
|
|
|
|
2024-09-17 04:35:23 +00:00
|
|
|
#if FEATURE_OPEN_URL
|
2025-05-08 17:28:39 +00:00
|
|
|
targetUrl = sanitizeURL(targetUrl);
|
|
|
|
if (targetUrl == '')
|
|
|
|
{
|
|
|
|
throw 'Invalid URL: "$targetUrl"';
|
|
|
|
}
|
|
|
|
|
2023-01-23 00:55:30 +00:00
|
|
|
#if linux
|
2024-05-13 20:16:49 +00:00
|
|
|
Sys.command('/usr/bin/xdg-open $targetUrl &');
|
2023-01-23 00:55:30 +00:00
|
|
|
#else
|
2023-06-25 16:36:21 +00:00
|
|
|
// This should work on Windows and HTML5.
|
2023-01-23 00:55:30 +00:00
|
|
|
FlxG.openURL(targetUrl);
|
|
|
|
#end
|
|
|
|
#else
|
2023-06-25 16:36:21 +00:00
|
|
|
throw 'Cannot open URLs on this platform.';
|
2023-01-23 00:55:30 +00:00
|
|
|
#end
|
|
|
|
}
|
2023-01-02 22:40:53 +00:00
|
|
|
|
2024-09-25 01:33:00 +00:00
|
|
|
#if FEATURE_DEBUG_TRACY
|
|
|
|
/**
|
|
|
|
* Initialize Tracy.
|
|
|
|
* NOTE: Call this from the main thread ONLY!
|
|
|
|
*/
|
|
|
|
public static function initTracy():Void
|
|
|
|
{
|
|
|
|
var appInfoMessage = funkin.util.logging.CrashHandler.buildSystemInfo();
|
|
|
|
|
|
|
|
trace("Friday Night Funkin': Connection to Tracy profiler successful.");
|
|
|
|
|
|
|
|
// Post system info like Git hash
|
|
|
|
cpp.vm.tracy.TracyProfiler.messageAppInfo(appInfoMessage);
|
|
|
|
|
|
|
|
cpp.vm.tracy.TracyProfiler.setThreadName("main");
|
|
|
|
}
|
|
|
|
#end
|
|
|
|
|
2023-01-23 00:55:30 +00:00
|
|
|
/**
|
|
|
|
* Dispatched when the game window is closed.
|
|
|
|
*/
|
|
|
|
public static final windowExit:FlxTypedSignal<Int->Void> = new FlxTypedSignal<Int->Void>();
|
2023-01-02 22:40:53 +00:00
|
|
|
|
2025-05-03 05:02:22 +00:00
|
|
|
/**
|
|
|
|
* Has `initWindowEvents()` been called already?
|
|
|
|
* This is to prevent multiple instances of the same function.
|
|
|
|
*/
|
|
|
|
private static var _initializedWindowEvents:Bool = false;
|
|
|
|
|
2023-06-25 16:36:21 +00:00
|
|
|
/**
|
|
|
|
* Wires up FlxSignals that happen based on window activity.
|
|
|
|
* For example, we can run a callback when the window is closed.
|
|
|
|
*/
|
2024-03-17 02:20:22 +00:00
|
|
|
public static function initWindowEvents():Void
|
2023-01-23 00:55:30 +00:00
|
|
|
{
|
2025-05-03 05:02:22 +00:00
|
|
|
if (_initializedWindowEvents) return; // Fix that annoying
|
2023-01-23 00:55:30 +00:00
|
|
|
// onUpdate is called every frame just before rendering.
|
2023-01-02 22:40:53 +00:00
|
|
|
|
2023-01-23 00:55:30 +00:00
|
|
|
// onExit is called when the game window is closed.
|
2023-03-02 01:27:29 +00:00
|
|
|
openfl.Lib.current.stage.application.onExit.add(function(exitCode:Int) {
|
2023-01-23 00:55:30 +00:00
|
|
|
windowExit.dispatch(exitCode);
|
|
|
|
});
|
2024-04-30 04:40:42 +00:00
|
|
|
|
2025-08-04 19:51:55 +00:00
|
|
|
#if (desktop || html5)
|
2024-04-30 04:40:42 +00:00
|
|
|
openfl.Lib.current.stage.addEventListener(openfl.events.KeyboardEvent.KEY_DOWN, (e:openfl.events.KeyboardEvent) -> {
|
2024-06-09 06:22:03 +00:00
|
|
|
for (key in PlayerSettings.player1.controls.getKeysForAction(WINDOW_FULLSCREEN))
|
2024-04-30 04:40:42 +00:00
|
|
|
{
|
2024-09-27 23:16:05 +00:00
|
|
|
// FlxG.stage.focus is set to null by the debug console stuff,
|
|
|
|
// so when that's in focus, we don't want to toggle fullscreen using F
|
|
|
|
// (annoying when tying "FlxG" in console... lol)
|
2024-10-04 12:20:39 +00:00
|
|
|
#if FLX_DEBUG
|
2024-09-27 23:16:05 +00:00
|
|
|
@:privateAccess
|
|
|
|
if (FlxG.game.debugger.visible)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2024-10-04 12:20:39 +00:00
|
|
|
#end
|
2024-09-27 23:16:05 +00:00
|
|
|
|
2024-04-30 04:40:42 +00:00
|
|
|
if (e.keyCode == key)
|
|
|
|
{
|
|
|
|
openfl.Lib.application.window.fullscreen = !openfl.Lib.application.window.fullscreen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2025-04-14 15:20:15 +00:00
|
|
|
#end
|
2025-07-22 13:24:23 +00:00
|
|
|
_initializedWindowEvents = true;
|
2023-01-23 00:55:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2025-08-03 09:00:27 +00:00
|
|
|
* Sets the title of the application window.
|
|
|
|
* @param value The title to use.
|
2023-01-23 00:55:30 +00:00
|
|
|
*/
|
2025-08-03 09:00:27 +00:00
|
|
|
public static function setWindowTitle(value:String):Void
|
2023-01-23 00:55:30 +00:00
|
|
|
{
|
2025-08-03 09:00:27 +00:00
|
|
|
lime.app.Application.current.window.title = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows an error dialog with an error icon.
|
|
|
|
* @param name The title of the dialog window.
|
|
|
|
* @param desc The error message to display.
|
|
|
|
*/
|
|
|
|
public static function showError(name:String, desc:String):Void
|
|
|
|
{
|
|
|
|
#if (windows && cpp)
|
|
|
|
funkin.external.windows.WinAPI.showError(desc, name);
|
2023-01-23 00:55:30 +00:00
|
|
|
#else
|
2025-08-03 09:00:27 +00:00
|
|
|
lime.app.Application.current.window.alert(desc, name);
|
2023-01-23 00:55:30 +00:00
|
|
|
#end
|
|
|
|
}
|
2023-06-22 05:41:01 +00:00
|
|
|
|
|
|
|
/**
|
2025-08-03 09:00:27 +00:00
|
|
|
* Shows a warning dialog with a warning icon.
|
|
|
|
* @param name The title of the dialog window.
|
|
|
|
* @param desc The warning message to display.
|
2023-06-22 05:41:01 +00:00
|
|
|
*/
|
2025-08-03 09:00:27 +00:00
|
|
|
public static function showWarning(name:String, desc:String):Void
|
2023-06-22 05:41:01 +00:00
|
|
|
{
|
2025-08-03 09:00:27 +00:00
|
|
|
#if (windows && cpp)
|
|
|
|
funkin.external.windows.WinAPI.showWarning(desc, name);
|
|
|
|
#else
|
|
|
|
lime.app.Application.current.window.alert(desc, name);
|
|
|
|
#end
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows an information dialog with an information icon.
|
|
|
|
* @param name The title of the dialog window.
|
|
|
|
* @param desc The information message to display.
|
|
|
|
*/
|
|
|
|
public static function showInformation(name:String, desc:String):Void
|
|
|
|
{
|
|
|
|
#if (windows && cpp)
|
|
|
|
funkin.external.windows.WinAPI.showInformation(desc, name);
|
|
|
|
#else
|
|
|
|
lime.app.Application.current.window.alert(desc, name);
|
|
|
|
#end
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows a question dialog with a question icon and OK/Cancel buttons.
|
|
|
|
* @param name The title of the dialog window.
|
|
|
|
* @param desc The question message to display.
|
|
|
|
*/
|
|
|
|
public static function showQuestion(name:String, desc:String):Void
|
|
|
|
{
|
|
|
|
#if (windows && cpp)
|
|
|
|
funkin.external.windows.WinAPI.showQuestion(desc, name);
|
|
|
|
#else
|
|
|
|
lime.app.Application.current.window.alert(desc, name);
|
|
|
|
#end
|
2023-06-22 05:41:01 +00:00
|
|
|
}
|
2025-02-20 22:49:35 +00:00
|
|
|
|
|
|
|
public static function setVSyncMode(value:lime.ui.WindowVSyncMode):Void
|
|
|
|
{
|
2025-04-15 03:19:07 +00:00
|
|
|
// vsync crap dont worky on mac rn derp
|
2025-05-03 08:52:29 +00:00
|
|
|
#if !mac
|
2025-04-14 17:07:22 +00:00
|
|
|
var res:Bool = FlxG.stage.application.window.setVSyncMode(value);
|
|
|
|
|
|
|
|
// SDL_GL_SetSwapInterval returns the value we assigned on success, https://wiki.libsdl.org/SDL2/SDL_GL_GetSwapInterval#return-value.
|
|
|
|
// In lime, we can compare this to the original value to get a boolean.
|
|
|
|
if (!res)
|
|
|
|
{
|
|
|
|
trace('Failed to set VSync mode to ' + value);
|
|
|
|
FlxG.stage.application.window.setVSyncMode(lime.ui.WindowVSyncMode.OFF);
|
|
|
|
}
|
2025-04-15 03:19:07 +00:00
|
|
|
#end
|
2025-02-20 22:49:35 +00:00
|
|
|
}
|
2022-03-27 02:18:26 +00:00
|
|
|
}
|