Cursor.cpp 8.8 KB

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