Cursor.cpp 9.8 KB

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