1 /*
2  * DSFML - The Simple and Fast Multimedia Library for D
3  *
4  * Copyright (c) 2013 - 2018 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  * DSFML is based on SFML (Copyright Laurent Gomila)
26  */
27 
28 /**
29  * Touch provides an interface to the state of the touches.
30  *
31  * It only contains static functions, so it's not meant to be instantiated.
32  *
33  * This class allows users to query the touches state at any time and directly,
34  * without having to deal with a window and its events. Compared to the
35  * `TouchBegan`, `TouchMoved` and `TouchEnded` events, Touch can retrieve the
36  * state of  the touches at any time (you don't need to store and update a
37  * boolean on your side in order to know if a touch is down), and you always get
38  * the real state of the touches, even if they happen when your window is out of
39  * focus and no event is triggered.
40  *
41  * The `getPosition` function can be used to retrieve the current position of a
42  * touch. There are two versions: one that operates in global coordinates
43  * (relative to the desktop) and one that operates in window coordinates
44  * (relative to a specific window).
45  *
46  * Touches are identified by an index (the "finger"), so that in multi-touch
47  * events, individual touches can be tracked correctly. As long as a finger
48  * touches the screen, it will keep the same index even if other fingers start
49  * or stop touching the screen in the meantime. As a consequence, active touch
50  * indices may not always be sequential (i.e. touch number 0 may be released
51  * while touch number 1 is still down).
52  *
53  * Example:
54  * ---
55  * if (Touch.isDown(0))
56  * {
57  *     // touch 0 is down
58  * }
59  *
60  * // get global position of touch 1
61  * Vector2i globalPos = Touch.getPosition(1);
62  *
63  * // get position of touch 1 relative to a window
64  * Vector2i relativePos = Touch.getPosition(1, window);
65  * ---
66  *
67  * See_Also:
68  * $(JOYSTICK_LINK), $(KEYBOARD_LINK), $(MOUSE_LINK)
69  */
70 module dsfml.window.touch;
71 
72 import dsfml.system.vector2;
73 import dsfml.window.window;
74 
75 /**
76  * Give access to the real-time state of the touches.
77  */
78 final abstract class Touch
79 {
80     /**
81     * Check if a touch event is currently down.
82     *
83     * Params:
84     *	finger = Finger index
85     *
86     * Returns: true if finger is currently touching the screen, false otherwise.
87     */
88     static bool isDown (uint finger)
89     {
90         return sfTouch_isDown (finger);
91     }
92 
93     /**
94     * Get the current position of a touch in desktop coordinates.
95     *
96     * This function returns the current touch position in global (desktop)
97     * coordinates.
98     *
99     * Params:
100     *	finger = Finger index
101     *
102     * Returns: Current position of finger, or undefined if it's not down.
103     */
104     static Vector2i getPosition (uint finger)
105     {
106         Vector2i getPosition;
107 
108         sfTouch_getPosition(finger, null, &getPosition.x, &getPosition.y);
109 
110         return getPosition;
111     }
112 
113     /**
114     * Get the current position of a touch in window coordinates.
115     *
116     * This function returns the current touch position in relative (window)
117     * coordinates.
118     *
119     * Params:
120     *	finger     = Finger index
121     *   relativeTo = Reference window
122 
123     *
124     * Returns:
125     *    Current position of finger, or undefined if it's not down
126     */
127     static Vector2i getPosition (uint finger, const(Window) relativeTo)
128     {
129         Vector2i getPosition;
130 
131         sfTouch_getPosition(finger, relativeTo.sfPtr, &getPosition.x, &getPosition.y);
132 
133         return getPosition;
134     }
135 }
136 
137 private extern(C)
138 {
139     //Check if a touch event is currently down
140     bool sfTouch_isDown (uint finger);
141 
142     //Get the current position of a given touch
143     void sfTouch_getPosition(uint finger, const(sfWindow)* relativeTo, int* x, int* y);
144 }