Button.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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(0, 0, 0, 0),
  33. mPressedRect(0, 0, 0, 0),
  34. mLabelOffset(0, 0),
  35. mPressed(false)
  36. {
  37. mClipChildren = true;
  38. mEnabled = true;
  39. mLabelContainer = new UIElement();
  40. addChild(mLabelContainer);
  41. }
  42. Button::~Button()
  43. {
  44. }
  45. XMLElement Button::loadParameters(XMLFile* file, const std::string& elementName, ResourceCache* cache)
  46. {
  47. XMLElement paramElem = BorderImage::loadParameters(file, elementName, cache);
  48. if (paramElem.hasChildElement("inactiverect"))
  49. setInactiveRect(paramElem.getChildElement("inactiverect").getIntRect("value"));
  50. if (paramElem.hasChildElement("pressedrect"))
  51. setPressedRect(paramElem.getChildElement("pressedrect").getIntRect("value"));
  52. if (paramElem.hasChildElement("labeloffset"))
  53. setLabelOffset(paramElem.getChildElement("labeloffset").getIntVector2("value"));
  54. return paramElem;
  55. }
  56. void Button::update(float timeStep)
  57. {
  58. if (!mHovering)
  59. setPressed(false);
  60. if (mLabelContainer->getSize() != getSize())
  61. mLabelContainer->setSize(getSize());
  62. }
  63. void Button::getBatches(std::vector<UIBatch>& batches, std::vector<UIQuad>& quads, const IntRect& currentScissor)
  64. {
  65. if (mLabelContainer->getSize() != getSize())
  66. mLabelContainer->setSize(getSize());
  67. if (mPressed)
  68. mImageRect = mPressedRect;
  69. else
  70. mImageRect = mInactiveRect;
  71. BorderImage::getBatches(batches, quads, currentScissor);
  72. mHovering = false;
  73. }
  74. void Button::onHover(const IntVector2& position, const IntVector2& screenPosition, unsigned buttons)
  75. {
  76. setPressed((buttons & MOUSEB_LEFT) != 0);
  77. mHovering = true;
  78. }
  79. void Button::onClick(const IntVector2& position, const IntVector2& screenPosition, unsigned buttons)
  80. {
  81. if (buttons & MOUSEB_LEFT)
  82. {
  83. setPressed(true);
  84. mHovering = true;
  85. using namespace Pressed;
  86. VariantMap eventData;
  87. eventData[P_ELEMENT] = (void*)this;
  88. sendEvent(EVENT_PRESSED, eventData);
  89. }
  90. }
  91. void Button::setInactiveRect(const IntRect& rect)
  92. {
  93. mInactiveRect = rect;
  94. }
  95. void Button::setInactiveRect(int left, int top, int right, int bottom)
  96. {
  97. mInactiveRect = IntRect(left, top, right, bottom);
  98. }
  99. void Button::setPressedRect(const IntRect& rect)
  100. {
  101. mPressedRect = rect;
  102. }
  103. void Button::setPressedRect(int left, int top, int right, int bottom)
  104. {
  105. mPressedRect = IntRect(left, top, right, bottom);
  106. }
  107. void Button::setLabel(UIElement* label)
  108. {
  109. mLabelContainer->removeChild(mLabel);
  110. mLabel = label;
  111. if (mLabel)
  112. {
  113. mLabelContainer->addChild(mLabel);
  114. // Center the label element on the button forcibly
  115. mLabel->setAlignment(HA_CENTER, VA_CENTER);
  116. updateLabelOffset();
  117. }
  118. }
  119. void Button::setLabelOffset(const IntVector2& offset)
  120. {
  121. mLabelOffset = offset;
  122. }
  123. void Button::setLabelOffset(int x, int y)
  124. {
  125. mLabelOffset = IntVector2(x, y);
  126. }
  127. void Button::setPressed(bool enable)
  128. {
  129. if (enable != mPressed)
  130. {
  131. mPressed = enable;
  132. updateLabelOffset();
  133. }
  134. }
  135. void Button::updateLabelOffset()
  136. {
  137. mLabelContainer->setPosition(mPressed ? mLabelOffset : IntVector2::sZero);
  138. }