| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- #include "Base.h"
- #include "Button.h"
- #include "Gamepad.h"
- namespace gameplay
- {
- Button::Button() : _dataBinding(0)
- {
- }
- Button::~Button()
- {
- }
- Button* Button::create(const char* id, Theme::Style* style)
- {
- Button* button = new Button();
- button->_id = id;
- button->_style = style;
- return button;
- }
- Button* Button::create(Theme::Style* style, Properties* properties)
- {
- Button* button = new Button();
- button->initialize(style, properties);
- // Different types of data bindings can be named differently in a button namespace.
- // Gamepad button mappings have the name "mapping" and correspond to Gamepad::ButtonMapping enums.
- const char* mapping = properties->getString("mapping");
- if (mapping)
- {
- button->_dataBinding = Gamepad::getButtonMappingFromString(mapping);
- }
- return button;
- }
- bool Button::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
- {
- switch (evt)
- {
- case Touch::TOUCH_PRESS:
- if (_contactIndex == INVALID_CONTACT_INDEX)
- {
- if (x > _clipBounds.x && x <= _clipBounds.x + _clipBounds.width &&
- y > _clipBounds.y && y <= _clipBounds.y + _clipBounds.height)
- {
- _contactIndex = (int) contactIndex;
- setState(Control::ACTIVE);
- notifyListeners(Control::Listener::PRESS);
- return _consumeInputEvents;
- }
- else
- {
- setState(Control::NORMAL);
- }
- }
- break;
- case Touch::TOUCH_RELEASE:
- if (_contactIndex == (int) contactIndex)
- {
- _contactIndex = INVALID_CONTACT_INDEX;
- notifyListeners(Control::Listener::RELEASE);
- if (!_parent->isScrolling() &&
- x > _clipBounds.x && x <= _clipBounds.x + _clipBounds.width &&
- y > _clipBounds.y && y <= _clipBounds.y + _clipBounds.height)
- {
- setState(Control::FOCUS);
- notifyListeners(Control::Listener::CLICK);
- }
- else
- {
- setState(Control::NORMAL);
- }
- return _consumeInputEvents;
- }
- break;
- case Touch::TOUCH_MOVE:
- if (_contactIndex == (int) contactIndex)
- return _consumeInputEvents;
- break;
- }
- return false;
- }
- bool Button::gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad, unsigned int analogIndex)
- {
- switch (evt)
- {
- case Gamepad::BUTTON_EVENT:
- if (_state == Control::FOCUS)
- {
- if (gamepad->isButtonDown(Gamepad::BUTTON_A) ||
- gamepad->isButtonDown(Gamepad::BUTTON_X))
- {
- notifyListeners(Control::Listener::PRESS);
- setState(Control::ACTIVE);
- return _consumeInputEvents;
- }
- }
- else if (_state == Control::ACTIVE)
- {
- if (!gamepad->isButtonDown(Gamepad::BUTTON_A) &&
- !gamepad->isButtonDown(Gamepad::BUTTON_X))
- {
- notifyListeners(Control::Listener::RELEASE);
- notifyListeners(Control::Listener::CLICK);
- setState(Control::FOCUS);
- return _consumeInputEvents;
- }
- }
- break;
- default:
- break;
- }
- return false;
- }
- const char* Button::getType() const
- {
- return "button";
- }
- const unsigned int Button::getDataBinding() const
- {
- return _dataBinding;
- }
- void Button::setDataBinding(unsigned int dataBinding)
- {
- _dataBinding = dataBinding;
- }
- }
|