OpenMiner/source/client/states/ConnectionErrorState.cpp
2020-07-08 00:12:22 +02:00

111 lines
3.7 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 <gk/core/ApplicationStateStack.hpp>
#include <gk/core/Mouse.hpp>
#include "Config.hpp"
#include "ConnectionErrorState.hpp"
#include "GameState.hpp"
#include "ServerLoadingState.hpp"
#include "TitleScreenState.hpp"
ConnectionErrorState::ConnectionErrorState(const std::string &error, const std::string &host, u16 port, gk::ApplicationState *parent) : InterfaceState(parent) {
gk::Mouse::setCursorGrabbed(false);
gk::Mouse::setCursorVisible(true);
m_host = host;
m_port = port;
m_background.setScale(Config::guiScale * 2, Config::guiScale * 2);
m_filter.setFillColor(gk::Color(0, 0, 0, 192));
m_text.setString(error);
m_text.setColor(gk::Color::Red);
m_text.updateVertexBuffer();
m_text.setScale(Config::guiScale * 1.5, Config::guiScale * 1.5);
m_text.setShadowEnabled(false);
m_menuWidget.setScale(Config::guiScale, Config::guiScale);
m_menuWidget.addButton("Reconnect", [this](TextButton &) {
m_stateStack->pop();
auto &game = m_stateStack->push<GameState>();
auto &serverLoadingState = m_stateStack->push<ServerLoadingState>(game, true, m_host, m_port, m_parent);
serverLoadingState.setUsername(Config::defaultUsername);
});
m_menuWidget.addButton("Cancel", [this](TextButton &) {
if (m_stateStack->size() > 1) {
m_stateStack->pop();
}
else {
m_stateStack->pop();
m_stateStack->push<TitleScreenState>(m_port);
}
});
updateWidgetPosition();
}
void ConnectionErrorState::onEvent(const SDL_Event &event) {
InterfaceState::onEvent(event);
if (!m_stateStack->empty() && &m_stateStack->top() == this) {
m_menuWidget.onEvent(event);
}
}
void ConnectionErrorState::updateWidgetPosition() {
m_background.setPosRect(0, 0, Config::screenWidth / m_background.getScale().x, Config::screenHeight / m_background.getScale().y);
m_background.setClipRect(0, 0, Config::screenWidth / m_background.getScale().x, Config::screenHeight / m_background.getScale().y);
m_filter.setSize(Config::screenWidth, Config::screenHeight);
m_text.setPosition(Config::screenWidth / 2 - m_text.getSize().x * Config::guiScale * 1.5 / 2,
Config::screenHeight / 2 - m_text.getSize().y * Config::guiScale * 1.5 / 2);
m_menuWidget.setPosition(
Config::screenWidth / 2.0f - m_menuWidget.getGlobalBounds().sizeX / 2,
Config::screenHeight - 110 * Config::guiScale
);
}
void ConnectionErrorState::draw(gk::RenderTarget &target, gk::RenderStates states) const {
if (&m_stateStack->top() == this) {
prepareDraw(target, states);
target.draw(m_background, states);
target.draw(m_filter, states);
target.draw(m_text, states);
target.draw(m_menuWidget, states);
}
}