Cursor.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. static const String shapeNames[] =
  31. {
  32. "normal",
  33. "resizevertical",
  34. "resizediagonal_topright",
  35. "resizehorizontal",
  36. "resizediagonal_topleft",
  37. "acceptdrop",
  38. "rejectdrop",
  39. ""
  40. };
  41. OBJECTTYPESTATIC(Cursor);
  42. Cursor::Cursor(Context* context) :
  43. BorderImage(context),
  44. shape_(CS_NORMAL)
  45. {
  46. // Show on top of all other UI elements
  47. priority_ = M_MAX_INT;
  48. for (unsigned i = 0; i < CS_MAX_SHAPES; ++i)
  49. {
  50. CursorShapeInfo& info = shapeInfos_[i];
  51. info.imageRect_ = IntRect::ZERO;
  52. info.hotSpot_ = IntVector2::ZERO;
  53. }
  54. }
  55. Cursor::~Cursor()
  56. {
  57. }
  58. void Cursor::RegisterObject(Context* context)
  59. {
  60. context->RegisterFactory<Cursor>();
  61. }
  62. void Cursor::SetStyle(const XMLElement& element)
  63. {
  64. UIElement::SetStyle(element);
  65. XMLElement shapeElem = element.GetChild("shape");
  66. while (shapeElem)
  67. {
  68. CursorShape shape = (CursorShape)GetStringListIndex(shapeElem.GetAttributeLower("name"), shapeNames, CS_NORMAL);
  69. DefineShape(shape, GetSubsystem<ResourceCache>()->GetResource<Texture2D>(shapeElem.GetAttribute("texture")),
  70. shapeElem.GetIntRect("imagerect"), shapeElem.GetIntVector2("hotspot"));
  71. shapeElem = shapeElem.GetNext("shape");
  72. }
  73. }
  74. void Cursor::DefineShape(CursorShape shape, Texture* texture, const IntRect& imageRect, const IntVector2& hotSpot)
  75. {
  76. CursorShapeInfo& info = shapeInfos_[shape];
  77. info.texture_ = texture;
  78. info.imageRect_ = imageRect;
  79. info.hotSpot_ = hotSpot;
  80. // Reset current shape if it was edited
  81. if (shape == shape_)
  82. SetShape(shape_);
  83. }
  84. void Cursor::SetShape(CursorShape shape)
  85. {
  86. shape_ = shape;
  87. CursorShapeInfo& info = shapeInfos_[shape_];
  88. texture_ = info.texture_;
  89. imageRect_ = info.imageRect_;
  90. SetSize(info.imageRect_.right_ - info.imageRect_.left_, info.imageRect_.bottom_ - info.imageRect_.top_);
  91. }
  92. void Cursor::GetBatches(PODVector<UIBatch>& batches, PODVector<UIQuad>& quads, const IntRect& currentScissor)
  93. {
  94. unsigned initialSize = quads.Size();
  95. const IntVector2& offset = shapeInfos_[shape_].hotSpot_;
  96. BorderImage::GetBatches(batches, quads, currentScissor);
  97. for (unsigned i = initialSize; i < quads.Size(); ++i)
  98. {
  99. quads[i].left_ -= offset.x_;
  100. quads[i].top_ -= offset.y_;
  101. quads[i].right_ -= offset.x_;
  102. quads[i].bottom_ -= offset.y_;
  103. }
  104. }