Cursor.cpp 9.9 KB

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