1 /*
2  * DSFML - The Simple and Fast Multimedia Library for D
3  *
4  * Copyright (c) 2013 - 2017 Jeremy DeHaan (dehaan.jeremiah@gmail.com)
5  *
6  * This software is provided 'as-is', without any express or implied warranty.
7  * In no event will the authors be held liable for any damages arising from the
8  * use of this software.
9  *
10  * Permission is granted to anyone to use this software for any purpose,
11  * including commercial applications, and to alter it and redistribute it
12  * freely, subject to the following restrictions:
13  *
14  * 1. The origin of this software must not be misrepresented; you must not claim
15  * that you wrote the original software. If you use this software in a product,
16  * an acknowledgment in the product documentation would be appreciated but is
17  * not required.
18  *
19  * 2. Altered source versions must be plainly marked as such, and must not be
20  * misrepresented as being the original software.
21  *
22  * 3. This notice may not be removed or altered from any source distribution
23  */
24 
25 /**
26  * Touch provides an interface to the state of the touches.
27  *
28  * It only contains static functions, so it's not meant to be instantiated.
29  *
30  * This class allows users to query the touches state at any time and directly,
31  * without having to deal with a window and its events. Compared to the
32  * `TouchBegan`, `TouchMoved` and `TouchEnded` events, Touch can retrieve the
33  * state of  the touches at any time (you don't need to store and update a
34  * boolean on your side in order to know if a touch is down), and you always get
35  * the real state of the touches, even if they happen when your window is out of
36  * focus and no event is triggered.
37  *
38  * The `getPosition` function can be used to retrieve the current position of a
39  * touch. There are two versions: one that operates in global coordinates
40  * (relative to the desktop) and one that operates in window coordinates
41  * (relative to a specific window).
42  *
43  * Touches are identified by an index (the "finger"), so that in multi-touch
44  * events, individual touches can be tracked correctly. As long as a finger
45  * touches the screen, it will keep the same index even if other fingers start
46  * or stop touching the screen in the meantime. As a consequence, active touch
47  * indices may not always be sequential (i.e. touch number 0 may be released
48  * while touch number 1 is still down).
49  *
50  * Example:
51  * ---
52  * if (Touch.isDown(0))
53  * {
54  *     // touch 0 is down
55  * }
56  *
57  * // get global position of touch 1
58  * Vector2i globalPos = Touch.getPosition(1);
59  *
60  * // get position of touch 1 relative to a window
61  * Vector2i relativePos = Touch.getPosition(1, window);
62  * ---
63  *
64  * See_Also:
65  * $(JOYSTICK_LINK), $(KEYBOARD_LINK), $(MOUSE_LINK)
66  */
67 module dsfml.window.touch;
68 
69 import dsfml.system.vector2;
70 import dsfml.window.window;
71 
72 /**
73  * Give access to the real-time state of the touches.
74  */
75 final abstract class Touch
76 {
77     /**
78     * Check if a touch event is currently down.
79     *
80     * Params:
81     *	finger = Finger index
82     *
83     * Returns: true if finger is currently touching the screen, false otherwise.
84     */
85     static bool isDown (uint finger)
86     {
87         return sfTouch_isDown (finger);
88     }
89 
90     /**
91     * Get the current position of a touch in desktop coordinates.
92     *
93     * This function returns the current touch position in global (desktop)
94     * coordinates.
95     *
96     * Params:
97     *	finger = Finger index
98     *
99     * Returns: Current position of finger, or undefined if it's not down.
100     */
101     static Vector2i getPosition (uint finger)
102     {
103         Vector2i getPosition;
104 
105         sfTouch_getPosition(finger, null, &getPosition.x, &getPosition.y);
106 
107         return getPosition;
108     }
109 
110     /**
111     * Get the current position of a touch in window coordinates.
112     *
113     * This function returns the current touch position in relative (window)
114     * coordinates.
115     *
116     * Params:
117     *	finger = Finger index
118     *
119     * Returns:
120     *    Current position of finger, or undefined if it's not down
121     */
122     static Vector2i getPosition (uint finger, const(Window) relativeTo)
123     {
124         Vector2i getPosition;
125 
126         sfTouch_getPosition(finger, relativeTo.sfPtr, &getPosition.x, &getPosition.y);
127 
128         return getPosition;
129     }
130 }
131 
132 private extern(C)
133 {
134 
135     //Check if a touch event is currently down
136     bool sfTouch_isDown (uint finger);
137 
138     //Get the current position of a given touch
139     void sfTouch_getPosition(uint finger, const(sfWindow)* relativeTo, int* x, int* y);
140 }