GamepadForm::GamepadForm() : QWidget() , ui(new Ui::GamepadForm()) { // Here all GUI widgets are created and initialized. ui->setupUi(this); // Disabling buttons since we are not connected to robot yet and can not send any commands. setButtonsEnabled(false); // Some script constants, matching protocol (extra buttons commands) const QString smileScript = "21:direct:brick.smile();"; const QString sayHiScript = "24:direct:brick.say(\"Hi!\");"; // Setting actions to extra buttons pressed. mButtonsMapper.setMapping(ui->buttonSmile, smileScript); mButtonsMapper.setMapping(ui->buttonSayHi, sayHiScript); // Connecting mapper to a slot which will process all extra button presses. connect(&mButtonsMapper, SIGNAL(mapped(QString)), this, SLOT(onButtonPressed(QString))); // Connecting buttons with mapper. connect(ui->buttonSmile, SIGNAL(clicked()), &mButtonsMapper, SLOT(map())); connect(ui->buttonSayHi, SIGNAL(clicked()), &mButtonsMapper, SLOT(map())); // Some script constants, matching protocol (up, down, left, right commands) const QString forwardScript = "67:direct:brick.motor(M3).setPower(100);brick.motor(M4).setPower(100);"; const QString backScript = "73:direct:brick.motor(M3).setPower(-(100));brick.motor(M4).setPower(-(100));"; const QString leftScript = "70:direct:brick.motor(M3).setPower(-(100));brick.motor(M4).setPower(100);"; const QString rightScript = "70:direct:brick.motor(M3).setPower(100);brick.motor(M4).setPower(-(100));"; const QString stopScript = "20:direct:brick.stop();"; // Setting up mapper for pad buttons, "pressed" signal. // Here we provide a command to be sent to a robot instead of id. mPadsMapper.setMapping(ui->buttonPadUp, forwardScript); mPadsMapper.setMapping(ui->buttonPadDown, backScript); mPadsMapper.setMapping(ui->buttonPadLeft, leftScript); mPadsMapper.setMapping(ui->buttonPadRight, rightScript); mPadsMapper.setMapping(ui->buttonStop, stopScript); connect(&mPadsMapper, SIGNAL(mapped(QString)), this, SLOT(onButtonPressed(QString))); connect(ui->buttonPadUp, SIGNAL(pressed()), &mPadsMapper, SLOT(map())); connect(ui->buttonPadDown, SIGNAL(pressed()), &mPadsMapper, SLOT(map())); connect(ui->buttonPadLeft, SIGNAL(pressed()), &mPadsMapper, SLOT(map())); connect(ui->buttonPadRight, SIGNAL(pressed()), &mPadsMapper, SLOT(map())); connect(ui->buttonStop, SIGNAL(pressed()), &mPadsMapper, SLOT(map())); }
bool ButtonControlLayer::onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event) { auto camera = cocos2d::Camera::getVisitingCamera(); if (_selectedItem != nullptr || !_visible || !camera) { return false; } for (Node *c = this->_parent; c != nullptr; c = c->getParent()) { if (c->isVisible() == false) { return false; } } _selectedItem = this->getItemForTouch(touch, camera); if (_selectedItem) { _selectedWithCamera = camera; _selectedItem->selected(); onButtonPressed(_selectedItem->getTag()); return true; } return false; }
void ButtonControlLayer::onTouchMoved(cocos2d::Touch* touch, cocos2d::Event* event) { auto currentItem = this->getItemForTouch(touch, _selectedWithCamera); if (currentItem != _selectedItem) { if (_selectedItem != nullptr) { onButtonReleased(_selectedItem->getTag()); _selectedItem->unselected(); } _selectedItem = currentItem; if (_selectedItem != nullptr) { _selectedItem->selected(); onButtonPressed(_selectedItem->getTag()); } } }