Files
fs2open.github.com/code/io/mouse.cpp
T
asarium 7e77b5a67c Merge branch 'master' into antipodes
Conflicts:
	.gitignore
	code/Makefile.am
	code/ai/ai.h
	code/ai/ai_profiles.cpp
	code/ai/ai_profiles.h
	code/ai/aibig.cpp
	code/ai/aicode.cpp
	code/ai/aiturret.cpp
	code/asteroid/asteroid.cpp
	code/asteroid/asteroid.h
	code/bmpman/bm_internal.h
	code/bmpman/bmpman.cpp
	code/bmpman/bmpman.h
	code/camera/camera.cpp
	code/camera/camera.h
	code/cfile/cfile.cpp
	code/cmdline/cmdline.cpp
	code/cmdline/cmdline.h
	code/controlconfig/controlsconfig.h
	code/controlconfig/controlsconfigcommon.cpp
	code/cutscene/movie.cpp
	code/cutscene/oggplayer.cpp
	code/debugconsole/consoleparse.cpp
	code/fred2/campaigneditordlg.cpp
	code/fred2/fred.cpp
	code/fred2/fred.rc
	code/fred2/missionsave.cpp
	code/fred2/resource.h
	code/fred2/sexp_tree.cpp
	code/fred2/sexp_tree.h
	code/fred2/shipflagsdlg.cpp
	code/fred2/shipflagsdlg.h
	code/fred2/shiptexturesdlg.cpp
	code/fred2/voiceactingmanager.cpp
	code/freespace2/freespace.cpp
	code/freespace2/freespace.rc
	code/gamehelp/contexthelp.cpp
	code/globalincs/alphacolors.cpp
	code/globalincs/alphacolors.h
	code/globalincs/version.h
	code/globalincs/vmallocator.h
	code/globalincs/windebug.cpp
	code/graphics/2d.cpp
	code/graphics/font.cpp
	code/graphics/generic.cpp
	code/graphics/generic.h
	code/graphics/grinternal.h
	code/graphics/gropengl.cpp
	code/graphics/gropengldraw.cpp
	code/graphics/gropenglextension.h
	code/graphics/gropenglpostprocessing.cpp
	code/graphics/gropenglstate.cpp
	code/graphics/gropenglstate.h
	code/graphics/gropengltexture.cpp
	code/graphics/gropengltnl.cpp
	code/hud/hud.cpp
	code/hud/hud.h
	code/hud/hudconfig.cpp
	code/hud/hudlock.cpp
	code/hud/hudmessage.cpp
	code/hud/hudparse.cpp
	code/hud/hudreticle.cpp
	code/hud/hudreticle.h
	code/hud/hudshield.cpp
	code/hud/hudsquadmsg.cpp
	code/hud/hudtarget.cpp
	code/hud/hudtarget.h
	code/hud/hudtargetbox.cpp
	code/inetfile/cftp.cpp
	code/inetfile/chttpget.cpp
	code/io/joy.cpp
	code/io/joy_ff.cpp
	code/io/key.cpp
	code/io/keycontrol.cpp
	code/io/mouse.cpp
	code/io/timer.cpp
	code/localization/localize.cpp
	code/localization/localize.h
	code/math/fvi.cpp
	code/math/vecmat.cpp
	code/menuui/credits.cpp
	code/menuui/mainhallmenu.cpp
	code/menuui/mainhallmenu.h
	code/menuui/optionsmenumulti.cpp
	code/menuui/playermenu.cpp
	code/mission/missionbriefcommon.cpp
	code/mission/missioncampaign.cpp
	code/mission/missiongoals.h
	code/mission/missionmessage.cpp
	code/mission/missionparse.cpp
	code/mission/missionparse.h
	code/missionui/missionbrief.cpp
	code/missionui/missioncmdbrief.cpp
	code/missionui/missiondebrief.cpp
	code/missionui/missionscreencommon.cpp
	code/missionui/missionshipchoice.cpp
	code/missionui/missionweaponchoice.cpp
	code/missionui/redalert.cpp
	code/missionui/redalert.h
	code/mod_table/mod_table.cpp
	code/mod_table/mod_table.h
	code/model/model.h
	code/model/modelcollide.cpp
	code/model/modelinterp.cpp
	code/model/modelread.cpp
	code/network/multi.cpp
	code/network/multi_dogfight.cpp
	code/network/multi_ingame.cpp
	code/network/multi_log.cpp
	code/network/multi_rate.cpp
	code/network/multiteamselect.cpp
	code/network/multiui.cpp
	code/network/multiutil.cpp
	code/network/stand_gui-unix.cpp
	code/object/collideshipship.cpp
	code/object/collideshipweapon.cpp
	code/object/object.cpp
	code/object/object.h
	code/object/objectsnd.cpp
	code/object/objectsort.cpp
	code/osapi/osapi.cpp
	code/osapi/osapi_unix.cpp
	code/osapi/outwnd.h
	code/parse/generic_log.cpp
	code/parse/lua.cpp
	code/parse/parselo.cpp
	code/parse/parselo.h
	code/parse/scripting.cpp
	code/parse/scripting.h
	code/parse/sexp.cpp
	code/parse/sexp.h
	code/physics/physics.cpp
	code/pilotfile/csg.cpp
	code/pilotfile/csg_convert.cpp
	code/pilotfile/pilotfile.cpp
	code/pilotfile/pilotfile_convert.h
	code/playerman/playercontrol.cpp
	code/radar/radardradis.cpp
	code/radar/radardradis.h
	code/radar/radarsetup.cpp
	code/radar/radarsetup.h
	code/ship/ship.cpp
	code/ship/ship.h
	code/ship/shipfx.cpp
	code/ship/shiphit.cpp
	code/sound/audiostr.cpp
	code/sound/ds.cpp
	code/sound/openal.cpp
	code/sound/rtvoice.cpp
	code/sound/sound.cpp
	code/sound/voicerec.cpp
	code/stats/medals.cpp
	code/stats/medals.h
	code/stats/scoring.cpp
	code/ui/ui.h
	code/weapon/beam.cpp
	code/weapon/weapon.h
	code/weapon/weapons.cpp
	code/wxfred2/aboutbox.cpp
	code/wxfred2/campaigneditor.cpp
	code/wxfred2/campaigneditor.h
	code/wxfred2/commandbriefingeditor.cpp
	code/wxfred2/debriefingeditor.cpp
	code/wxfred2/fredframe.cpp
	code/wxfred2/fredframe.h
	code/wxfred2/missionspecseditor.cpp
	code/wxfred2/missionspecseditor.h
	code/wxfred2/voiceactingmanagereditor.cpp
	code/wxfred2/wxfred.xrc
	code/wxfred2/wxfred2.cpp
	code/wxfred2/wxfred2.h
	configure.ac
	fs2open.Doxyfile
	jansson/LICENSE
	jansson/dump.c
	jansson/hashtable.c
	jansson/hashtable.h
	jansson/jansson.h
	jansson/jansson_config.h
	jansson/jansson_private.h
	jansson/load.c
	jansson/memory.c
	jansson/pack_unpack.c
	jansson/strbuffer.c
	jansson/strbuffer.h
	jansson/strconv.c
	jansson/utf.c
	jansson/utf.h
	jansson/value.c
	libpng/png.c
	libpng/png.h
	libpng/pngconf.h
	libpng/pngdebug.h
	libpng/pngerror.c
	libpng/pngget.c
	libpng/pnginfo.h
	libpng/pnglibconf.h
	libpng/pngmem.c
	libpng/pngpread.c
	libpng/pngpriv.h
	libpng/pngread.c
	libpng/pngrio.c
	libpng/pngrtran.c
	libpng/pngrutil.c
	libpng/pngset.c
	libpng/pngstruct.h
	libpng/pngtrans.c
	libpng/pngwio.c
	libpng/pngwrite.c
	libpng/pngwtran.c
	libpng/pngwutil.c
	projects/Eclipse/Freespace2/.project
	projects/MSVC_2005/Fred2.vcproj
	projects/MSVC_2005/Freespace2.sln
	projects/MSVC_2005/Freespace2.vcproj
	projects/MSVC_2005/code.vcproj
	projects/MSVC_2005/wxFRED2.vcproj
	projects/MSVC_2008/Fred2.vcproj
	projects/MSVC_2008/Freespace2.doxproj
	projects/MSVC_2008/Freespace2.vcproj
	projects/MSVC_2008/code.vcproj
	projects/MSVC_2010/Fred2.vcxproj
	projects/MSVC_2010/Freespace2.vcxproj
	projects/MSVC_2010/code.vcxproj
	projects/MSVC_2010/code.vcxproj.filters
	projects/MSVC_2010/wxFRED2.vcxproj
	projects/MSVC_2011/Fred2.vcxproj
	projects/MSVC_2011/Freespace2.vcxproj
	projects/MSVC_2011/code.vcxproj
	projects/MSVC_2011/code.vcxproj.filters
	projects/MSVC_2011/wxFRED2.vcxproj
	projects/MSVC_2013/Fred2.vcxproj
	projects/MSVC_2013/Freespace2.vcxproj
	projects/MSVC_2013/code.vcxproj
	projects/MSVC_2013/code.vcxproj.filters
	projects/MSVC_2013/libjpeg.vcxproj
	projects/MSVC_2013/liblua.vcxproj
	projects/MSVC_2013/libpng.vcxproj
	projects/MSVC_2013/wxFRED2.vcxproj
	projects/MSVC_2013/zlib.vcxproj
	projects/MSVC_6/Fred2.dsp
	projects/MSVC_6/Freespace2.dsp
	projects/MSVC_6/code.dsp
	projects/MSVC_6/wxFRED2.dsp
	projects/Xcode/English.lproj/InfoPlist.strings
	projects/Xcode/FS2_Open.xcodeproj/project.pbxproj
	projects/Xcode4/English.lproj/InfoPlist.strings
	projects/Xcode4/FS2_Open.xcodeproj/project.pbxproj
	projects/Xcode4/Frameworks.tgz
	projects/codeblocks/Freespace2/Freespace2.cbp
	zlib/adler32.c
	zlib/compress.c
	zlib/crc32.c
	zlib/crc32.h
	zlib/deflate.c
	zlib/deflate.h
	zlib/infback.c
	zlib/inffast.c
	zlib/inffixed.h
	zlib/inflate.c
	zlib/inftrees.c
	zlib/trees.c
	zlib/uncompr.c
	zlib/zconf.h
	zlib/zlib.h
	zlib/zutil.c
	zlib/zutil.h
