xoreos  0.0.5
cursorman.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include "src/common/scopedptr.h"
26 #include "src/common/util.h"
27 #include "src/common/error.h"
28 
31 
32 #include "src/graphics/graphics.h"
33 #include "src/graphics/windowman.h"
34 
36 
37 namespace Graphics {
38 
39 namespace Aurora {
40 
41 CursorManager::CursorManager() : _hidden(false), _currentCursor(0) {
42 }
43 
45  clear();
46  showCursor();
47 }
48 
50  return !_hidden;
51 }
52 
54  return _currentGroup;
55 }
56 
58  return _currentState;
59 }
60 
63 
64  reset();
65 
66  for (CursorMap::iterator g = _cursors.begin(); g != _cursors.end(); ++g)
67  for (StateMap::iterator c = g->second.begin(); c != g->second.end(); ++c)
68  delete c->second;
69 
70  _cursors.clear();
71 
74 
77 }
78 
79 bool CursorManager::add(const Common::UString &name, const Common::UString &group,
80  const Common::UString &state, int hotspotX, int hotspotY) {
81 
83 
84  try {
85  Common::ScopedPtr<Cursor> cursor(new Cursor(name, hotspotX, hotspotY));
86 
87  CursorMap::iterator g = _cursors.find(group);
88  if (g == _cursors.end()) {
89  std::pair<CursorMap::iterator, bool> result;
90 
91  result = _cursors.insert(std::make_pair(group, StateMap()));
92 
93  g = result.first;
94  }
95 
96  std::pair<StateMap::iterator, bool> result;
97 
98  result = g->second.insert(std::make_pair(state, cursor.get()));
99  if (!result.second)
100  throw Common::Exception("Cursor already exists");
101 
102  cursor.release();
103  } catch (...) {
104  Common::exceptionDispatcherWarning("Could not add cursor \"%s\" as \"%s\":\"%s\"",
105  name.c_str(), group.c_str(), state.c_str());
106  return false;
107  }
108 
109  return true;
110 }
111 
114 
115  _defaultGroup = group;
116  _defaultState = state;
117 }
118 
121 
124 
125  _currentCursor = 0;
126 
127  update();
128 }
129 
132 
134 }
135 
138 
139  set(group, _defaultState);
140 }
141 
144 
145  set(_defaultGroup, state);
146 }
147 
148 void CursorManager::set(const Common::UString &group, const Common::UString &state) {
150 
151  _currentGroup = group;
152  _currentState = state;
153 
155 
156  update();
157 }
158 
159 uint8 CursorManager::getPosition(int &x, int &y) const {
160  return SDL_GetMouseState(&x, &y);
161 }
162 
163 void CursorManager::setPosition(int x, int y) {
164  WindowMan.setCursorPosition(x, y);
165 }
166 
167 void CursorManager::toScreenCoordinates(int x, int y, float &sX, float &sY) {
168  const float sW = WindowMan.getWindowWidth();
169  const float sH = WindowMan.getWindowHeight();
170 
171  sX = ((float) x) - (sW / 2.0f);
172  sY = (sH - ((float) y)) - (sH / 2.0f);
173 }
174 
175 void CursorManager::fromScreenCoordinates(float sX, float sY, int &x, int &y) {
176  const float sW = WindowMan.getWindowWidth();
177  const float sH = WindowMan.getWindowHeight();
178 
179  x = (int) sX + (sW / 2.0f);
180  y = (int) (-sY - (sH / 2.0f)) + sH;
181 }
182 
185 
186  _hidden = true;
187 
188  update();
189 }
190 
193 
194  _hidden = false;
195 
196  update();
197 }
198 
199 Cursor *CursorManager::find(Common::UString &group, Common::UString &state, bool def) const {
200  // Try to find the group. If not found, look for the default cursor.
201  CursorMap::const_iterator g = _cursors.find(group);
202  if (g == _cursors.end())
203  return def ? 0 : find(group = _defaultGroup, state = _defaultState, true);
204 
205  // Try to find the state. If not found, use the first available.
206  StateMap::const_iterator c = g->second.find(state);
207  if (c == g->second.end()) {
208  c = g->second.begin();
209 
210  if (c == g->second.end())
211  return def ? 0 : find(group = _defaultGroup, state = _defaultState, true);
212  }
213 
214  group = g->first;
215  state = c->first;
216 
217  return c->second;
218 }
219 
221  GfxMan.setCursor(_hidden ? 0 : _currentCursor);
222  WindowMan.showCursor(!_hidden && !_currentCursor);
223 }
224 
225 } // End of namespace Aurora
226 
227 } // End of namespace Graphics
bool add(const Common::UString &name, const Common::UString &group, const Common::UString &state="", int hotspotX=-1, int hotspotY=-1)
Add a cursor.
Definition: cursorman.cpp:79
The global graphics manager.
void setPosition(int x, int y)
Move the cursor to a specific position.
Definition: cursorman.cpp:163
const Common::UString & getCurrentGroup() const
Return the current group.
Definition: cursorman.cpp:53
Common::UString _currentState
Definition: cursorman.h:107
A class holding an UTF-8 string.
Definition: ustring.h:48
PointerType release()
Returns the plain pointer value and releases ScopedPtr.
Definition: scopedptr.h:103
void reset()
Reset the cursor to the system&#39;s default.
Definition: cursorman.cpp:119
uint8_t uint8
Definition: types.h:200
The global window manager.
void setGroup(const Common::UString &group)
Set the cursor to another group, using the current state name.
Definition: cursorman.cpp:136
void set()
Set the cursor to the registered default.
Definition: cursorman.cpp:130
void setState(const Common::UString &state)
Set the cursor to a different state within the current group.
Definition: cursorman.cpp:142
Common::UString _currentGroup
Definition: cursorman.h:106
const Common::UString & getCurrentState() const
Return the current state.
Definition: cursorman.cpp:57
void clear()
Remove all managed cursors.
Definition: cursorman.cpp:61
A simple scoped smart pointer template.
void exceptionDispatcherWarning(const char *s,...)
Exception dispatcher that prints the exception as a warning, and adds another reason on top...
Definition: error.cpp:158
Basic exceptions to throw.
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
Utility templates and functions.
void toScreenCoordinates(int x, int y, float &sX, float &sY)
Convert cursor position to screen position.
Definition: cursorman.cpp:167
#define DECLARE_SINGLETON(T)
Note that you need to use this macro from the global namespace.
Definition: singleton.h:122
bool isVisible() const
Is a cursor current visible?
Definition: cursorman.cpp:49
Cursor * find(Common::UString &group, Common::UString &state, bool def=false) const
Definition: cursorman.cpp:199
Common::UString _defaultState
Definition: cursorman.h:110
void hideCursor()
Hide the cursor completely.
Definition: cursorman.cpp:183
void setDefault(const Common::UString &group, const Common::UString &state="")
Register this cursor as the default cursor.
Definition: cursorman.cpp:112
StackException Exception
Definition: error.h:59
A scoped plain pointer, allowing pointer-y access and normal deletion.
Definition: scopedptr.h:120
A cursor as used in the Aurora engines.
Convenience class that locks a mutex on creation and unlocks it on destruction.
Definition: mutex.h:71
The Aurora cursor manager.
The global Aurora cursor manager.
Definition: cursorman.h:42
Common::UString _defaultGroup
Definition: cursorman.h:109
void fromScreenCoordinates(float sX, float sY, int &x, int &y)
Convert screen position to cursor position.
Definition: cursorman.cpp:175
#define WindowMan
Shortcut for accessing the window manager.
Definition: windowman.h:137
PointerType get() const
Returns the plain pointer value.
Definition: scopedptr.h:96
std::map< Common::UString, Cursor * > StateMap
Definition: cursorman.h:101
A cursor as used in the Aurora engines.
Definition: cursor.h:39
void showCursor()
Unhide the cursor.
Definition: cursorman.cpp:191
uint8 getPosition(int &x, int &y) const
Get the current cursor position.
Definition: cursorman.cpp:159
void clear()
Clear the string&#39;s contents.
Definition: ustring.cpp:236
#define GfxMan
Shortcut for accessing the graphics manager.
Definition: graphics.h:299