Cursor.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  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 String shapeNames[] =
  33. {
  34. "normal",
  35. "resizevertical",
  36. "resizediagonal_topright",
  37. "resizehorizontal",
  38. "resizediagonal_topleft",
  39. "acceptdrop",
  40. "rejectdrop",
  41. ""
  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. }
  64. void Cursor::SetStyle(const XMLElement& element)
  65. {
  66. UIElement::SetStyle(element);
  67. XMLElement shapeElem = element.GetChild("shape");
  68. while (shapeElem)
  69. {
  70. CursorShape shape = (CursorShape)GetStringListIndex(shapeElem.GetAttributeLower("name"), shapeNames, CS_NORMAL);
  71. DefineShape(shape, GetSubsystem<ResourceCache>()->GetResource<Texture2D>(shapeElem.GetAttribute("texture")),
  72. shapeElem.GetIntRect("imagerect"), shapeElem.GetIntVector2("hotspot"));
  73. shapeElem = shapeElem.GetNext("shape");
  74. }
  75. }
  76. void Cursor::DefineShape(CursorShape shape, Texture* texture, const IntRect& imageRect, const IntVector2& hotSpot)
  77. {
  78. CursorShapeInfo& info = shapeInfos_[shape];
  79. info.texture_ = texture;
  80. info.imageRect_ = imageRect;
  81. info.hotSpot_ = hotSpot;
  82. // Reset current shape if it was edited
  83. if (shape == shape_)
  84. SetShape(shape_);
  85. }
  86. void Cursor::SetShape(CursorShape shape)
  87. {
  88. shape_ = shape;
  89. CursorShapeInfo& info = shapeInfos_[shape_];
  90. texture_ = info.texture_;
  91. imageRect_ = info.imageRect_;
  92. SetSize(info.imageRect_.Size());
  93. }
  94. void Cursor::GetBatches(PODVector<UIBatch>& batches, PODVector<UIQuad>& quads, const IntRect& currentScissor)
  95. {
  96. unsigned initialSize = quads.Size();
  97. const IntVector2& offset = shapeInfos_[shape_].hotSpot_;
  98. BorderImage::GetBatches(batches, quads, currentScissor);
  99. for (unsigned i = initialSize; i < quads.Size(); ++i)
  100. {
  101. quads[i].left_ -= offset.x_;
  102. quads[i].top_ -= offset.y_;
  103. quads[i].right_ -= offset.x_;
  104. quads[i].bottom_ -= offset.y_;
  105. }
  106. }
  107. }