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