Cursor.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. //
  2. // Copyright (c) 2008-2015 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. #include "../Core/Context.h"
  23. #include "../UI/Cursor.h"
  24. #include "../Input/Input.h"
  25. #include "../Input/InputEvents.h"
  26. #include "../IO/Log.h"
  27. #include "../Container/Ptr.h"
  28. #include "../Resource/ResourceCache.h"
  29. #include "../Graphics/Texture2D.h"
  30. #include "../UI/UI.h"
  31. #include "../DebugNew.h"
  32. namespace Atomic
  33. {
  34. static const char* shapeNames[] =
  35. {
  36. "Normal",
  37. "IBeam",
  38. "Cross",
  39. "ResizeVertical",
  40. "ResizeDiagonalTopRight",
  41. "ResizeHorizontal",
  42. "ResizeDiagonalTopLeft",
  43. "ResizeAll",
  44. "AcceptDrop",
  45. "RejectDrop",
  46. "Busy",
  47. "BusyArrow"
  48. };
  49. /// OS cursor shape lookup table matching cursor shape enumeration
  50. static const int osCursorLookup[CS_MAX_SHAPES] =
  51. {
  52. SDL_SYSTEM_CURSOR_ARROW, // CS_NORMAL
  53. SDL_SYSTEM_CURSOR_IBEAM, // CS_IBEAM
  54. SDL_SYSTEM_CURSOR_CROSSHAIR, // CS_CROSS
  55. SDL_SYSTEM_CURSOR_SIZENS, // CS_RESIZEVERTICAL
  56. SDL_SYSTEM_CURSOR_SIZENESW, // CS_RESIZEDIAGONAL_TOPRIGHT
  57. SDL_SYSTEM_CURSOR_SIZEWE, // CS_RESIZEHORIZONTAL
  58. SDL_SYSTEM_CURSOR_SIZENWSE, // CS_RESIZEDIAGONAL_TOPLEFT
  59. SDL_SYSTEM_CURSOR_SIZEALL, // CS_RESIZE_ALL
  60. SDL_SYSTEM_CURSOR_HAND, // CS_ACCEPTDROP
  61. SDL_SYSTEM_CURSOR_NO, // CS_REJECTDROP
  62. SDL_SYSTEM_CURSOR_WAIT, // CS_BUSY
  63. SDL_SYSTEM_CURSOR_WAITARROW // CS_BUSY_ARROW
  64. };
  65. extern const char* UI_CATEGORY;
  66. Cursor::Cursor(Context* context) :
  67. BorderImage(context),
  68. shape_(shapeNames[CS_NORMAL]),
  69. useSystemShapes_(false),
  70. osShapeDirty_(false)
  71. {
  72. // Define the defaults for system cursor usage.
  73. for (unsigned i = 0; i < CS_MAX_SHAPES; i++)
  74. shapeInfos_[shapeNames[i]] = CursorShapeInfo(i);
  75. // Subscribe to OS mouse cursor visibility changes to be able to reapply the cursor shape
  76. SubscribeToEvent(E_MOUSEVISIBLECHANGED, HANDLER(Cursor, HandleMouseVisibleChanged));
  77. }
  78. Cursor::~Cursor()
  79. {
  80. for (HashMap<String, CursorShapeInfo>::Iterator i = shapeInfos_.Begin(); i != shapeInfos_.End(); ++i)
  81. {
  82. if (i->second_.osCursor_)
  83. {
  84. SDL_FreeCursor(i->second_.osCursor_);
  85. i->second_.osCursor_ = 0;
  86. }
  87. }
  88. }
  89. void Cursor::RegisterObject(Context* context)
  90. {
  91. context->RegisterFactory<Cursor>(UI_CATEGORY);
  92. COPY_BASE_ATTRIBUTES(BorderImage);
  93. UPDATE_ATTRIBUTE_DEFAULT_VALUE("Priority", M_MAX_INT);
  94. ACCESSOR_ATTRIBUTE("Use System Shapes", GetUseSystemShapes, SetUseSystemShapes, bool, false, AM_FILE);
  95. MIXED_ACCESSOR_ATTRIBUTE("Shapes", GetShapesAttr, SetShapesAttr, VariantVector, Variant::emptyVariantVector, AM_FILE);
  96. }
  97. void Cursor::GetBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor)
  98. {
  99. unsigned initialSize = vertexData.Size();
  100. const IntVector2& offset = shapeInfos_[shape_].hotSpot_;
  101. Vector2 floatOffset(-(float)offset.x_, -(float)offset.y_);
  102. BorderImage::GetBatches(batches, vertexData, currentScissor);
  103. for (unsigned i = initialSize; i < vertexData.Size(); i += 6)
  104. {
  105. vertexData[i] += floatOffset.x_;
  106. vertexData[i + 1] += floatOffset.y_;
  107. }
  108. }
  109. void Cursor::DefineShape(CursorShape shape, Image* image, const IntRect& imageRect, const IntVector2& hotSpot)
  110. {
  111. if (shape < CS_NORMAL || shape >= CS_MAX_SHAPES)
  112. {
  113. LOGERROR("Shape index out of bounds, can not define cursor shape");
  114. return;
  115. }
  116. DefineShape(shapeNames[shape], image, imageRect, hotSpot);
  117. }
  118. void Cursor::DefineShape(const String& shape, Image* image, const IntRect& imageRect, const IntVector2& hotSpot)
  119. {
  120. if (!image)
  121. return;
  122. ResourceCache* cache = GetSubsystem<ResourceCache>();
  123. if (!shapeInfos_.Contains(shape))
  124. shapeInfos_[shape] = CursorShapeInfo();
  125. CursorShapeInfo& info = shapeInfos_[shape];
  126. // Prefer to get the texture with same name from cache to prevent creating several copies of the texture
  127. info.texture_ = cache->GetResource<Texture2D>(image->GetName(), false);
  128. if (!info.texture_)
  129. {
  130. Texture2D* texture = new Texture2D(context_);
  131. texture->SetData(SharedPtr<Image>(image));
  132. info.texture_ = texture;
  133. }
  134. info.image_ = image;
  135. info.imageRect_ = imageRect;
  136. info.hotSpot_ = hotSpot;
  137. // Remove existing SDL cursor
  138. if (info.osCursor_)
  139. {
  140. SDL_FreeCursor(info.osCursor_);
  141. info.osCursor_ = 0;
  142. }
  143. // Reset current shape if it was edited
  144. if (shape_ == shape)
  145. {
  146. shape_ = String::EMPTY;
  147. SetShape(shape);
  148. }
  149. }
  150. void Cursor::SetShape(const String& shape)
  151. {
  152. if (shape == String::EMPTY || shape.Empty() || shape_ == shape || !shapeInfos_.Contains(shape))
  153. return;
  154. shape_ = shape;
  155. CursorShapeInfo& info = shapeInfos_[shape_];
  156. texture_ = info.texture_;
  157. imageRect_ = info.imageRect_;
  158. SetSize(info.imageRect_.Size());
  159. // To avoid flicker, the UI subsystem will apply the OS shape once per frame. Exception: if we are using the
  160. // busy shape, set it immediately as we may block before that
  161. osShapeDirty_ = true;
  162. if (shape_ == shapeNames[CS_BUSY])
  163. ApplyOSCursorShape();
  164. }
  165. void Cursor::SetShape(CursorShape shape)
  166. {
  167. if (shape < CS_NORMAL || shape >= CS_MAX_SHAPES || shape_ == shapeNames[shape])
  168. return;
  169. SetShape(shapeNames[shape]);
  170. }
  171. void Cursor::SetUseSystemShapes(bool enable)
  172. {
  173. if (enable != useSystemShapes_)
  174. {
  175. useSystemShapes_ = enable;
  176. // Reapply current shape
  177. osShapeDirty_ = true;
  178. }
  179. }
  180. void Cursor::SetShapesAttr(const VariantVector& value)
  181. {
  182. unsigned index = 0;
  183. if (!value.Size())
  184. return;
  185. for (VariantVector::ConstIterator i = value.Begin(); i != value.End(); ++i)
  186. {
  187. VariantVector shapeVector = i->GetVariantVector();
  188. if (shapeVector.Size() >= 4)
  189. {
  190. String shape = shapeVector[0].GetString();
  191. ResourceRef ref = shapeVector[1].GetResourceRef();
  192. IntRect imageRect = shapeVector[2].GetIntRect();
  193. IntVector2 hotSpot = shapeVector[3].GetIntVector2();
  194. DefineShape(shape, GetSubsystem<ResourceCache>()->GetResource<Image>(ref.name_), imageRect, hotSpot);
  195. }
  196. }
  197. }
  198. VariantVector Cursor::GetShapesAttr() const
  199. {
  200. VariantVector ret;
  201. for (HashMap<String, CursorShapeInfo>::ConstIterator i = shapeInfos_.Begin(); i != shapeInfos_.End(); ++i)
  202. {
  203. if (i->second_.imageRect_ != IntRect::ZERO)
  204. {
  205. // Could use a map but this simplifies the UI xml.
  206. VariantVector shape;
  207. shape.Push(i->first_);
  208. shape.Push(GetResourceRef(i->second_.texture_, Texture2D::GetTypeStatic()));
  209. shape.Push(i->second_.imageRect_);
  210. shape.Push(i->second_.hotSpot_);
  211. ret.Push(shape);
  212. }
  213. }
  214. return ret;
  215. }
  216. void Cursor::ApplyOSCursorShape()
  217. {
  218. // Mobile platforms do not support applying OS cursor shapes: comment out to avoid log error messages
  219. #if !defined(ANDROID) && !defined(IOS)
  220. if (!osShapeDirty_ || !GetSubsystem<Input>()->IsMouseVisible() || GetSubsystem<UI>()->GetCursor() != this)
  221. return;
  222. CursorShapeInfo& info = shapeInfos_[shape_];
  223. // Remove existing SDL cursor if is not a system shape while we should be using those, or vice versa
  224. if (info.osCursor_ && info.systemDefined_ != useSystemShapes_)
  225. {
  226. SDL_FreeCursor(info.osCursor_);
  227. info.osCursor_ = 0;
  228. }
  229. // Create SDL cursor now if necessary
  230. if (!info.osCursor_)
  231. {
  232. // Create a system default shape
  233. if (useSystemShapes_ && info.systemCursor_ >= 0 && info.systemCursor_ < CS_MAX_SHAPES)
  234. {
  235. info.osCursor_ = SDL_CreateSystemCursor((SDL_SystemCursor)osCursorLookup[info.systemCursor_]);
  236. info.systemDefined_ = true;
  237. if (!info.osCursor_)
  238. LOGERROR("Could not create system cursor");
  239. }
  240. // Create from image
  241. else if (info.image_)
  242. {
  243. SDL_Surface* surface = info.image_->GetSDLSurface(info.imageRect_);
  244. if (surface)
  245. {
  246. info.osCursor_ = SDL_CreateColorCursor(surface, info.hotSpot_.x_, info.hotSpot_.y_);
  247. info.systemDefined_ = false;
  248. if (!info.osCursor_)
  249. LOGERROR("Could not create cursor from image " + info.image_->GetName());
  250. SDL_FreeSurface(surface);
  251. }
  252. }
  253. }
  254. if (info.osCursor_)
  255. SDL_SetCursor(info.osCursor_);
  256. osShapeDirty_ = false;
  257. #endif
  258. }
  259. void Cursor::HandleMouseVisibleChanged(StringHash eventType, VariantMap& eventData)
  260. {
  261. ApplyOSCursorShape();
  262. }
  263. }