Button.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. mHoverRect(0, 0, 0, 0),
  34. mPressedRect(0, 0, 0, 0),
  35. mLabelOffset(0, 0),
  36. mState(BUTTON_INACTIVE),
  37. mHoveringThisFrame(false)
  38. {
  39. mClipChildren = true;
  40. mEnabled = true;
  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("hoverrect"))
  51. setHoverRect(paramElem.getChildElement("hoverrect").getIntRect("value"));
  52. if (paramElem.hasChildElement("pressedrect"))
  53. setPressedRect(paramElem.getChildElement("pressedrect").getIntRect("value"));
  54. if (paramElem.hasChildElement("labeloffset"))
  55. setLabelOffset(paramElem.getChildElement("labeloffset").getIntVector2("value"));
  56. return paramElem;
  57. }
  58. void Button::update(float timeStep)
  59. {
  60. if (!mHoveringThisFrame)
  61. setState(BUTTON_INACTIVE);
  62. mHoveringThisFrame = false;
  63. }
  64. void Button::getBatches(std::vector<UIBatch>& batches, std::vector<UIQuad>& quads, const IntRect& currentScissor)
  65. {
  66. switch (mState)
  67. {
  68. default:
  69. mImageRect = mInactiveRect;
  70. break;
  71. case BUTTON_HOVER:
  72. mImageRect = mHoverRect;
  73. break;
  74. case BUTTON_PRESSED:
  75. mImageRect = mPressedRect;
  76. break;
  77. }
  78. BorderImage::getBatches(batches, quads, currentScissor);
  79. }
  80. void Button::onHover(const IntVector2& position, const IntVector2& screenPosition, unsigned buttons)
  81. {
  82. if (buttons & MOUSEB_LEFT)
  83. setState(BUTTON_PRESSED);
  84. else
  85. setState(BUTTON_HOVER);
  86. mHoveringThisFrame = true;
  87. }
  88. void Button::onClick(const IntVector2& position, const IntVector2& screenPosition, unsigned buttons)
  89. {
  90. if (buttons & MOUSEB_LEFT)
  91. {
  92. setState(BUTTON_PRESSED);
  93. mHoveringThisFrame = true;
  94. using namespace Pressed;
  95. VariantMap eventData;
  96. eventData[P_ELEMENT] = (void*)this;
  97. sendEvent(EVENT_PRESSED, eventData);
  98. }
  99. }
  100. void Button::setInactiveRect(const IntRect& rect)
  101. {
  102. mInactiveRect = rect;
  103. }
  104. void Button::setInactiveRect(int left, int top, int right, int bottom)
  105. {
  106. mInactiveRect = IntRect(left, top, right, bottom);
  107. }
  108. void Button::setHoverRect(const IntRect& rect)
  109. {
  110. mHoverRect = rect;
  111. }
  112. void Button::setHoverRect(int left, int top, int right, int bottom)
  113. {
  114. mHoverRect = IntRect(left, top, right, bottom);
  115. }
  116. void Button::setPressedRect(const IntRect& rect)
  117. {
  118. mPressedRect = rect;
  119. }
  120. void Button::setPressedRect(int left, int top, int right, int bottom)
  121. {
  122. mPressedRect = IntRect(left, top, right, bottom);
  123. }
  124. void Button::setLabel(UIElement* label)
  125. {
  126. removeChild(mLabel);
  127. mLabel = label;
  128. if (mLabel)
  129. {
  130. addChild(mLabel);
  131. // Center the label element on the button forcibly
  132. mLabel->setAlignment(HA_CENTER, VA_CENTER);
  133. updateLabelOffset();
  134. }
  135. }
  136. void Button::setLabelOffset(const IntVector2& offset)
  137. {
  138. mLabelOffset = offset;
  139. }
  140. void Button::setLabelOffset(int x, int y)
  141. {
  142. mLabelOffset = IntVector2(x, y);
  143. }
  144. void Button::setState(ButtonState state)
  145. {
  146. if (state != mState)
  147. {
  148. mState = state;
  149. updateLabelOffset();
  150. }
  151. }
  152. void Button::updateLabelOffset()
  153. {
  154. if (!mLabel)
  155. return;
  156. if (mState == BUTTON_PRESSED)
  157. mLabel->setPosition(mLabelOffset);
  158. else
  159. mLabel->setPosition(IntVector2::sZero);
  160. }