Skip to content

Commit

Permalink
[ENHANCEMENT] Add Script Events for losing/gaining focus (#3721)
Browse files Browse the repository at this point in the history
* add events for focus gain/lost

* add callbacks

* move the events to a new interface

* Ok NOW it works

* the event can now longer be canceled
  • Loading branch information
AbnormalPoof authored Jan 17, 2025
1 parent 2b7f62e commit 4b127b6
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 0 deletions.
3 changes: 3 additions & 0 deletions source/funkin/modding/IScriptedClass.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
16 changes: 16 additions & 0 deletions source/funkin/modding/events/ScriptEvent.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
6 changes: 6 additions & 0 deletions source/funkin/modding/events/ScriptEventDispatcher.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
14 changes: 14 additions & 0 deletions source/funkin/modding/events/ScriptEventType.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
4 changes: 4 additions & 0 deletions source/funkin/modding/module/Module.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
Expand Down
14 changes: 14 additions & 0 deletions source/funkin/ui/MusicBeatState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 14 additions & 0 deletions source/funkin/ui/MusicBeatSubState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 4b127b6

Please sign in to comment.