The Battle for Wesnoth  1.19.7+dev
input.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2022 - 2024
3  by Thomas Iorns <mesilliac@tomanui.nz>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #include "sdl/input.hpp"
17 
18 #include "sdl/point.hpp"
19 #include "video.hpp"
20 
21 #include <SDL2/SDL_mouse.h>
22 #include <SDL2/SDL_keyboard.h>
23 
24 namespace sdl
25 {
26 
27 uint32_t get_mouse_state(int *x, int *y)
28 {
29  uint32_t buttons = SDL_GetMouseState(x, y);
30 
31  if (video::headless()) {
32  return buttons;
33  }
34 
35  // The game canvas may be offset inside the window,
36  // as well as potentially having a different size.
38  *x -= input_area.x;
39  *y -= input_area.y;
40 
41  // Translate to game-native coordinates
42  point canvas_size = video::game_canvas_size();
43  *x = (*x * canvas_size.x) / input_area.w;
44  *y = (*y * canvas_size.y) / input_area.h;
45 
46  return buttons;
47 }
48 
50 {
51  return SDL_GetMouseState(nullptr, nullptr);
52 }
53 
55 {
56  point p;
57  get_mouse_state(&p.x, &p.y);
58  return p;
59 }
60 
61 unsigned get_mods()
62 {
63  unsigned mods = SDL_GetModState();
64 
65  // Filter for only the mods we use: shift, ctrl, alt, gui
66  mods &= KMOD_SHIFT | KMOD_CTRL | KMOD_ALT | KMOD_GUI;
67 
68  // Set both left and right modifiers if either is active
69  if(mods & KMOD_SHIFT) {
70  mods |= KMOD_SHIFT;
71  }
72 
73  if(mods & KMOD_CTRL) {
74  mods |= KMOD_CTRL;
75  }
76 
77  if(mods & KMOD_ALT)
78  mods |= KMOD_ALT;
79 
80  if(mods & KMOD_GUI) {
81  mods |= KMOD_GUI;
82  }
83 
84  return mods;
85 }
86 
87 } // namespace sdl
Contains functions for cleanly handling SDL input.
uint32_t get_mouse_button_mask()
Returns the current mouse button mask.
Definition: input.cpp:49
uint32_t get_mouse_state(int *x, int *y)
A wrapper for SDL_GetMouseState that gives coordinates in draw space.
Definition: input.cpp:27
unsigned get_mods()
Returns a bitmask of active modifier keys (ctrl, shift, alt, gui).
Definition: input.cpp:61
point get_mouse_location()
Returns the current mouse location in draw space.
Definition: input.cpp:54
bool headless()
The game is running headless.
Definition: video.cpp:139
point game_canvas_size()
The size of the game canvas, in drawing coordinates / game pixels.
Definition: video.cpp:432
rect input_area()
Returns the input area of the window, in display coordinates.
Definition: video.cpp:476
Holds a 2D point.
Definition: point.hpp:25
An abstract description of a rectangle with integer coordinates.
Definition: rect.hpp:47
mock_party p