Cursor.cpp 9.8 KB

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