1 /**
2 *	Joysticks, keyboard and mouse.
3 */
4 module glfw3d.Input;
5 
6 version(Have_derelict_glfw3) {
7 	import derelict.glfw3.glfw3;
8 } else {
9 	import glfw3d.glfw3;
10 }
11 import glfw3d.Main;
12 import std.string : fromStringz;
13 
14 /**
15 *	Params:
16 *		key = GLFW_KEY_*
17 *		scancode = platform-specific scancode
18 *	Returns: localized name of the specified printable key
19 */
20 string glfw3dGetKeyName(int key, int scancode) {
21 	return glfwGetKeyName(key, scancode).fromStringz.idup;
22 }
23 
24 /**
25 *	Main joystick class
26 */
27 class Joystick {
28 	private int joy;
29 
30 	/**
31 	*	Params:
32 	*		j = GLFW_JOYSTICK_*
33 	*/
34 	this(int j) {
35 		this.joy = j;
36 	}
37 
38 	/**
39 	*	Returns: true if current joystick is present
40 	*/
41 	bool isPresent() {
42 		if(glfwJoystickPresent(this.joy) == GLFW_TRUE) {
43 			return true;
44 		} else {
45 			return false;
46 		}
47 	}
48 
49 	/**
50 	*	Returns: axes of current joystick
51 	*/
52 	float[] getAxes() {
53 		int count;
54 		const(float)* o = glfwGetJoystickAxes(this.joy, &count);
55 		if(!o || count == 0)
56 			throw new glfw3dException("Cannot get joystick axes");
57 		float[] output;
58 		for(size_t i = 0; i < count; i++)
59 			output ~= o[i];
60 		return output;
61 	}
62 
63 	/**
64 	*	Returns: buttons of current joystick
65 	*/
66 	ubyte[] getButtons() {
67 		int count;
68 		const(ubyte)* o = glfwGetJoystickButtons(this.joy, &count);
69 		if(!o || count == 0)
70 			throw new glfw3dException("Cannot get joystick buttons");
71 		ubyte[] output;
72 		for(size_t i = 0; i < count; i++)
73 			output ~= o[i];
74 		return output;
75 	}
76 
77 	/**
78 	*	Returns: name of curent joystick
79 	*/
80 	string getName() {
81 		return glfwGetJoystickName(this.joy).fromStringz.idup;
82 	}
83 }