Skip to content

Commit

Permalink
add experimental Mouse class (macOS)
Browse files Browse the repository at this point in the history
  • Loading branch information
gewang committed Nov 19, 2024
1 parent 03a2699 commit 61fc41e
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 7 deletions.
82 changes: 82 additions & 0 deletions src/core/chuck_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1496,6 +1496,51 @@ t_CKBOOL init_class_HID( Chuck_Env * env )
// end the class import
type_engine_import_class_end( env );
*/

// init Mouse class | 1.5.4.2 (ge & spencer) added
if( !type_engine_import_class_begin( env, "Mouse", "Object",
env->global(), Mouse_ctor, Mouse_dtor,
"Global mouse tracking object." ) )
return FALSE;

// add pos() | 1.5.4.2 (ge & spencer ) added
func = make_new_sfun( "vec2", "pos", Mouse_pos );
func->doc = "get the normalized X and Y positions of the mouse cursor, respectively in the range [0.0,1.0]; same as .xy().";
if( !type_engine_import_sfun( env, func ) ) goto error;

// add xy() | 1.5.4.2 (ge & spencer ) added
func = make_new_sfun( "vec2", "xy", Mouse_pos );
func->doc = "get the normalized X and Y positions of the mouse cursor, respectively in the range [0.0,1.0]; same as .pos().";
if( !type_engine_import_sfun( env, func ) ) goto error;

// add x() | 1.5.4.2 (ge & spencer ) added
func = make_new_sfun( "float", "x", Mouse_pos_x );
func->doc = "get the normalized X position of the mouse cursor, in the range [0.0,1.0].";
if( !type_engine_import_sfun( env, func ) ) goto error;

// add y() | 1.5.4.2 (ge & spencer ) added
func = make_new_sfun( "float", "y", Mouse_pos_y );
func->doc = "get the normalized Y positions of the mouse cursor, in the range [0.0,1.0].";
if( !type_engine_import_sfun( env, func ) ) goto error;

// add abs() | 1.5.4.2 (ge & spencer ) added
func = make_new_sfun( "vec2", "abs", Mouse_abs );
func->doc = "get the absolute X and Y coordinates of the mouse cursor; this is dependent on screen resolution.";
if( !type_engine_import_sfun( env, func ) ) goto error;

// add abs() | 1.5.4.2 (ge & spencer ) added
func = make_new_sfun( "vec2", "absX", Mouse_abs_x );
func->doc = "get the absolute X coordinate of the mouse cursor; this is dependent on screen resolution.";
if( !type_engine_import_sfun( env, func ) ) goto error;

// add abs() | 1.5.4.2 (ge & spencer ) added
func = make_new_sfun( "vec2", "absY", Mouse_abs_y );
func->doc = "get the absolute Y coordinate of the mouse cursor; this is dependent on screen resolution.";
if( !type_engine_import_sfun( env, func ) ) goto error;

// end the class import
type_engine_import_class_end( env );

return TRUE;

error:
Expand Down Expand Up @@ -3005,6 +3050,43 @@ CK_DLL_MFUN( HidOut_send )
// the_msg.data[2] = (t_CKBYTE)OBJ_MEMBER_INT(fake_msg, HidMsg_offset_data3);
RETURN->v_int = mout->send( &the_msg );
}

// Mouse constructor
CK_DLL_CTOR( Mouse_ctor ) { }
// Mouse desctructor
CK_DLL_DTOR( Mouse_dtor ) { }

// get normalized mouse XY position, range [0,1]
CK_DLL_SFUN( Mouse_pos )
{
RETURN->v_vec2 = ck_get_mouse_xy_normalize();
}

CK_DLL_SFUN( Mouse_pos_x )
{
RETURN->v_float = ck_get_mouse_xy_normalize().x;
}

CK_DLL_SFUN( Mouse_pos_y )
{
RETURN->v_float = ck_get_mouse_xy_normalize().y;
}

CK_DLL_SFUN( Mouse_abs )
{
RETURN->v_vec2 = ck_get_mouse_xy_absolute();
}

CK_DLL_SFUN( Mouse_abs_x )
{
RETURN->v_float = ck_get_mouse_xy_absolute().x;
}

CK_DLL_SFUN( Mouse_abs_y )
{
RETURN->v_float = ck_get_mouse_xy_absolute().y;
}

#endif // __DISABLE_HID__


Expand Down
13 changes: 12 additions & 1 deletion src/core/chuck_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,18 @@ CK_DLL_MFUN( HidOut_num );
CK_DLL_MFUN( HidOut_name );
CK_DLL_MFUN( HidOut_printerr );
CK_DLL_MFUN( HidOut_send );
#endif // __DISABLE_HID__

