2
0

Button.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. mState(BUTTON_INACTIVE)
  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. setState(BUTTON_INACTIVE);
  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. switch (mState)
  66. {
  67. default:
  68. mImageRect = mInactiveRect;
  69. break;
  70. case BUTTON_PRESSED:
  71. mImageRect = mPressedRect;
  72. break;
  73. }
  74. BorderImage::getBatches(batches, quads, currentScissor);
  75. mHovering = false;
  76. }
  77. void Button::onHover(const IntVector2& position, const IntVector2& screenPosition, unsigned buttons)
  78. {
  79. if (buttons & MOUSEB_LEFT)
  80. setState(BUTTON_PRESSED);
  81. else
  82. setState(BUTTON_INACTIVE);
  83. mHovering = true;
  84. }
  85. void Button::onClick(const IntVector2& position, const IntVector2& screenPosition, unsigned buttons)
  86. {
  87. if (buttons & MOUSEB_LEFT)
  88. {
  89. setState(BUTTON_PRESSED);
  90. mHovering = true;
  91. using namespace Pressed;
  92. VariantMap eventData;
  93. eventData[P_ELEMENT] = (void*)this;
  94. sendEvent(EVENT_PRESSED, eventData);
  95. }
  96. }
  97. void Button::setInactiveRect(const IntRect& rect)
  98. {
  99. mInactiveRect = rect;
  100. }
  101. void Button::setInactiveRect(int left, int top, int right, int bottom)
  102. {
  103. mInactiveRect = IntRect(left, top, right, bottom);
  104. }
  105. void Button::setPressedRect(const IntRect& rect)
  106. {
  107. mPressedRect = rect;
  108. }
  109. void Button::setPressedRect(int left, int top, int right, int bottom)
  110. {
  111. mPressedRect = IntRect(left, top, right, bottom);
  112. }
  113. void Button::setLabel(UIElement* label)
  114. {
  115. mLabelContainer->removeChild(mLabel);
  116. mLabel = label;
  117. if (mLabel)
  118. {
  119. mLabelContainer->addChild(mLabel);
  120. // Center the label element on the button forcibly
  121. mLabel->setAlignment(HA_CENTER, VA_CENTER);
  122. updateLabelOffset();
  123. }
  124. }
  125. void Button::setLabelOffset(const IntVector2& offset)
  126. {
  127. mLabelOffset = offset;
  128. }
  129. void Button::setLabelOffset(int x, int y)
  130. {
  131. mLabelOffset = IntVector2(x, y);
  132. }
  133. void Button::setState(ButtonState state)
  134. {
  135. if (state != mState)
  136. {
  137. mState = state;
  138. updateLabelOffset();
  139. }
  140. }
  141. void Button::updateLabelOffset()
  142. {
  143. if (mState == BUTTON_PRESSED)
  144. mLabelContainer->setPosition(mLabelOffset);
  145. else
  146. mLabelContainer->setPosition(IntVector2::sZero);
  147. }