105 lines
3.4 KiB
C++
105 lines
3.4 KiB
C++
/*
|
|
* =====================================================================================
|
|
*
|
|
* OpenMiner
|
|
*
|
|
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <openminer@unarelith.net>
|
|
* Copyright (C) 2019-2020 the OpenMiner contributors (see CONTRIBUTORS.md)
|
|
*
|
|
* This file is part of OpenMiner.
|
|
*
|
|
* OpenMiner is free software; you can redistribute it and/or
|
|
* 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.
|
|
*
|
|
* OpenMiner 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
|
|
* Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
* along with OpenMiner; if not, write to the Free Software Foundation, Inc.,
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
*
|
|
* =====================================================================================
|
|
*/
|
|
#include "TextButton.hpp"
|
|
|
|
TextButton::TextButton(Widget *parent) : TextButton(200, parent) {
|
|
}
|
|
|
|
TextButton::TextButton(u16 width, Widget *parent) : Widget(width, 20, parent) {
|
|
m_text.setColor(m_defaultColor);
|
|
m_text.setShadowColor({56, 56, 56});
|
|
|
|
m_background.setClipRect(0, 66, width, 20);
|
|
m_hoverBackground.setClipRect(0, 86, width, 20);
|
|
m_disabledBackground.setClipRect(0, 46, width, 20);
|
|
|
|
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);
|
|
}
|
|
|
|
TextButton::TextButton(const CppCallback &callback, Widget *parent) : TextButton(parent) {
|
|
m_cppCallback = callback;
|
|
}
|
|
|
|
void TextButton::onEvent(const sf::Event &event) {
|
|
if (event.type == sf::Event::MouseMoved) {
|
|
m_isHovered = isPointInWidget(event.mouseMove.x, event.mouseMove.y);
|
|
|
|
if (m_isEnabled && m_isHovered)
|
|
m_text.setColor(m_hoverColor);
|
|
else if (m_isEnabled && !m_isHovered)
|
|
m_text.setColor(m_defaultColor);
|
|
}
|
|
else if (event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left && m_isHovered && m_isEnabled) {
|
|
m_isHovered = isPointInWidget(event.mouseButton.x, event.mouseButton.y);
|
|
|
|
if (m_isEnabled && m_isHovered)
|
|
m_text.setColor(m_hoverColor);
|
|
else if (m_isEnabled && !m_isHovered)
|
|
m_text.setColor(m_defaultColor);
|
|
|
|
if (m_isHovered) {
|
|
if (m_cppCallback)
|
|
m_cppCallback(*this);
|
|
else if (m_luaCallback)
|
|
m_luaCallback(*this);
|
|
}
|
|
}
|
|
}
|
|
|
|
void TextButton::setText(const std::string &text) {
|
|
m_text.setString(text);
|
|
m_text.updateVertexBuffer();
|
|
m_text.setPosition(m_width / 2 - m_text.getSize().x / 2,
|
|
m_height / 2 - m_text.getSize().y / 2, 0);
|
|
}
|
|
|
|
void TextButton::draw(gk::RenderTarget &target, gk::RenderStates states) const {
|
|
states.transform *= getTransform();
|
|
|
|
if (!m_isEnabled) {
|
|
target.draw(m_disabledBackground, states);
|
|
target.draw(m_disabledBackgroundBorder, states);
|
|
}
|
|
else if (m_isHovered) {
|
|
target.draw(m_hoverBackground, states);
|
|
target.draw(m_hoverBackgroundBorder, states);
|
|
}
|
|
else {
|
|
target.draw(m_background, states);
|
|
target.draw(m_backgroundBorder, states);
|
|
}
|
|
|
|
target.draw(m_text, states);
|
|
}
|
|
|