CheckBox.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 "Context.h"
  26. #include "InputEvents.h"
  27. #include "UIEvents.h"
  28. #include "DebugNew.h"
  29. OBJECTTYPESTATIC(CheckBox);
  30. CheckBox::CheckBox(Context* context) :
  31. BorderImage(context),
  32. checkedOffset_(IntVector2::ZERO),
  33. checked_(false)
  34. {
  35. active_ = true;
  36. }
  37. CheckBox::~CheckBox()
  38. {
  39. }
  40. void CheckBox::RegisterObject(Context* context)
  41. {
  42. context->RegisterFactory<CheckBox>();
  43. }
  44. void CheckBox::SetStyle(const XMLElement& element)
  45. {
  46. BorderImage::SetStyle(element);
  47. if (element.HasChild("checkedoffset"))
  48. SetCheckedOffset(element.GetChild("checkedoffset").GetIntVector2("value"));
  49. }
  50. void CheckBox::GetBatches(PODVector<UIBatch>& batches, PODVector<UIQuad>& quads, const IntRect& currentScissor)
  51. {
  52. IntVector2 offset(IntVector2::ZERO);
  53. if (hovering_ || selected_)
  54. offset += hoverOffset_;
  55. if (checked_)
  56. offset += checkedOffset_;
  57. BorderImage::GetBatches(batches, quads, currentScissor, offset);
  58. }
  59. void CheckBox::OnClick(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  60. {
  61. if (buttons & MOUSEB_LEFT)
  62. SetChecked(!checked_);
  63. }
  64. void CheckBox::SetChecked(bool enable)
  65. {
  66. if (enable != checked_)
  67. {
  68. checked_ = enable;
  69. using namespace Toggled;
  70. VariantMap eventData;
  71. eventData[P_ELEMENT] = (void*)this;
  72. eventData[P_STATE] = checked_;
  73. SendEvent(E_TOGGLED, eventData);
  74. }
  75. }
  76. void CheckBox::SetCheckedOffset(const IntVector2& offset)
  77. {
  78. checkedOffset_ = offset;
  79. }
  80. void CheckBox::SetCheckedOffset(int x, int y)
  81. {
  82. checkedOffset_ = IntVector2(x, y);
  83. }