CheckBox.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 "CheckBox.h"
  25. #include "InputEvents.h"
  26. #include "ResourceCache.h"
  27. #include "Texture.h"
  28. #include "UIEvents.h"
  29. #include "DebugNew.h"
  30. CheckBox::CheckBox(const std::string& name) :
  31. BorderImage(name),
  32. mUncheckedRect(IntRect::sZero),
  33. mCheckedRect(IntRect::sZero),
  34. mChecked(false)
  35. {
  36. mEnabled = true;
  37. }
  38. CheckBox::~CheckBox()
  39. {
  40. }
  41. void CheckBox::setStyle(const XMLElement& element, ResourceCache* cache)
  42. {
  43. if (!cache)
  44. EXCEPTION("Null resource cache for UI element");
  45. BorderImage::setStyle(element, cache);
  46. if (element.hasChildElement("uncheckedrect"))
  47. setUncheckedRect(element.getChildElement("uncheckedrect").getIntRect("value"));
  48. if (element.hasChildElement("checkedrect"))
  49. setCheckedRect(element.getChildElement("checkedrect").getIntRect("value"));
  50. }
  51. void CheckBox::getBatches(std::vector<UIBatch>& batches, std::vector<UIQuad>& quads, const IntRect& currentScissor)
  52. {
  53. if (mChecked)
  54. mImageRect = mCheckedRect;
  55. else
  56. mImageRect = mUncheckedRect;
  57. BorderImage::getBatches(batches, quads, currentScissor);
  58. }
  59. void CheckBox::onClick(const IntVector2& position, const IntVector2& screenPosition, unsigned buttons)
  60. {
  61. if (buttons & MOUSEB_LEFT)
  62. {
  63. mChecked = !mChecked;
  64. using namespace Toggled;
  65. VariantMap eventData;
  66. eventData[P_ELEMENT] = (void*)this;
  67. eventData[P_STATE] = mChecked;
  68. sendEvent(EVENT_TOGGLED, eventData);
  69. }
  70. }
  71. void CheckBox::setChecked(bool enable)
  72. {
  73. // Note: event is intentionally not sent when manually set
  74. mChecked = enable;
  75. }
  76. void CheckBox::setUncheckedRect(const IntRect& rect)
  77. {
  78. mUncheckedRect = rect;
  79. }
  80. void CheckBox::setUncheckedRect(int left, int top, int right, int bottom)
  81. {
  82. mUncheckedRect = IntRect(left, top, right, bottom);
  83. }
  84. void CheckBox::setCheckedRect(const IntRect& rect)
  85. {
  86. mCheckedRect = rect;
  87. }
  88. void CheckBox::setCheckedRect(int left, int top, int right, int bottom)
  89. {
  90. mCheckedRect = IntRect(left, top, right, bottom);
  91. }