Cursor.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 "Cursor.h"
  25. #include "ResourceCache.h"
  26. #include "StringUtils.h"
  27. #include "Texture2D.h"
  28. #include "DebugNew.h"
  29. static std::string shapeNames[] =
  30. {
  31. "normal",
  32. "resizevertical",
  33. "resizediagonal_topright",
  34. "resizehorizontal",
  35. "resizediagonal_topleft",
  36. "acceptdrop",
  37. "rejectdrop"
  38. };
  39. Cursor::Cursor(const std::string& name) :
  40. BorderImage(name),
  41. mShape(CS_NORMAL)
  42. {
  43. // Show on top of all other UI elements
  44. mPriority = M_MAX_INT;
  45. for (unsigned i = 0; i < CS_MAX_SHAPES; ++i)
  46. {
  47. CursorShapeData& data = mShapeData[i];
  48. data.mImageRect = IntRect::sZero;
  49. data.mHotSpot = IntVector2::sZero;
  50. }
  51. }
  52. Cursor::~Cursor()
  53. {
  54. }
  55. void Cursor::setStyle(const XMLElement& element, ResourceCache* cache)
  56. {
  57. UIElement::setStyle(element, cache);
  58. XMLElement shapeElem = element.getChildElement("shape");
  59. while (shapeElem)
  60. {
  61. CursorShape shape = (CursorShape)getIndexFromStringList(shapeElem.getStringLower("name"), shapeNames, 7, 0);
  62. defineShape(shape, cache->getResource<Texture2D>(shapeElem.getString("texture")), shapeElem.getIntRect("imagerect"),
  63. shapeElem.getIntVector2("hotspot"));
  64. shapeElem = shapeElem.getNextElement("shape");
  65. }
  66. }
  67. void Cursor::defineShape(CursorShape shape, Texture* texture, const IntRect& imageRect, const IntVector2& hotSpot)
  68. {
  69. CursorShapeData& data = mShapeData[shape];
  70. data.mTexture = texture;
  71. data.mImageRect = imageRect;
  72. data.mHotSpot = hotSpot;
  73. // Reset current shape if it was edited
  74. if (shape == mShape)
  75. setShape(mShape);
  76. }
  77. void Cursor::setShape(CursorShape shape)
  78. {
  79. mShape = shape;
  80. CursorShapeData& data = mShapeData[mShape];
  81. mTexture = data.mTexture;
  82. mImageRect = data.mImageRect;
  83. setSize(data.mImageRect.mRight - data.mImageRect.mLeft, data.mImageRect.mBottom - data.mImageRect.mTop);
  84. }
  85. void Cursor::getBatches(std::vector<UIBatch>& batches, std::vector<UIQuad>& quads, const IntRect& currentScissor)
  86. {
  87. unsigned initialSize = quads.size();
  88. const IntVector2& offset = mShapeData[mShape].mHotSpot;
  89. BorderImage::getBatches(batches, quads, currentScissor);
  90. for (unsigned i = initialSize; i < quads.size(); ++i)
  91. {
  92. quads[i].mLeft -= offset.mX;
  93. quads[i].mTop -= offset.mY;
  94. quads[i].mRight -= offset.mX;
  95. quads[i].mBottom -= offset.mY;
  96. }
  97. }