<commit_before>package flixel.input.mouse;

import flash.errors.Error;
import flixel.FlxBasic;
import flixel.FlxCamera;
import flixel.FlxG;
import flixel.FlxObject;
import flixel.FlxSprite;
import flixel.group.FlxGroup;
import flixel.math.FlxAngle;
import flixel.util.FlxDestroyUtil;
import flixel.math.FlxPoint;

/**
 * Provides mouse event detection for FlxObjects and FlxSprites (pixel-perfect for those).
 * To use it, initialize the manager and register objects / sprites. 
 * 
 * 		FlxG.plugins.add(new FlxMouseEventManager());
 * 		var object = new FlxObject();
 * 		FlxMouseEventManager.add(object, onMouseDown, onMouseUp, onMouseOver, onMouseOut);
 * 
 * Or simply add a new object and this plugin will initialize itself: 
 * 
 *      FlxMouseEventManager.add(object, onMouseDown, onMouseUp, onMouseOver, onMouseOut);
 * 
 * Also implement the callbacks with the object's type as parameters:
 * 
 * 		function onMouseDown(object:FlxObject) {}
 * 		function onMouseUp(object:FlxObject) {}
 * 		function onMouseOver(object:FlxObject) {}
 * 		function onMouseOut(object:FlxObject) {} 
 * 
 * @author TiagoLr (~~~ ProG4mr ~~~)
 */
class FlxMouseEventManager extends FlxBasic
{
	private static var _registeredObjects:Array<ObjectMouseData<FlxObject>>;
	private static var _mouseOverObjects:Array<ObjectMouseData<FlxObject>>;

	private static var _point:FlxPoint;
	
	/**
	 * As alternative you can call FlxMouseEventManager.init().
	 */
	public static inline function init():Void
	{
		if (FlxG.plugins.get(FlxMouseEventManager) == null)
			FlxG.plugins.add(new FlxMouseEventManager());
	}
	
	/**
	 * Adds an object to the FlxMouseEventManager registry. Automatically initializes the plugin.
	 *
	 * @param	OnMouseDown		Callback when mouse is pressed down over this object. Must have Object as argument - e.g. onMouseDown(object:FlxObject).
	 * @param	OnMouseUp		Callback when mouse is released over this object. Must have Object as argument - e.g. onMouseDown(object:FlxObject).
	 * @param	OnMouseOver		Callback when mouse is this object. Must have Object as argument - e.g. onMouseDown(object:FlxObject).
	 * @param	OnMouseOut		Callback when mouse moves out of this object. Must have Object as argument - e.g. onMouseDown(object:FlxObject).
	 * @param	MouseChildren	If mouseChildren is enabled, other objects overlaped by this will still receive mouse events.
	 * @param	MouseEnabled	If mouseEnabled this object will receive mouse events.
	 * @param	PixelPerfect	If enabled the collision check will be pixel-perfect. Only works for FlxSprites.
	 */
	public static function add<T:FlxObject>(Object:T, ?OnMouseDown:T->Void, ?OnMouseUp:T->Void, ?OnMouseOver:T->Void, ?OnMouseOut:T->Void, 
	                                        MouseChildren = false, MouseEnabled = true, PixelPerfect = true):T
	{
		init(); // MEManager is initialized and added to plugins if it was not there already.
		
		var newReg = new ObjectMouseData<T>(Object, OnMouseDown, OnMouseUp, OnMouseOver, OnMouseOut, MouseChildren, MouseEnabled, PixelPerfect);
		
		if (Std.is(Object, FlxSprite))
		{
			newReg.sprite = cast Object;
		}
		
		_registeredObjects.unshift(cast newReg);
		return Object;
	}
	
	/**
	 * Removes a sprite from the registry.
	 */
	public static function remove<T:FlxObject>(Object:T):T
	{
		for (reg in _registeredObjects)
		{
			if (reg.object == Object)
			{
				reg.object = null;
				reg.sprite = null;
				reg.onMouseDown = null;
				reg.onMouseUp = null;
				reg.onMouseOver = null;
				reg.onMouseOut = null;
				_registeredObjects.remove(reg);
			}
		}
		return Object;
	}

