1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
|
/***************************************************************************
* Copyright (C) 2005-2019 by the FIFE team *
* http://www.fifengine.net *
* This file is part of FIFE. *
* *
* FIFE is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
***************************************************************************/
// Standard C++ library includes
// 3rd party library includes
#include <SDL.h>
// FIFE includes
// These includes are split up in two parts, separated by one empty line
// First block: files included from the FIFE root src directory
// Second block: files included from the same folder
#include "util/structures/rect.h"
#include "util/time/timemanager.h"
#include "util/log/logger.h"
#include "video/imagemanager.h"
#include "animation.h"
#include "image.h"
#include "renderbackend.h"
#include "cursor.h"
namespace FIFE {
/** Logger to use for this source file.
* @relates Logger
*/
static Logger _log(LM_CURSOR);
Cursor::Cursor(RenderBackend* renderbackend):
m_cursor_id(NC_ARROW),
m_cursor_type(CURSOR_NATIVE),
m_drag_type(CURSOR_NONE),
m_native_cursor(NULL),
m_renderbackend(renderbackend),
m_animtime(0),
m_drag_animtime(0),
m_drag_offset_x(0),
m_drag_offset_y(0),
m_mx(0),
m_my(0),
m_timemanager(TimeManager::instance()),
m_invalidated(false),
m_native_image_cursor_enabled(false) {
assert(m_timemanager);
set(m_cursor_id);
}
void Cursor::set(uint32_t cursor_id) {
m_cursor_type = CURSOR_NATIVE;
if (!SDL_ShowCursor(1)) {
SDL_PumpEvents();
}
setNativeCursor(cursor_id);
m_cursor_image.reset();
m_cursor_animation.reset();
}
void Cursor::set(ImagePtr image) {
assert(image != 0);
m_cursor_image = image;
m_cursor_type = CURSOR_IMAGE;
if (m_native_image_cursor_enabled) {
if (!setNativeImageCursor(image)) {
return;
}
if (!SDL_ShowCursor(1)) {
SDL_PumpEvents();
}
}
else if (SDL_ShowCursor(0)) {
SDL_PumpEvents();
}
m_cursor_id = NC_ARROW;
m_cursor_animation.reset();
}
void Cursor::set(AnimationPtr anim) {
assert(anim != 0);
m_cursor_animation = anim;
m_cursor_type = CURSOR_ANIMATION;
if (m_native_image_cursor_enabled) {
if (!setNativeImageCursor(anim->getFrameByTimestamp(0))) {
return;
}
if (!SDL_ShowCursor(1)) {
SDL_PumpEvents();
}
}
else if (SDL_ShowCursor(0)) {
SDL_PumpEvents();
}
m_animtime = m_timemanager->getTime();
m_cursor_id = NC_ARROW;
m_cursor_image.reset();
}
void Cursor::setDrag(ImagePtr image, int32_t drag_offset_x, int32_t drag_offset_y) {
assert(image != 0);
m_cursor_drag_image = image;
m_drag_type = CURSOR_IMAGE;
m_drag_offset_x = drag_offset_x;
m_drag_offset_y = drag_offset_y;
m_cursor_drag_animation.reset();
}
void Cursor::setDrag(AnimationPtr anim, int32_t drag_offset_x, int32_t drag_offset_y) {
assert(anim != 0);
m_cursor_drag_animation = anim;
m_drag_type = CURSOR_ANIMATION;
m_drag_offset_x = drag_offset_x;
m_drag_offset_y = drag_offset_y;
m_drag_animtime = m_timemanager->getTime();
m_cursor_drag_image.reset();
}
void Cursor::resetDrag() {
m_drag_type = CURSOR_NONE;
m_drag_animtime = 0;
m_drag_offset_x = 0;
m_drag_offset_y = 0;
m_cursor_drag_animation.reset();
m_cursor_drag_image.reset();
}
void Cursor::setPosition(uint32_t x, uint32_t y) {
m_mx = x;
m_my = y;
SDL_WarpMouseInWindow(RenderBackend::instance()->getWindow(), m_mx, m_my);
}
void Cursor::getPosition(int32_t* x, int32_t* y) {
*x = m_mx;
*y = m_my;
}
void Cursor::invalidate() {
if (m_native_cursor != NULL) {
SDL_FreeCursor(m_native_cursor);
m_native_cursor = NULL;
m_native_cursor_image.reset();
m_invalidated = true;
}
}
void Cursor::draw() {
if (m_invalidated) {
if (m_cursor_type == CURSOR_NATIVE ) {
set(m_cursor_id);
}
else if (m_native_image_cursor_enabled) {
if (m_cursor_type == CURSOR_IMAGE ) {
set(m_cursor_image);
}
else if (m_cursor_type == CURSOR_ANIMATION ) {
set(m_cursor_animation);
}
}
m_invalidated = false;
}
SDL_GetMouseState(&m_mx, &m_my);
if ((m_cursor_type == CURSOR_NATIVE) && (m_drag_type == CURSOR_NONE)) {
return;
}
// render possible drag image
ImagePtr img;
if (m_drag_type == CURSOR_IMAGE) {
img = m_cursor_drag_image;
}
else if (m_drag_type == CURSOR_ANIMATION) {
int32_t animtime = (m_timemanager->getTime() - m_drag_animtime) % m_cursor_drag_animation->getDuration();
img = m_cursor_drag_animation->getFrameByTimestamp(animtime);
}
if (img != 0) {
Rect area(m_mx + m_drag_offset_x + img->getXShift(), m_my + m_drag_offset_y + img->getYShift(), img->getWidth(), img->getHeight());
m_renderbackend->pushClipArea(area, false);
img->render(area);
m_renderbackend->renderVertexArrays();
m_renderbackend->popClipArea();
}
ImagePtr img2;
// render possible cursor image
if (m_cursor_type == CURSOR_IMAGE) {
img2 = m_cursor_image;
}
else if (m_cursor_type == CURSOR_ANIMATION) {
int32_t animtime = (m_timemanager->getTime() - m_animtime) % m_cursor_animation->getDuration();
img2 = m_cursor_animation->getFrameByTimestamp(animtime);
}
if (img2 != 0) {
if (m_native_image_cursor_enabled) {
setNativeImageCursor(img2);
} else {
Rect area(m_mx + img2->getXShift(), m_my + img2->getYShift(), img2->getWidth(), img2->getHeight());
m_renderbackend->pushClipArea(area, false);
img2->render(area);
m_renderbackend->renderVertexArrays();
m_renderbackend->popClipArea();
}
}
}
uint32_t Cursor::getNativeId(uint32_t cursor_id) {
switch (cursor_id) {
case NC_ARROW:
return SDL_SYSTEM_CURSOR_ARROW;
case NC_IBEAM:
return SDL_SYSTEM_CURSOR_IBEAM;
case NC_WAIT:
return SDL_SYSTEM_CURSOR_WAIT;
case NC_CROSS:
return SDL_SYSTEM_CURSOR_CROSSHAIR;
case NC_WAITARROW:
return SDL_SYSTEM_CURSOR_WAITARROW;
case NC_RESIZENWSE:
return SDL_SYSTEM_CURSOR_SIZENWSE;
case NC_RESIZENESW:
return SDL_SYSTEM_CURSOR_SIZENESW;
case NC_RESIZEWE:
return SDL_SYSTEM_CURSOR_SIZEWE;
case NC_RESIZENS:
return SDL_SYSTEM_CURSOR_SIZENS;
case NC_RESIZEALL:
return SDL_SYSTEM_CURSOR_SIZEALL;
case NC_NO:
return SDL_SYSTEM_CURSOR_NO;
case NC_HAND:
return SDL_SYSTEM_CURSOR_HAND;
}
return cursor_id;
}
void Cursor::setNativeCursor(uint32_t cursor_id) {
cursor_id = getNativeId(cursor_id);
SDL_Cursor* cursor = SDL_CreateSystemCursor(static_cast<SDL_SystemCursor>(cursor_id));
if (!cursor) {
FL_WARN(_log, "No cursor matching cursor_id was found.");
return;
}
SDL_SetCursor(cursor);
if (m_native_cursor != NULL) {
SDL_FreeCursor(m_native_cursor);
}
m_native_cursor = cursor;
}
bool Cursor::setNativeImageCursor(ImagePtr image) {
if (image == m_native_cursor_image) {
// we're already using this image
return true;
}
if (image->getState() == IResource::RES_NOT_LOADED) {
image->load();
}
// SDL only accepts whole surfaces here so if this image uses a shared surface
// we need to prepare a temporary surface with just the relevant part
ImagePtr temp_image = image;
if (image->isSharedImage()) {
temp_image = ImageManager::instance()->create();
temp_image->copySubimage(0, 0, image);
}
SDL_Cursor* cursor = SDL_CreateColorCursor(
temp_image->getSurface(),
-image->getXShift(),
-image->getYShift());
if (cursor == NULL) {
FL_WARN(_log, LMsg("SDL_CreateColorCursor: \"") << SDL_GetError() <<
"\". Falling back to software cursor.");
if (image->isSharedImage()) {
ImageManager::instance()->remove(temp_image);
}
setNativeImageCursorEnabled(false);
return false;
}
SDL_SetCursor(cursor);
m_native_cursor_image = image;
if (image->isSharedImage()) {
ImageManager::instance()->remove(temp_image);
}
if (m_native_cursor != NULL) {
SDL_FreeCursor(m_native_cursor);
}
m_native_cursor = cursor;
return true;
}
void Cursor::setNativeImageCursorEnabled(bool native_image_cursor_enabled) {
if (m_native_image_cursor_enabled != native_image_cursor_enabled) {
m_native_image_cursor_enabled = native_image_cursor_enabled;
if (m_cursor_type == CURSOR_IMAGE ) {
set(m_cursor_image);
}
else if (m_cursor_type == CURSOR_ANIMATION ) {
set(m_cursor_animation);
}
}
}
bool Cursor::isNativeImageCursorEnabled() const {
return m_native_image_cursor_enabled;
}
}
|