mirror of
https://github.com/scp-fs2open/fs2open.github.com.git
synced 2026-11-07 08:15:59 -10:00
* Updates the AxisInverted hooks to use the new CC_bind. Currently assumes only Joy0, so this may need further tweaking or replacement * * Add ability to claim up to 4 joysticks for player use, using the launcher (via registry or fs2_open.ini) or the new menu system * Hack into existing control polling system to get the new primary/secondary binding schema to work. This should be reworked into a reverse-lookup system. * Finally break the mouse loose from overlapping with Joystick binidngs * Fix bug where joy button wouldn't register on search function * Fix bug with mousewheel not always decaying as it should. * Fix rebase conflict with selecting item during search mode * Fix bug where mouse buttons were incorrectly labeled. * Move some enums around * Fix bug with joy axis labels being displayed as buttons. * Fix possible bug where Other conflict axis wouldn't clear * Updated default joystick axis text. A new strings.tbl will need to be supplied for localization support * Deprecate nAxisInverted ADE functions and replace with AxisInverted(int, int, bool) * Merge Axis mappings into Control_config * More utility functions for CC_bind and CCB * Adds another ADE function for inverting the axis action, instead of individual bindings * Hacks to get plr file to compile. Currently slices out many of the new features like secondary axis and the mouse axis, and the additional joystick bindings (joy1, joy2, joy3) * Fix bug with uncontrollable spin (slopily) * Fix some warnings * Add flags for Hats and Balls for future implementation * Fix bug where inverting an axis would trigger an assert * Fix bug where joy wasn't detected as present * Most axis actions should be relative, instead of absolute * Hopefully optimize joy_get_unscaled_reading * Properly break out of double-for search function for search and bind modes in config menu * Refactor control_get_axes_readings to provide more consistent behavior * Use JOY_REL_THROTTLE instead of Action::REL_THROTTLE when indexing into Control_config * Bank axis has a default binding, Throttle does not. * Fix bug where conflicts weren't being detected. Fix bug where default presets weren't being detectd * Elegantly break out of double fors with goto. Fix bug with bind mode not detecting axis * Save new binding scheme to playerfile * Add cid_assign() to standardize cid assignment with shorts * Add write and read handlers for int8_t * Remove saving controls to csg. This will be replaced by presets. * Fix bug with reading pilotfile * Use "Joy-n" instead of "joystick n" to match default identifiers in config menu * Fix bug with CC_TYPE_TRIGGER controls always marked as used, if not bound with a key. Fix bug with Joy-0 btn 1 triggering multiple controls that are not bound to it. * Improve readibility of the joy info output to log * Message when a joystick connects and using legacy system. * Fix bug where system joystick 0 was mapped in pJoystick that should've been nullptr * Fix bug where *not* having a joystick will cause bind mode in the config menu to immediately cancel. * Add reverse lookup functions for actions. mActionToVal[] does string -> int, mValToAction() does int -> string * Add the foundation for writing the presets files. * Adds a bunch of reverse lookup functions to get the stringified version of various enums, etc. * presets.h and presets.cpp are in something of a barebones at this point * Add new subdir for preset files 'player/presets' * add utility for finding the distance of a bitfield/bitmask. Used to translate mouse button masks into mouse button id's * Write PresetFileHandler as standalone, instead of a subclass. Still mostly a copy of JSONFileHandler * Make functions to map and reverse-map the bindings for use with the preset files and ultimately the ControlConfigDefaults.tbl as well * Check presets from file to see if they are duplicate with an existing preset. * Check for uniqueness of presets fron controlconfigdefaults.tbl before adding them, and warn the user if there are duplicates. * Rename the test functions into proper load/save. * save function has some standard checks * load function is called on init * make finding duplicates its own function outside of the preset label rendering function * Initial draft of saving presets in-game * Fix bug where all loaded presets were thought to be duplicate of defaults. * Fix bug where preset cycling would get stuck on the first preset other than default * Fix bug where saving a mouse button saves the wrong textified string. * Prevent preset names from being too long. * Crash still occurs if the name is too long, as if set outside of FSO * Workaround text running off the popups in 640x480. Doesn't look as nice, sadly * CSG's now track preset names instead of containing bindings * remake control_config_reset_defaults() into control_config_use_preset() * provide a function to get the currently selected preset. * Do PLR backward compatibility, hopefully. * PLR now tracks presets instead of saving the bindings. * On Loading pre PLR3 or CSG6 data, create a new preset. * Strip off file extension before saving preset files. Jason.json.json just looks dumb * Add "-joy_info" commandline option. Will output SDL joystick info of all detected joysticks to stdout * Fix non-standard behavior with registry_read_string(). * Returning NULL instead of the default value, or an empty string, will trigger an access violation with SCP_string and other string copy functions. * Modify io::joystick::init() to take an optional param, no_register, to use with -joy_info * Address CI complaints * Address CI issues 2. * Supress preset duplicate warning if in unit tests. * Address scripting unittest failure due to incorrect argument type. (Ints don't technically exist in LUA) * Use Mouse enums instead of Joy enums or magic numbers * More signed/unsigned madness * Address CI issues 3 * Address gcc-5 issues. (No auto in lambdas) * Fix bug with bit_distance() * Address gcc-5 issues 2 * Address clang issues * Address clang-tidy issues * Address clang-tidy issues in undosys. May need to be cherry picked to Controls2. * Address clang-tidy issues * Improve logic of preset name prompt. * Fix bug with selected presets not being saved * No, a cstr is not a std::string. * Can't use i there because its already defined. * Fix bug where CSG's wouldn't save selected preset * Fix bug where presets wouldn't be loaded correctly from player files * Fix bug where loaded preset files would not update the preset cycle position correctly * Some QoL changes for the preset name prompt * Address Appveyor issues. * Add forward compatibility for player file. This is required because currently any "missing" data in the pilotfile will trigger an Error, and crash FSO. * Fix bug where mouse X axis was unusable. Apparently stating that MOUSE_Z_AXIS IS MOUSE_Z_AXIS doesn't get the point across. * Address CI issues. * Address CI issues. * sel default assigned to -1 * static_cast enum to an int * Assimilate -joy_info int -get_flags, replacing the existing method that launchers use to get joystick information. * Add '\n' to PST log messages. * Change logic of checking joystick .ini keys: Should the multi-joy key not be found, try legacy joy key. * Add some more missing '\n' to mprintf messages. * Notify the player when converting to the new multi-joy system. (Using ReleaseWarning for now...) * Warn the player if a preset defined in their .PLR could not be found. * Actually disable control items without a valid tab, instead of relying on the fact that they're disabled by default. * Add CC_preset::is_duplicate_of() method to make checking for preset uniqueness less of a brain teaser. * Doxy the first_override param of control_config_common_read_section * Adds the os::dialogs::Information() method, which displays an information-only dialog in similar format to Warning and Error dialogs. Does not throw Warning nor Error exceptions * load_player now uses os::dialogs::Information() instead of ::ReleaseWarning, thus not halting unit tests. * Breaks retail localization support for Joystick and Mice labels - This is because the new multi-joy axis end up very confusing "Joy-0 Joystick/Mouse X Axis" * Renames Joy_axis_text to Axis_text, intended to be used both by joysticks and the mouse. * Moves mouse axis and button labels into an array to ease future XSTR support * Adds asserts to ensure indices are within the array sizes of label arrays * Adds XSTR indices for Axis labels, mouse buttons, and mouse wheels. * Seperates conflict check comparisons from equality comparisons. * Conflict checks can ignore certain combinations of CC_bind::flag's; i.e. an Axis bound to a different control but is inverted or relative will still cause a conflict with another control that has the same axis * Equality comparisons is exact equality, allowing presets to check if binds have the same binding and flag configurations. * More UI string changes and localization * Tweaks positioning of top button labels in CC menu * Adds the following XSTR's: 1659 "Preset: %s" 1660 "Preset: custom" 1661 "Next Preset" * Fix bug where mouse axis turned into mouse button when inverting control by its control name. * Fix bug with the new conflict checking subroutine not working with non-axis controls * Address CI issues. * Don't trigger modal popups while in unit tests. * Address clang tidy issues * use nullptr * be consistent with conditional style * use booleans to stuff debug states, methods with just Assertions in it will trigger a false positive on clang tidy * use string::empty() instead of checking for "". * Remove dead code of -joy_info. Functionality has been merged with -get_flags * Address clang-tidy [readability-simplify-boolean-expr] --z64 says "I don't like it!" * fix axis check * Implement revised axis action virtvars for scripting. Also protect against a control mismatch in the CSG file due to a script-inverted control * Encapsulate CC_bind properly * Add mouse and joystick cid's to AxisInverted description * Update translate_key() and its user to correctly translate multijoy bindings for training messages. * Prompt user trying to escape from config menu when they have changes. * Remove dead function. * Fix crash during training missions due to key translation that would overflow internal buffer. * Went back with cstring as return value instead of SCP string, because caller would need a significant refactor * Update doxy of the textify functions * Unify textify_scancode() and textify_scancode_universal() to ensure common code path. * Add "Multijoy" entry to json caps. (For Knossos) * Make CI happy. * Remove unused lambda captures. * Instead of overwrite during plr2 => plr3 conversion, try to use the existing preset file. * Fix conditional for checking if file exists within presets::save_preset_file() * Revert Majority of PR#3739 * PLR and CSG versions are incremented past versions set by 3739 * Forward compatibility feature of #3739 (Only save sexp containers into the CSG if they are used) is removed * Remove forward compatibilty of PLR file when going in between pre- and post-Controls5 FSO versions * Add missing startSection() and endSection() for csg_write_controls() * Fix clang-tidy error #1 * Fix clang-tidy error #2 * fix clandy tidy error #1 part 2/2 * Fix clang tidy error 3 Co-authored-by: Goober5000 <[email protected]> Co-authored-by: Tyler Blount <[email protected]> Co-authored-by: wookieejedi <[email protected]> Co-authored-by: BMagnu <[email protected]>
641 lines
13 KiB
C++
641 lines
13 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 "controlconfig/controlsconfig.h"
|
|
#include "graphics/2d.h"
|
|
#include "io/mouse.h"
|
|
#include "options/Option.h"
|
|
#include "scripting/scripting.h"
|
|
|
|
#define THREADED // to use the proper set of macros
|
|
#include "osapi/osapi.h"
|
|
#include "cmdline/cmdline.h"
|
|
|
|
#include "gamesequence/gamesequence.h"
|
|
|
|
int mouse_inited = 0;
|
|
|
|
LOCAL int Mouse_x;
|
|
LOCAL int Mouse_y;
|
|
|
|
LOCAL int Mouse_wheel_x;
|
|
LOCAL int Mouse_wheel_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_x1_pressed = 0;
|
|
int mouse_x2_pressed = 0;
|
|
int mouse_left_up = 0;
|
|
int mouse_right_up = 0;
|
|
int mouse_middle_up = 0;
|
|
int mouse_x1_up = 0;
|
|
int mouse_x2_up = 0;
|
|
|
|
int Mouse_dx = 0;
|
|
int Mouse_dy = 0;
|
|
int Mouse_dz = 0;
|
|
|
|
int Mouse_sensitivity = 4;
|
|
|
|
static auto MouseSensitivityOption =
|
|
options::OptionBuilder<int>("Input.MouseSensitivity", "Sensitivity", "The sentitivity of the mouse input.")
|
|
.category("Input")
|
|
.range(0, 9)
|
|
.level(options::ExpertLevel::Beginner)
|
|
.default_val(4)
|
|
.bind_to(&Mouse_sensitivity)
|
|
.importance(0)
|
|
.finish();
|
|
|
|
bool Use_mouse_to_fly = false;
|
|
|
|
static auto UseMouseOption = options::OptionBuilder<bool>("Input.UseMouse", "Mouse", "Use the mouse for flying")
|
|
.category("Input")
|
|
.level(options::ExpertLevel::Beginner)
|
|
.default_val(false)
|
|
.bind_to(&Use_mouse_to_fly)
|
|
.importance(1)
|
|
.finish();
|
|
|
|
namespace
|
|
{
|
|
bool mouse_key_event_handler(const SDL_Event& e)
|
|
{
|
|
if (!os::events::isWindowEvent(e, os::getSDLMainWindow())) {
|
|
return false;
|
|
}
|
|
|
|
switch (e.button.button) {
|
|
case SDL_BUTTON_LEFT:
|
|
mouse_mark_button(MOUSE_LEFT_BUTTON, e.button.state);
|
|
break;
|
|
case SDL_BUTTON_RIGHT:
|
|
mouse_mark_button(MOUSE_RIGHT_BUTTON, e.button.state);
|
|
break;
|
|
case SDL_BUTTON_MIDDLE:
|
|
mouse_mark_button(MOUSE_MIDDLE_BUTTON, e.button.state);
|
|
break;
|
|
case SDL_BUTTON_X1:
|
|
mouse_mark_button(MOUSE_X1_BUTTON, e.button.state);
|
|
break;
|
|
case SDL_BUTTON_X2:
|
|
mouse_mark_button(MOUSE_X2_BUTTON, e.button.state);
|
|
break;
|
|
default:
|
|
// SDL gave us an unknown button. Just log it
|
|
mprintf(("Unknown SDL button %i\n", e.button.button));
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool mouse_motion_event_handler(const SDL_Event& e)
|
|
{
|
|
if (!os::events::isWindowEvent(e, os::getSDLMainWindow())) {
|
|
return false;
|
|
}
|
|
|
|
mouse_event(e.motion.x, e.motion.y, e.motion.xrel, e.motion.yrel);
|
|
|
|
return true;
|
|
}
|
|
|
|
bool mouse_wheel_event_handler(const SDL_Event& e)
|
|
{
|
|
if (!os::events::isWindowEvent(e, os::getSDLMainWindow())) {
|
|
return false;
|
|
}
|
|
|
|
#if SDL_VERSION_ATLEAST(2, 0, 4)
|
|
mousewheel_motion(e.wheel.x, e.wheel.y, e.wheel.direction == SDL_MOUSEWHEEL_FLIPPED);
|
|
#else
|
|
mousewheel_motion(e.wheel.x, e.wheel.y, false);
|
|
#endif
|
|
|
|
return true;
|
|
}
|
|
}
|
|
|
|
void mouse_force_pos(int x, int y);
|
|
|
|
/**
|
|
* @brief Decays the mousewheel position back to 0 and clears the appropriate flags when nuetral
|
|
* @param[in] btn The button (wheel direction) to check against
|
|
*/
|
|
void mousewheel_decay(int btn);
|
|
|
|
static bool Mouse_in_focus = true;
|
|
|
|
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;
|
|
Mouse_wheel_x = 0;
|
|
Mouse_wheel_y = 0;
|
|
|
|
// 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_EventState( SDL_MOUSEWHEEL, SDL_ENABLE );
|
|
|
|
SDL_UnlockMutex( mouse_lock );
|
|
|
|
os::events::addEventListener(SDL_MOUSEBUTTONDOWN, os::events::DEFAULT_LISTENER_WEIGHT, mouse_key_event_handler);
|
|
os::events::addEventListener(SDL_MOUSEBUTTONUP, os::events::DEFAULT_LISTENER_WEIGHT, mouse_key_event_handler);
|
|
|
|
os::events::addEventListener(SDL_MOUSEMOTION, os::events::DEFAULT_LISTENER_WEIGHT, mouse_motion_event_handler);
|
|
|
|
os::events::addEventListener(SDL_MOUSEWHEEL, os::events::DEFAULT_LISTENER_WEIGHT, mouse_wheel_event_handler);
|
|
|
|
atexit( mouse_close );
|
|
}
|
|
|
|
|
|
/******************************************************************************
|
|
* @brief Marks a mouse button as up or down
|
|
*
|
|
* @param[in] flags Which button(s) are pressed/released
|
|
* @param[in] set Direction of the button(s). 1 = pressed, 0 = released
|
|
*
|
|
* @note This function is extern'ed by osapi.cpp and freespace.cpp
|
|
*/
|
|
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 (!(mouse_flags & MOUSE_X1_BUTTON)) {
|
|
|
|
if ((flags & MOUSE_X1_BUTTON) && (set == 1)) {
|
|
mouse_x1_pressed++;
|
|
}
|
|
} else {
|
|
if ((flags & MOUSE_X1_BUTTON) && (set == 0)) {
|
|
mouse_x1_up++;
|
|
}
|
|
}
|
|
|
|
if (!(mouse_flags & MOUSE_X2_BUTTON)) {
|
|
|
|
if ((flags & MOUSE_X2_BUTTON) && (set == 1)) {
|
|
mouse_x2_pressed++;
|
|
}
|
|
} else {
|
|
if ((flags & MOUSE_X2_BUTTON) && (set == 0)) {
|
|
mouse_x2_up++;
|
|
}
|
|
}
|
|
|
|
if ( set ){
|
|
mouse_flags |= flags;
|
|
} else {
|
|
mouse_flags &= ~flags;
|
|
}
|
|
|
|
SDL_UnlockMutex( mouse_lock );
|
|
|
|
Script_system.SetHookVar("MouseButton", 'i', flags);
|
|
|
|
if (Script_system.IsActiveAction(CHA_MOUSEPRESSED) || Script_system.IsActiveAction(CHA_MOUSERELEASED)) {
|
|
//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;
|
|
Mouse_wheel_x = Mouse_wheel_y = 0;
|
|
SDL_LockMutex( mouse_lock );
|
|
mouse_left_pressed = 0;
|
|
mouse_right_pressed = 0;
|
|
mouse_middle_pressed = 0;
|
|
mouse_x1_pressed = 0;
|
|
mouse_x2_pressed = 0;
|
|
mouse_flags = 0;
|
|
SDL_UnlockMutex( mouse_lock );
|
|
}
|
|
|
|
int mouse_down_count(const CC_bind &bind, int reset_count)
|
|
{
|
|
if (bind.get_cid() != CID_MOUSE) {
|
|
return 0;
|
|
}
|
|
|
|
int n = 1 << bind.get_btn();
|
|
return mouse_down_count(n, reset_count);
|
|
}
|
|
|
|
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;
|
|
|
|
case MOUSE_X1_BUTTON:
|
|
tmp = mouse_x1_pressed;
|
|
if (reset_count) {
|
|
mouse_x1_pressed = 0;
|
|
}
|
|
break;
|
|
|
|
case MOUSE_X2_BUTTON:
|
|
tmp = mouse_x2_pressed;
|
|
if (reset_count) {
|
|
mouse_x2_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(const CC_bind &bind)
|
|
{
|
|
if (bind.get_cid() != CID_MOUSE) {
|
|
return 0;
|
|
}
|
|
int n = 1 << bind.get_btn();
|
|
return mouse_up_count(n);
|
|
}
|
|
|
|
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;
|
|
|
|
case MOUSE_X1_BUTTON:
|
|
tmp = mouse_x1_up;
|
|
mouse_x1_up = 0;
|
|
break;
|
|
|
|
case MOUSE_X2_BUTTON:
|
|
tmp = mouse_x2_up;
|
|
mouse_x2_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(const CC_bind &bind)
|
|
{
|
|
if (bind.get_cid() != CID_MOUSE) {
|
|
return 0;
|
|
}
|
|
int btn = 1 << bind.get_btn();
|
|
|
|
return mouse_down(btn);
|
|
}
|
|
|
|
int mouse_down(int btn) {
|
|
int tmp;
|
|
if ( !mouse_inited ) return 0;
|
|
|
|
// Bail if not a button or wheel direction
|
|
if ((btn < LOWEST_MOUSE_BUTTON) || (btn > HIGHEST_MOUSE_WHEEL)) return 0;
|
|
|
|
|
|
SDL_LockMutex( mouse_lock );
|
|
|
|
|
|
if (mouse_flags & btn) {
|
|
tmp = 1;
|
|
if ((btn >= LOWEST_MOUSE_WHEEL) && (btn <= HIGHEST_MOUSE_WHEEL)) {
|
|
mousewheel_decay(btn);
|
|
}
|
|
} else {
|
|
tmp = 0;
|
|
}
|
|
|
|
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::getSDLMainWindow(), x, y);
|
|
}
|
|
}
|
|
|
|
// Reset deltas so we don't have duplicate mouse deltas
|
|
void mouse_reset_deltas()
|
|
{
|
|
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 (Script_system.IsActiveAction(CHA_MOUSEMOVED) && (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)
|
|
{
|
|
SDL_GetMouseState(mx, my);
|
|
}
|
|
|
|
void mouse_set_pos(int xpos, int ypos)
|
|
{
|
|
mouse_force_pos(xpos, ypos);
|
|
}
|
|
|
|
void mousewheel_motion(int x, int y, bool reversed) {
|
|
if (reversed) {
|
|
x = -x;
|
|
y = -y;
|
|
}
|
|
|
|
Mouse_wheel_x += x;
|
|
Mouse_wheel_y += y;
|
|
|
|
// These nested if's should take care of all edge cases.
|
|
// Since x and y's magnitudes can be larger than 1, it is possible to ignore the idle state
|
|
if (Mouse_wheel_y > 0) {
|
|
// UP
|
|
mouse_flags |= MOUSE_WHEEL_UP;
|
|
mouse_flags &= ~MOUSE_WHEEL_DOWN;
|
|
} else if (Mouse_wheel_y < 0) {
|
|
// DOWN
|
|
mouse_flags |= MOUSE_WHEEL_DOWN;
|
|
mouse_flags &= ~MOUSE_WHEEL_UP;
|
|
} else {
|
|
mouse_flags &= ~(MOUSE_WHEEL_UP | MOUSE_WHEEL_DOWN);
|
|
}
|
|
|
|
if (Mouse_wheel_x > 0) {
|
|
// RIGHT
|
|
mouse_flags |= MOUSE_WHEEL_RIGHT;
|
|
mouse_flags &= ~MOUSE_WHEEL_LEFT;
|
|
} else if (Mouse_wheel_x < 0) {
|
|
// LEFT
|
|
mouse_flags |= MOUSE_WHEEL_LEFT;
|
|
mouse_flags &= ~MOUSE_WHEEL_RIGHT;
|
|
} else {
|
|
mouse_flags &= ~(MOUSE_WHEEL_RIGHT | MOUSE_WHEEL_LEFT);
|
|
}
|
|
}
|
|
|
|
void mousewheel_decay(int btn) {
|
|
if (btn & MOUSE_WHEEL_UP) {
|
|
Mouse_wheel_y -= 1;
|
|
}
|
|
if (btn & MOUSE_WHEEL_DOWN) {
|
|
Mouse_wheel_y += 1;
|
|
}
|
|
if (btn & MOUSE_WHEEL_LEFT) {
|
|
Mouse_wheel_x -= 1;
|
|
}
|
|
if (btn & MOUSE_WHEEL_RIGHT) {
|
|
Mouse_wheel_x += 1;
|
|
}
|
|
|
|
if (Mouse_wheel_x == 0) {
|
|
mouse_flags &= ~(MOUSE_WHEEL_UP | MOUSE_WHEEL_DOWN);
|
|
}
|
|
if (Mouse_wheel_y == 0) {
|
|
mouse_flags &= ~(MOUSE_WHEEL_RIGHT | MOUSE_WHEEL_LEFT);
|
|
}
|
|
}
|
|
|
|
short bit_distance(short x) {
|
|
short i;
|
|
const short max_dist = sizeof(short) * 8;
|
|
|
|
for (i = 0; i < max_dist; ++i, x >>= 1) {
|
|
if (x & 0x01) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (i >= max_dist) {
|
|
return -1;
|
|
}
|
|
|
|
return i;
|
|
}
|