2015-04-25 16:48:43 +02:00

453 lines
7.9 KiB
C++

/*
* Copyright (C) Volition, Inc. 1999. All rights reserved.
*
* All source code herein is the property of Volition, Inc. You may not sell
* or otherwise commercially exploit the source or things you created based on the
* source.
*
*/
#ifdef _WIN32
#include <windows.h>
#include <windowsx.h>
#endif
#include "io/mouse.h"
#include "graphics/2d.h"
#include "parse/scripting.h"
#define THREADED // to use the proper set of macros
#include "osapi/osapi.h"
#include "cmdline/cmdline.h"
int mouse_inited = 0;
LOCAL int Mouse_x;
LOCAL int Mouse_y;
SDL_mutex* mouse_lock;
int mouse_flags;
int mouse_left_pressed = 0;
int mouse_right_pressed = 0;
int mouse_middle_pressed = 0;
int mouse_left_up = 0;
int mouse_right_up = 0;
int mouse_middle_up = 0;
int Mouse_dx = 0;
int Mouse_dy = 0;
int Mouse_dz = 0;
int Last_mouse_dx = 0;
int Last_mouse_dy = 0;
int Last_mouse_dz = 0;
int Mouse_sensitivity = 4;
int Use_mouse_to_fly = 0;
void mouse_force_pos(int x, int y);
static bool Mouse_in_focus = true;
void mouse_got_focus()
{
if ( !mouse_inited ) return;
Mouse_in_focus = true;
mouse_flush();
}
void mouse_lost_focus()
{
if ( !mouse_inited ) return;
Mouse_in_focus = false;
mouse_flush();
}
void mouse_close()
{
if (!mouse_inited)
return;
mouse_inited = 0;
SDL_DestroyMutex( mouse_lock );
}
void mouse_init()
{
// Initialize queue
if ( mouse_inited ) return;
mouse_inited = 1;
mouse_lock = SDL_CreateMutex();
SDL_LockMutex( mouse_lock );
mouse_flags = 0;
Mouse_x = gr_screen.max_w / 2;
Mouse_y = gr_screen.max_h / 2;
// we do want to make sure that button presses go through event polling though
// (should be on by default already, just here as a reminder)
SDL_EventState( SDL_MOUSEBUTTONDOWN, SDL_ENABLE );
SDL_EventState( SDL_MOUSEBUTTONUP, SDL_ENABLE );
SDL_UnlockMutex( mouse_lock );
atexit( mouse_close );
}
// ----------------------------------------------------------------------------
// mouse_mark_button() is called asynchronously by the OS when a mouse button
// goes up or down. The mouse button that is affected is passed via the
// flags parameter.
//
// parameters: flags ==> mouse button pressed/released
// set ==> 1 - button is pressed
// 0 - button is released
void mouse_mark_button( uint flags, int set)
{
if ( !mouse_inited ) return;
SDL_LockMutex( mouse_lock );
if ( !(mouse_flags & MOUSE_LEFT_BUTTON) ) {
if ( (flags & MOUSE_LEFT_BUTTON) && (set == 1) ) {
mouse_left_pressed++;
}
}
else {
if ( (flags & MOUSE_LEFT_BUTTON) && (set == 0) ){
mouse_left_up++;
}
}
if ( !(mouse_flags & MOUSE_RIGHT_BUTTON) ) {
if ( (flags & MOUSE_RIGHT_BUTTON) && (set == 1) ){
mouse_right_pressed++;
}
}
else {
if ( (flags & MOUSE_RIGHT_BUTTON) && (set == 0) ){
mouse_right_up++;
}
}
if ( !(mouse_flags & MOUSE_MIDDLE_BUTTON) ) {
if ( (flags & MOUSE_MIDDLE_BUTTON) && (set == 1) ){
mouse_middle_pressed++;
}
}
else {
if ( (flags & MOUSE_MIDDLE_BUTTON) && (set == 0) ){
mouse_middle_up++;
}
}
if ( set ){
mouse_flags |= flags;
} else {
mouse_flags &= ~flags;
}
SDL_UnlockMutex( mouse_lock );
Script_system.SetHookVar("MouseButton", 'i', &flags);
//WMC - On Mouse Pressed and On Mouse Released hooks
if (set == 1)
{
Script_system.RunCondition(CHA_MOUSEPRESSED);
}
else if (set == 0)
{
Script_system.RunCondition(CHA_MOUSERELEASED);
}
Script_system.RemHookVar("MouseButton");
}
void mouse_flush()
{
if (!mouse_inited)
return;
mouse_reset_deltas();
Mouse_dx = Mouse_dy = Mouse_dz = 0;
SDL_LockMutex( mouse_lock );
mouse_left_pressed = 0;
mouse_right_pressed = 0;
mouse_middle_pressed = 0;
mouse_flags = 0;
SDL_UnlockMutex( mouse_lock );
}
int mouse_down_count(int n, int reset_count)
{
int tmp = 0;
if ( !mouse_inited ) return 0;
if ( (n < LOWEST_MOUSE_BUTTON) || (n > HIGHEST_MOUSE_BUTTON)) return 0;
SDL_LockMutex( mouse_lock );
switch (n) {
case MOUSE_LEFT_BUTTON:
tmp = mouse_left_pressed;
if ( reset_count ) {
mouse_left_pressed = 0;
}
break;
case MOUSE_RIGHT_BUTTON:
tmp = mouse_right_pressed;
if ( reset_count ) {
mouse_right_pressed = 0;
}
break;
case MOUSE_MIDDLE_BUTTON:
tmp = mouse_middle_pressed;
if ( reset_count ) {
mouse_middle_pressed = 0;
}
break;
} // end switch
SDL_UnlockMutex( mouse_lock );
return tmp;
}
// mouse_up_count() returns the number of times button n has gone from down to up
// since the last call
//
// parameters: n - button of mouse (see #define's in mouse.h)
//
int mouse_up_count(int n)
{
int tmp = 0;
if ( !mouse_inited ) return 0;
if ( (n < LOWEST_MOUSE_BUTTON) || (n > HIGHEST_MOUSE_BUTTON)) return 0;
SDL_LockMutex( mouse_lock );
switch (n) {
case MOUSE_LEFT_BUTTON:
tmp = mouse_left_up;
mouse_left_up = 0;
break;
case MOUSE_RIGHT_BUTTON:
tmp = mouse_right_up;
mouse_right_up = 0;
break;
case MOUSE_MIDDLE_BUTTON:
tmp = mouse_middle_up;
mouse_middle_up = 0;
break;
default:
Assert(0); // can't happen
break;
} // end switch
SDL_UnlockMutex( mouse_lock );
return tmp;
}
// returns 1 if mouse button btn is down, 0 otherwise
int mouse_down(int btn)
{
int tmp;
if ( !mouse_inited ) return 0;
if ( (btn < LOWEST_MOUSE_BUTTON) || (btn > HIGHEST_MOUSE_BUTTON)) return 0;
SDL_LockMutex( mouse_lock );
if ( mouse_flags & btn )
tmp = 1;
else
tmp = 0;
SDL_UnlockMutex( mouse_lock );
return tmp;
}
// returns the fraction of time btn has been down since last call
// (currently returns 1 if buttons is down, 0 otherwise)
//
float mouse_down_time(int btn)
{
float tmp;
if ( !mouse_inited ) return 0.0f;
if ( (btn < LOWEST_MOUSE_BUTTON) || (btn > HIGHEST_MOUSE_BUTTON)) return 0.0f;
SDL_LockMutex( mouse_lock );
if ( mouse_flags & btn )
tmp = 1.0f;
else
tmp = 0.0f;
SDL_UnlockMutex( mouse_lock );
return tmp;
}
void mouse_get_delta(int *dx, int *dy, int *dz)
{
if (dx)
*dx = Mouse_dx;
if (dy)
*dy = Mouse_dy;
if (dz)
*dz = Mouse_dz;
}
// Forces the actual windows cursor to be at (x,y). This may be independent of our tracked (x,y) mouse pos.
void mouse_force_pos(int x, int y)
{
if (os_foreground()) { // only mess with windows's mouse if we are in control of it
SDL_WarpMouseInWindow(os_get_window(), x, y);
}
}
#include "gamesequence/gamesequence.h"
// Reset deltas so we don't have duplicate mouse deltas
void mouse_reset_deltas()
{
Last_mouse_dx = Mouse_dx;
Last_mouse_dy = Mouse_dy;
Last_mouse_dz = Mouse_dz;
Mouse_dx = Mouse_dy = Mouse_dz = 0;
}
void mouse_event(int x, int y, int dx, int dy)
{
Mouse_x = x;
Mouse_y = y;
// Add up these delta values so we don't overwrite previous events,
// should be reset in gr_flip my mouse_reset_deltas()
Mouse_dx += dx;
Mouse_dy += dy;
if(Mouse_dx != 0 || Mouse_dy != 0)
{
Script_system.RunCondition(CHA_MOUSEMOVED);
}
}
int mouse_get_pos(int *xpos, int *ypos)
{
int flags;
if (!Mouse_in_focus)
{
if (xpos)
*xpos = Mouse_x;
if (ypos)
*ypos = Mouse_y;
return 0;
}
if (!mouse_inited) {
*xpos = *ypos = 0;
return 0;
}
flags = mouse_flags;
if (Mouse_x < 0){
Mouse_x = 0;
}
if (Mouse_y < 0){
Mouse_y = 0;
}
if (Mouse_x >= gr_screen.max_w){
Mouse_x = gr_screen.max_w - 1;
}
if (Mouse_y >= gr_screen.max_h){
Mouse_y = gr_screen.max_h - 1;
}
if (xpos){
*xpos = Mouse_x;
}
if (ypos){
*ypos = Mouse_y;
}
return flags;
}
int mouse_get_pos_unscaled( int *xpos, int *ypos )
{
int flags = mouse_get_pos( xpos, ypos );
gr_unsize_screen_pos( (xpos) ? xpos : NULL, (ypos) ? ypos : NULL, NULL, NULL, GR_RESIZE_MENU );
return flags;
}
void mouse_get_real_pos(int *mx, int *my)
{
POINT pnt;
getWindowMousePos(&pnt);
*mx = pnt.x;
*my = pnt.y;
}
void mouse_set_pos(int xpos, int ypos)
{
mouse_force_pos(xpos, ypos);
}
// portable routine to get the mouse position, relative
// to current window
void getWindowMousePos(POINT * pt)
{
Assert(pt != NULL);
int x = 0;
int y = 0;
SDL_GetMouseState(&x, &y);
pt->x = x;
pt->y = y;
}