Cursor.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. //
  2. // Copyright (c) 2008-2013 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 "StringUtils.h"
  31. #include "Texture2D.h"
  32. #include "UI.h"
  33. #include "DebugNew.h"
  34. namespace Urho3D
  35. {
  36. static const char* shapeNames[] =
  37. {
  38. "Normal",
  39. "ResizeVertical",
  40. "ResizeDiagonalTopRight",
  41. "ResizeHorizontal",
  42. "ResizeDiagonalTopLeft",
  43. "AcceptDrop",
  44. "RejectDrop",
  45. "Busy",
  46. 0
  47. };
  48. /// OS cursor shape lookup table matching cursor shape enumeration
  49. static const int osCursorLookup[CS_MAX_SHAPES] =
  50. {
  51. SDL_SYSTEM_CURSOR_ARROW, // CS_NORMAL
  52. SDL_SYSTEM_CURSOR_SIZENS, // CS_RESIZEVERTICAL
  53. SDL_SYSTEM_CURSOR_SIZENESW, // CS_RESIZEDIAGONAL_TOPRIGHT
  54. SDL_SYSTEM_CURSOR_SIZEWE, // CS_RESIZEHORIZONTAL
  55. SDL_SYSTEM_CURSOR_SIZENWSE, // CS_RESIZEDIAGONAL_TOPLEFT
  56. SDL_SYSTEM_CURSOR_HAND, // CS_ACCEPTDROP
  57. SDL_SYSTEM_CURSOR_NO, // CS_REJECTDROP
  58. SDL_SYSTEM_CURSOR_WAIT // CS_BUSY
  59. };
  60. extern const char* UI_CATEGORY;
  61. Cursor::Cursor(Context* context) :
  62. BorderImage(context),
  63. shape_(CS_NORMAL),
  64. useSystemShapes_(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::SetUseSystemShapes(bool enable)
  90. {
  91. if (enable != useSystemShapes_)
  92. {
  93. useSystemShapes_ = enable;
  94. // Reapply current shape
  95. ApplyShape();
  96. }
  97. }
  98. void Cursor::DefineShape(CursorShape shape, Image* image, const IntRect& imageRect, const IntVector2& hotSpot)
  99. {
  100. if (shape < CS_NORMAL || shape >= CS_MAX_SHAPES)
  101. {
  102. LOGERROR("Shape index out of bounds, can not define cursor shape");
  103. return;
  104. }
  105. if (!image)
  106. return;
  107. ResourceCache* cache = GetSubsystem<ResourceCache>();
  108. CursorShapeInfo& info = shapeInfos_[shape];
  109. // Prefer to get the texture with same name from cache to prevent creating several copies of the texture
  110. if (cache->Exists(image->GetName()))
  111. info.texture_ = cache->GetResource<Texture2D>(image->GetName());
  112. else
  113. {
  114. Texture2D* texture = new Texture2D(context_);
  115. texture->Load(SharedPtr<Image>(image));
  116. info.texture_ = texture;
  117. }
  118. info.image_ = image;
  119. info.imageRect_ = imageRect;
  120. info.hotSpot_ = hotSpot;
  121. // Remove existing SDL cursor
  122. if (info.osCursor_)
  123. {
  124. SDL_FreeCursor(info.osCursor_);
  125. info.osCursor_ = 0;
  126. }
  127. // Reset current shape if it was edited
  128. if (shape == shape_)
  129. ApplyShape();
  130. }
  131. void Cursor::SetShape(CursorShape shape)
  132. {
  133. if (shape_ == shape || shape < CS_NORMAL || shape >= CS_MAX_SHAPES)
  134. return;
  135. shape_ = shape;
  136. ApplyShape();
  137. }
  138. void Cursor::SetShapesAttr(VariantVector value)
  139. {
  140. unsigned index = 0;
  141. if (!value.Size())
  142. return;
  143. unsigned numShapes = value[index++].GetUInt();
  144. while (numShapes-- && (index + 4) <= value.Size())
  145. {
  146. CursorShape shape = (CursorShape)GetStringListIndex(value[index++].GetString().CString(), shapeNames, CS_MAX_SHAPES);
  147. if (shape != CS_MAX_SHAPES)
  148. {
  149. ResourceRef ref = value[index++].GetResourceRef();
  150. IntRect imageRect = value[index++].GetIntRect();
  151. IntVector2 hotSpot = value[index++].GetIntVector2();
  152. DefineShape(shape, GetSubsystem<ResourceCache>()->GetResource<Image>(ref.name_), imageRect, hotSpot);
  153. }
  154. else
  155. index += 3;
  156. }
  157. }
  158. VariantVector Cursor::GetShapesAttr() const
  159. {
  160. VariantVector ret;
  161. unsigned numShapes = 0;
  162. for (unsigned i = 0; i < CS_MAX_SHAPES; ++i)
  163. {
  164. if (shapeInfos_[i].imageRect_ != IntRect::ZERO)
  165. ++numShapes;
  166. }
  167. ret.Push(numShapes);
  168. for (unsigned i = 0; i < CS_MAX_SHAPES; ++i)
  169. {
  170. if (shapeInfos_[i].imageRect_ != IntRect::ZERO)
  171. {
  172. ret.Push(String(shapeNames[i]));
  173. ret.Push(GetResourceRef(shapeInfos_[i].texture_, Texture2D::GetTypeStatic()));
  174. ret.Push(shapeInfos_[i].imageRect_);
  175. ret.Push(shapeInfos_[i].hotSpot_);
  176. }
  177. }
  178. return ret;
  179. }
  180. void Cursor::GetBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor)
  181. {
  182. unsigned initialSize = vertexData.Size();
  183. const IntVector2& offset = shapeInfos_[shape_].hotSpot_;
  184. Vector2 floatOffset(-(float)offset.x_, -(float)offset.y_);
  185. BorderImage::GetBatches(batches, vertexData, currentScissor);
  186. for (unsigned i = initialSize; i < vertexData.Size(); i += 6)
  187. {
  188. vertexData[i] += floatOffset.x_;
  189. vertexData[i + 1] += floatOffset.y_;
  190. }
  191. }
  192. void Cursor::ApplyShape()
  193. {
  194. CursorShapeInfo& info = shapeInfos_[shape_];
  195. texture_ = info.texture_;
  196. imageRect_ = info.imageRect_;
  197. SetSize(info.imageRect_.Size());
  198. // If the OS cursor is being shown, define/set SDL cursor shape if necessary
  199. // Only do this when we are the active UI cursor
  200. if (GetSubsystem<Input>()->IsMouseVisible() && GetSubsystem<UI>()->GetCursor() == this)
  201. {
  202. // Remove existing SDL cursor if is not a system shape while we should be using those, or vice versa
  203. if (info.osCursor_ && info.systemDefined_ != useSystemShapes_)
  204. {
  205. SDL_FreeCursor(info.osCursor_);
  206. info.osCursor_ = 0;
  207. }
  208. // Create SDL cursor now if necessary
  209. if (!info.osCursor_)
  210. {
  211. // Create a system default shape
  212. if (useSystemShapes_)
  213. {
  214. info.osCursor_ = SDL_CreateSystemCursor((SDL_SystemCursor)osCursorLookup[shape_]);
  215. info.systemDefined_ = true;
  216. if (!info.osCursor_)
  217. LOGERROR("Could not create system cursor");
  218. }
  219. // Create from image
  220. else if (info.image_)
  221. {
  222. SDL_Surface* surface = info.image_->GetSDLSurface(info.imageRect_);
  223. if (surface)
  224. {
  225. info.osCursor_ = SDL_CreateColorCursor(surface, info.hotSpot_.x_, info.hotSpot_.y_);
  226. info.systemDefined_ = false;
  227. if (!info.osCursor_)
  228. LOGERROR("Could not create cursor from image " + info.image_->GetName());
  229. SDL_FreeSurface(surface);
  230. }
  231. }
  232. }
  233. if (info.osCursor_)
  234. SDL_SetCursor(info.osCursor_);
  235. }
  236. }
  237. void Cursor::HandleMouseVisibleChanged(StringHash eventType, VariantMap& eventData)
  238. {
  239. ApplyShape();
  240. }
  241. }