Cursor.cpp 8.6 KB

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