File: cursor.cpp

package info (click to toggle)
freespace2 24.0.2%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: trixie
  • size: 43,188 kB
  • sloc: cpp: 583,107; ansic: 21,729; python: 1,174; sh: 464; makefile: 248; xml: 181
file content (279 lines) | stat: -rw-r--r-- 5,881 bytes parent folder | download | duplicates (4)
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

#include <cmath>
#include <algorithm>

#include "cmdline/cmdline.h"
#include "bmpman/bmpman.h"
#include "io/cursor.h"
#include "io/timer.h"
#include "gamesequence/gamesequence.h"
#include "popup/popup.h"
#include "popup/popupdead.h"

#include <utility>
#include <globalincs/systemvars.h>
#include <graphics/2d.h>

namespace
{
	SDL_Cursor* bitmapToCursor(int bitmapNum)
	{
		auto bitmapSurface = bm_to_sdl_surface(bitmapNum);

		// For now set the hot coordinates to the upper left corner
		SDL_Cursor* cursorHandle = SDL_CreateColorCursor(bitmapSurface, 0, 0);

		SDL_FreeSurface(bitmapSurface);

		return cursorHandle;
	}

	void setRelativeMouseMode(bool grab) {
		if (Cmdline_nograb) {
			// Never grab the mouse if this is enabled
			SDL_SetRelativeMouseMode(SDL_FALSE);
		} else {
			SDL_SetRelativeMouseMode(grab ? SDL_TRUE : SDL_FALSE);
		}
	}
	
	void changeMouseStatus(bool show, bool grab)
	{
		if (show)
		{
			// If shown don't grab the mouse
			setRelativeMouseMode(false);
			SDL_ShowCursor(1);
		}
		else
		{
			if (grab)
			{
				setRelativeMouseMode(true);
			}
			else
			{
				setRelativeMouseMode(false);
			}

			SDL_ShowCursor(0);
		}
	}
}

namespace io
{
	namespace mouse
	{

		Cursor::Cursor(Cursor&& other) noexcept
		{
			*this = std::move(other);
		}
		
		Cursor& Cursor::operator=(Cursor&& other) noexcept
		{
			std::swap(this->mAnimationFrames, other.mAnimationFrames);
			
			this->mBitmapHandle = other.mBitmapHandle;
			this->mBeginTimeStamp = other.mBeginTimeStamp;
			this->mLastFrame = other.mLastFrame;
			
			other.mBitmapHandle = -1;
			other.mBeginTimeStamp = UI_TIMESTAMP::invalid();
			other.mLastFrame = static_cast<size_t>(-1);
			
			return *this;
		}

		Cursor::~Cursor()
		{
			SCP_vector<SDL_Cursor*>::iterator iter;

			for (iter = mAnimationFrames.begin(); iter != mAnimationFrames.end(); ++iter)
			{
				SDL_FreeCursor(*iter);
			}

			mAnimationFrames.clear();
			
			// Free cursor
			bm_release(mBitmapHandle);
			mBitmapHandle = -1;
		}
			
		void Cursor::addFrame(SDL_Cursor* frame)
		{
			mAnimationFrames.push_back(frame);
		}

		void Cursor::enable()
		{
			if (mAnimationFrames.size() > 1)
			{
				// Animated, set the begin and do everything else in setCurrentFrame()
				mBeginTimeStamp = ui_timestamp();
				mLastFrame = static_cast<size_t>(-1);
			}
			else
			{
				// Not animated, just set the first frame
				SDL_SetCursor(mAnimationFrames.front());
			}
		}

		void Cursor::setCurrentFrame()
		{
			if (mAnimationFrames.size() > 1)
			{
				// We are animated, compute the current frame
				float diffSeconds = i2fl(ui_timestamp_since(mBeginTimeStamp)) / TIMESTAMP_FREQUENCY;

				// Use the bmpman function for this. That also ensures that APNG cursors work correctly 
				auto frameIndex = static_cast<size_t>(bm_get_anim_frame(mBitmapHandle, diffSeconds, 0.0f, true));

				Assert(frameIndex < mAnimationFrames.size());

				if (mLastFrame != frameIndex)
				{
					SDL_SetCursor(mAnimationFrames[frameIndex]);
					mLastFrame = frameIndex;
				}
			}
		}

		int Cursor::getBitmapHandle()
		{
			return mBitmapHandle;
		}

		CursorManager* CursorManager::mSingleton = nullptr;

		CursorManager::CursorManager() : mCurrentCursor(nullptr)
		{
			mStatusStack.push_back(std::make_pair(true, false));
		}

		CursorManager::~CursorManager()
		{
		}

		Cursor* CursorManager::loadCursor(const char* fileName, bool animated)
		{
			int handle;

			if (animated)
			{
				handle = bm_load_animation(fileName, nullptr, nullptr);
			}
			else
			{
				handle = bm_load(fileName);
			}

			if (handle < 0)
			{
				mprintf(("Failed to load cursor bitmap %s!\n", fileName));
				return nullptr;
			}

			Cursor* cursor = this->loadFromBitmap(handle);

			return cursor;
		}

		Cursor* CursorManager::loadFromBitmap(int bitmapHandle)
		{
			Assertion(gr_screen.mode != GR_STUB, "Cursors can not be used with the stub renderer!");

			Assertion(bm_is_valid(bitmapHandle), "%d is no valid bitmap handle!", bitmapHandle);

			int nframes;
			int fps;

			bm_get_info(bitmapHandle, nullptr, nullptr, nullptr, &nframes, &fps);

			std::unique_ptr<Cursor> cursor(new Cursor(bitmapHandle));

			for (int i = 0; i < nframes; ++i)
			{
				auto sdlCursor = bitmapToCursor(bitmapHandle + i);
				if (sdlCursor != nullptr) {
					cursor->addFrame(sdlCursor);
				} else {
					// Failed to convert bitmap
					return nullptr;
				}
			}

			mLoadedCursors.push_back(std::move(cursor));

			return mLoadedCursors.back().get();
		}

		void CursorManager::setCurrentCursor(Cursor* cursor)
		{
			Assertion(cursor, "Invalid cursor pointer passed!");

			mCurrentCursor = cursor;
			mCurrentCursor->enable();
		}

		void CursorManager::showCursor(bool show, bool grab)
		{
			auto current = mStatusStack.back();
			if (show == current.first && grab == current.second)
			{
				// Don't bother calling anithing if it's not going to change anything
				return;
			}

			changeMouseStatus(show, grab);

			mStatusStack.back() = std::make_pair(show, grab);
		}
			
		void CursorManager::pushStatus()
		{
			// Copy the last status into the top
			mStatusStack.push_back(mStatusStack.back());
		}
		
		std::pair<bool, bool> CursorManager::popStatus()
		{
			Assertion(mStatusStack.size() > 1, "Can't pop the last status!");
			
			auto current = mStatusStack.back();
			
			mStatusStack.pop_back();
			
			auto newState = mStatusStack.back();
			changeMouseStatus(newState.first, newState.second);
			
			return current;
		}

		void CursorManager::init()
		{
			mSingleton = new CursorManager();

			// Hide the cursor initially
			mSingleton->showCursor(false);
		}

		void CursorManager::doFrame()
		{
			CursorManager* manager = get();

			if (manager->mCurrentCursor != nullptr && manager->isCursorShown())
			{
				manager->mCurrentCursor->setCurrentFrame();
			}
		}

		void CursorManager::shutdown()
		{
			delete mSingleton;
		}
	}
}