Button.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 "Button.h"
  25. #include "InputEvents.h"
  26. #include "ResourceCache.h"
  27. #include "Texture.h"
  28. #include "UIEvents.h"
  29. #include "DebugNew.h"
  30. Button::Button(const std::string& name) :
  31. BorderImage(name),
  32. mInactiveRect(IntRect::sZero),
  33. mPressedRect(IntRect::sZero),
  34. mLabelOffset(IntVector2::sZero),
  35. mPressed(false)
  36. {
  37. mClipChildren = true;
  38. mEnabled = true;
  39. }
  40. Button::~Button()
  41. {
  42. }
  43. void Button::setStyle(const XMLElement& element, ResourceCache* cache)
  44. {
  45. if (!cache)
  46. EXCEPTION("Null resource cache for UI element");
  47. BorderImage::setStyle(element, cache);
  48. if (element.hasChildElement("inactiverect"))
  49. setInactiveRect(element.getChildElement("inactiverect").getIntRect("value"));
  50. if (element.hasChildElement("pressedrect"))
  51. setPressedRect(element.getChildElement("pressedrect").getIntRect("value"));
  52. if (element.hasChildElement("labeloffset"))
  53. setLabelOffset(element.getChildElement("labeloffset").getIntVector2("value"));
  54. }
  55. void Button::update(float timeStep)
  56. {
  57. if (!mHovering)
  58. setPressed(false);
  59. }
  60. void Button::getBatches(std::vector<UIBatch>& batches, std::vector<UIQuad>& quads, const IntRect& currentScissor)
  61. {
  62. if (mPressed)
  63. mImageRect = mPressedRect;
  64. else
  65. mImageRect = mInactiveRect;
  66. BorderImage::getBatches(batches, quads, currentScissor);
  67. }
  68. void Button::onHover(const IntVector2& position, const IntVector2& screenPosition, unsigned buttons)
  69. {
  70. setPressed((buttons & MOUSEB_LEFT) != 0);
  71. mHovering = true;
  72. }
  73. void Button::onClick(const IntVector2& position, const IntVector2& screenPosition, unsigned buttons)
  74. {
  75. if (buttons & MOUSEB_LEFT)
  76. {
  77. setPressed(true);
  78. mHovering = true;
  79. using namespace Pressed;
  80. VariantMap eventData;
  81. eventData[P_ELEMENT] = (void*)this;
  82. sendEvent(EVENT_PRESSED, eventData);
  83. }
  84. }
  85. void Button::setInactiveRect(const IntRect& rect)
  86. {
  87. mInactiveRect = rect;
  88. }
  89. void Button::setInactiveRect(int left, int top, int right, int bottom)
  90. {
  91. mInactiveRect = IntRect(left, top, right, bottom);
  92. }
  93. void Button::setPressedRect(const IntRect& rect)
  94. {
  95. mPressedRect = rect;
  96. }
  97. void Button::setPressedRect(int left, int top, int right, int bottom)
  98. {
  99. mPressedRect = IntRect(left, top, right, bottom);
  100. }
  101. void Button::setLabelOffset(const IntVector2& offset)
  102. {
  103. mLabelOffset = offset;
  104. }
  105. void Button::setLabelOffset(int x, int y)
  106. {
  107. mLabelOffset = IntVector2(x, y);
  108. }
  109. void Button::setPressed(bool enable)
  110. {
  111. mPressed = enable;
  112. setChildOffset(mPressed ? mLabelOffset : IntVector2::sZero);
  113. }