Cursor.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 "ResourceCache.h"
  26. #include "StringUtils.h"
  27. #include "Texture2D.h"
  28. #include "DebugNew.h"
  29. namespace Urho3D
  30. {
  31. static const char* shapeNames[] =
  32. {
  33. "Normal",
  34. "ResizeVertical",
  35. "ResizeDiagonalTopRight",
  36. "ResizeHorizontal",
  37. "ResizeDiagonalTopLeft",
  38. "AcceptDrop",
  39. "RejectDrop",
  40. 0
  41. };
  42. OBJECTTYPESTATIC(Cursor);
  43. Cursor::Cursor(Context* context) :
  44. BorderImage(context),
  45. shape_(CS_NORMAL)
  46. {
  47. // Show on top of all other UI elements
  48. priority_ = M_MAX_INT;
  49. for (unsigned i = 0; i < CS_MAX_SHAPES; ++i)
  50. {
  51. CursorShapeInfo& info = shapeInfos_[i];
  52. info.imageRect_ = IntRect::ZERO;
  53. info.hotSpot_ = IntVector2::ZERO;
  54. }
  55. }
  56. Cursor::~Cursor()
  57. {
  58. }
  59. void Cursor::RegisterObject(Context* context)
  60. {
  61. context->RegisterFactory<Cursor>();
  62. ACCESSOR_ATTRIBUTE(Cursor, VAR_VARIANTVECTOR, "Shapes", GetShapesAttr, SetShapesAttr, VariantVector, VariantVector(), AM_FILE);
  63. COPY_BASE_ATTRIBUTES(Cursor, BorderImage);
  64. }
  65. void Cursor::DefineShape(CursorShape shape, Texture* texture, const IntRect& imageRect, const IntVector2& hotSpot)
  66. {
  67. CursorShapeInfo& info = shapeInfos_[shape];
  68. info.texture_ = texture;
  69. info.imageRect_ = imageRect;
  70. info.hotSpot_ = hotSpot;
  71. // Reset current shape if it was edited
  72. if (shape == shape_)
  73. SetShape(shape_);
  74. }
  75. void Cursor::SetShape(CursorShape shape)
  76. {
  77. shape_ = shape;
  78. CursorShapeInfo& info = shapeInfos_[shape_];
  79. texture_ = info.texture_;
  80. imageRect_ = info.imageRect_;
  81. SetSize(info.imageRect_.Size());
  82. }
  83. void Cursor::SetShapesAttr(VariantVector value)
  84. {
  85. unsigned index = 0;
  86. if (!value.Size())
  87. return;
  88. unsigned numShapes = value[index++].GetUInt();
  89. while (numShapes-- && (index + 4) <= value.Size())
  90. {
  91. CursorShape shape = (CursorShape)GetStringListIndex(value[index++].GetString().CString(), shapeNames, CS_MAX_SHAPES);
  92. if (shape != CS_MAX_SHAPES)
  93. {
  94. ResourceRef ref = value[index++].GetResourceRef();
  95. IntRect imageRect = value[index++].GetIntRect();
  96. IntVector2 hotSpot = value[index++].GetIntVector2();
  97. DefineShape(shape, GetSubsystem<ResourceCache>()->GetResource<Texture2D>(ref.id_), imageRect, hotSpot);
  98. }
  99. else
  100. index += 3;
  101. }
  102. }
  103. VariantVector Cursor::GetShapesAttr() const
  104. {
  105. VariantVector ret;
  106. unsigned numShapes = 0;
  107. for (unsigned i = 0; i < CS_MAX_SHAPES; ++i)
  108. {
  109. if (shapeInfos_[i].imageRect_ != IntRect::ZERO)
  110. ++numShapes;
  111. }
  112. ret.Push(numShapes);
  113. for (unsigned i = 0; i < CS_MAX_SHAPES; ++i)
  114. {
  115. if (shapeInfos_[i].imageRect_ != IntRect::ZERO)
  116. {
  117. ret.Push(String(shapeNames[i]));
  118. ret.Push(GetResourceRef(shapeInfos_[i].texture_, Texture2D::GetTypeStatic()));
  119. ret.Push(shapeInfos_[i].imageRect_);
  120. ret.Push(shapeInfos_[i].hotSpot_);
  121. }
  122. }
  123. return ret;
  124. }
  125. void Cursor::GetBatches(PODVector<UIBatch>& batches, PODVector<UIQuad>& quads, const IntRect& currentScissor)
  126. {
  127. unsigned initialSize = quads.Size();
  128. const IntVector2& offset = shapeInfos_[shape_].hotSpot_;
  129. BorderImage::GetBatches(batches, quads, currentScissor);
  130. for (unsigned i = initialSize; i < quads.Size(); ++i)
  131. {
  132. quads[i].left_ -= offset.x_;
  133. quads[i].top_ -= offset.y_;
  134. quads[i].right_ -= offset.x_;
  135. quads[i].bottom_ -= offset.y_;
  136. }
  137. }
  138. }