Cursor.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. namespace SystemUI
  32. {
  33. static const char* shapeNames[] =
  34. {
  35. "Normal",
  36. "IBeam",
  37. "Cross",
  38. "ResizeVertical",
  39. "ResizeDiagonalTopRight",
  40. "ResizeHorizontal",
  41. "ResizeDiagonalTopLeft",
  42. "ResizeAll",
  43. "AcceptDrop",
  44. "RejectDrop",
  45. "Busy",
  46. "BusyArrow"
  47. };
  48. /// OS cursor shape lookup table matching cursor shape enumeration
  49. #if !defined(ANDROID) && !defined(IOS)
  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. #endif
  66. extern const char* UI_CATEGORY;
  67. Cursor::Cursor(Context* context) :
  68. BorderImage(context),
  69. shape_(shapeNames[CS_NORMAL]),
  70. useSystemShapes_(false),
  71. osShapeDirty_(false)
  72. {
  73. // Define the defaults for system cursor usage.
  74. for (unsigned i = 0; i < CS_MAX_SHAPES; i++)
  75. shapeInfos_[shapeNames[i]] = CursorShapeInfo(i);
  76. // Subscribe to OS mouse cursor visibility changes to be able to reapply the cursor shape
  77. SubscribeToEvent(E_MOUSEVISIBLECHANGED, HANDLER(Cursor, HandleMouseVisibleChanged));
  78. }
  79. Cursor::~Cursor()
  80. {
  81. for (HashMap<String, CursorShapeInfo>::Iterator i = shapeInfos_.Begin(); i != shapeInfos_.End(); ++i)
  82. {
  83. if (i->second_.osCursor_)
  84. {
  85. SDL_FreeCursor(i->second_.osCursor_);
  86. i->second_.osCursor_ = 0;
  87. }
  88. }
  89. }
  90. void Cursor::RegisterObject(Context* context)
  91. {
  92. context->RegisterFactory<Cursor>(UI_CATEGORY);
  93. COPY_BASE_ATTRIBUTES(BorderImage);
  94. UPDATE_ATTRIBUTE_DEFAULT_VALUE("Priority", M_MAX_INT);
  95. ACCESSOR_ATTRIBUTE("Use System Shapes", GetUseSystemShapes, SetUseSystemShapes, bool, false, AM_FILE);
  96. MIXED_ACCESSOR_ATTRIBUTE("Shapes", GetShapesAttr, SetShapesAttr, VariantVector, Variant::emptyVariantVector, AM_FILE);
  97. }
  98. void Cursor::GetBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor)
  99. {
  100. unsigned initialSize = vertexData.Size();
  101. const IntVector2& offset = shapeInfos_[shape_].hotSpot_;
  102. Vector2 floatOffset(-(float)offset.x_, -(float)offset.y_);
  103. BorderImage::GetBatches(batches, vertexData, currentScissor);
  104. for (unsigned i = initialSize; i < vertexData.Size(); i += 6)
  105. {
  106. vertexData[i] += floatOffset.x_;
  107. vertexData[i + 1] += floatOffset.y_;
  108. }
  109. }
  110. void Cursor::DefineShape(CursorShape shape, Image* image, const IntRect& imageRect, const IntVector2& hotSpot)
  111. {
  112. if (shape < CS_NORMAL || shape >= CS_MAX_SHAPES)
  113. {
  114. LOGERROR("Shape index out of bounds, can not define cursor shape");
  115. return;
  116. }
  117. DefineShape(shapeNames[shape], image, imageRect, hotSpot);
  118. }
  119. void Cursor::DefineShape(const String& shape, Image* image, const IntRect& imageRect, const IntVector2& hotSpot)
  120. {
  121. if (!image)
  122. return;
  123. ResourceCache* cache = GetSubsystem<ResourceCache>();
  124. if (!shapeInfos_.Contains(shape))
  125. shapeInfos_[shape] = CursorShapeInfo();
  126. CursorShapeInfo& info = shapeInfos_[shape];
  127. // Prefer to get the texture with same name from cache to prevent creating several copies of the texture
  128. info.texture_ = cache->GetResource<Texture2D>(image->GetName(), false);
  129. if (!info.texture_)
  130. {
  131. Texture2D* texture = new Texture2D(context_);
  132. texture->SetData(SharedPtr<Image>(image));
  133. info.texture_ = texture;
  134. }
  135. info.image_ = image;
  136. info.imageRect_ = imageRect;
  137. info.hotSpot_ = hotSpot;
  138. // Remove existing SDL cursor
  139. if (info.osCursor_)
  140. {
  141. SDL_FreeCursor(info.osCursor_);
  142. info.osCursor_ = 0;
  143. }
  144. // Reset current shape if it was edited
  145. if (shape_ == shape)
  146. {
  147. shape_ = String::EMPTY;
  148. SetShape(shape);
  149. }
  150. }
  151. void Cursor::SetShape(const String& shape)
  152. {
  153. if (shape == String::EMPTY || shape.Empty() || shape_ == shape || !shapeInfos_.Contains(shape))
  154. return;
  155. shape_ = shape;
  156. CursorShapeInfo& info = shapeInfos_[shape_];
  157. texture_ = info.texture_;
  158. imageRect_ = info.imageRect_;
  159. SetSize(info.imageRect_.Size());
  160. // To avoid flicker, the UI subsystem will apply the OS shape once per frame. Exception: if we are using the
  161. // busy shape, set it immediately as we may block before that
  162. osShapeDirty_ = true;
  163. if (shape_ == shapeNames[CS_BUSY])
  164. ApplyOSCursorShape();
  165. }
  166. void Cursor::SetShape(CursorShape shape)
  167. {
  168. if (shape < CS_NORMAL || shape >= CS_MAX_SHAPES || shape_ == shapeNames[shape])
  169. return;
  170. SetShape(shapeNames[shape]);
  171. }
  172. void Cursor::SetUseSystemShapes(bool enable)
  173. {
  174. if (enable != useSystemShapes_)
  175. {
  176. useSystemShapes_ = enable;
  177. // Reapply current shape
  178. osShapeDirty_ = true;
  179. }
  180. }
  181. void Cursor::SetShapesAttr(const VariantVector& value)
  182. {
  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. }
  264. }