-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
cursor.cpp
238 lines (190 loc) · 6.02 KB
/
cursor.cpp
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
/*
Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
Part of the Battle for Wesnoth Project https://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
/**
* @file
* Support for different cursors-shapes.
*/
#include "cursor.hpp"
#include "picture.hpp"
#include "preferences/game.hpp"
#include "sdl/utils.hpp"
#include <boost/logic/tribool.hpp>
#include <array>
#include <memory>
namespace cursor
{
namespace
{
using cursor_ptr_t = std::unique_ptr<SDL_Cursor, std::function<void(SDL_Cursor*)>>;
struct cursor_data
{
cursor_ptr_t cursor;
boost::tribool is_color;
std::string image_bw;
std::string image_color;
int hot_x;
int hot_y;
};
//
// Array with each available cursor type.
// macOS needs 16x16 b&w cursors. TODO: is that still the case?
//
std::array<cursor_data, cursor::NUM_CURSORS> available_cursors {{
#ifdef __APPLE__
{ nullptr, boost::indeterminate, "normal.png", "normal.png", 0, 0 },
{ nullptr, boost::indeterminate, "wait-alt.png", "wait.png", 0, 0 },
{ nullptr, boost::indeterminate, "move.png", "move.png", 0, 0 },
{ nullptr, boost::indeterminate, "attack.png", "attack.png", 0, 0 },
{ nullptr, boost::indeterminate, "select.png", "select.png", 0, 0 },
{ nullptr, boost::indeterminate, "move_drag_alt.png", "move_drag.png", 2, 20 },
{ nullptr, boost::indeterminate, "attack_drag_alt.png", "attack_drag.png", 3, 22 },
{ nullptr, boost::indeterminate, "no_cursor.png", "", 0, 0 }
#else
{ nullptr, boost::indeterminate, "normal.png", "normal.png", 0, 0 },
{ nullptr, boost::indeterminate, "wait.png", "wait.png", 0, 0 },
{ nullptr, boost::indeterminate, "move.png", "move.png", 0, 0 },
{ nullptr, boost::indeterminate, "attack.png", "attack.png", 0, 0 },
{ nullptr, boost::indeterminate, "select.png", "select.png", 0, 0 },
{ nullptr, boost::indeterminate, "move_drag.png", "move_drag.png", 2, 20 },
{ nullptr, boost::indeterminate, "attack_drag.png", "attack_drag.png", 3, 22 },
{ nullptr, boost::indeterminate, "no_cursor.png", "", 0, 0 }
#endif
}};
cursor::CURSOR_TYPE current_cursor = cursor::NORMAL;
bool have_focus = true;
bool use_color_cursors()
{
return game_config::editor == false && preferences::use_color_cursors();
}
SDL_Cursor* create_cursor(surface surf)
{
const surface nsurf(make_neutral_surface(surf));
if(nsurf == nullptr) {
return nullptr;
}
// The width must be a multiple of 8 (SDL requirement)
#ifdef __APPLE__
std::size_t cursor_width = 16;
#else
std::size_t cursor_width = nsurf->w;
if((cursor_width % 8) != 0) {
cursor_width += 8 - (cursor_width % 8);
}
#endif
std::vector<uint8_t> data((cursor_width * nsurf->h) / 8, 0);
std::vector<uint8_t> mask(data.size(), 0);
// See https://wiki.libsdl.org/SDL_CreateCursor for documentation
// on the format that data has to be in to pass to SDL_CreateCursor
const_surface_lock lock(nsurf);
const uint32_t* const pixels = lock.pixels();
for(int y = 0; y != nsurf->h; ++y) {
for(int x = 0; x != nsurf->w; ++x) {
if(static_cast<std::size_t>(x) < cursor_width) {
uint8_t r, g, b, a;
SDL_GetRGBA(pixels[y * nsurf->w + x], nsurf->format, &r, &g, &b, &a);
const std::size_t index = y * cursor_width + x;
const std::size_t shift = 7 - (index % 8);
const uint8_t trans = (a < 128 ? 0 : 1) << shift;
const uint8_t black = (trans == 0 || (r + g + b) / 3 > 128 ? 0 : 1) << shift;
data[index / 8] |= black;
mask[index / 8] |= trans;
}
}
}
return SDL_CreateCursor(&data[0], &mask[0], cursor_width, nsurf->h, 0, 0);
}
SDL_Cursor* get_cursor(cursor::CURSOR_TYPE type)
{
const bool use_color = use_color_cursors();
cursor_data& data = available_cursors[type];
if(data.cursor == nullptr || boost::indeterminate(data.is_color) || data.is_color != use_color) {
static const std::string color_prefix = "cursors/";
static const std::string bw_prefix = "cursors-bw/";
if(use_color) {
const surface surf(image::get_image(color_prefix + data.image_color));
// Construct a temporary ptr to provide a new deleter.
data.cursor = cursor_ptr_t(SDL_CreateColorCursor(surf, data.hot_x, data.hot_y), SDL_FreeCursor);
} else {
const surface surf(image::get_image(bw_prefix + data.image_bw));
// Construct a temporary ptr to provide a new deleter.
data.cursor = cursor_ptr_t(create_cursor(surf), SDL_FreeCursor);
}
data.is_color = use_color;
}
return data.cursor.get();
}
} // end anon namespace
manager::manager()
{
SDL_ShowCursor(SDL_ENABLE);
set();
}
manager::~manager()
{
SDL_ShowCursor(SDL_ENABLE);
}
void set(CURSOR_TYPE type)
{
// Change only if it's a valid cursor
if(type != NUM_CURSORS) {
current_cursor = type;
} else if(current_cursor == NUM_CURSORS) {
// Except if the current one is also invalid.
// In this case, change to a valid one.
current_cursor = NORMAL;
}
SDL_Cursor* cursor_image = get_cursor(current_cursor);
// Causes problem on Mac:
// if (cursor_image != nullptr && cursor_image != SDL_GetCursor())
SDL_SetCursor(cursor_image);
SDL_ShowCursor(SDL_ENABLE);
}
void set_dragging(bool drag)
{
switch(current_cursor) {
case MOVE:
if(drag) cursor::set(MOVE_DRAG);
break;
case ATTACK:
if(drag) cursor::set(ATTACK_DRAG);
break;
case MOVE_DRAG:
if(!drag) cursor::set(MOVE);
break;
case ATTACK_DRAG:
if(!drag) cursor::set(ATTACK);
break;
default:
break;
}
}
CURSOR_TYPE get()
{
return current_cursor;
}
void set_focus(bool focus)
{
have_focus = focus;
if(!focus) {
set();
}
}
setter::setter(CURSOR_TYPE type)
: old_(current_cursor)
{
set(type);
}
setter::~setter()
{
set(old_);
}
} // end namespace cursor