libgui/source/gui/GuiDragListener.cpp

80 lines
2.3 KiB
C++
Raw Normal View History

2017-10-29 10:28:14 +01:00
/****************************************************************************
* Copyright (C) 2016 Maschell
* based on GuiButton by 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/GuiDragListener.h>
#include <gui/GuiController.h>
2017-10-29 10:28:14 +01:00
/**
* Constructor for the GuiDragListener class.
*/
2020-08-13 12:38:07 +02:00
GuiDragListener::GuiDragListener(float w, float h) {
2017-10-29 10:28:14 +01:00
width = w;
height = h;
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;
}
2017-10-29 10:28:14 +01:00
}
/**
* Destructor for the GuiDragListener class.
*/
2018-06-21 20:44:58 +02:00
GuiDragListener::~GuiDragListener() {
2017-10-29 10:28:14 +01:00
}
2018-06-21 20:44:58 +02:00
void GuiDragListener::setState(int32_t i, int32_t c) {
2020-08-13 12:38:07 +02:00
GuiElement::setState(i, c);
2017-10-29 10:28:14 +01:00
}
2020-08-13 12:38:07 +02:00
void GuiDragListener::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;
}
}
}
}
2020-08-13 12:38:07 +02:00
void GuiDragListener::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;
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;
}
bool isHeld = trigger[i]->held(c);
2020-08-13 12:38:07 +02:00
if (isHeld && this->isInside(c->data.x, c->data.y)) {
2018-06-21 20:44:58 +02:00
int32_t dx = c->data.x - c->lastData.x;
int32_t dy = c->data.y - c->lastData.y;
2017-10-29 10:28:14 +01:00
2020-08-13 12:38:07 +02:00
if (dx == 0 && dy == 0) continue;
2017-10-29 10:28:14 +01:00
2020-08-13 12:38:07 +02:00
dragged(this, c, trigger[i], dx, dy);
2017-10-29 10:28:14 +01:00
}
}
}