| 1 |
/*************************************************************************** |
| 2 |
* Copyright (C) 2005-2019 by the FIFE team * |
| 3 |
* http://www.fifengine.net * |
| 4 |
* This file is part of FIFE. * |
| 5 |
* * |
| 6 |
* FIFE is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Lesser General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2.1 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 14 |
* Lesser General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Lesser General Public * |
| 17 |
* License along with this library; if not, write to the * |
| 18 |
* Free Software Foundation, Inc., * |
| 19 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * |
| 20 |
***************************************************************************/ |
| 21 |
|
| 22 |
#ifndef FIFE_CURSOR_H |
| 23 |
#define FIFE_CURSOR_H |
| 24 |
|
| 25 |
// Standard C++ library includes |
| 26 |
|
| 27 |
// 3rd party library includes |
| 28 |
|
| 29 |
// FIFE includes |
| 30 |
// These includes are split up in two parts, separated by one empty line |
| 31 |
// First block: files included from the FIFE root src directory |
| 32 |
// Second block: files included from the same folder |
| 33 |
|
| 34 |
#include "animation.h" |
| 35 |
|
| 36 |
struct SDL_Cursor; |
| 37 |
|
| 38 |
namespace FIFE { |
| 39 |
|
| 40 |
class RenderBackend; |
| 41 |
class TimeManager; |
| 42 |
|
| 43 |
/** Defines the type of shown cursor |
| 44 |
* native -> default cursor |
| 45 |
* image -> cursor from image pool |
| 46 |
* animation -> cursor from animation pool |
| 47 |
*/ |
| 48 |
enum MouseCursorType { |
| 49 |
CURSOR_NONE, |
| 50 |
CURSOR_NATIVE, |
| 51 |
CURSOR_IMAGE, |
| 52 |
CURSOR_ANIMATION |
| 53 |
}; |
| 54 |
|
| 55 |
enum NativeCursor { |
| 56 |
// Start on 1000000 to avoid id-clashes with SDL |
| 57 |
NC_ARROW = 1000000, // Standard arrow |
| 58 |
NC_IBEAM, // I-beam for text selection |
| 59 |
NC_WAIT, // Hourglass |
| 60 |
NC_CROSS, // Crosshair |
| 61 |
NC_WAITARROW, // Small wait cursor (or Wait if not available) |
| 62 |
NC_RESIZENWSE, // Double arrow pointing northwest and southeast |
| 63 |
NC_RESIZENESW, // Double arrow pointing northeast and southwest |
| 64 |
NC_RESIZEWE, // Double arrow pointing west and east |
| 65 |
NC_RESIZENS, // Double arrow pointing north and south |
| 66 |
NC_RESIZEALL, // Four-pointed arrow pointing north, south, east, and west |
| 67 |
NC_NO, // Slashed circle |
| 68 |
NC_HAND // Hand. Common for links, etc. |
| 69 |
}; |
| 70 |
|
| 71 |
/** Cursor class manages mouse cursor handling |
| 72 |
*/ |
| 73 |
class Cursor { |
| 74 |
public: |
| 75 |
/** Constructor. |
| 76 |
*/ |
| 77 |
Cursor(RenderBackend* renderbackend); |
| 78 |
|
| 79 |
/** Destructor. |
| 80 |
*/ |
| 81 |
virtual ~Cursor() { invalidate(); } |
| 82 |
|
| 83 |
void invalidate(); |
| 84 |
|
| 85 |
/** draws cursor on screen |
| 86 |
*/ |
| 87 |
virtual void draw(); |
| 88 |
|
| 89 |
/** Sets the current mouse cursor |
| 90 |
* @param cursor_id For native cursors, this is the resource id to native cursor, or one of the values in NativeCursor |
| 91 |
*/ |
| 92 |
void set(uint32_t cursor_id=0); |
| 93 |
|
| 94 |
/** Sets the current mouse cursor type to image |
| 95 |
* @param image ImagePtr to a image used for the cursor |
| 96 |
*/ |
| 97 |
void set(ImagePtr image); |
| 98 |
|
| 99 |
/** Sets the current mouse cursor type to animation |
| 100 |
* @param anim AnimationPtr to a loaded animation used for the cursor |
| 101 |
*/ |
| 102 |
void set(AnimationPtr anim); |
| 103 |
|
| 104 |
/** Sets the current drag image cursor |
| 105 |
* @param image ImagePtr to a image used for the drag |
| 106 |
* @param drag_offset_x X Offset to display image when dragging. |
| 107 |
* @param drag_offset_y Y Offset to display image when dragging. |
| 108 |
* @note to reset the cursors drag call cursor.setDrag(Cursor::CURSOR_NONE, 0, 0) |
| 109 |
*/ |
| 110 |
void setDrag(ImagePtr image, int32_t drag_offset_x=0, int32_t drag_offset_y=0); |
| 111 |
|
| 112 |
/** Sets the current drag animated cursor |
| 113 |
* @param anim AnimationPtr to a loaded animation used for the drag |
| 114 |
* @param drag_offset_x X Offset to display animation when dragging. |
| 115 |
* @param drag_offset_y Y Offset to display animation when dragging. |
| 116 |
* @note to reset the cursors drag call cursor.setDrag(Cursor::CURSOR_NONE, 0, 0) |
| 117 |
*/ |
| 118 |
void setDrag(AnimationPtr anim, int32_t drag_offset_x=0, int32_t drag_offset_y=0); |
| 119 |
|
| 120 |
/** Resets the cursor drag type to CURSOR_NONE |
| 121 |
*/ |
| 122 |
void resetDrag(); |
| 123 |
|
| 124 |
/** Gets the current mouse cursor type |
| 125 |
*/ |
| 126 |
MouseCursorType getType() const { return m_cursor_type; } |
| 127 |
|
| 128 |
/** Gets the current mouse cursor handle |
| 129 |
*/ |
| 130 |
uint32_t getId() const { return m_cursor_id; } |
| 131 |
|
| 132 |
/** Gets the current mouse image |
| 133 |
*/ |
| 134 |
ImagePtr getImage() { return m_cursor_image; } |
| 135 |
|
| 136 |
/** Gets the current mouse animation |
| 137 |
*/ |
| 138 |
AnimationPtr getAnimation() { return m_cursor_animation; } |
| 139 |
|
| 140 |
/** Gets the current mouse cursor type |
| 141 |
*/ |
| 142 |
MouseCursorType getDragType() const { return m_drag_type; } |
| 143 |
|
| 144 |
/** Gets the current mouse drag image |
| 145 |
*/ |
| 146 |
ImagePtr getDragImage() { return m_cursor_drag_image; } |
| 147 |
|
| 148 |
/** Gets the current mouse drag animation |
| 149 |
*/ |
| 150 |
AnimationPtr getDragAnimation() { return m_cursor_drag_animation; } |
| 151 |
|
| 152 |
/** Gets the current mouse x position |
| 153 |
*/ |
| 154 |
uint32_t getX() const { return m_mx; } |
| 155 |
|
| 156 |
/** Gets the current mouse y position |
| 157 |
*/ |
| 158 |
uint32_t getY() const { return m_my; } |
| 159 |
|
| 160 |
/** Set the mouse position |
| 161 |
* @param x,y: The new position in screen coordinates |
| 162 |
*/ |
| 163 |
void setPosition(uint32_t x, uint32_t y); |
| 164 |
|
| 165 |
/** Get the current mouse position |
| 166 |
*/ |
| 167 |
void getPosition(int32_t* x, int32_t* y); |
| 168 |
|
| 169 |
/** Enables or disables the native image cursor feature. |
| 170 |
* If enabled, cursors set to an image or an animation will be set using SDL |
| 171 |
* and drawn by the native platform; otherwise they will be drawn by FIFE. |
| 172 |
* This setting has no effect on NativeCursor types which are always native |
| 173 |
* and on the drag cursor which is always drawn by FIFE. |
| 174 |
*/ |
| 175 |
void setNativeImageCursorEnabled(bool native_image_cursor_enabled); |
| 176 |
|
| 177 |
/** Returns whether cursors set to an image or an animation are drawn natively. |
| 178 |
* @see setNativeImageCursorEnabled() |
| 179 |
*/ |
| 180 |
bool isNativeImageCursorEnabled() const; |
| 181 |
|
| 182 |
protected: |
| 183 |
/** Sets the cursor to a native type. |
| 184 |
* @param cursor_id One of the values in NativeCursor |
| 185 |
*/ |
| 186 |
void setNativeCursor(uint32_t cursor_id); |
| 187 |
|
| 188 |
/** Sets the SDL cursor to the specified image. |
| 189 |
* Falls back to software cursor on failure. |
| 190 |
* @return false on error, true otherwise |
| 191 |
*/ |
| 192 |
bool setNativeImageCursor(ImagePtr image); |
| 193 |
|
| 194 |
/** To get some consistancy between platforms, this function checks |
| 195 |
* if cursor_id matches any of the values in NativeCursor, and |
| 196 |
* returns the SDL_SystemCursor. |
| 197 |
* If no match is found, cursor_id is returned. |
| 198 |
* |
| 199 |
* @param cursor_id One of the values in NativeCursor |
| 200 |
*/ |
| 201 |
uint32_t getNativeId(uint32_t cursor_id); |
| 202 |
|
| 203 |
private: |
| 204 |
uint32_t m_cursor_id; |
| 205 |
MouseCursorType m_cursor_type; |
| 206 |
MouseCursorType m_drag_type; |
| 207 |
|
| 208 |
SDL_Cursor* m_native_cursor; |
| 209 |
|
| 210 |
ImagePtr m_cursor_image; |
| 211 |
ImagePtr m_cursor_drag_image; |
| 212 |
|
| 213 |
AnimationPtr m_cursor_animation; |
| 214 |
AnimationPtr m_cursor_drag_animation; |
| 215 |
|
| 216 |
RenderBackend* m_renderbackend; |
| 217 |
|
| 218 |
uint32_t m_animtime; |
| 219 |
uint32_t m_drag_animtime; |
| 220 |
|
| 221 |
int32_t m_drag_offset_x; |
| 222 |
int32_t m_drag_offset_y; |
| 223 |
int32_t m_mx; |
| 224 |
int32_t m_my; |
| 225 |
TimeManager* m_timemanager; |
| 226 |
|
| 227 |
bool m_invalidated; |
| 228 |
bool m_native_image_cursor_enabled; |
| 229 |
ImagePtr m_native_cursor_image; |
| 230 |
}; |
| 231 |
|
| 232 |
} //FIFE |
| 233 |
|
| 234 |
#endif |