1 module polyplex.core.input.touch;
2 import polyplex.math;
3 import derelict.sdl2.sdl;
4 
5 
6 public struct FingerState {
7 	/**
8 		The SDL id for the finger.
9 	*/
10 	public SDL_FingerID Id;
11 
12 	/**
13 		The origin device the finger was registered on.
14 	*/
15 	public int DeviceId;
16 
17 	/**
18 		Position in X and Y axis, pressure on Z.
19 		All values are normalized (between 0 and 1)
20 	*/
21 	public Vector3 Position;
22 }
23 
24 public class TouchState {
25 	public FingerState[] Fingers;
26 }
27 
28 public class Touch {
29 	private static int touch_devices = -1;
30 
31 	/**
32 		Returns the amount of touch devices connected to the system.
33 		Calling this will aswell update the internal counter.
34 	*/
35 	public static int Count() {
36 		if (touch_devices != SDL_GetNumTouchDevices()) touch_devices = SDL_GetNumTouchDevices();
37 		return touch_devices;
38 	}
39 
40 	/**
41 		Returns the state for current touch screen. (-1 for all touch screens.)
42 	*/
43 	public static TouchState GetState(int touch_id = -1) {
44 		// Get the number of touch screens connected to this system.
45 		if (touch_devices == 0) touch_devices = SDL_GetNumTouchDevices();
46 		
47 		TouchState st = new TouchState();
48 		if (touch_id == -1) {
49 			foreach(touch_dev; 0 .. touch_devices) {
50 				int fingers = SDL_GetNumTouchFingers(touch_dev);
51 				foreach(finger; 0 .. fingers) {
52 					SDL_Finger* f = SDL_GetTouchFinger(touch_dev, finger);
53 					st.Fingers ~= FingerState(f.id, touch_dev, Vector3(f.x, f.y, f.pressure));
54 				}
55 			}
56 		} else {
57 			int fingers = SDL_GetNumTouchFingers(touch_id);
58 			foreach(finger; 0 .. fingers) {
59 				SDL_Finger* f = SDL_GetTouchFinger(touch_id, finger);
60 				st.Fingers ~= FingerState(f.id, touch_id, Vector3(f.x, f.y, f.pressure));
61 			}
62 		}
63 		return st;
64 	}
65 }