CheckBox.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "CheckBox.h"
  24. #include "Context.h"
  25. #include "InputEvents.h"
  26. #include "UIEvents.h"
  27. #include "DebugNew.h"
  28. namespace Urho3D
  29. {
  30. extern const char* UI_CATEGORY;
  31. CheckBox::CheckBox(Context* context) :
  32. BorderImage(context),
  33. checkedOffset_(IntVector2::ZERO),
  34. checked_(false)
  35. {
  36. SetEnabled(true);
  37. focusMode_ = FM_FOCUSABLE_DEFOCUSABLE;
  38. }
  39. CheckBox::~CheckBox()
  40. {
  41. }
  42. void CheckBox::RegisterObject(Context* context)
  43. {
  44. context->RegisterFactory<CheckBox>(UI_CATEGORY);
  45. COPY_BASE_ATTRIBUTES(BorderImage);
  46. UPDATE_ATTRIBUTE_DEFAULT_VALUE("Is Enabled", true);
  47. UPDATE_ATTRIBUTE_DEFAULT_VALUE("Focus Mode", FM_FOCUSABLE_DEFOCUSABLE);
  48. ACCESSOR_ATTRIBUTE("Is Checked", IsChecked, SetChecked, bool, false, AM_FILE);
  49. ACCESSOR_ATTRIBUTE("Checked Image Offset", GetCheckedOffset, SetCheckedOffset, IntVector2, IntVector2::ZERO, AM_FILE);
  50. }
  51. void CheckBox::GetBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor)
  52. {
  53. IntVector2 offset(IntVector2::ZERO);
  54. if (hovering_ || selected_ || HasFocus())
  55. offset += hoverOffset_;
  56. if (checked_)
  57. offset += checkedOffset_;
  58. BorderImage::GetBatches(batches, vertexData, currentScissor, offset);
  59. }
  60. void CheckBox::OnClickBegin(const IntVector2& position, const IntVector2& screenPosition, int button, int buttons, int qualifiers, Cursor* cursor)
  61. {
  62. if (button == MOUSEB_LEFT && editable_)
  63. SetChecked(!checked_);
  64. }
  65. void CheckBox::OnKey(int key, int buttons, int qualifiers)
  66. {
  67. if (HasFocus() && key == KEY_SPACE)
  68. {
  69. // Simulate LMB click
  70. OnClickBegin(IntVector2(), IntVector2(), MOUSEB_LEFT, 0, 0, 0);
  71. }
  72. }
  73. void CheckBox::SetChecked(bool enable)
  74. {
  75. if (enable != checked_)
  76. {
  77. checked_ = enable;
  78. using namespace Toggled;
  79. VariantMap& eventData = GetEventDataMap();
  80. eventData[P_ELEMENT] = this;
  81. eventData[P_STATE] = checked_;
  82. SendEvent(E_TOGGLED, eventData);
  83. }
  84. }
  85. void CheckBox::SetCheckedOffset(const IntVector2& offset)
  86. {
  87. checkedOffset_ = offset;
  88. }
  89. void CheckBox::SetCheckedOffset(int x, int y)
  90. {
  91. checkedOffset_ = IntVector2(x, y);
  92. }
  93. }