Skip to content

Commit

Permalink
deprecate old plugin functions, add replcements (#2952)
Browse files Browse the repository at this point in the history
* deprecate old plugin functions, add replcements

* add info to warning
  • Loading branch information
Geokureli authored Nov 16, 2023
1 parent 5ebdcbb commit fe42dc0
Showing 1 changed file with 65 additions and 46 deletions.
111 changes: 65 additions & 46 deletions flixel/system/frontEnds/PluginFrontEnd.hx
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,65 @@ class PluginFrontEnd

/**
* Adds a new plugin to the global plugin array.
* **DEPRECATED:** In a later version this will be changed to behave like `addPlugin`.
*
* @param Plugin Any object that extends FlxBasic. Useful for managers and other things.
* @return The same plugin you passed in.
* @param plugin Any object that extends FlxBasic. Useful for managers and other things.
* @return The same plugin you passed in.
*/
@:generic
public function add<T:FlxBasic>(Plugin:T):T
@:deprecated("FlxG.plugins.add is deprecated, use `addIfUniqueType` or `addPlugin`, instead.\nNote: In a later version `add` will be changed to behave like `addPlugin`")
public inline function add<T:FlxBasic>(plugin:T):T
{
// Don't add repeats
for (plugin in list)
return addIfUniqueType(plugin);
}

/**
* Adds a new plugin to the global plugin array, does not check for existing instances of this type.
* **Note:** This is a temporary function. Eventually `add` will allow duplicates
*
* @param plugin Any object that extends FlxBasic. Useful for managers and other things.
* @return The same plugin you passed in.
*/
public function addPlugin<T:FlxBasic>(plugin:T):T
{
// No repeats found
list.push(plugin);
return plugin;
}

/**
* Adds a new plugin to the global plugin array.
* **Note:** If there is already a plugin of this type, it will not be added
*
* @param plugin Any object that extends FlxBasic. Useful for managers and other things.
* @return The same plugin you passed in.
*/
public function addIfUniqueType<T:FlxBasic>(plugin:T):T
{
// Check for repeats
for (p in list)
{
if (FlxStringUtil.sameClassName(Plugin, plugin))
{
return Plugin;
}
if (FlxStringUtil.sameClassName(plugin, p))
return plugin;
}

// No repeats! safe to add a new instance of this plugin
list.push(Plugin);
return Plugin;
// No repeats found
list.push(plugin);
return plugin;
}

/**
* Retrieves a plugin based on its class name from the global plugin array.
* Retrieves the first plugin found that matches the specified type.
*
* @param ClassType The class name of the plugin you want to retrieve. See the FlxPath or FlxTimer constructors for example usage.
* @return The plugin object, or null if no matching plugin was found.
* @param type The class name of the plugin you want to retrieve.
* See the `FlxPath` or `FlxTimer` constructors for example usage.
* @return The plugin object, or null if no matching plugin was found.
*/
public function get<T:FlxBasic>(ClassType:Class<T>):T
public inline function get<T:FlxBasic>(type:Class<T>):T
{
for (plugin in list)
{
if (Std.isOfType(plugin, ClassType))
if (Std.isOfType(plugin, type))
{
return cast plugin;
}
Expand All @@ -61,47 +88,39 @@ class PluginFrontEnd
/**
* Removes an instance of a plugin from the global plugin array.
*
* @param Plugin The plugin instance you want to remove.
* @return The same plugin you passed in.
* @param plugin The plugin instance you want to remove.
* @return The same plugin you passed in.
*/
public function remove<T:FlxBasic>(Plugin:T):T
public inline function remove<T:FlxBasic>(plugin:T):T
{
// Don't add repeats
var i:Int = list.length - 1;

while (i >= 0)
{
if (list[i] == Plugin)
{
list.splice(i, 1);
return Plugin;
}
i--;
}

return Plugin;
list.remove(plugin);
return plugin;
}

/**
* Removes all instances of a plugin from the global plugin array.
*
* @param ClassType The class name of the plugin type you want removed from the array.
* @return Whether or not at least one instance of this plugin type was removed.
* @param type The class name of the plugin type you want removed from the array.
* @return Whether or not at least one instance of this plugin type was removed.
*/
public function removeType(ClassType:Class<FlxBasic>):Bool
@:deprecated("FlxG.plugin.removeType is deprecated, use `removeAllByType` instead")
public inline function removeType(type:Class<FlxBasic>):Bool
{
return removeAllByType(type);
}

public function removeAllByType(type:Class<FlxBasic>):Bool
{
// Don't add repeats
var results:Bool = false;
var i:Int = list.length - 1;

while (i >= 0)
var i = list.length;
while (i-- > 0)
{
if (Std.isOfType(list[i], ClassType))
if (Std.isOfType(list[i], type))
{
list.splice(i, 1);
results = true;
}
i--;
}

return results;
Expand All @@ -110,9 +129,9 @@ class PluginFrontEnd
@:allow(flixel.FlxG)
function new()
{
add(FlxTimer.globalManager = new FlxTimerManager());
add(FlxTween.globalManager = new FlxTweenManager());
add(FlxMouseEvent.globalManager = new FlxMouseEventManager());
addPlugin(FlxTimer.globalManager = new FlxTimerManager());
addPlugin(FlxTween.globalManager = new FlxTweenManager());
addPlugin(FlxMouseEvent.globalManager = new FlxMouseEventManager());
}

/**
Expand Down

0 comments on commit fe42dc0

Please sign in to comment.