	/**
	 * Reorders the registered objects, using the current object drawing order.
	 * This should be called if you alter the draw/update order of a registered object,
	 * That is, if you alter the position of a registered object inside its FlxGroup.
	 * It may also be called if the objects are not registered by the same order they are
	 * added to FlxGroup.
	 */<commit_msg>Added removeAll to simplify resource cleanup.<commit_after>package flixel.input.mouse;

import flash.errors.Error;
import flixel.FlxBasic;
import flixel.FlxCamera;
import flixel.FlxG;
import flixel.FlxObject;
import flixel.FlxSprite;
import flixel.group.FlxGroup;
import flixel.math.FlxAngle;
import flixel.util.FlxDestroyUtil;
import flixel.math.FlxPoint;

/**
 * Provides mouse event detection for FlxObjects and FlxSprites (pixel-perfect for those).
 * To use it, initialize the manager and register objects / sprites. 
 * 
 * 		FlxG.plugins.add(new FlxMouseEventManager());
 * 		var object = new FlxObject();
 * 		FlxMouseEventManager.add(object, onMouseDown, onMouseUp, onMouseOver, onMouseOut);
 * 
 * Or simply add a new object and this plugin will initialize itself: 
 * 
 *      FlxMouseEventManager.add(object, onMouseDown, onMouseUp, onMouseOver, onMouseOut);
 * 
 * Also implement the callbacks with the object's type as parameters:
 * 
 * 		function onMouseDown(object:FlxObject) {}
 * 		function onMouseUp(object:FlxObject) {}
 * 		function onMouseOver(object:FlxObject) {}
 * 		function onMouseOut(object:FlxObject) {} 
 * 
 * @author TiagoLr (~~~ ProG4mr ~~~)
 */
class FlxMouseEventManager extends FlxBasic
{
	private static var _registeredObjects:Array<ObjectMouseData<FlxObject>>;
	private static var _mouseOverObjects:Array<ObjectMouseData<FlxObject>>;

	private static var _point:FlxPoint;
	
	/**
	 * As alternative you can call FlxMouseEventManager.init().
	 */
	public static inline function init():Void
	{
		if (FlxG.plugins.get(FlxMouseEventManager) == null)
			FlxG.plugins.add(new FlxMouseEventManager());
	}
	
	/**
	 * Adds an object to the FlxMouseEventManager registry. Automatically initializes the plugin.
	 *
	 * @param	OnMouseDown		Callback when mouse is pressed down over this object. Must have Object as argument - e.g. onMouseDown(object:FlxObject).
	 * @param	OnMouseUp		Callback when mouse is released over this object. Must have Object as argument - e.g. onMouseDown(object:FlxObject).
	 * @param	OnMouseOver		Callback when mouse is this object. Must have Object as argument - e.g. onMouseDown(object:FlxObject).
	 * @param	OnMouseOut		Callback when mouse moves out of this object. Must have Object as argument - e.g. onMouseDown(object:FlxObject).
	 * @param	MouseChildren	If mouseChildren is enabled, other objects overlaped by this will still receive mouse events.
	 * @param	MouseEnabled	If mouseEnabled this object will receive mouse events.
	 * @param	PixelPerfect	If enabled the collision check will be pixel-perfect. Only works for FlxSprites.
	 */
	public static function add<T:FlxObject>(Object:T, ?OnMouseDown:T->Void, ?OnMouseUp:T->Void, ?OnMouseOver:T->Void, ?OnMouseOut:T->Void, 
	                                        MouseChildren = false, MouseEnabled = true, PixelPerfect = true):T
	{
		init(); // MEManager is initialized and added to plugins if it was not there already.
		
		var newReg = new ObjectMouseData<T>(Object, OnMouseDown, OnMouseUp, OnMouseOver, OnMouseOut, MouseChildren, MouseEnabled, PixelPerfect);
		
		if (Std.is(Object, FlxSprite))
		{
			newReg.sprite = cast Object;
		}
		
		_registeredObjects.unshift(cast newReg);
		return Object;
	}
	
	/**
	 * Removes a sprite from the registry.
	 */
	public static function remove<T:FlxObject>(Object:T):T
	{
		for (reg in _registeredObjects)
		{
			if (reg.object == Object)
			{
				reg.object = null;
				reg.sprite = null;
				reg.onMouseDown = null;
				reg.onMouseUp = null;
				reg.onMouseOver = null;
				reg.onMouseOut = null;
				_registeredObjects.remove(reg);
			}
		}
		return Object;
	}

	/**
	 * Removes all sprites from the registry.
	 */
	public static function removeAll():Void {
		if (_registeredObjects != null)
		{
			for (reg in _registeredObjects)
			{
				remove(reg.object);
			}
		}
		_registeredObjects = new Array<ObjectMouseData<FlxObject>>();
		_mouseOverObjects = new Array<ObjectMouseData<FlxObject>>();
	}

	/**
	 * Reorders the registered objects, using the current object drawing order.
	 * This should be called if you alter the draw/update order of a registered object,
	 * That is, if you alter the position of a registered object inside its FlxGroup.
	 * It may also be called if the objects are not registered by the same order they are
	 * added to FlxGroup.
	 */
{'fraction_non_alphanumeric': 0.0931609473175447, 'fraction_numerical': 0.0002416626389560174, 'mean_word_length': 3.986144578313253, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7309015', 'n_tokens_mistral': 2726, 'n_tokens_neox': 2488, 'n_words': 791}