2025-04-14 15:20:15 +00:00
// Hey!! Slight note from little me, Zack, I got rid of the for loops because essentially the functions just return the first touch they find back then.
// Now it looks more clean but the descriptions for some variables are inaccurate. Well they were already inaccurate but still.
// - Zack xoxo
//
// Another slight note from little me, it turns out it WAS accurate, my ass during clean up just completely forgot. My fault!! I gotta focus on multi-touch next time.
// Easy to fix but I'll tackle it for later.
// - Zack
2024-12-15 14:53:02 +00:00
package funkin . util ;
2025-04-14 15:20:15 +00:00
import flixel . FlxBasic ;
import flixel . FlxCamera ;
import flixel . FlxG ;
import flixel . FlxObject ;
#if FLX_TOUCH
import flixel . input . touch . FlxTouch ;
#end
import flixel . input . mouse . FlxMouse ;
import flixel . math . FlxPoint ;
/ * *
* Utility class f o r h a n d l i n g t o u c h i n p u t w i t h i n t h e FlxG c o n t e x t .
* /
class TouchUtil
{
/ * *
* Indicates if a n y t o u c h i s c u r r e n t l y p r e s s e d .
* /
public static var pressed( get , never ) : Bool ;
/ * *
* Indicates if a n y t o u c h w a s j u s t p r e s s e d t h i s f r a m e .
* /
public static var justPressed( get , never ) : Bool ;
/ * *
* Indicates if a n y t o u c h w a s j u s t r e l e a s e d t h i s f r a m e .
* /
public static var justReleased( get , never ) : Bool ;
/ * *
* Indicates if a n y t o u c h i s r e l e a s e d t h i s f r a m e .
* /
public static var released( get , never ) : Bool ;
/ * *
* Indicates if a n y t o u c h i s m o v e d t h i s f r a m e .
* /
public static var justMoved( get , never ) : Bool ;
/ * *
* The first touch in the FlxG . touches list .
* /
#if mobile
public static var touch( get , never ) : FlxTouch ;
#else
public static var touch( get , never ) : FlxMouse ;
#end
2025-04-14 17:32:31 +00:00
// static var _touchTween:FlxTween;
2025-04-14 15:20:15 +00:00
/ * *
* Checks if t h e s p e c i f i e d o b j e c t o v e r l a p s w i t h a n y a c t i v e t o u c h .
*
* @ param object The FlxBasic object to check for o v e r l a p .
* @ param camera Optional camera for t h e o v e r l a p c h e c k . D e f a u l t s t o t h e o b j e c t ' s c a m e r a .
*
* @ return ` true ` if t h e r e i s a n o v e r l a p w i t h a n y t o u c h ; ` f a l s e ` o t h e r w i s e .
* /
2025-05-02 19:03:45 +00:00
public static function overlaps ( ? object : FlxBasic , ? camera : FlxCamera ) : Bool
2025-04-14 15:20:15 +00:00
{
if ( object == null || touch == null ) return false ;
return touch . overlaps ( object , camera ? ? object . camera ) ;
2025-04-14 17:32:31 +00:00
return false ;
2025-04-14 15:20:15 +00:00
}
/ * *
* Checks if t h e s p e c i f i e d o b j e c t o v e r l a p s w i t h a n y a c t i v e t o u c h u s i n g p r e c i s e p o i n t c h e c k s .
*
* @ param object The FlxObject to check for o v e r l a p .
* @ param camera Optional camera for t h e o v e r l a p c h e c k . D e f a u l t s t o a l l c a m e r a s o f t h e o b j e c t .
*
* @ return ` true ` if t h e r e i s a p r e c i s e o v e r l a p w i t h a n y t o u c h ; ` f a l s e ` o t h e r w i s e .
* /
2025-05-02 19:03:45 +00:00
public static function overlapsComplex ( ? object : FlxObject , ? camera : FlxCamera ) : Bool
2025-04-14 15:20:15 +00:00
{
if ( object == null || touch == null ) return false ;
if ( camera == null ) camera = object . cameras [ 0 ] ;
@ : privateAccess
return object . overlapsPoint ( touch . getWorldPosition ( camera , object . _point ) , true , camera ) ;
2025-04-14 17:32:31 +00:00
return false ;
2025-04-14 15:20:15 +00:00
}
/ * *
* Checks if t h e s p e c i f i e d o b j e c t o v e r l a p s w i t h a s p e c i f i c p o i n t u s i n g p r e c i s e p o i n t c h e c k s .
*
* @ param object The FlxObject to check for o v e r l a p .
* @ param point The FlxPoint to check against the object .
* @ param inScreenSpace Whether to take scroll factors into account when checking for o v e r l a p .
* @ param camera Optional camera for t h e o v e r l a p c h e c k . D e f a u l t s t o a l l c a m e r a s o f t h e o b j e c t .
*
* @ return ` true ` if t h e r e i s a p r e c i s e o v e r l a p w i t h t h e s p e c i f i e d p o i n t ; ` f a l s e ` o t h e r w i s e .
* /
2025-05-02 19:03:45 +00:00
public static function overlapsComplexPoint ( ? object : FlxObject , point : FlxPoint , ? inScreenSpace : Bool = false , ? camera : FlxCamera ) : Bool
2025-04-14 15:20:15 +00:00
{
if ( object == null || point == null ) return false ;
if ( camera == null ) camera = object . cameras [ 0 ] ;
@ : privateAccess
if ( object . overlapsPoint ( point , inScreenSpace , camera ) )
{
point . putWeak ( ) ;
return true ;
}
point . putWeak ( ) ;
2025-04-14 17:32:31 +00:00
2025-04-14 15:20:15 +00:00
return false ;
}
2025-05-09 10:28:31 +00:00
/ * *
* A helper function to c h e c k i f t h e s e l e c t i o n i s p r e s s e d u s i n g t o u c h .
*
* @ param object The optional FlxBasic to check for o v e r l a p .
* @ param camera Optional camera for t h e o v e r l a p c h e c k . D e f a u l t s t o a l l c a m e r a s o f t h e o b j e c t .
* @ param useOverlapsComplex If true and atleast the object is not null , the function will u s e c o m p l e x o v e r l a p s m e t h o d .
* /
public static function pressAction ( ? object : FlxBasic , ? camera : FlxCamera , useOverlapsComplex : Bool = true ) : Bool
{
if ( TouchUtil . touch == null || ( TouchUtil . touch != null && TouchUtil . touch . ticksDeltaSincePress > 200 ) ) return false ;
if ( object == null && camera == null )
{
return justReleased ;
}
e lse if ( object != null )
{
final overlapsObject : Bool = useOverlapsComplex ? overlapsComplex ( cast ( object , FlxObject ) , camera ) : overlaps ( object , camera ) ;
return justReleased && overlapsObject ;
}
return false ;
}
2025-04-14 15:20:15 +00:00
// weird mix between "looks weird" and "looks neat" but i'll keep it for now -Zack
@ : noCompletion
inline static function get_justMoved ( ) : Bool
return touch != null && touch . justMoved ;
@ : noCompletion
inline static function get_pressed ( ) : Bool
return touch != null && touch . pressed ;
@ : noCompletion
inline static function get_justPressed ( ) : Bool
return touch != null && touch . justPressed ;
@ : noCompletion
inline static function get_justReleased ( ) : Bool
return touch != null && touch . justReleased ;
@ : noCompletion
static function get_released ( ) : Bool
return touch != null && touch . released ;
#if mobile
@ : noCompletion
static function get_touch ( ) : FlxTouch
{
for ( touch in FlxG . touches . list )
{
if ( touch != null ) return touch ;
}
return FlxG . touches . getFirst ( ) ;
}
#else
@ : noCompletion
2025-06-28 03:46:00 +00:00
static function get_touch ( ) : FlxMouse
2025-04-14 17:32:31 +00:00
{
FlxG . mouse . visible = true ;
2025-04-14 15:20:15 +00:00
return FlxG . mouse ;
2025-04-14 17:32:31 +00:00
}
2025-04-14 15:20:15 +00:00
#end
}