Move check for modifier keys to sdl/input

This commit is contained in:
Tommy 2022-07-05 12:35:44 +12:00
parent 034bc786cc
commit 9477354dc8
3 changed files with 47 additions and 32 deletions

View file

@ -22,6 +22,7 @@
#include "hotkey/hotkey_command.hpp"
#include "key.hpp"
#include "log.hpp"
#include "sdl/input.hpp" // for sdl::get_mods
#include "serialization/unicode.hpp"
#include <boost/algorithm/string.hpp>
@ -42,33 +43,6 @@ hotkey_list hotkeys_;
game_config_view default_hotkey_cfg_;
const int TOUCH_MOUSE_INDEX = 255;
unsigned int sdl_get_mods()
{
unsigned int mods;
mods = SDL_GetModState();
// Filter for only the mods we use: shift, ctrl, alt, gui
mods &= KMOD_SHIFT | KMOD_CTRL | KMOD_ALT | KMOD_GUI;
// save the matching for checking right vs left keys
if(mods & KMOD_SHIFT) {
mods |= KMOD_SHIFT;
}
if(mods & KMOD_CTRL) {
mods |= KMOD_CTRL;
}
if(mods & KMOD_ALT)
mods |= KMOD_ALT;
if(mods & KMOD_GUI) {
mods |= KMOD_GUI;
}
return mods;
}
}; // namespace
const std::string hotkey_base::get_name() const
@ -145,7 +119,7 @@ hotkey_ptr create_hotkey(const std::string& id, const SDL_Event& event)
{
hotkey_ptr base = std::make_shared<hotkey_void>();
const hotkey_command& command = get_hotkey_command(id);
unsigned mods = sdl_get_mods();
unsigned mods = sdl::get_mods();
switch(event.type) {
case SDL_KEYUP: {
@ -250,7 +224,7 @@ bool hotkey_mouse::matches_helper(const SDL_Event& event) const
return false;
}
unsigned int mods = sdl_get_mods();
unsigned mods = sdl::get_mods();
if((mods != mod_)) {
return false;
}
@ -288,7 +262,7 @@ const std::string hotkey_keyboard::get_name_helper() const
bool hotkey_keyboard::matches_helper(const SDL_Event& event) const
{
unsigned int mods = sdl_get_mods();
unsigned mods = sdl::get_mods();
const hotkey_command& command = get_hotkey_command(get_command());
if((event.type == SDL_KEYDOWN || event.type == SDL_KEYUP)
@ -475,7 +449,7 @@ bool is_hotkeyable_event(const SDL_Event& event)
return true;
}
unsigned mods = sdl_get_mods();
unsigned mods = sdl::get_mods();
if(mods & KMOD_CTRL || mods & KMOD_ALT || mods & KMOD_GUI) {
return event.type == SDL_KEYUP;

View file

@ -17,6 +17,7 @@
#include "sdl/point.hpp"
#include <SDL2/SDL_mouse.h>
#include <SDL2/SDL_keyboard.h>
namespace {
SDL_Point drawing_surface_size;
@ -54,6 +55,32 @@ SDL_Point get_mouse_location()
return p;
}
unsigned get_mods()
{
unsigned mods = SDL_GetModState();
// Filter for only the mods we use: shift, ctrl, alt, gui
mods &= KMOD_SHIFT | KMOD_CTRL | KMOD_ALT | KMOD_GUI;
// Set both left and right modifiers if either is active
if(mods & KMOD_SHIFT) {
mods |= KMOD_SHIFT;
}
if(mods & KMOD_CTRL) {
mods |= KMOD_CTRL;
}
if(mods & KMOD_ALT)
mods |= KMOD_ALT;
if(mods & KMOD_GUI) {
mods |= KMOD_GUI;
}
return mods;
}
void update_input_dimensions(
int draw_width, int draw_height,
int input_width, int input_height

View file

@ -35,9 +35,23 @@ uint32_t get_mouse_state(int *x, int *y);
/** Returns the current mouse button mask */
uint32_t get_mouse_button_mask();
/** Returns the currnet mouse location in draw space. */
/** Returns the current mouse location in draw space. */
SDL_Point get_mouse_location();
/**
* Returns a bitmask of active modifier keys (ctrl, shift, alt, gui).
*
* Unused modifier keys (caps lock, scroll lock, num lock, AltGr) are
* filtered out and will always be unset.
*
* Left and right keys are not distinguished. If either is detected, both
* will be set. For example if only left shift is down, both KMOD_LSHIFT
* and KMOD_RSHIFT will be set in the returned bitmask.
*
* @returns A bitmask of SDL_Keymod values representing the active state.
*/
unsigned get_mods();
/**
* Update the cached drawing area and input area sizes. These correspond to
* the size of the drawing surface in pixels, and the size of the window in