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 "Config.hpp"
|
2020-03-15 12:11:11 +01:00
|
|
|
#include "Events.hpp"
|
2018-06-28 10:54:17 +02:00
|
|
|
#include "MenuWidget.hpp"
|
|
|
|
|
2018-06-28 11:31:51 +02:00
|
|
|
MenuWidget::MenuWidget(u16 width, u16 height, Widget *parent) : Widget(parent) {
|
2018-12-28 06:45:19 +01:00
|
|
|
reset(width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MenuWidget::reset(u16 width, u16 height) {
|
2018-06-28 11:31:51 +02:00
|
|
|
m_width = width;
|
|
|
|
m_height = height;
|
|
|
|
|
2018-12-28 06:45:19 +01:00
|
|
|
m_buttons.clear();
|
2019-01-21 23:10:38 +01:00
|
|
|
m_buttons.reserve(m_width * m_height);
|
2020-06-26 14:11:27 +02:00
|
|
|
|
|
|
|
Widget::m_width = 0;
|
|
|
|
Widget::m_height = 0;
|
2018-06-28 11:31:51 +02:00
|
|
|
}
|
|
|
|
|
2020-05-11 17:30:54 +02:00
|
|
|
void MenuWidget::onEvent(const sf::Event &event) {
|
2020-01-27 15:33:06 +09:00
|
|
|
for (std::size_t i = 0 ; i < m_buttons.size() ; ++i) {
|
|
|
|
m_buttons.at(i).onEvent(event);
|
|
|
|
|
2020-05-11 17:30:54 +02:00
|
|
|
if (event.type == sf::Event::Resized) {
|
2020-01-27 15:33:06 +09:00
|
|
|
int x = i % m_width;
|
|
|
|
int y = i / m_width;
|
|
|
|
|
2020-03-15 12:11:11 +01:00
|
|
|
updateButtonPosition(m_buttons.at(i), x, y);
|
2020-01-27 15:33:06 +09:00
|
|
|
}
|
2018-06-28 10:54:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-15 12:11:11 +01:00
|
|
|
void MenuWidget::onGuiScaleChanged(const GuiScaleChangedEvent &event) {
|
|
|
|
setScale(event.guiScale, event.guiScale, 1);
|
|
|
|
|
|
|
|
for (std::size_t i = 0 ; i < m_buttons.size() ; ++i) {
|
|
|
|
int x = i % m_width;
|
|
|
|
int y = i / m_width;
|
|
|
|
|
|
|
|
updateButtonPosition(m_buttons.at(i), x, y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-26 14:11:27 +02:00
|
|
|
TextButton &MenuWidget::addButton(const std::string &text, const TextButton::CppCallback &callback, u16 width) {
|
2019-01-21 23:10:38 +01:00
|
|
|
int x = m_buttons.size() % m_width;
|
|
|
|
int y = m_buttons.size() / m_width;
|
|
|
|
|
2020-06-26 14:11:27 +02:00
|
|
|
m_buttons.emplace_back(width, this);
|
2019-02-26 15:44:32 +01:00
|
|
|
|
|
|
|
TextButton &button = m_buttons.back();
|
2018-06-28 10:54:17 +02:00
|
|
|
button.setText(text);
|
2018-06-28 11:31:51 +02:00
|
|
|
button.setCallback(callback);
|
2020-06-26 15:23:05 +02:00
|
|
|
|
2020-03-15 12:11:11 +01:00
|
|
|
updateButtonPosition(button, x, y);
|
2020-06-26 15:23:05 +02:00
|
|
|
|
2020-03-15 12:11:11 +01:00
|
|
|
return button;
|
|
|
|
}
|
|
|
|
|
2020-06-26 16:03:06 +02:00
|
|
|
void MenuWidget::setButtonEnabled(const std::string &text, bool isEnabled) {
|
|
|
|
for (auto &it : m_buttons) {
|
|
|
|
if (it.text() == text)
|
|
|
|
it.setEnabled(isEnabled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-15 12:11:11 +01:00
|
|
|
void MenuWidget::updateButtonPosition(TextButton &button, int x, int y) {
|
2020-06-26 16:03:06 +02:00
|
|
|
button.setPosition(x * (button.width() + m_horizontalSpacing),
|
|
|
|
y * (button.height() + m_verticalSpacing));
|
2020-06-26 14:11:27 +02:00
|
|
|
|
|
|
|
if (button.getPosition().x + button.width() > Widget::m_width) {
|
|
|
|
Widget::m_width = button.getPosition().x + button.width();
|
|
|
|
}
|
|
|
|
if (button.getPosition().y + button.height() > Widget::m_height) {
|
|
|
|
Widget::m_height = button.getPosition().y + button.height();
|
|
|
|
}
|
2018-06-28 10:54:17 +02:00
|
|
|
}
|
|
|
|
|
2018-12-29 02:23:23 +01:00
|
|
|
void MenuWidget::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
|
|
|
|
|
|
|
for (const TextButton &button : m_buttons) {
|
2018-06-29 06:26:57 +02:00
|
|
|
if (!button.text().empty())
|
|
|
|
target.draw(button, states);
|
2018-06-28 10:54:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|