Gamedev Framework (gf)  0.6.0
A C++11 framework for 2D games
Cursor.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2017 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  *
21  * Part of this file comes from SFML, with the same license:
22  * Copyright (C) 2007-2015 Laurent Gomila (laurent@sfml-dev.org)
23  */
24 #ifndef GF_CURSOR_H
25 #define GF_CURSOR_H
26 
27 #include <cstdint>
28 
29 #include "Portability.h"
30 #include "Vector.h"
31 
32 struct SDL_Cursor;
33 struct SDL_Surface;
34 
35 namespace gf {
36 #ifndef DOXYGEN_SHOULD_SKIP_THIS
37 inline namespace v1 {
38 #endif
39 
40  class Image;
41 
42  /**
43  * @ingroup window
44  * @brief A mouse cursor
45  *
46  * This class abstracts the operating system resources
47  * associated with either a native system cursor or a custom
48  * cursor.
49  *
50  * After loading the cursor the graphical appearance
51  * with either loadFromPixels() or loadFromSystem(), the
52  * cursor can be changed with gf::Window::setMouseCursor().
53  *
54  * The behaviour is undefined if the cursor is destroyed while
55  * in use by the window.
56  *
57  * Usage example:
58  * @code
59  * gf::Window window;
60  *
61  * // ... create window as usual ...
62  *
63  * gf::Cursor cursor;
64  *
65  * if (cursor.loadFromSystem(gf::Cursor::Hand)) {
66  * window.setMouseCursor(cursor);
67  * }
68  * @endcode
69  *
70  * @see gf::Window::setMouseCursor()
71  */
72  class GF_API Cursor {
73  public:
74 
75  /**
76  * @brief Enumeration of the native system cursor types
77  */
78  enum Type {
79  Arrow, ///< Arrow cursor (default)
80  ArrowWait, ///< Busy arrow cursor
81  Wait, ///< Busy cursor
82  Text, ///< I-beam, cursor when hovering over a field allowing text entry
83  Hand, ///< Pointing hand cursor
84  SizeHorizontal, ///< Horizontal double arrow cursor
85  SizeVertical, ///< Vertical double arrow cursor
86  SizeTopLeftBottomRight, ///< Double arrow cursor going from top-left to bottom-right
87  SizeBottomLeftTopRight, ///< Double arrow cursor going from bottom-left to top-right
88  SizeAll, ///< Combination of SizeHorizontal and SizeVertical
89  Cross, ///< Crosshair cursor
90  NotAllowed ///< Action not allowed cursor
91  };
92 
93  /**
94  * @brief Default constructor
95  *
96  * This constructor doesn't actually create the cursor;
97  * initially the new instance is invalid and must not be
98  * used until either loadFromPixels() or loadFromSystem()
99  * is called and successfully created a cursor.
100  */
101  Cursor();
102 
103  /**
104  * @brief Deleted copy constructor
105  */
106  Cursor(const Cursor&) = delete;
107 
108  /**
109  * @brief Deleted copy assignment
110  */
111  Cursor& operator=(const Cursor&) = delete;
112 
113  /**
114  * @brief Move constructor
115  */
116  Cursor(Cursor&& other);
117 
118  /**
119  * @brief Move assignment
120  */
121  Cursor& operator=(Cursor&& other);
122 
123  /**
124  * @brief Destructor
125  *
126  * This destructor releases the system resources
127  * associated with this cursor, if any.
128  */
129  ~Cursor();
130 
131  /**
132  * @brief Create a cursor with the provided pixels
133  *
134  * @a pixels must be an array of @a width by @a height pixels
135  * in 32-bit RGBA format. If not, this will cause undefined behavior.
136  *
137  * If @a pixels is null or either @a width or @a height are 0,
138  * the current cursor is left unchanged and the function will
139  * return false.
140  *
141  * In addition to specifying the pixel data, you can also
142  * specify the location of the hotspot of the cursor. The
143  * hotspot is the pixel coordinate within the cursor image
144  * which will be located exactly where the mouse pointer
145  * position is. Any mouse actions that are performed will
146  * return the window/screen location of the hotspot.
147  *
148  *
149  * @param pixels An array of pixels of the image
150  * @param size The width and height of the image
151  * @param hotspot The location of the hotspot
152  * @returns True if the cursor was successfully loaded; false otherwise
153  *
154  * @sa loadFromImage()
155  */
156  bool loadFromPixels(const uint8_t* pixels, Vector2u size, Vector2u hotspot);
157 
158  /**
159  * @brief Create a cursor from the provided image
160  *
161  * @param image The image
162  * @param hotspot THe location of the hotspot
163  * @returns True if the cursor was successfully loaded; false otherwise
164  *
165  * @sa loadFromPixels()
166  */
167  bool loadFromImage(const Image& image, Vector2u hotspot);
168 
169  /**
170  * @brief Create a native system cursor
171  *
172  * @param type The native system cursor type
173  * @return True if the cursor was successfully loaded; false otherwise
174  */
175  bool loadFromSystem(Type type);
176 
177  private:
178  friend class Window;
179  SDL_Cursor *m_cursor;
180  };
181 
182 #ifndef DOXYGEN_SHOULD_SKIP_THIS
183 }
184 #endif
185 }
186 
187 #endif // GF_CURSOR_H
Vertical double arrow cursor.
Definition: Cursor.h:85
Action not allowed cursor.
Definition: Cursor.h:90
Cursor & operator=(const Cursor &)=delete
Deleted copy assignment.
Arrow cursor (default)
Definition: Cursor.h:79
Cursor & operator=(Cursor &&other)
Move assignment.
Type
Enumeration of the native system cursor types.
Definition: Cursor.h:78
Cursor()
Default constructor.
friend class RenderTarget
Definition: Shader.h:388
bool loadFromPixels(const uint8_t *pixels, Vector2u size, Vector2u hotspot)
Create a cursor with the provided pixels.
Double arrow cursor going from bottom-left to top-right.
Definition: Cursor.h:87
Busy arrow cursor.
Definition: Cursor.h:80
bool loadFromImage(const Image &image, Vector2u hotspot)
Create a cursor from the provided image.
Horizontal double arrow cursor.
Definition: Cursor.h:84
Crosshair cursor.
Definition: Cursor.h:89
Combination of SizeHorizontal and SizeVertical.
Definition: Cursor.h:88
Busy cursor.
Definition: Cursor.h:81
Class for loading, manipulating and saving images.
Definition: Image.h:92
The namespace for gf classes.
Definition: Action.h:34
I-beam, cursor when hovering over a field allowing text entry.
Definition: Cursor.h:82
Pointing hand cursor.
Definition: Cursor.h:83
Double arrow cursor going from top-left to bottom-right.
Definition: Cursor.h:86
Cursor(const Cursor &)=delete
Deleted copy constructor.
A mouse cursor.
Definition: Cursor.h:72
#define GF_API
Definition: Portability.h:35
Cursor(Cursor &&other)
Move constructor.
~Cursor()
Destructor.
bool loadFromSystem(Type type)
Create a native system cursor.