1 /**
2  * Retrograde Engine
3  *
4  * Authors:
5  *  Mike Bierlee, m.bierlee@lostmoment.com
6  * Copyright: 2014-2021 Mike Bierlee
7  * License:
8  *  This software is licensed under the terms of the MIT license.
9  *  The full terms of the license can be found in the LICENSE.txt file.
10  */
11 
12 module retrograde.sdl2.input;
13 
14 version(Have_derelict_sdl2) {
15 
16 import poodinis;
17 
18 import std.experimental.logger;
19 import std.string;
20 
21 import derelict.sdl2.sdl;
22 
23 class Sdl2InputDeviceManager {
24     private SDL_Joystick*[SDL_JoystickID] joysticks;
25     private double _mouseSensitivity = 6;
26 
27     @Autowire
28     private Logger logger;
29 
30     public @property void captureMouse(bool capture) {
31         SDL_SetRelativeMouseMode(capture);
32     }
33 
34     public @property bool captureMouse() {
35         return cast(bool) SDL_GetRelativeMouseMode();
36     }
37 
38     public bool emitMouseMotionReset = true;
39 
40     public @property void mouseAcceleration(bool mouseAcceleration) {
41         SDL_SetHint(SDL_HINT_MOUSE_RELATIVE_MODE_WARP, mouseAcceleration ? "1" : "0");
42     }
43 
44     public @property bool mouseAcceleration() {
45         auto hint = SDL_GetHint(SDL_HINT_MOUSE_RELATIVE_MODE_WARP);
46         return hint !is null && *hint == '1';
47     }
48 
49     public @property void mouseSensitivityModifier(double sensitivity) {
50         _mouseSensitivity = sensitivity;
51     }
52 
53     public @property double mouseSensitivityModifier() {
54         return _mouseSensitivity;
55     }
56 
57     public void initialize() {
58         if (SDL_WasInit(SDL_INIT_JOYSTICK)) {
59             SDL_JoystickEventState(SDL_ENABLE);
60         }
61     }
62 
63     public void cleanup() {
64         foreach(joystick ; joysticks) {
65             if (joystick is null) {
66                 continue;
67             }
68 
69             SDL_JoystickClose(joystick);
70         }
71         joysticks.destroy();
72     }
73 
74     public void openJoystick(int joystickNumber) {
75         SDL_Joystick* joystick = SDL_JoystickOpen(joystickNumber);
76         if (joystick) {
77             auto index = SDL_JoystickInstanceID(joystick);
78             joysticks[index] = joystick;
79         } else {
80             logger.errorf("Couldn't open joystick %s", joystickNumber);
81         }
82     }
83 
84     public void closeJoystick(int joystickNumber) {
85         auto joystick = joystickNumber in joysticks;
86         if (joystick) {
87             SDL_JoystickClose(*joystick);
88             joysticks[joystickNumber] = null;
89         }
90     }
91 }
92 
93 }