CheckBox.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #include "Base.h"
  2. #include "CheckBox.h"
  3. #include "Game.h"
  4. namespace gameplay
  5. {
  6. CheckBox::CheckBox() : _checked(false), _image(NULL)
  7. {
  8. }
  9. CheckBox::~CheckBox()
  10. {
  11. }
  12. CheckBox* CheckBox::create(const char* id, Theme::Style* style)
  13. {
  14. CheckBox* cb = new CheckBox();
  15. cb->_id = id ? id : "";
  16. cb->initialize("CheckBox", style, NULL);
  17. return cb;
  18. }
  19. Control* CheckBox::create(Theme::Style* style, Properties* properties)
  20. {
  21. CheckBox* cb = new CheckBox();
  22. cb->initialize("CheckBox", style, properties);
  23. return cb;
  24. }
  25. void CheckBox::initialize(const char* typeName, Theme::Style* style, Properties* properties)
  26. {
  27. Button::initialize(typeName, style, properties);
  28. if (properties)
  29. {
  30. _checked = properties->getBool("checked");
  31. }
  32. }
  33. bool CheckBox::isChecked()
  34. {
  35. return _checked;
  36. }
  37. void CheckBox::setChecked(bool checked)
  38. {
  39. if (_checked != checked)
  40. {
  41. _checked = checked;
  42. setDirty(DIRTY_STATE);
  43. notifyListeners(Control::Listener::VALUE_CHANGED);
  44. }
  45. }
  46. void CheckBox::addListener(Control::Listener* listener, int eventFlags)
  47. {
  48. if ((eventFlags & Control::Listener::TEXT_CHANGED) == Control::Listener::TEXT_CHANGED)
  49. {
  50. GP_ERROR("TEXT_CHANGED event is not applicable to CheckBox.");
  51. eventFlags &= ~Control::Listener::TEXT_CHANGED;
  52. }
  53. Control::addListener(listener, eventFlags);
  54. }
  55. bool CheckBox::keyEvent(Keyboard::KeyEvent evt, int key)
  56. {
  57. if (getState() == ACTIVE && evt == Keyboard::KEY_RELEASE && key == Keyboard::KEY_RETURN)
  58. {
  59. setChecked( !_checked );
  60. }
  61. return Button::keyEvent(evt, key);
  62. }
  63. void CheckBox::controlEvent(Control::Listener::EventType evt)
  64. {
  65. Button::controlEvent(evt);
  66. switch (evt)
  67. {
  68. case Control::Listener::CLICK:
  69. setChecked( !_checked );
  70. break;
  71. }
  72. }
  73. void CheckBox::updateState(State state)
  74. {
  75. Label::updateState(state);
  76. _image = getImage(_checked ? "checked" : "unchecked", state);
  77. }
  78. void CheckBox::updateBounds()
  79. {
  80. Label::updateBounds();
  81. Vector2 size;
  82. if (_checked)
  83. {
  84. const Rectangle& selectedRegion = getImageRegion("checked", NORMAL);
  85. size.set(selectedRegion.width, selectedRegion.height);
  86. }
  87. else
  88. {
  89. const Rectangle& unselectedRegion = getImageRegion("unchecked", NORMAL);
  90. size.set(unselectedRegion.width, unselectedRegion.height);
  91. }
  92. if (_autoSize & AUTO_SIZE_HEIGHT)
  93. {
  94. // Text-only width was already measured in Label::update - append image
  95. const Theme::Border& border = getBorder(NORMAL);
  96. const Theme::Border& padding = getPadding();
  97. setHeightInternal(std::max(_bounds.height, size.y + border.top + border.bottom + padding.top + padding.bottom));
  98. }
  99. if (_autoSize & AUTO_SIZE_WIDTH)
  100. {
  101. // Text-only width was already measured in Label::update - append image
  102. setWidthInternal(_bounds.height + 5 + _bounds.width);
  103. }
  104. }
  105. void CheckBox::updateAbsoluteBounds(const Vector2& offset)
  106. {
  107. Label::updateAbsoluteBounds(offset);
  108. _textBounds.x += _bounds.height + 5;
  109. }
  110. unsigned int CheckBox::drawImages(Form* form, const Rectangle& clip)
  111. {
  112. if (!_image)
  113. return 0;
  114. // Left, v-center.
  115. // TODO: Set an alignment for icons.
  116. const Rectangle& region = _image->getRegion();
  117. const Theme::UVs& uvs = _image->getUVs();
  118. Vector4 color = _image->getColor();
  119. color.w *= _opacity;
  120. Vector2 pos(_viewportBounds.x, _viewportBounds.y);
  121. SpriteBatch* batch = _style->getTheme()->getSpriteBatch();
  122. startBatch(form, batch);
  123. batch->draw(pos.x, pos.y, _viewportBounds.height, _viewportBounds.height, uvs.u1, uvs.v1, uvs.u2, uvs.v2, color, _viewportClipBounds);
  124. finishBatch(form, batch);
  125. return 1;
  126. }
  127. const char* CheckBox::getType() const
  128. {
  129. return "checkBox";
  130. }
  131. }