1 module dgt.gamepad;
2 
3 import derelict.sdl2.sdl;
4 
5 //The maximum value SDL reports for a trigger
6 static immutable TRIGGER_MAX = 32767.0;
7 
8 /**
9 An attached game controller with a traditional setup
10 
11 Only the controllers SDL supports out of the box will work, and no plans exist to expand this capability
12 */
13 struct Gamepad
14 {
15     private SDL_GameController* controller;
16 
17     @nogc nothrow:
18     @disable this();
19 
20     package this(SDL_GameController* controller)
21     {
22         this.controller = controller;
23     }
24 
25     package void destroy()
26     {
27         SDL_GameControllerClose(controller);
28     }
29 
30     private float getAxis(SDL_GameControllerAxis axis)
31     {
32         return SDL_GameControllerGetAxis(controller, axis) / TRIGGER_MAX;
33     }
34 
35     private bool getButton(SDL_GameControllerButton button)
36     {
37         return SDL_GameControllerGetButton(controller, button) != 0;
38     }
39 
40     public:
41     ///The X value of the left stick
42     @property float leftX()
43     {
44         return getAxis(SDL_CONTROLLER_AXIS_LEFTX);
45     }
46 
47     //The Y value of the left stick
48     @property float leftY()
49     {
50         return getAxis(SDL_CONTROLLER_AXIS_LEFTY);
51     }
52 
53     ///The X value of the right stick
54     @property float rightX()
55     {
56         return getAxis(SDL_CONTROLLER_AXIS_RIGHTX);
57     }
58 
59     ///The Y value of the right stick
60     @property float rightY()
61     {
62         return getAxis(SDL_CONTROLLER_AXIS_RIGHTY);
63     }
64 
65     ///How much the left trigger is being pressed
66     @property float triggerLeft()
67     {
68         return getAxis(SDL_CONTROLLER_AXIS_TRIGGERLEFT);
69     }
70 
71     ///How much the right trigger is being pressed
72     @property float triggerRight()
73     {
74         return getAxis(SDL_CONTROLLER_AXIS_TRIGGERRIGHT);
75     }
76 
77     ///If the bottom face button is being pressed (A on an XBOX controller)
78     @property bool faceDown()
79     {
80         return getButton(SDL_CONTROLLER_BUTTON_A);
81     }
82 
83     ///If the right face button is being pressed (B on an XBOX controller)
84     @property bool faceRight()
85     {
86         return getButton(SDL_CONTROLLER_BUTTON_B);
87     }
88 
89     ///If the left face button is being pressed (X on an XBOX controller)
90     @property bool faceLeft()
91     {
92         return getButton(SDL_CONTROLLER_BUTTON_X);
93     }
94 
95     ///If the top face button is being pressed (Y on an XBOX controller)
96     @property bool faceUp()
97     {
98         return getButton(SDL_CONTROLLER_BUTTON_Y);
99     }
100 
101     ///If the select button is being pressed (back on an XBOX controller)
102     @property bool select()
103     {
104         return getButton(SDL_CONTROLLER_BUTTON_BACK);
105     }
106 
107     ///If the main button is being pressed (the center X on an XBOX controller)
108     @property bool main()
109     {
110         return getButton(SDL_CONTROLLER_BUTTON_GUIDE);
111     }
112 
113     ///If the start button is being pressed
114     @property bool start()
115     {
116         return getButton(SDL_CONTROLLER_BUTTON_START);
117     }
118 
119     ///If the left stick is being pressed in
120     @property bool leftStick()
121     {
122         return getButton(SDL_CONTROLLER_BUTTON_LEFTSTICK);
123     }
124 
125     ///If the right stick is being pressed in
126     @property bool rightStick()
127     {
128         return getButton(SDL_CONTROLLER_BUTTON_RIGHTSTICK);
129     }
130 
131     ///If the left shoulder button is being pressed in
132     @property bool leftShoulder()
133     {
134         return getButton(SDL_CONTROLLER_BUTTON_LEFTSHOULDER);
135     }
136 
137     ///If the right shoulder button is being pressed in
138     @property bool rightShoulder()
139     {
140         return getButton(SDL_CONTROLLER_BUTTON_RIGHTSHOULDER);
141     }
142 
143     ///If Up on the dpad is being pressed
144     @property bool dpadUp()
145     {
146         return getButton(SDL_CONTROLLER_BUTTON_DPAD_UP);
147     }
148 
149     ///If Down on the dpad is being pressed
150     @property bool dpadDown()
151     {
152         return getButton(SDL_CONTROLLER_BUTTON_DPAD_DOWN);
153     }
154 
155     ///If Left on the dpad is being pressed
156     @property bool dpadLeft()
157     {
158         return getButton(SDL_CONTROLLER_BUTTON_DPAD_LEFT);
159     }
160 
161     ///If Right on the dpad is being pressed
162     @property bool dpadRight()
163     {
164         return getButton(SDL_CONTROLLER_BUTTON_DPAD_RIGHT);
165     }
166 }