Cursor.cpp 3.7 KB

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