CheckBox.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // Copyright (c) 2008-2015 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 "../../Core/Context.h"
  23. #include "../../Input/InputEvents.h"
  24. #include "CheckBox.h"
  25. #include "SystemUIEvents.h"
  26. #include "../../DebugNew.h"
  27. namespace Atomic
  28. {
  29. namespace SystemUI
  30. {
  31. extern const char* UI_CATEGORY;
  32. CheckBox::CheckBox(Context* context) :
  33. BorderImage(context),
  34. checkedOffset_(IntVector2::ZERO),
  35. checked_(false)
  36. {
  37. SetEnabled(true);
  38. focusMode_ = FM_FOCUSABLE_DEFOCUSABLE;
  39. }
  40. CheckBox::~CheckBox()
  41. {
  42. }
  43. void CheckBox::RegisterObject(Context* context)
  44. {
  45. context->RegisterFactory<CheckBox>(UI_CATEGORY);
  46. COPY_BASE_ATTRIBUTES(BorderImage);
  47. UPDATE_ATTRIBUTE_DEFAULT_VALUE("Is Enabled", true);
  48. UPDATE_ATTRIBUTE_DEFAULT_VALUE("Focus Mode", FM_FOCUSABLE_DEFOCUSABLE);
  49. ACCESSOR_ATTRIBUTE("Is Checked", IsChecked, SetChecked, bool, false, AM_FILE);
  50. ACCESSOR_ATTRIBUTE("Checked Image Offset", GetCheckedOffset, SetCheckedOffset, IntVector2, IntVector2::ZERO, AM_FILE);
  51. }
  52. void CheckBox::GetBatches(PODVector<SystemUIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor)
  53. {
  54. IntVector2 offset(IntVector2::ZERO);
  55. if (hovering_ || selected_ || HasFocus())
  56. offset += hoverOffset_;
  57. if (checked_)
  58. offset += checkedOffset_;
  59. BorderImage::GetBatches(batches, vertexData, currentScissor, offset);
  60. }
  61. void CheckBox::OnClickBegin(const IntVector2& position, const IntVector2& screenPosition, int button, int buttons, int qualifiers,
  62. Cursor* cursor)
  63. {
  64. if (button == MOUSEB_LEFT && editable_)
  65. SetChecked(!checked_);
  66. }
  67. void CheckBox::OnKey(int key, int buttons, int qualifiers)
  68. {
  69. if (HasFocus() && key == KEY_SPACE)
  70. {
  71. // Simulate LMB click
  72. OnClickBegin(IntVector2(), IntVector2(), MOUSEB_LEFT, 0, 0, 0);
  73. }
  74. }
  75. void CheckBox::SetChecked(bool enable)
  76. {
  77. if (enable != checked_)
  78. {
  79. checked_ = enable;
  80. using namespace Toggled;
  81. VariantMap& eventData = GetEventDataMap();
  82. eventData[P_ELEMENT] = this;
  83. eventData[P_STATE] = checked_;
  84. SendEvent(E_TOGGLED, eventData);
  85. }
  86. }
  87. void CheckBox::SetCheckedOffset(const IntVector2& offset)
  88. {
  89. checkedOffset_ = offset;
  90. }
  91. void CheckBox::SetCheckedOffset(int x, int y)
  92. {
  93. checkedOffset_ = IntVector2(x, y);
  94. }
  95. }
  96. }