| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- #include "Base.h"
- #include "CheckBox.h"
- #include "Game.h"
- namespace gameplay
- {
- CheckBox::CheckBox() : _checked(false), _image(NULL)
- {
- }
- CheckBox::~CheckBox()
- {
- }
- CheckBox* CheckBox::create(const char* id, Theme::Style* style)
- {
- GP_ASSERT(style);
- CheckBox* checkBox = new CheckBox();
- if (id)
- checkBox->_id = id;
- checkBox->setStyle(style);
- return checkBox;
- }
- CheckBox* CheckBox::create(Theme::Style* style, Properties* properties)
- {
- GP_ASSERT(properties);
- CheckBox* checkBox = new CheckBox();
- checkBox->initialize(style, properties);
- properties->getVector2("imageSize", &checkBox->_imageSize);
- checkBox->_checked = properties->getBool("checked");
- return checkBox;
- }
- bool CheckBox::isChecked()
- {
- return _checked;
- }
- void CheckBox::setChecked(bool checked)
- {
- if (_checked != checked)
- {
- _checked = checked;
- _dirty = true;
- notifyListeners(Control::Listener::VALUE_CHANGED);
- }
- }
- void CheckBox::setImageSize(float width, float height)
- {
- _imageSize.set(width, height);
- _dirty = true;
- }
- const Vector2& CheckBox::getImageSize() const
- {
- return _imageSize;
- }
- void CheckBox::addListener(Control::Listener* listener, int eventFlags)
- {
- if ((eventFlags & Control::Listener::TEXT_CHANGED) == Control::Listener::TEXT_CHANGED)
- {
- GP_ERROR("TEXT_CHANGED event is not applicable to CheckBox.");
- eventFlags &= ~Control::Listener::TEXT_CHANGED;
- }
- Control::addListener(listener, eventFlags);
- }
- bool CheckBox::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
- {
- switch (evt)
- {
- case Touch::TOUCH_RELEASE:
- if (_contactIndex == (int) contactIndex && _state == Control::ACTIVE)
- {
- if (!_parent->isScrolling() &&
- x > _clipBounds.x && x <= _clipBounds.x + _clipBounds.width &&
- y > _clipBounds.y && y <= _clipBounds.y + _clipBounds.height)
- {
- setChecked( !_checked );
- }
- }
- break;
- }
- return Button::touchEvent(evt, x, y, contactIndex);
- }
- bool CheckBox::gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad, unsigned int analogIndex)
- {
- switch (evt)
- {
- case Gamepad::BUTTON_EVENT:
- if (_state == Control::ACTIVE)
- {
- if (!gamepad->isButtonDown(Gamepad::BUTTON_A) &&
- !gamepad->isButtonDown(Gamepad::BUTTON_X))
- {
- setChecked( !_checked );
- }
- }
- break;
- }
- return Button::gamepadEvent(evt, gamepad, analogIndex);
- }
- bool CheckBox::keyEvent(Keyboard::KeyEvent evt, int key)
- {
- if (_state == ACTIVE && evt == Keyboard::KEY_RELEASE && key == Keyboard::KEY_RETURN)
- {
- setChecked( !_checked );
- }
- return Button::keyEvent(evt, key);
- }
- void CheckBox::update(const Control* container, const Vector2& offset)
- {
- Label::update(container, offset);
- Vector2 size;
- if (_imageSize.isZero())
- {
- if (_checked)
- {
- const Rectangle& selectedRegion = getImageRegion("checked", _state);
- size.set(selectedRegion.width, selectedRegion.height);
- }
- else
- {
- const Rectangle& unselectedRegion = getImageRegion("unchecked", _state);
- size.set(unselectedRegion.width, unselectedRegion.height);
- }
- }
- else
- {
- size.set(_imageSize);
- }
-
- if (_autoWidth == Control::AUTO_SIZE_FIT)
- {
- // Text-only width was already measured in Label::update - append image
- setWidth(size.x + _bounds.width + 5);
- }
- if (_autoHeight == Control::AUTO_SIZE_FIT)
- {
- // Text-only width was already measured in Label::update - append image
- setHeight(std::max(getHeight(), size.y));
- }
- _textBounds.x += size.x + 5;
-
- if (_checked)
- {
- _image = getImage("checked", _state);
- }
- else
- {
- _image = getImage("unchecked", _state);
- }
- }
- void CheckBox::drawImages(SpriteBatch* spriteBatch, const Rectangle& clip)
- {
- GP_ASSERT(spriteBatch);
- GP_ASSERT(_image);
- // Left, v-center.
- // TODO: Set an alignment for icons.
-
- const Rectangle& region = _image->getRegion();
- const Theme::UVs& uvs = _image->getUVs();
- Vector4 color = _image->getColor();
- color.w *= _opacity;
- Vector2 size;
- if (_imageSize.isZero())
- {
- size.set(region.width, region.height);
- }
- else
- {
- size.set(_imageSize);
- }
- Vector2 pos(_viewportBounds.x, _viewportBounds.y + _viewportBounds.height * 0.5f - size.y * 0.5f);
- spriteBatch->draw(pos.x, pos.y, size.x, size.y, uvs.u1, uvs.v1, uvs.u2, uvs.v2, color, _viewportClipBounds);
- }
- const char* CheckBox::getType() const
- {
- return "checkBox";
- }
- }
|