2018-06-28 10:54:17 +02:00
|
|
|
/*
|
|
|
|
* =====================================================================================
|
|
|
|
*
|
|
|
|
* Filename: TextButton.cpp
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
*
|
|
|
|
* Created: 28/06/2018 10:18:03
|
|
|
|
*
|
|
|
|
* Author: Quentin Bazin, <quent42340@gmail.com>
|
|
|
|
*
|
|
|
|
* =====================================================================================
|
|
|
|
*/
|
|
|
|
#include "TextButton.hpp"
|
|
|
|
|
2018-06-28 11:31:51 +02:00
|
|
|
TextButton::TextButton(Widget *parent) : Widget(200, 20, parent) {
|
2018-06-28 10:54:17 +02:00
|
|
|
m_background.setClipRect(0, 66, 200, 20);
|
|
|
|
m_hoverBackground.setClipRect(0, 86, 200, 20);
|
2018-06-29 06:26:57 +02:00
|
|
|
m_disabledBackground.setClipRect(0, 46, 200, 20);
|
2018-06-28 10:54:17 +02:00
|
|
|
}
|
|
|
|
|
2018-06-28 11:31:51 +02:00
|
|
|
TextButton::TextButton(const Callback &callback, Widget *parent) : TextButton(parent) {
|
|
|
|
m_callback = callback;
|
|
|
|
}
|
|
|
|
|
2018-12-28 06:19:40 +01:00
|
|
|
void TextButton::onEvent(const SDL_Event &event) {
|
|
|
|
if (event.type == SDL_MOUSEMOTION) {
|
|
|
|
m_isHovered = isPointInWidget(event.motion.x, event.motion.y);
|
2018-06-28 10:54:17 +02:00
|
|
|
}
|
2018-12-28 06:19:40 +01:00
|
|
|
else if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT && m_isHovered && m_callback) {
|
2018-06-30 05:07:57 +02:00
|
|
|
m_callback(*this);
|
2018-06-28 10:54:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextButton::setText(const std::string &text) {
|
|
|
|
m_text.setText(text);
|
|
|
|
m_text.setPosition(m_width / 2 - m_text.getSize().x / 2, m_height / 2 - m_text.getSize().y / 2, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextButton::draw(RenderTarget &target, RenderStates states) const {
|
2018-12-25 01:45:10 +01:00
|
|
|
states.transform *= getTransform();
|
2018-06-28 10:54:17 +02:00
|
|
|
|
2018-06-29 06:26:57 +02:00
|
|
|
if (!m_isEnabled)
|
|
|
|
target.draw(m_disabledBackground, states);
|
|
|
|
else if (m_isHovered)
|
2018-06-28 10:54:17 +02:00
|
|
|
target.draw(m_hoverBackground, states);
|
|
|
|
else
|
|
|
|
target.draw(m_background, states);
|
|
|
|
target.draw(m_text, states);
|
|
|
|
}
|
|
|
|
|