libgui/source/gui/GuiButton.cpp

269 lines
8.3 KiB
C++
Raw 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/GuiButton.h>
#include <gui/GuiTrigger.h>
#include <gui/GuiController.h>
2017-10-29 10:28:14 +01:00
/**
* Constructor for the GuiButton class.
*/
2018-06-21 20:44:58 +02:00
GuiButton::GuiButton(float w, float h) {
2017-10-29 10:28:14 +01:00
width = w;
height = h;
2018-06-21 20:44:58 +02:00
image = NULL;
imageOver = NULL;
imageHold = NULL;
imageClick = NULL;
icon = NULL;
iconOver = NULL;
2020-08-13 12:38:07 +02:00
for (int32_t i = 0; i < 4; i++) {
2018-06-21 20:44:58 +02:00
label[i] = NULL;
labelOver[i] = NULL;
labelHold[i] = NULL;
labelClick[i] = NULL;
}
2020-08-13 12:38:07 +02:00
for (int32_t i = 0; i < iMaxGuiTriggers; i++) {
2018-06-21 20:44:58 +02:00
trigger[i] = NULL;
}
soundOver = NULL;
soundHold = NULL;
soundClick = NULL;
clickedTrigger = NULL;
heldTrigger = NULL;
selectable = true;
holdable = false;
clickable = true;
2017-10-29 10:28:14 +01:00
}
/**
* Destructor for the GuiButton class.
*/
2018-06-21 20:44:58 +02:00
GuiButton::~GuiButton() {
2017-10-29 10:28:14 +01:00
}
2020-08-13 12:38:07 +02:00
void GuiButton::setImage(GuiImage *img) {
2018-06-21 20:44:58 +02:00
image = img;
2020-08-13 12:38:07 +02:00
if (img) img->setParent(this);
2017-10-29 10:28:14 +01:00
}
2020-08-13 12:38:07 +02:00
void GuiButton::setImageOver(GuiImage *img) {
2018-06-21 20:44:58 +02:00
imageOver = img;
2020-08-13 12:38:07 +02:00
if (img) img->setParent(this);
2017-10-29 10:28:14 +01:00
}
2020-08-13 12:38:07 +02:00
void GuiButton::setImageHold(GuiImage *img) {
2018-06-21 20:44:58 +02:00
imageHold = img;
2020-08-13 12:38:07 +02:00
if (img) img->setParent(this);
2017-10-29 10:28:14 +01:00
}
2020-08-13 12:38:07 +02:00
void GuiButton::setImageClick(GuiImage *img) {
2018-06-21 20:44:58 +02:00
imageClick = img;
2020-08-13 12:38:07 +02:00
if (img) img->setParent(this);
2017-10-29 10:28:14 +01:00
}
2020-08-13 12:38:07 +02:00
void GuiButton::setIcon(GuiImage *img) {
2018-06-21 20:44:58 +02:00
icon = img;
2020-08-13 12:38:07 +02:00
if (img) img->setParent(this);
2017-10-29 10:28:14 +01:00
}
2020-08-13 12:38:07 +02:00
void GuiButton::setIconOver(GuiImage *img) {
2018-06-21 20:44:58 +02:00
iconOver = img;
2020-08-13 12:38:07 +02:00
if (img) img->setParent(this);
2017-10-29 10:28:14 +01:00
}
2020-08-13 12:38:07 +02:00
void GuiButton::setLabel(GuiText *txt, int32_t n) {
2018-06-21 20:44:58 +02:00
label[n] = txt;
2020-08-13 12:38:07 +02:00
if (txt) txt->setParent(this);
2017-10-29 10:28:14 +01:00
}
2020-08-13 12:38:07 +02:00
void GuiButton::setLabelOver(GuiText *txt, int32_t n) {
2018-06-21 20:44:58 +02:00
labelOver[n] = txt;
2020-08-13 12:38:07 +02:00
if (txt) txt->setParent(this);
2017-10-29 10:28:14 +01:00
}
2020-08-13 12:38:07 +02:00
void GuiButton::setLabelHold(GuiText *txt, int32_t n) {
2018-06-21 20:44:58 +02:00
labelHold[n] = txt;
2020-08-13 12:38:07 +02:00
if (txt) txt->setParent(this);
2017-10-29 10:28:14 +01:00
}
2020-08-13 12:38:07 +02:00
void GuiButton::setLabelClick(GuiText *txt, int32_t n) {
2018-06-21 20:44:58 +02:00
labelClick[n] = txt;
2020-08-13 12:38:07 +02:00
if (txt) txt->setParent(this);
2017-10-29 10:28:14 +01:00
}
2020-08-13 12:38:07 +02:00
void GuiButton::setSoundOver(GuiSound *snd) {
2018-06-21 20:44:58 +02:00
soundOver = snd;
2017-10-29 10:28:14 +01:00
}
2020-08-13 12:38:07 +02:00
void GuiButton::setSoundHold(GuiSound *snd) {
2018-06-21 20:44:58 +02:00
soundHold = snd;
2017-10-29 10:28:14 +01:00
}
2020-08-13 12:38:07 +02:00
void GuiButton::setSoundClick(GuiSound *snd) {
2018-06-21 20:44:58 +02:00
soundClick = snd;
2017-10-29 10:28:14 +01:00
}
2020-08-13 12:38:07 +02:00
void GuiButton::setTrigger(GuiTrigger *t, int32_t idx) {
if (idx >= 0 && idx < iMaxGuiTriggers) {
2017-10-29 10:28:14 +01:00
trigger[idx] = t;
2018-06-21 20:44:58 +02:00
} else {
2020-08-13 12:38:07 +02:00
for (int32_t i = 0; i < iMaxGuiTriggers; i++) {
if (!trigger[i]) {
2017-10-29 10:28:14 +01:00
trigger[i] = t;
break;
}
}
}
}
2018-06-21 20:44:58 +02:00
void GuiButton::resetState(void) {
clickedTrigger = NULL;
2017-10-29 10:28:14 +01:00
heldTrigger = NULL;
GuiElement::resetState();
}
/**
* Draw the button on screen
*/
2018-06-21 20:44:58 +02:00
void GuiButton::draw(CVideo *v) {
2020-08-13 12:38:07 +02:00
if (!this->isVisible())
2018-06-21 20:44:58 +02:00
return;
// draw image
2020-08-13 12:38:07 +02:00
if ((isDrawOverOnlyWhenSelected() && (isStateSet(STATE_SELECTED) && imageOver)) ||
(!isDrawOverOnlyWhenSelected() && (isStateSet(STATE_OVER | STATE_SELECTED | STATE_CLICKED | STATE_HELD) && imageOver)))
2018-06-21 20:44:58 +02:00
imageOver->draw(v);
2020-08-13 12:38:07 +02:00
else if (image)
2018-06-21 20:44:58 +02:00
image->draw(v);
2020-08-13 12:38:07 +02:00
if ((isDrawOverOnlyWhenSelected() && (isStateSet(STATE_SELECTED) && iconOver)) ||
(!isDrawOverOnlyWhenSelected() && (isStateSet(STATE_OVER | STATE_SELECTED | STATE_CLICKED | STATE_HELD) && iconOver)))
2018-06-21 20:44:58 +02:00
iconOver->draw(v);
2020-08-13 12:38:07 +02:00
else if (icon)
2018-06-21 20:44:58 +02:00
icon->draw(v);
// draw text
2020-08-13 12:38:07 +02:00
for (int32_t i = 0; i < 4; i++) {
if (isStateSet(STATE_OVER | STATE_SELECTED | STATE_CLICKED | STATE_HELD) && labelOver[i])
2018-06-21 20:44:58 +02:00
labelOver[i]->draw(v);
2020-08-13 12:38:07 +02:00
else if (label[i])
2018-06-21 20:44:58 +02:00
label[i]->draw(v);
}
2017-10-29 10:28:14 +01:00
}
2020-08-13 12:38:07 +02:00
void GuiButton::update(GuiController *c) {
if (!c || isStateSet(STATE_DISABLED | STATE_HIDDEN | STATE_DISABLE_INPUT, c->chanIdx))
2018-06-21 20:44:58 +02:00
return;
2020-08-13 12:38:07 +02:00
else if (parentElement && (parentElement->isStateSet(STATE_DISABLED | STATE_HIDDEN | STATE_DISABLE_INPUT, c->chanIdx)))
2018-06-21 20:44:58 +02:00
return;
2020-08-13 12:38:07 +02:00
if (selectable) {
if (c->data.validPointer && this->isInside(c->data.x, c->data.y)) {
if (!isStateSet(STATE_OVER, c->chanIdx)) {
2020-04-24 12:29:52 +02:00
setState(STATE_OVER, c->chanIdx);
2018-06-21 20:44:58 +02:00
//if(this->isRumbleActive())
// this->rumble(t->chan);
2020-08-13 12:38:07 +02:00
if (soundOver)
2018-06-21 20:44:58 +02:00
soundOver->Play();
2020-08-13 12:38:07 +02:00
if (effectsOver && !effects) {
2018-06-21 20:44:58 +02:00
// initiate effects
effects = effectsOver;
effectAmount = effectAmountOver;
effectTarget = effectTargetOver;
}
2017-10-29 10:28:14 +01:00
pointedOn(this, c);
2018-06-21 20:44:58 +02:00
}
2020-08-13 12:38:07 +02:00
} else if (isStateSet(STATE_OVER, c->chanIdx)) {
2020-04-24 12:29:52 +02:00
this->clearState(STATE_OVER, c->chanIdx);
2017-10-29 10:28:14 +01:00
pointedOff(this, c);
2020-08-13 12:38:07 +02:00
if (effectTarget == effectTargetOver && effectAmount == effectAmountOver) {
2018-06-21 20:44:58 +02:00
// initiate effects (in reverse)
effects = effectsOver;
effectAmount = -effectAmountOver;
effectTarget = 100;
}
2017-10-29 10:28:14 +01:00
}
}
2020-08-13 12:38:07 +02:00
for (int32_t i = 0; i < iMaxGuiTriggers; i++) {
if (!trigger[i])
2017-10-29 10:28:14 +01:00
continue;
// button triggers
2020-08-13 12:38:07 +02:00
if (clickable) {
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
int32_t isClicked = trigger[i]->clicked(c);
2017-10-29 10:28:14 +01:00
2020-08-13 12:38:07 +02:00
if (!clickedTrigger && (isClicked != GuiTrigger::CLICKED_NONE)
&& (trigger[i]->isClickEverywhere() || (isStateSet(STATE_SELECTED | STATE_OVER, c->chanIdx) && trigger[i]->isSelectionClickEverywhere()) || this->isInside(c->data.x, c->data.y))) {
if (soundClick)
2017-10-29 10:28:14 +01:00
soundClick->Play();
clickedTrigger = trigger[i];
2020-08-13 12:38:07 +02:00
if (!isStateSet(STATE_CLICKED, c->chanIdx)) {
if (isClicked == GuiTrigger::CLICKED_TOUCH) {
2020-04-24 12:29:52 +02:00
setState(STATE_CLICKED_TOUCH, c->chanIdx);
2018-06-21 20:44:58 +02:00
} else {
2020-04-24 12:29:52 +02:00
setState(STATE_CLICKED, c->chanIdx);
2017-10-29 10:28:14 +01:00
}
}
clicked(this, c, trigger[i]);
2020-08-13 12:38:07 +02:00
} else if ((isStateSet(STATE_CLICKED, c->chanIdx) || isStateSet(STATE_CLICKED_TOUCH, c->chanIdx)) && (clickedTrigger == trigger[i]) && !isStateSet(STATE_HELD, c->chanIdx) && !trigger[i]->held(c) &&
((isClicked == GuiTrigger::CLICKED_NONE) || trigger[i]->released(c))) {
if ((isStateSet(STATE_CLICKED_TOUCH, c->chanIdx) && this->isInside(c->data.x, c->data.y)) || (isStateSet(STATE_CLICKED, c->chanIdx))) {
2017-10-29 10:28:14 +01:00
clickedTrigger = NULL;
2020-04-24 12:29:52 +02:00
clearState(STATE_CLICKED, c->chanIdx);
2017-10-29 10:28:14 +01:00
released(this, c, trigger[i]);
}
}
}
2020-08-13 12:38:07 +02:00
if (holdable) {
2017-10-29 10:28:14 +01:00
bool isHeld = trigger[i]->held(c);
2020-08-13 12:38:07 +02:00
if ((!heldTrigger || heldTrigger == trigger[i]) && isHeld
&& (trigger[i]->isHoldEverywhere() || (isStateSet(STATE_SELECTED | STATE_OVER, c->chanIdx) && trigger[i]->isSelectionClickEverywhere()) || this->isInside(c->data.x, c->data.y))) {
2017-10-29 10:28:14 +01:00
heldTrigger = trigger[i];
2020-08-13 12:38:07 +02:00
if (!isStateSet(STATE_HELD, c->chanIdx))
2020-04-24 12:29:52 +02:00
setState(STATE_HELD, c->chanIdx);
2017-10-29 10:28:14 +01:00
held(this, c, trigger[i]);
2020-08-13 12:38:07 +02:00
} else if (isStateSet(STATE_HELD, c->chanIdx) && (heldTrigger == trigger[i]) && (!isHeld || trigger[i]->released(c))) {
2017-10-29 10:28:14 +01:00
//! click is removed at this point and converted to held
2020-08-13 12:38:07 +02:00
if (clickedTrigger == trigger[i]) {
2017-10-29 10:28:14 +01:00
clickedTrigger = NULL;
2020-04-24 12:29:52 +02:00
clearState(STATE_CLICKED, c->chanIdx);
2017-10-29 10:28:14 +01:00
}
heldTrigger = NULL;
2020-04-24 12:29:52 +02:00
clearState(STATE_HELD, c->chanIdx);
2017-10-29 10:28:14 +01:00
released(this, c, trigger[i]);
}
}
}
}