libgui/source/gui/GuiTrigger.cpp

154 lines
4.2 KiB
C++
Raw Permalink Normal View History

2017-10-29 10:28:14 +01:00
/****************************************************************************
* 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/>.
****************************************************************************/
2018-06-21 20:44:58 +02:00
#include <gui/GuiElement.h>
#include <gui/GuiController.h>
#include <gui/GuiTrigger.h>
2017-10-29 10:28:14 +01:00
/**
* Constructor for the GuiTrigger class.
*/
GuiTrigger::GuiTrigger()
: chan(CHANNEL_ALL)
, btns(BUTTON_NONE)
, bClickEverywhere(false)
, bHoldEverywhere(false)
, bSelectionClickEverywhere(false)
2018-06-21 20:44:58 +02:00
, bLastTouched(false) {
2017-10-29 10:28:14 +01:00
}
2018-06-21 20:44:58 +02:00
GuiTrigger::GuiTrigger(uint32_t ch, uint32_t btn, bool clickEverywhere, bool holdEverywhere, bool selectionClickEverywhere)
2017-10-29 10:28:14 +01:00
: chan(ch)
, btns(btn)
, bClickEverywhere(clickEverywhere)
, bHoldEverywhere(holdEverywhere)
, bSelectionClickEverywhere(selectionClickEverywhere)
2018-06-21 20:44:58 +02:00
, bLastTouched(false) {
2017-10-29 10:28:14 +01:00
}
/**
* Destructor for the GuiTrigger class.
*/
2018-06-21 20:44:58 +02:00
GuiTrigger::~GuiTrigger() {
2017-10-29 10:28:14 +01:00
}
/**
* Sets a simple trigger. Requires:
* - Element is selected
* - Trigger button is pressed
*/
2018-06-21 20:44:58 +02:00
void GuiTrigger::setTrigger(uint32_t ch, uint32_t btn) {
chan = ch;
btns = btn;
2017-10-29 10:28:14 +01:00
}
2018-06-21 20:44:58 +02:00
bool GuiTrigger::left(const GuiController *controller) const {
2017-10-29 10:28:14 +01:00
if((controller->chan & chan) == 0) {
return false;
}
2018-06-21 20:44:58 +02:00
if((controller->data.buttons_h | controller->data.buttons_d) & (BUTTON_LEFT | STICK_L_LEFT)) {
return true;
}
return false;
2017-10-29 10:28:14 +01:00
}
2018-06-21 20:44:58 +02:00
bool GuiTrigger::right(const GuiController *controller) const {
2017-10-29 10:28:14 +01:00
if((controller->chan & chan) == 0) {
return false;
}
2018-06-21 20:44:58 +02:00
if((controller->data.buttons_h | controller->data.buttons_d) & (BUTTON_RIGHT | STICK_L_RIGHT)) {
return true;
}
return false;
2017-10-29 10:28:14 +01:00
}
2018-06-21 20:44:58 +02:00
bool GuiTrigger::up(const GuiController *controller) const {
2017-10-29 10:28:14 +01:00
if((controller->chan & chan) == 0) {
return false;
}
2018-06-21 20:44:58 +02:00
if((controller->data.buttons_h | controller->data.buttons_d) & (BUTTON_UP | STICK_L_UP)) {
return true;
}
return false;
2017-10-29 10:28:14 +01:00
}
2018-06-21 20:44:58 +02:00
bool GuiTrigger::down(const GuiController *controller) const {
2017-10-29 10:28:14 +01:00
if((controller->chan & chan) == 0) {
return false;
}
2018-06-21 20:44:58 +02:00
if((controller->data.buttons_h | controller->data.buttons_d) & (BUTTON_DOWN | STICK_L_DOWN)) {
return true;
}
return false;
2017-10-29 10:28:14 +01:00
}
2018-06-21 20:44:58 +02:00
int32_t GuiTrigger::clicked(const GuiController *controller) const {
2017-10-29 10:28:14 +01:00
if((controller->chan & chan) == 0) {
return CLICKED_NONE;
}
2018-06-21 20:44:58 +02:00
int32_t bResult = CLICKED_NONE;
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
if(controller->data.touched && controller->data.validPointer && (btns & VPAD_TOUCH) && !controller->lastData.touched) {
2017-10-29 10:28:14 +01:00
bResult = CLICKED_TOUCH;
}
2018-06-21 20:44:58 +02:00
if(controller->data.buttons_d & btns) {
bResult = CLICKED_BUTTON;
}
return bResult;
2017-10-29 10:28:14 +01:00
}
2018-06-21 20:44:58 +02:00
bool GuiTrigger::held(const GuiController *controller) const {
2017-10-29 10:28:14 +01:00
if((controller->chan & chan) == 0) {
return false;
}
bool bResult = false;
2018-06-21 20:44:58 +02:00
if(controller->data.touched && (btns & VPAD_TOUCH) && controller->data.validPointer && controller->lastData.touched && controller->lastData.validPointer) {
2017-10-29 10:28:14 +01:00
bResult = true;
}
2018-06-21 20:44:58 +02:00
if(controller->data.buttons_h & btns) {
bResult = true;
}
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
return bResult;
2017-10-29 10:28:14 +01:00
}
2018-06-21 20:44:58 +02:00
bool GuiTrigger::released(const GuiController *controller) const {
2017-10-29 10:28:14 +01:00
if((controller->chan & chan) == 0) {
return false;
}
if(clicked(controller) || held(controller))
return false;
bool bResult = false;
2018-06-21 20:44:58 +02:00
if(!controller->data.touched && (btns & VPAD_TOUCH) && controller->lastData.touched && controller->lastData.validPointer) {
2017-10-29 10:28:14 +01:00
bResult = true;
}
2018-06-21 20:44:58 +02:00
if(controller->data.buttons_r & btns) {
bResult = true;
}
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
return bResult;
2017-10-29 10:28:14 +01:00
}