libgui/source/gui/GuiElement.cpp

285 lines
8.9 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/GuiElement.h>
2017-10-29 10:28:14 +01:00
//! TODO remove this!
2018-06-21 20:44:58 +02:00
static int32_t screenwidth = 1280;
static int32_t screenheight = 720;
2017-10-29 10:28:14 +01:00
/**
* Constructor for the Object class.
*/
2018-06-21 20:44:58 +02:00
GuiElement::GuiElement() {
xoffset = 0.0f;
yoffset = 0.0f;
zoffset = 0.0f;
width = 0.0f;
height = 0.0f;
alpha = 1.0f;
scaleX = 1.0f;
scaleY = 1.0f;
scaleZ = 1.0f;
2020-08-13 12:38:07 +02:00
for (int32_t i = 0; i < 5; i++)
2017-10-29 10:28:14 +01:00
state[i] = STATE_DEFAULT;
2018-06-21 20:44:58 +02:00
stateChan = -1;
parentElement = NULL;
rumble = true;
selectable = false;
clickable = false;
holdable = false;
drawOverOnlyWhenSelected = false;
visible = true;
yoffsetDyn = 0;
xoffsetDyn = 0;
alphaDyn = -1;
scaleDyn = 1;
effects = EFFECT_NONE;
effectAmount = 0;
effectTarget = 0;
effectsOver = EFFECT_NONE;
effectAmountOver = 0;
effectTargetOver = 0;
angle = 0.0f;
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
// default alignment - align to top left
alignment = (ALIGN_CENTER | ALIGN_MIDDLE);
2017-10-29 10:28:14 +01:00
}
/**
* Get the left position of the GuiElement.
* @see SetLeft()
* @return Left position in pixel.
*/
2018-06-21 20:44:58 +02:00
float GuiElement::getLeft() {
float pWidth = 0;
float pLeft = 0;
float pScaleX = 1.0f;
2017-10-29 10:28:14 +01:00
2020-08-13 12:38:07 +02:00
if (parentElement) {
2018-06-21 20:44:58 +02:00
pWidth = parentElement->getWidth();
pLeft = parentElement->getLeft();
pScaleX = parentElement->getScaleX();
}
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
pLeft += xoffsetDyn;
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
float x = pLeft;
2017-10-29 10:28:14 +01:00
//! TODO: the conversion from int to float and back to int is bad for performance, change that
2020-08-13 12:38:07 +02:00
if (alignment & ALIGN_CENTER) {
2018-06-21 20:44:58 +02:00
x = pLeft + pWidth * 0.5f * pScaleX - width * 0.5f * getScaleX();
2020-08-13 12:38:07 +02:00
} else if (alignment & ALIGN_RIGHT) {
2018-06-21 20:44:58 +02:00
x = pLeft + pWidth * pScaleX - width * getScaleX();
}
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
return x + xoffset;
2017-10-29 10:28:14 +01:00
}
/**
* Get the top position of the GuiElement.
* @see SetTop()
* @return Top position in pixel.
*/
2018-06-21 20:44:58 +02:00
float GuiElement::getTop() {
float pHeight = 0;
float pTop = 0;
float pScaleY = 1.0f;
2017-10-29 10:28:14 +01:00
2020-08-13 12:38:07 +02:00
if (parentElement) {
2018-06-21 20:44:58 +02:00
pHeight = parentElement->getHeight();
pTop = parentElement->getTop();
pScaleY = parentElement->getScaleY();
}
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
pTop += yoffsetDyn;
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
float y = pTop;
2017-10-29 10:28:14 +01:00
//! TODO: the conversion from int to float and back to int is bad for performance, change that
2020-08-13 12:38:07 +02:00
if (alignment & ALIGN_MIDDLE) {
2018-06-21 20:44:58 +02:00
y = pTop + pHeight * 0.5f * pScaleY - getHeight() * 0.5f * getScaleY();
2020-08-13 12:38:07 +02:00
} else if (alignment & ALIGN_BOTTOM) {
2018-06-21 20:44:58 +02:00
y = pTop + pHeight * pScaleY - getHeight() * getScaleY();
}
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
return y + yoffset;
2017-10-29 10:28:14 +01:00
}
2018-06-21 20:44:58 +02:00
void GuiElement::setEffect(int32_t eff, int32_t amount, int32_t target) {
2020-08-13 12:38:07 +02:00
if (eff & EFFECT_SLIDE_IN) {
2018-06-21 20:44:58 +02:00
// these calculations overcompensate a little
2020-08-13 12:38:07 +02:00
if (eff & EFFECT_SLIDE_TOP) {
if (eff & EFFECT_SLIDE_FROM)
yoffsetDyn = (int32_t) -getHeight() * scaleY;
2018-06-21 20:44:58 +02:00
else
yoffsetDyn = -screenheight;
2020-08-13 12:38:07 +02:00
} else if (eff & EFFECT_SLIDE_LEFT) {
if (eff & EFFECT_SLIDE_FROM)
xoffsetDyn = (int32_t) -getWidth() * scaleX;
2018-06-21 20:44:58 +02:00
else
xoffsetDyn = -screenwidth;
2020-08-13 12:38:07 +02:00
} else if (eff & EFFECT_SLIDE_BOTTOM) {
if (eff & EFFECT_SLIDE_FROM)
yoffsetDyn = (int32_t) getHeight() * scaleY;
2018-06-21 20:44:58 +02:00
else
yoffsetDyn = screenheight;
2020-08-13 12:38:07 +02:00
} else if (eff & EFFECT_SLIDE_RIGHT) {
if (eff & EFFECT_SLIDE_FROM)
xoffsetDyn = (int32_t) getWidth() * scaleX;
2018-06-21 20:44:58 +02:00
else
xoffsetDyn = screenwidth;
}
}
2020-08-13 12:38:07 +02:00
if ((eff & EFFECT_FADE) && amount > 0) {
2018-06-21 20:44:58 +02:00
alphaDyn = 0;
2020-08-13 12:38:07 +02:00
} else if ((eff & EFFECT_FADE) && amount < 0) {
2018-06-21 20:44:58 +02:00
alphaDyn = alpha;
}
effects |= eff;
effectAmount = amount;
effectTarget = target;
2017-10-29 10:28:14 +01:00
}
//!Sets an effect to be enabled on wiimote cursor over
//!\param e Effect to enable
//!\param a Amount of the effect (usage varies on effect)
//!\param t Target amount of the effect (usage varies on effect)
2018-06-21 20:44:58 +02:00
void GuiElement::setEffectOnOver(int32_t e, int32_t a, int32_t t) {
effectsOver |= e;
effectAmountOver = a;
effectTargetOver = t;
2017-10-29 10:28:14 +01:00
}
2018-06-21 20:44:58 +02:00
void GuiElement::resetEffects() {
yoffsetDyn = 0;
xoffsetDyn = 0;
alphaDyn = -1;
scaleDyn = 1;
effects = EFFECT_NONE;
effectAmount = 0;
effectTarget = 0;
effectsOver = EFFECT_NONE;
effectAmountOver = 0;
effectTargetOver = 0;
2017-10-29 10:28:14 +01:00
}
2020-08-13 12:38:07 +02:00
2018-06-21 20:44:58 +02:00
void GuiElement::updateEffects() {
2020-08-13 12:38:07 +02:00
if (!this->isVisible() && parentElement)
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
if (effects & (EFFECT_SLIDE_IN | EFFECT_SLIDE_OUT | EFFECT_SLIDE_FROM)) {
if (effects & EFFECT_SLIDE_IN) {
if (effects & EFFECT_SLIDE_LEFT) {
2018-06-21 20:44:58 +02:00
xoffsetDyn += effectAmount;
2017-10-29 10:28:14 +01:00
2020-08-13 12:38:07 +02:00
if (xoffsetDyn >= 0) {
2018-06-21 20:44:58 +02:00
xoffsetDyn = 0;
effects = 0;
2017-10-29 10:28:14 +01:00
effectFinished(this);
2018-06-21 20:44:58 +02:00
}
2020-08-13 12:38:07 +02:00
} else if (effects & EFFECT_SLIDE_RIGHT) {
2018-06-21 20:44:58 +02:00
xoffsetDyn -= effectAmount;
2017-10-29 10:28:14 +01:00
2020-08-13 12:38:07 +02:00
if (xoffsetDyn <= 0) {
2018-06-21 20:44:58 +02:00
xoffsetDyn = 0;
effects = 0;
2017-10-29 10:28:14 +01:00
effectFinished(this);
2018-06-21 20:44:58 +02:00
}
2020-08-13 12:38:07 +02:00
} else if (effects & EFFECT_SLIDE_TOP) {
2018-06-21 20:44:58 +02:00
yoffsetDyn += effectAmount;
2017-10-29 10:28:14 +01:00
2020-08-13 12:38:07 +02:00
if (yoffsetDyn >= 0) {
2018-06-21 20:44:58 +02:00
yoffsetDyn = 0;
effects = 0;
2017-10-29 10:28:14 +01:00
effectFinished(this);
2018-06-21 20:44:58 +02:00
}
2020-08-13 12:38:07 +02:00
} else if (effects & EFFECT_SLIDE_BOTTOM) {
2018-06-21 20:44:58 +02:00
yoffsetDyn -= effectAmount;
2017-10-29 10:28:14 +01:00
2020-08-13 12:38:07 +02:00
if (yoffsetDyn <= 0) {
2018-06-21 20:44:58 +02:00
yoffsetDyn = 0;
effects = 0;
2017-10-29 10:28:14 +01:00
effectFinished(this);
2018-06-21 20:44:58 +02:00
}
}
} else {
2020-08-13 12:38:07 +02:00
if (effects & EFFECT_SLIDE_LEFT) {
2018-06-21 20:44:58 +02:00
xoffsetDyn -= effectAmount;
2017-10-29 10:28:14 +01:00
2020-08-13 12:38:07 +02:00
if (xoffsetDyn <= -screenwidth) {
2018-06-21 20:44:58 +02:00
effects = 0; // shut off effect
2017-10-29 10:28:14 +01:00
effectFinished(this);
2020-08-13 12:38:07 +02:00
} else if ((effects & EFFECT_SLIDE_FROM) && xoffsetDyn <= -getWidth()) {
2018-06-21 20:44:58 +02:00
effects = 0; // shut off effect
2017-10-29 10:28:14 +01:00
effectFinished(this);
2018-06-21 20:44:58 +02:00
}
2020-08-13 12:38:07 +02:00
} else if (effects & EFFECT_SLIDE_RIGHT) {
2018-06-21 20:44:58 +02:00
xoffsetDyn += effectAmount;
2017-10-29 10:28:14 +01:00
2020-08-13 12:38:07 +02:00
if (xoffsetDyn >= screenwidth) {
2018-06-21 20:44:58 +02:00
effects = 0; // shut off effect
2017-10-29 10:28:14 +01:00
effectFinished(this);
2020-08-13 12:38:07 +02:00
} else if ((effects & EFFECT_SLIDE_FROM) && xoffsetDyn >= getWidth() * scaleX) {
2018-06-21 20:44:58 +02:00
effects = 0; // shut off effect
2017-10-29 10:28:14 +01:00
effectFinished(this);
2018-06-21 20:44:58 +02:00
}
2020-08-13 12:38:07 +02:00
} else if (effects & EFFECT_SLIDE_TOP) {
2018-06-21 20:44:58 +02:00
yoffsetDyn -= effectAmount;
2017-10-29 10:28:14 +01:00
2020-08-13 12:38:07 +02:00
if (yoffsetDyn <= -screenheight) {
2018-06-21 20:44:58 +02:00
effects = 0; // shut off effect
2017-10-29 10:28:14 +01:00
effectFinished(this);
2020-08-13 12:38:07 +02:00
} else if ((effects & EFFECT_SLIDE_FROM) && yoffsetDyn <= -getHeight()) {
2018-06-21 20:44:58 +02:00
effects = 0; // shut off effect
2017-10-29 10:28:14 +01:00
effectFinished(this);
2018-06-21 20:44:58 +02:00
}
2020-08-13 12:38:07 +02:00
} else if (effects & EFFECT_SLIDE_BOTTOM) {
2018-06-21 20:44:58 +02:00
yoffsetDyn += effectAmount;
2017-10-29 10:28:14 +01:00
2020-08-13 12:38:07 +02:00
if (yoffsetDyn >= screenheight) {
2018-06-21 20:44:58 +02:00
effects = 0; // shut off effect
2017-10-29 10:28:14 +01:00
effectFinished(this);
2020-08-13 12:38:07 +02:00
} else if ((effects & EFFECT_SLIDE_FROM) && yoffsetDyn >= getHeight()) {
2018-06-21 20:44:58 +02:00
effects = 0; // shut off effect
2017-10-29 10:28:14 +01:00
effectFinished(this);
2018-06-21 20:44:58 +02:00
}
}
}
2020-08-13 12:38:07 +02:00
} else if (effects & EFFECT_FADE) {
2018-06-21 20:44:58 +02:00
alphaDyn += effectAmount * (1.0f / 255.0f);
2017-10-29 10:28:14 +01:00
2020-08-13 12:38:07 +02:00
if (effectAmount < 0 && alphaDyn <= 0) {
2018-06-21 20:44:58 +02:00
alphaDyn = 0;
effects = 0; // shut off effect
effectFinished(this);
2020-08-13 12:38:07 +02:00
} else if (effectAmount > 0 && alphaDyn >= alpha) {
2018-06-21 20:44:58 +02:00
alphaDyn = alpha;
effects = 0; // shut off effect
effectFinished(this);
}
2020-08-13 12:38:07 +02:00
} else if (effects & EFFECT_SCALE) {
2018-06-21 20:44:58 +02:00
scaleDyn += effectAmount * 0.01f;
2017-10-29 10:28:14 +01:00
2020-08-13 12:38:07 +02:00
if ((effectAmount < 0 && scaleDyn <= (effectTarget * 0.01f))
|| (effectAmount > 0 && scaleDyn >= (effectTarget * 0.01f))) {
2018-06-21 20:44:58 +02:00
scaleDyn = effectTarget * 0.01f;
effects = 0; // shut off effect
effectFinished(this);
}
}
2017-10-29 10:28:14 +01:00
}