CK_DLL_CTOR( Mouse_ctor );
CK_DLL_DTOR( Mouse_dtor );
CK_DLL_SFUN( Mouse_pos );
CK_DLL_SFUN( Mouse_pos_x );
CK_DLL_SFUN( Mouse_pos_y );
CK_DLL_SFUN( Mouse_abs );
CK_DLL_SFUN( Mouse_abs_x );
CK_DLL_SFUN( Mouse_abs_y );

#endif // __DISABLE_HID__1




Expand Down
65 changes: 65 additions & 0 deletions src/core/util_hid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7800,7 +7800,72 @@ const char * Keyboard_name( int k )
#endif


//-----------------------------------------------------------------------------
// unified easy mouse functions
//-----------------------------------------------------------------------------
// name: ck_get_mouse_xy_normalize() | 1.5.4.2 (ge & spencer) added
// desc: get normalize [0,1] mouse cursor position
//-----------------------------------------------------------------------------
t_CKVEC2 ck_get_mouse_xy_normalize()
{
t_CKVEC2 retval;
#ifdef __PLATFORM_APPLE__
// get screen coords
CGEventRef cg_event = CGEventCreate(NULL);
CGPoint mouseLocation = CGEventGetLocation(cg_event);

t_CKFLOAT cursorX = (t_CKFLOAT)mouseLocation.x;
t_CKFLOAT cursorY = (t_CKFLOAT)mouseLocation.y;

CGDirectDisplayID display;
CGDisplayCount displayCount;

if( CGGetDisplaysWithPoint( mouseLocation, 1, &display, &displayCount ) == kCGErrorSuccess )
{
CGRect bounds = CGDisplayBounds( display );
retval.x = ( (t_CKFLOAT)(mouseLocation.x-bounds.origin.x) ) / bounds.size.width;
retval.y = ( (t_CKFLOAT)(mouseLocation.y-bounds.origin.y) ) / bounds.size.height;
}

// reclaim
CFRelease( cg_event );
#elif #defined(__PLATFORM_WINDOWS__)
#elif #defined(__PLATFORM_LINUX__)
#else
#endif

// done
return retval;
}
//-----------------------------------------------------------------------------
// name: ck_get_mouse_xy_absolute() | 1.5.4.2 (ge & spencer) added
// desc: get absolute mouse cursor position, dependent on screen resolution
//-----------------------------------------------------------------------------
t_CKVEC2 ck_get_mouse_xy_absolute()
{
t_CKVEC2 retval;
#ifdef __PLATFORM_APPLE__
// get screen coords
CGEventRef cg_event = CGEventCreate(NULL);
CGPoint mouseLocation = CGEventGetLocation(cg_event);
// return value
retval.x = (t_CKFLOAT)mouseLocation.x;
retval.y = (t_CKFLOAT)mouseLocation.y;
// reclaim
CFRelease( cg_event );
#elif #defined(__PLATFORM_WINDOWS__)
#elif #defined(__PLATFORM_LINUX__)
#else
#endif
return retval;
}




//-----------------------------------------------------------------------------
#pragma mark Hid graveyard
//-----------------------------------------------------------------------------
/***** empty functions for stuff that isn't yet cross platform *****/
#ifndef __PLATFORM_APPLE__
/*** empty functions for Mac-only stuff ***/
Expand Down
12 changes: 6 additions & 6 deletions src/core/util_hid.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
#include "chuck_def.h"




//-----------------------------------------------------------------------------
// definitions
//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -66,8 +64,6 @@ struct HidMsg
};




/* device types */
extern const t_CKUINT CK_HID_DEV_NONE;
extern const t_CKUINT CK_HID_DEV_JOYSTICK;
Expand Down Expand Up @@ -200,7 +196,6 @@ extern const char * TiltSensor_name( int ts );
t_CKINT TiltSensor_setPollRate( t_CKINT usec );
t_CKINT TiltSensor_getPollRate( );


extern void MultiTouchDevice_init();
extern void MultiTouchDevice_quit();
extern void MultiTouchDevice_probe();
Expand All @@ -209,7 +204,6 @@ extern int MultiTouchDevice_open( int ts );
extern int MultiTouchDevice_close( int ts );
extern const char * MultiTouchDevice_name( int ts );


extern void Tablet_init();
extern void Tablet_quit();
extern void Tablet_probe();
Expand All @@ -219,6 +213,12 @@ extern int Tablet_close( int ts );
extern const char * Tablet_name( int ts );


// get normalized [0,1] mouse cursor position
t_CKVEC2 ck_get_mouse_xy_normalize();
// get absolute mouse cursor position
t_CKVEC2 ck_get_mouse_xy_absolute();




#endif

0 comments on commit 61fc41e

Please sign in to comment.