Cursor.cpp 10 KB

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