2018-06-28 10:54:17 +02:00
|
|
|
/*
|
|
|
|
* =====================================================================================
|
|
|
|
*
|
2020-02-08 18:34:26 +09:00
|
|
|
* OpenMiner
|
2020-02-25 01:42:10 +09:00
|
|
|
*
|
2020-02-08 18:34:26 +09:00
|
|
|
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <openminer@unarelith.net>
|
2020-02-25 01:42:10 +09:00
|
|
|
* Copyright (C) 2019-2020 the OpenMiner contributors (see CONTRIBUTORS.md)
|
|
|
|
*
|
|
|
|
* This file is part of OpenMiner.
|
2018-06-28 10:54:17 +02:00
|
|
|
*
|
2020-02-25 01:42:10 +09:00
|
|
|
* OpenMiner is free software; you can redistribute it and/or
|
2020-02-08 18:34:26 +09:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2018-06-28 10:54:17 +02:00
|
|
|
*
|
2020-02-25 01:42:10 +09:00
|
|
|
* OpenMiner is distributed in the hope that it will be useful,
|
2020-02-08 18:34:26 +09:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2018-06-28 10:54:17 +02:00
|
|
|
*
|
2020-02-08 18:34:26 +09:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
2020-02-25 01:42:10 +09:00
|
|
|
* along with OpenMiner; if not, write to the Free Software Foundation, Inc.,
|
2020-02-08 18:34:26 +09:00
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2018-06-28 10:54:17 +02:00
|
|
|
*
|
|
|
|
* =====================================================================================
|
|
|
|
*/
|
|
|
|
#include "TextButton.hpp"
|
|
|
|
|
2020-06-26 14:11:27 +02:00
|
|
|
TextButton::TextButton(Widget *parent) : TextButton(200, parent) {
|
|
|
|
}
|
|
|
|
|
|
|
|
TextButton::TextButton(u16 width, Widget *parent) : Widget(width, 20, parent) {
|
2020-06-26 18:22:35 +02:00
|
|
|
m_text.setColor(m_defaultColor);
|
2020-06-26 16:03:06 +02:00
|
|
|
m_text.setShadowColor({56, 56, 56});
|
|
|
|
|
2020-06-26 14:11:27 +02:00
|
|
|
m_background.setClipRect(0, 66, width, 20);
|
|
|
|
m_hoverBackground.setClipRect(0, 86, width, 20);
|
|
|
|
m_disabledBackground.setClipRect(0, 46, width, 20);
|
2020-06-26 15:23:05 +02:00
|
|
|
|
|
|
|
m_backgroundBorder.setClipRect(200 - 2, 66, 2, 20);
|
|
|
|
m_hoverBackgroundBorder.setClipRect(200 - 2, 86, 2, 20);
|
|
|
|
m_disabledBackgroundBorder.setClipRect(200 - 2, 46, 2, 20);
|
|
|
|
|
|
|
|
m_backgroundBorder.setPosition(width - 2, 0);
|
|
|
|
m_hoverBackgroundBorder.setPosition(width - 2, 0);
|
|
|
|
m_disabledBackgroundBorder.setPosition(width - 2, 0);
|
2018-06-28 10:54:17 +02:00
|
|
|
}
|
|
|
|
|
2019-01-07 01:26:02 +01:00
|
|
|
TextButton::TextButton(const CppCallback &callback, Widget *parent) : TextButton(parent) {
|
|
|
|
m_cppCallback = callback;
|
2018-06-28 11:31:51 +02:00
|
|
|
}
|
|
|
|
|
2020-07-07 19:33:01 +02:00
|
|
|
void TextButton::onEvent(const SDL_Event &event) {
|
|
|
|
if (event.type == SDL_MOUSEMOTION) {
|
|
|
|
m_isHovered = isPointInWidget(event.motion.x, event.motion.y);
|
2019-01-04 22:41:53 +01:00
|
|
|
|
2020-06-26 16:03:06 +02:00
|
|
|
if (m_isEnabled && m_isHovered)
|
2020-06-26 18:22:35 +02:00
|
|
|
m_text.setColor(m_hoverColor);
|
2020-06-26 16:03:06 +02:00
|
|
|
else if (m_isEnabled && !m_isHovered)
|
2020-06-26 18:22:35 +02:00
|
|
|
m_text.setColor(m_defaultColor);
|
2018-06-28 10:54:17 +02:00
|
|
|
}
|
2020-07-07 19:33:01 +02:00
|
|
|
else if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT && m_isHovered && m_isEnabled) {
|
|
|
|
m_isHovered = isPointInWidget(event.button.x, event.button.y);
|
2020-06-26 18:22:35 +02:00
|
|
|
|
2020-06-26 19:04:51 +02:00
|
|
|
if (m_isEnabled && m_isHovered)
|
|
|
|
m_text.setColor(m_hoverColor);
|
|
|
|
else if (m_isEnabled && !m_isHovered)
|
|
|
|
m_text.setColor(m_defaultColor);
|
|
|
|
|
2020-06-26 18:22:35 +02:00
|
|
|
if (m_isHovered) {
|
|
|
|
if (m_cppCallback)
|
|
|
|
m_cppCallback(*this);
|
|
|
|
else if (m_luaCallback)
|
|
|
|
m_luaCallback(*this);
|
|
|
|
}
|
2018-06-28 10:54:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextButton::setText(const std::string &text) {
|
2020-06-20 01:24:06 +02:00
|
|
|
m_text.setString(text);
|
2020-03-04 11:28:45 +01:00
|
|
|
m_text.updateVertexBuffer();
|
2020-07-20 13:49:43 +02:00
|
|
|
m_text.setPosition(
|
2020-07-20 16:06:49 +02:00
|
|
|
std::roundf(m_width / 2.f - m_text.getSize().x / 2.f),
|
|
|
|
std::roundf(m_height / 2.f - m_text.getSize().y / 2.f)
|
2020-07-20 13:49:43 +02:00
|
|
|
);
|
2018-06-28 10:54:17 +02:00
|
|
|
}
|
|
|
|
|
2018-12-29 02:23:23 +01:00
|
|
|
void TextButton::draw(gk::RenderTarget &target, gk::RenderStates states) const {
|
2018-12-25 01:45:10 +01:00
|
|
|
states.transform *= getTransform();
|
2018-06-28 10:54:17 +02:00
|
|
|
|
2020-06-26 15:23:05 +02:00
|
|
|
if (!m_isEnabled) {
|
2018-06-29 06:26:57 +02:00
|
|
|
target.draw(m_disabledBackground, states);
|
2020-06-26 15:23:05 +02:00
|
|
|
target.draw(m_disabledBackgroundBorder, states);
|
|
|
|
}
|
|
|
|
else if (m_isHovered) {
|
2018-06-28 10:54:17 +02:00
|
|
|
target.draw(m_hoverBackground, states);
|
2020-06-26 15:23:05 +02:00
|
|
|
target.draw(m_hoverBackgroundBorder, states);
|
|
|
|
}
|
|
|
|
else {
|
2018-06-28 10:54:17 +02:00
|
|
|
target.draw(m_background, states);
|
2020-06-26 15:23:05 +02:00
|
|
|
target.draw(m_backgroundBorder, states);
|
|
|
|
}
|
2018-12-31 04:12:22 +01:00
|
|
|
|
2018-06-28 10:54:17 +02:00
|
|
|
target.draw(m_text, states);
|
|
|
|
}
|
|
|
|
|