CheckBox.cpp 3.1 KB

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