CheckBox.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // Copyright (c) 2008-2020 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 "../Core/Context.h"
  24. #include "../Input/InputEvents.h"
  25. #include "../UI/CheckBox.h"
  26. #include "../UI/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() = default;
  40. void CheckBox::RegisterObject(Context* context)
  41. {
  42. context->RegisterFactory<CheckBox>(UI_CATEGORY);
  43. URHO3D_COPY_BASE_ATTRIBUTES(BorderImage);
  44. URHO3D_UPDATE_ATTRIBUTE_DEFAULT_VALUE("Is Enabled", true);
  45. URHO3D_UPDATE_ATTRIBUTE_DEFAULT_VALUE("Focus Mode", FM_FOCUSABLE_DEFOCUSABLE);
  46. URHO3D_ACCESSOR_ATTRIBUTE("Is Checked", IsChecked, SetChecked, bool, false, AM_FILE);
  47. URHO3D_ACCESSOR_ATTRIBUTE("Checked Image Offset", GetCheckedOffset, SetCheckedOffset, IntVector2, IntVector2::ZERO, AM_FILE);
  48. }
  49. void CheckBox::GetBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor)
  50. {
  51. IntVector2 offset(IntVector2::ZERO);
  52. if (enabled_)
  53. {
  54. if (hovering_ || selected_ || HasFocus())
  55. offset += hoverOffset_;
  56. }
  57. else
  58. offset += disabledOffset_;
  59. if (checked_)
  60. offset += checkedOffset_;
  61. BorderImage::GetBatches(batches, vertexData, currentScissor, offset);
  62. }
  63. void CheckBox::OnClickBegin(const IntVector2& position, const IntVector2& screenPosition, MouseButton button, MouseButtonFlags buttons, QualifierFlags qualifiers,
  64. Cursor* cursor)
  65. {
  66. if (button == MOUSEB_LEFT && editable_)
  67. SetChecked(!checked_);
  68. }
  69. void CheckBox::OnKey(Key key, MouseButtonFlags buttons, QualifierFlags qualifiers)
  70. {
  71. if (HasFocus() && key == KEY_SPACE)
  72. {
  73. // Simulate LMB click
  74. OnClickBegin(IntVector2(), IntVector2(), MOUSEB_LEFT, MOUSEB_NONE, QUAL_NONE, nullptr);
  75. }
  76. }
  77. void CheckBox::SetChecked(bool enable)
  78. {
  79. if (enable != checked_)
  80. {
  81. checked_ = enable;
  82. using namespace Toggled;
  83. VariantMap& eventData = GetEventDataMap();
  84. eventData[P_ELEMENT] = this;
  85. eventData[P_STATE] = checked_;
  86. SendEvent(E_TOGGLED, eventData);
  87. }
  88. }
  89. void CheckBox::SetCheckedOffset(const IntVector2& offset)
  90. {
  91. checkedOffset_ = offset;
  92. }
  93. void CheckBox::SetCheckedOffset(int x, int y)
  94. {
  95. checkedOffset_ = IntVector2(x, y);
  96. }
  97. }