Button.cpp 5.1 KB

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