Cursor.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Context.h"
  25. #include "Cursor.h"
  26. #include "ResourceCache.h"
  27. #include "StringUtils.h"
  28. #include "Texture2D.h"
  29. #include "DebugNew.h"
  30. namespace Urho3D
  31. {
  32. static const char* shapeNames[] =
  33. {
  34. "Normal",
  35. "ResizeVertical",
  36. "ResizeDiagonalTopRight",
  37. "ResizeHorizontal",
  38. "ResizeDiagonalTopLeft",
  39. "AcceptDrop",
  40. "RejectDrop",
  41. 0
  42. };
  43. OBJECTTYPESTATIC(Cursor);
  44. Cursor::Cursor(Context* context) :
  45. BorderImage(context),
  46. shape_(CS_NORMAL)
  47. {
  48. // Show on top of all other UI elements
  49. priority_ = M_MAX_INT;
  50. for (unsigned i = 0; i < CS_MAX_SHAPES; ++i)
  51. {
  52. CursorShapeInfo& info = shapeInfos_[i];
  53. info.imageRect_ = IntRect::ZERO;
  54. info.hotSpot_ = IntVector2::ZERO;
  55. }
  56. }
  57. Cursor::~Cursor()
  58. {
  59. }
  60. void Cursor::RegisterObject(Context* context)
  61. {
  62. context->RegisterFactory<Cursor>();
  63. ACCESSOR_ATTRIBUTE(Cursor, VAR_VARIANTVECTOR, "Shapes", GetShapesAttr, SetShapesAttr, VariantVector, VariantVector(), AM_FILE);
  64. COPY_BASE_ATTRIBUTES(Cursor, BorderImage);
  65. }
  66. void Cursor::DefineShape(CursorShape shape, Texture* texture, const IntRect& imageRect, const IntVector2& hotSpot)
  67. {
  68. CursorShapeInfo& info = shapeInfos_[shape];
  69. info.texture_ = texture;
  70. info.imageRect_ = imageRect;
  71. info.hotSpot_ = hotSpot;
  72. // Reset current shape if it was edited
  73. if (shape == shape_)
  74. SetShape(shape_);
  75. }
  76. void Cursor::SetShape(CursorShape shape)
  77. {
  78. shape_ = shape;
  79. CursorShapeInfo& info = shapeInfos_[shape_];
  80. texture_ = info.texture_;
  81. imageRect_ = info.imageRect_;
  82. SetSize(info.imageRect_.Size());
  83. }
  84. void Cursor::SetShapesAttr(VariantVector value)
  85. {
  86. unsigned index = 0;
  87. if (!value.Size())
  88. return;
  89. unsigned numShapes = value[index++].GetUInt();
  90. while (numShapes-- && (index + 4) <= value.Size())
  91. {
  92. CursorShape shape = (CursorShape)GetStringListIndex(value[index++].GetString().CString(), shapeNames, CS_MAX_SHAPES);
  93. if (shape != CS_MAX_SHAPES)
  94. {
  95. ResourceRef ref = value[index++].GetResourceRef();
  96. IntRect imageRect = value[index++].GetIntRect();
  97. IntVector2 hotSpot = value[index++].GetIntVector2();
  98. DefineShape(shape, GetSubsystem<ResourceCache>()->GetResource<Texture2D>(ref.id_), imageRect, hotSpot);
  99. }
  100. else
  101. index += 3;
  102. }
  103. }
  104. VariantVector Cursor::GetShapesAttr() const
  105. {
  106. VariantVector ret;
  107. unsigned numShapes = 0;
  108. for (unsigned i = 0; i < CS_MAX_SHAPES; ++i)
  109. {
  110. if (shapeInfos_[i].imageRect_ != IntRect::ZERO)
  111. ++numShapes;
  112. }
  113. ret.Push(numShapes);
  114. for (unsigned i = 0; i < CS_MAX_SHAPES; ++i)
  115. {
  116. if (shapeInfos_[i].imageRect_ != IntRect::ZERO)
  117. {
  118. ret.Push(String(shapeNames[i]));
  119. ret.Push(GetResourceRef(shapeInfos_[i].texture_, Texture2D::GetTypeStatic()));
  120. ret.Push(shapeInfos_[i].imageRect_);
  121. ret.Push(shapeInfos_[i].hotSpot_);
  122. }
  123. }
  124. return ret;
  125. }
  126. void Cursor::GetBatches(PODVector<UIBatch>& batches, PODVector<UIQuad>& quads, const IntRect& currentScissor)
  127. {
  128. unsigned initialSize = quads.Size();
  129. const IntVector2& offset = shapeInfos_[shape_].hotSpot_;
  130. BorderImage::GetBatches(batches, quads, currentScissor);
  131. for (unsigned i = initialSize; i < quads.Size(); ++i)
  132. {
  133. quads[i].left_ -= offset.x_;
  134. quads[i].top_ -= offset.y_;
  135. quads[i].right_ -= offset.x_;
  136. quads[i].bottom_ -= offset.y_;
  137. }
  138. }
  139. }