libgui-sdl/source/gui/GuiTrigger.cpp

143 lines
4.1 KiB
C++

/****************************************************************************
* Copyright (C) 2015 Dimok
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#include <gui/GuiController.h>
/**
* Constructor for the GuiTrigger class.
*/
GuiTrigger::GuiTrigger()
: chan(CHANNEL_ALL), btns(BUTTON_NONE), bClickEverywhere(false), bHoldEverywhere(false), bSelectionClickEverywhere(false), bLastTouched(false) {
}
GuiTrigger::GuiTrigger(uint32_t ch, uint32_t btn, bool clickEverywhere, bool holdEverywhere, bool selectionClickEverywhere)
: chan(ch), btns(btn), bClickEverywhere(clickEverywhere), bHoldEverywhere(holdEverywhere), bSelectionClickEverywhere(selectionClickEverywhere), bLastTouched(false) {
}
/**
* Destructor for the GuiTrigger class.
*/
GuiTrigger::~GuiTrigger() = default;
/**
* Sets a simple trigger. Requires:
* - Element is selected
* - Trigger button is pressed
*/
void GuiTrigger::setTrigger(uint32_t ch, uint32_t btn) {
chan = ch;
btns = btn;
}
bool GuiTrigger::left(const GuiController *controller) const {
if ((controller->chan & chan) == 0) {
return false;
}
if ((controller->data.buttons_h | controller->data.buttons_d) & (BUTTON_LEFT | STICK_L_LEFT)) {
return true;
}
return false;
}
bool GuiTrigger::right(const GuiController *controller) const {
if ((controller->chan & chan) == 0) {
return false;
}
if ((controller->data.buttons_h | controller->data.buttons_d) & (BUTTON_RIGHT | STICK_L_RIGHT)) {
return true;
}
return false;
}
bool GuiTrigger::up(const GuiController *controller) const {
if ((controller->chan & chan) == 0) {
return false;
}
if ((controller->data.buttons_h | controller->data.buttons_d) & (BUTTON_UP | STICK_L_UP)) {
return true;
}
return false;
}
bool GuiTrigger::down(const GuiController *controller) const {
if ((controller->chan & chan) == 0) {
return false;
}
if ((controller->data.buttons_h | controller->data.buttons_d) & (BUTTON_DOWN | STICK_L_DOWN)) {
return true;
}
return false;
}
int32_t GuiTrigger::clicked(const GuiController *controller) const {
if ((controller->chan & chan) == 0) {
return CLICKED_NONE;
}
int32_t bResult = CLICKED_NONE;
if (controller->data.touched && controller->data.validPointer && (btns & TOUCHED) && !controller->lastData.touched) {
bResult = CLICKED_TOUCH;
}
if (controller->data.buttons_d & btns) {
bResult = CLICKED_BUTTON;
}
return bResult;
}
bool GuiTrigger::held(const GuiController *controller) const {
if ((controller->chan & chan) == 0) {
return false;
}
bool bResult = false;
if (controller->data.touched && (btns & TOUCHED) && controller->data.validPointer && controller->lastData.touched && controller->lastData.validPointer) {
bResult = true;
}
if (controller->data.buttons_h & btns) {
bResult = true;
}
return bResult;
}
bool GuiTrigger::released(const GuiController *controller) const {
if ((controller->chan & chan) == 0) {
return false;
}
if (clicked(controller) || held(controller)) {
return false;
}
bool bResult = false;
if (!controller->data.touched && (btns & TOUCHED) && controller->lastData.touched && controller->lastData.validPointer) {
bResult = true;
}
if (controller->data.buttons_r & btns) {
bResult = true;
}
return bResult;
}