1 //
2 // Copyright (c) 2008-2017 the Urho3D project.
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 //
22
23 #include "../Precompiled.h"
24
25 #include "../Core/Context.h"
26 #include "../Graphics/Texture2D.h"
27 #include "../Input/Input.h"
28 #include "../IO/Log.h"
29 #include "../Resource/ResourceCache.h"
30 #include "../UI/UI.h"
31
32 #include <SDL/SDL_mouse.h>
33
34 #include "../DebugNew.h"
35
36 namespace Urho3D
37 {
38
39 static const char* shapeNames[] =
40 {
41 "Normal",
42 "IBeam",
43 "Cross",
44 "ResizeVertical",
45 "ResizeDiagonalTopRight",
46 "ResizeHorizontal",
47 "ResizeDiagonalTopLeft",
48 "ResizeAll",
49 "AcceptDrop",
50 "RejectDrop",
51 "Busy",
52 "BusyArrow"
53 };
54
55 /// OS cursor shape lookup table matching cursor shape enumeration
56 #if !defined(__ANDROID__) && !defined(IOS) && !defined(TVOS)
57 static const int osCursorLookup[CS_MAX_SHAPES] =
58 {
59 SDL_SYSTEM_CURSOR_ARROW, // CS_NORMAL
60 SDL_SYSTEM_CURSOR_IBEAM, // CS_IBEAM
61 SDL_SYSTEM_CURSOR_CROSSHAIR, // CS_CROSS
62 SDL_SYSTEM_CURSOR_SIZENS, // CS_RESIZEVERTICAL
63 SDL_SYSTEM_CURSOR_SIZENESW, // CS_RESIZEDIAGONAL_TOPRIGHT
64 SDL_SYSTEM_CURSOR_SIZEWE, // CS_RESIZEHORIZONTAL
65 SDL_SYSTEM_CURSOR_SIZENWSE, // CS_RESIZEDIAGONAL_TOPLEFT
66 SDL_SYSTEM_CURSOR_SIZEALL, // CS_RESIZE_ALL
67 SDL_SYSTEM_CURSOR_HAND, // CS_ACCEPTDROP
68 SDL_SYSTEM_CURSOR_NO, // CS_REJECTDROP
69 SDL_SYSTEM_CURSOR_WAIT, // CS_BUSY
70 SDL_SYSTEM_CURSOR_WAITARROW // CS_BUSY_ARROW
71 };
72 #endif
73
74 extern const char* UI_CATEGORY;
75
Cursor(Context * context)76 Cursor::Cursor(Context* context) :
77 BorderImage(context),
78 shape_(shapeNames[CS_NORMAL]),
79 useSystemShapes_(false),
80 osShapeDirty_(false)
81 {
82 // Define the defaults for system cursor usage.
83 for (unsigned i = 0; i < CS_MAX_SHAPES; i++)
84 shapeInfos_[shapeNames[i]] = CursorShapeInfo(i);
85
86 // Subscribe to OS mouse cursor visibility changes to be able to reapply the cursor shape
87 SubscribeToEvent(E_MOUSEVISIBLECHANGED, URHO3D_HANDLER(Cursor, HandleMouseVisibleChanged));
88 }
89
~Cursor()90 Cursor::~Cursor()
91 {
92 for (HashMap<String, CursorShapeInfo>::Iterator i = shapeInfos_.Begin(); i != shapeInfos_.End(); ++i)
93 {
94 if (i->second_.osCursor_)
95 {
96 SDL_FreeCursor(i->second_.osCursor_);
97 i->second_.osCursor_ = 0;
98 }
99 }
100 }
101
RegisterObject(Context * context)102 void Cursor::RegisterObject(Context* context)
103 {
104 context->RegisterFactory<Cursor>(UI_CATEGORY);
105
106 URHO3D_COPY_BASE_ATTRIBUTES(BorderImage);
107 URHO3D_UPDATE_ATTRIBUTE_DEFAULT_VALUE("Priority", M_MAX_INT);
108 URHO3D_ACCESSOR_ATTRIBUTE("Use System Shapes", GetUseSystemShapes, SetUseSystemShapes, bool, false, AM_FILE);
109 URHO3D_MIXED_ACCESSOR_ATTRIBUTE("Shapes", GetShapesAttr, SetShapesAttr, VariantVector, Variant::emptyVariantVector, AM_FILE);
110 }
111
GetBatches(PODVector<UIBatch> & batches,PODVector<float> & vertexData,const IntRect & currentScissor)112 void Cursor::GetBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor)
113 {
114 unsigned initialSize = vertexData.Size();
115 const IntVector2& offset = shapeInfos_[shape_].hotSpot_;
116 Vector2 floatOffset(-(float)offset.x_, -(float)offset.y_);
117
118 BorderImage::GetBatches(batches, vertexData, currentScissor);
119 for (unsigned i = initialSize; i < vertexData.Size(); i += 6)
120 {
121 vertexData[i] += floatOffset.x_;
122 vertexData[i + 1] += floatOffset.y_;
123 }
124 }
125
DefineShape(CursorShape shape,Image * image,const IntRect & imageRect,const IntVector2 & hotSpot)126 void Cursor::DefineShape(CursorShape shape, Image* image, const IntRect& imageRect, const IntVector2& hotSpot)
127 {
128 if (shape < CS_NORMAL || shape >= CS_MAX_SHAPES)
129 {
130 URHO3D_LOGERROR("Shape index out of bounds, can not define cursor shape");
131 return;
132 }
133
134 DefineShape(shapeNames[shape], image, imageRect, hotSpot);
135 }
136
DefineShape(const String & shape,Image * image,const IntRect & imageRect,const IntVector2 & hotSpot)137 void Cursor::DefineShape(const String& shape, Image* image, const IntRect& imageRect, const IntVector2& hotSpot)
138 {
139 if (!image)
140 return;
141
142 ResourceCache* cache = GetSubsystem<ResourceCache>();
143
144 if (!shapeInfos_.Contains(shape))
145 shapeInfos_[shape] = CursorShapeInfo();
146
147 CursorShapeInfo& info = shapeInfos_[shape];
148
149 // Prefer to get the texture with same name from cache to prevent creating several copies of the texture
150 info.texture_ = cache->GetResource<Texture2D>(image->GetName(), false);
151 if (!info.texture_)
152 {
153 Texture2D* texture = new Texture2D(context_);
154 texture->SetData(SharedPtr<Image>(image));
155 info.texture_ = texture;
156 }
157
158 info.image_ = image;
159 info.imageRect_ = imageRect;
160 info.hotSpot_ = hotSpot;
161
162 // Remove existing SDL cursor
163 if (info.osCursor_)
164 {
165 SDL_FreeCursor(info.osCursor_);
166 info.osCursor_ = 0;
167 }
168
169 // Reset current shape if it was edited
170 if (shape_ == shape)
171 {
172 shape_ = String::EMPTY;
173 SetShape(shape);
174 }
175 }
176
177
SetShape(const String & shape)178 void Cursor::SetShape(const String& shape)
179 {
180 if (shape == String::EMPTY || shape.Empty() || shape_ == shape || !shapeInfos_.Contains(shape))
181 return;
182
183 shape_ = shape;
184
185 CursorShapeInfo& info = shapeInfos_[shape_];
186 texture_ = info.texture_;
187 imageRect_ = info.imageRect_;
188 SetSize(info.imageRect_.Size());
189
190 // To avoid flicker, the UI subsystem will apply the OS shape once per frame. Exception: if we are using the
191 // busy shape, set it immediately as we may block before that
192 osShapeDirty_ = true;
193 if (shape_ == shapeNames[CS_BUSY])
194 ApplyOSCursorShape();
195 }
196
SetShape(CursorShape shape)197 void Cursor::SetShape(CursorShape shape)
198 {
199 if (shape < CS_NORMAL || shape >= CS_MAX_SHAPES || shape_ == shapeNames[shape])
200 return;
201
202 SetShape(shapeNames[shape]);
203 }
204
SetUseSystemShapes(bool enable)205 void Cursor::SetUseSystemShapes(bool enable)
206 {
207 if (enable != useSystemShapes_)
208 {
209 useSystemShapes_ = enable;
210 // Reapply current shape
211 osShapeDirty_ = true;
212 }
213 }
214
SetShapesAttr(const VariantVector & value)215 void Cursor::SetShapesAttr(const VariantVector& value)
216 {
217 if (!value.Size())
218 return;
219
220 for (VariantVector::ConstIterator i = value.Begin(); i != value.End(); ++i)
221 {
222 VariantVector shapeVector = i->GetVariantVector();
223 if (shapeVector.Size() >= 4)
224 {
225 String shape = shapeVector[0].GetString();
226 ResourceRef ref = shapeVector[1].GetResourceRef();
227 IntRect imageRect = shapeVector[2].GetIntRect();
228 IntVector2 hotSpot = shapeVector[3].GetIntVector2();
229
230 DefineShape(shape, GetSubsystem<ResourceCache>()->GetResource<Image>(ref.name_), imageRect, hotSpot);
231 }
232 }
233 }
234
GetShapesAttr() const235 VariantVector Cursor::GetShapesAttr() const
236 {
237 VariantVector ret;
238
239 for (HashMap<String, CursorShapeInfo>::ConstIterator i = shapeInfos_.Begin(); i != shapeInfos_.End(); ++i)
240 {
241 if (i->second_.imageRect_ != IntRect::ZERO)
242 {
243 // Could use a map but this simplifies the UI xml.
244 VariantVector shape;
245 shape.Push(i->first_);
246 shape.Push(GetResourceRef(i->second_.texture_, Texture2D::GetTypeStatic()));
247 shape.Push(i->second_.imageRect_);
248 shape.Push(i->second_.hotSpot_);
249 ret.Push(shape);
250 }
251 }
252
253 return ret;
254 }
255
ApplyOSCursorShape()256 void Cursor::ApplyOSCursorShape()
257 {
258 // Mobile platforms do not support applying OS cursor shapes: comment out to avoid log error messages
259 #if !defined(__ANDROID__) && !defined(IOS) && !defined(TVOS)
260 if (!osShapeDirty_ || !GetSubsystem<Input>()->IsMouseVisible() || GetSubsystem<UI>()->GetCursor() != this)
261 return;
262
263 CursorShapeInfo& info = shapeInfos_[shape_];
264
265 // Remove existing SDL cursor if is not a system shape while we should be using those, or vice versa
266 if (info.osCursor_ && info.systemDefined_ != useSystemShapes_)
267 {
268 SDL_FreeCursor(info.osCursor_);
269 info.osCursor_ = 0;
270 }
271
272 // Create SDL cursor now if necessary
273 if (!info.osCursor_)
274 {
275 // Create a system default shape
276 if (useSystemShapes_ && info.systemCursor_ >= 0 && info.systemCursor_ < CS_MAX_SHAPES)
277 {
278 info.osCursor_ = SDL_CreateSystemCursor((SDL_SystemCursor)osCursorLookup[info.systemCursor_]);
279 info.systemDefined_ = true;
280 if (!info.osCursor_)
281 URHO3D_LOGERROR("Could not create system cursor");
282 }
283 // Create from image
284 else if (info.image_)
285 {
286 SDL_Surface* surface = info.image_->GetSDLSurface(info.imageRect_);
287
288 if (surface)
289 {
290 info.osCursor_ = SDL_CreateColorCursor(surface, info.hotSpot_.x_, info.hotSpot_.y_);
291 info.systemDefined_ = false;
292 if (!info.osCursor_)
293 URHO3D_LOGERROR("Could not create cursor from image " + info.image_->GetName());
294 SDL_FreeSurface(surface);
295 }
296 }
297 }
298
299 if (info.osCursor_)
300 SDL_SetCursor(info.osCursor_);
301
302 osShapeDirty_ = false;
303 #endif
304 }
305
HandleMouseVisibleChanged(StringHash eventType,VariantMap & eventData)306 void Cursor::HandleMouseVisibleChanged(StringHash eventType, VariantMap& eventData)
307 {
308 ApplyOSCursorShape();
309 }
310
311 }
312