diff --git a/source/funkin/modding/IScriptedClass.hx b/source/funkin/modding/IScriptedClass.hx index 14aa6b494c..25e61996ca 100644 --- a/source/funkin/modding/IScriptedClass.hx +++ b/source/funkin/modding/IScriptedClass.hx @@ -37,6 +37,9 @@ interface IStateChangingScriptedClass extends IScriptedClass public function onSubStateOpenEnd(event:SubStateScriptEvent):Void; public function onSubStateCloseBegin(event:SubStateScriptEvent):Void; public function onSubStateCloseEnd(event:SubStateScriptEvent):Void; + + public function onFocusLost(event:FocusScriptEvent):Void; + public function onFocusGained(event:FocusScriptEvent):Void; } /** diff --git a/source/funkin/modding/events/ScriptEvent.hx b/source/funkin/modding/events/ScriptEvent.hx index 0624c2503a..139da64023 100644 --- a/source/funkin/modding/events/ScriptEvent.hx +++ b/source/funkin/modding/events/ScriptEvent.hx @@ -436,6 +436,22 @@ class StateChangeScriptEvent extends ScriptEvent } } +/** + * An event that is fired when the game loses or gains focus. + */ +class FocusScriptEvent extends ScriptEvent +{ + public function new(type:ScriptEventType):Void + { + super(type, false); + } + + public override function toString():String + { + return 'FocusScriptEvent(type=' + type + ')'; + } +} + /** * An event that is fired when moving out of or into an FlxSubState. */ diff --git a/source/funkin/modding/events/ScriptEventDispatcher.hx b/source/funkin/modding/events/ScriptEventDispatcher.hx index 7e19173c46..2bd9961251 100644 --- a/source/funkin/modding/events/ScriptEventDispatcher.hx +++ b/source/funkin/modding/events/ScriptEventDispatcher.hx @@ -177,6 +177,12 @@ class ScriptEventDispatcher case SUBSTATE_CLOSE_END: t.onSubStateCloseEnd(cast event); return; + case FOCUS_LOST: + t.onFocusLost(cast event); + return; + case FOCUS_GAINED: + t.onFocusGained(cast event); + return; default: // Continue; } } diff --git a/source/funkin/modding/events/ScriptEventType.hx b/source/funkin/modding/events/ScriptEventType.hx index 6ac85649f9..e44780dcfb 100644 --- a/source/funkin/modding/events/ScriptEventType.hx +++ b/source/funkin/modding/events/ScriptEventType.hx @@ -223,6 +223,20 @@ enum abstract ScriptEventType(String) from String to String */ var SUBSTATE_CLOSE_END = 'SUBSTATE_CLOSE_END'; + /** + * Called when the game regains focus. + * + * This event is not cancelable. + */ + var FOCUS_GAINED = 'FOCUS_GAINED'; + + /** + * Called when the game loses focus. + * + * This event is not cancelable. + */ + var FOCUS_LOST = 'FOCUS_LOST'; + /** * Called when the game starts a conversation. * diff --git a/source/funkin/modding/module/Module.hx b/source/funkin/modding/module/Module.hx index be9b7146b0..740e42f36e 100644 --- a/source/funkin/modding/module/Module.hx +++ b/source/funkin/modding/module/Module.hx @@ -109,6 +109,10 @@ class Module implements IPlayStateScriptedClass implements IStateChangingScripte public function onStateChangeEnd(event:StateChangeScriptEvent) {} + public function onFocusGained(event:FocusScriptEvent) {} + + public function onFocusLost(event:FocusScriptEvent) {} + public function onSubStateOpenBegin(event:SubStateScriptEvent) {} public function onSubStateOpenEnd(event:SubStateScriptEvent) {} diff --git a/source/funkin/ui/MusicBeatState.hx b/source/funkin/ui/MusicBeatState.hx index 8668b64c12..31e3bec509 100644 --- a/source/funkin/ui/MusicBeatState.hx +++ b/source/funkin/ui/MusicBeatState.hx @@ -87,6 +87,20 @@ class MusicBeatState extends FlxTransitionableState implements IEventHandler dispatchEvent(new UpdateScriptEvent(elapsed)); } + override function onFocus():Void + { + super.onFocus(); + + dispatchEvent(new FocusScriptEvent(FOCUS_GAINED)); + } + + override function onFocusLost():Void + { + super.onFocusLost(); + + dispatchEvent(new FocusScriptEvent(FOCUS_LOST)); + } + function createWatermarkText() { // Both have an xPos of 0, but a width equal to the full screen. diff --git a/source/funkin/ui/MusicBeatSubState.hx b/source/funkin/ui/MusicBeatSubState.hx index 37e5a31f7c..d046ab94b8 100644 --- a/source/funkin/ui/MusicBeatSubState.hx +++ b/source/funkin/ui/MusicBeatSubState.hx @@ -89,6 +89,20 @@ class MusicBeatSubState extends FlxSubState implements IEventHandler dispatchEvent(new UpdateScriptEvent(elapsed)); } + override function onFocus():Void + { + super.onFocus(); + + dispatchEvent(new FocusScriptEvent(FOCUS_GAINED)); + } + + override function onFocusLost():Void + { + super.onFocusLost(); + + dispatchEvent(new FocusScriptEvent(FOCUS_LOST)); + } + public function initConsoleHelpers():Void {} function reloadAssets()