[WorldMenuState] Added to replace WorldInfoState. Now using WorldCreationState to rename worlds.

This commit is contained in:
Quentin Bazin 2020-06-22 23:09:30 +02:00
parent 219cd5c3e6
commit f858b1f8a2
7 changed files with 127 additions and 185 deletions

View File

@ -32,24 +32,38 @@
#include "Config.hpp"
#include "TitleScreenState.hpp"
#include "WorldCreationState.hpp"
#include "WorldSelectionState.hpp"
namespace fs = ghc::filesystem;
WorldCreationState::WorldCreationState(TitleScreenState *titleScreen) : InterfaceState(titleScreen) {
m_textInput.setString("");
WorldCreationState::WorldCreationState(TitleScreenState *titleScreen, const std::string &originalName) : InterfaceState(titleScreen) {
m_textInput.setString(originalName);
m_textInput.setCharacterLimit(32);
m_textInput.setBackgroundSize(150, 20);
m_textInput.setBackgroundOutline(1, gk::Color::White);
m_textInput.setPadding(5, 6);
m_textInput.setScale(Config::guiScale, Config::guiScale);
m_createButton.setText("Create");
m_createButton.setText(originalName.empty() ? "Create" : "Rename");
m_createButton.setScale(Config::guiScale, Config::guiScale);
m_createButton.setCallback([this, titleScreen](TextButton &) {
m_createButton.setCallback([this, titleScreen, originalName](TextButton &) {
std::string worldName = m_textInput.string();
if (!fs::exists("saves/" + worldName + ".dat")) {
if (gk::regexMatch(worldName, "^[A-Za-z0-9_]+$")) {
if (!originalName.empty()) {
fs::copy_file("saves/" + originalName + ".dat", "saves/" + worldName + ".dat");
fs::remove("saves/" + originalName + ".dat");
m_stateStack->pop();
// FIXME: This is needed because there's currently no way to refresh WorldSelectionState
m_stateStack->pop(); // WorldSelectionState
m_stateStack->push<WorldSelectionState>(titleScreen);
}
m_stateStack->pop();
if (originalName.empty())
titleScreen->startSingleplayer(true, worldName);
}
else {

View File

@ -35,7 +35,7 @@ class TitleScreenState;
class WorldCreationState : public InterfaceState {
public:
WorldCreationState(TitleScreenState *titleScreen);
WorldCreationState(TitleScreenState *titleScreen, const std::string &originalName = "");
void onEvent(const sf::Event &event) override;

View File

@ -48,7 +48,7 @@ WorldDeletionState::WorldDeletionState(const std::string &worldName, TitleScreen
if (fs::exists(saveFilepath))
fs::remove(saveFilepath);
m_stateStack->pop(); // WorldDeletionState
m_stateStack->pop(); // WorldInfoState
m_stateStack->pop(); // WorldMenuState
// FIXME: This is needed because there's currently no way to refresh WorldSelectionState
m_stateStack->pop(); // WorldSelectionState

View File

@ -1,162 +0,0 @@
/*
* =====================================================================================
*
* 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/Utils.hpp>
#include <filesystem.hpp>
#include "Config.hpp"
#include "TitleScreenState.hpp"
#include "WorldDeletionState.hpp"
#include "WorldInfoState.hpp"
#include "WorldSelectionState.hpp"
namespace fs = ghc::filesystem;
WorldInfoState::WorldInfoState(const std::string &worldName, TitleScreenState *titleScreen) : InterfaceState(titleScreen) {
m_textInput.setString(worldName);
m_textInput.setCharacterLimit(32);
m_textInput.setBackgroundSize(150, 20);
m_textInput.setBackgroundOutline(1, gk::Color::White);
m_textInput.setPadding(5, 6);
m_textInput.setScale(Config::guiScale, Config::guiScale);
m_playButton.setText("Play");
m_playButton.setScale(Config::guiScale, Config::guiScale);
m_playButton.setCallback([this, worldName, titleScreen](TextButton &) {
if (fs::exists("saves/" + worldName + ".dat")) {
m_stateStack->pop();
titleScreen->startSingleplayer(true, worldName);
}
else {
m_errorText.setString("World '" + worldName + "' doesn't exist");
m_errorText.updateVertexBuffer();
updateWidgetPosition();
}
});
m_renameButton.setText("Rename");
m_renameButton.setScale(Config::guiScale, Config::guiScale);
m_renameButton.setCallback([this, titleScreen, world=worldName](TextButton &) {
std::string worldName = m_textInput.string();
if (!fs::exists("saves/" + worldName + ".dat")) {
if (gk::regexMatch(worldName, "^[A-Za-z0-9_]+$")) {
fs::copy_file("saves/" + world + ".dat", "saves/" + worldName + ".dat");
fs::remove("saves/" + world + ".dat");
m_stateStack->pop();
// FIXME: This is needed because there's currently no way to refresh WorldSelectionState
m_stateStack->pop(); // WorldSelectionState
m_stateStack->push<WorldSelectionState>(titleScreen);
}
else {
m_errorText.setString("Invalid world name");
m_errorText.updateVertexBuffer();
updateWidgetPosition();
}
}
else {
m_errorText.setString("World '" + worldName + "' already exists");
m_errorText.updateVertexBuffer();
updateWidgetPosition();
}
});
m_cancelButton.setText("Cancel");
m_cancelButton.setScale(Config::guiScale, Config::guiScale);
m_cancelButton.setCallback([this](TextButton &) {
m_stateStack->pop();
});
m_deleteButton.setText("Delete");
m_deleteButton.setScale(Config::guiScale, Config::guiScale);
m_deleteButton.setCallback([this, worldName, titleScreen](TextButton &) {
m_stateStack->push<WorldDeletionState>(worldName, titleScreen);
});
m_errorText.setColor(gk::Color::Red);
m_errorText.setScale(Config::guiScale, Config::guiScale);
updateWidgetPosition();
}
void WorldInfoState::onEvent(const sf::Event &event) {
InterfaceState::onEvent(event);
if (event.type == sf::Event::Resized) {
updateWidgetPosition();
}
if (!m_stateStack->empty() && &m_stateStack->top() == this) {
m_textInput.onEvent(event);
m_playButton.onEvent(event);
m_renameButton.onEvent(event);
m_cancelButton.onEvent(event);
m_deleteButton.onEvent(event);
}
}
void WorldInfoState::update() {
}
void WorldInfoState::updateWidgetPosition() {
m_textInput.setPosition(
Config::screenWidth / 2.0f - m_textInput.getBackgroundSize().x * Config::guiScale / 2.0f,
Config::screenHeight / 2.0f - m_textInput.getBackgroundSize().y * Config::guiScale / 2.0f
);
m_playButton.setPosition(Config::screenWidth / 2.0f - m_playButton.getGlobalBounds().sizeX / 2, Config::screenHeight - 420);
m_renameButton.setPosition(Config::screenWidth / 2.0f - m_playButton.getGlobalBounds().sizeX / 2, Config::screenHeight - 340);
m_deleteButton.setPosition(Config::screenWidth / 2.0f - m_deleteButton.getGlobalBounds().sizeX / 2, Config::screenHeight - 260);
m_cancelButton.setPosition(Config::screenWidth / 2.0f - m_cancelButton.getGlobalBounds().sizeX / 2, Config::screenHeight - 180);
m_errorText.setPosition(
Config::screenWidth / 2.0f - m_errorText.getSize().x * Config::guiScale / 2.0f,
Config::screenHeight / 2.0f - 30 * Config::guiScale
);
}
void WorldInfoState::draw(gk::RenderTarget &target, gk::RenderStates states) const {
if (m_parent)
target.draw(*m_parent, states);
if (&m_stateStack->top() == this) {
prepareDraw(target, states);
target.draw(m_textInput, states);
target.draw(m_playButton, states);
target.draw(m_renameButton, states);
target.draw(m_cancelButton, states);
target.draw(m_deleteButton, states);
if (!m_errorText.string().empty())
target.draw(m_errorText, states);
}
}

View File

@ -0,0 +1,97 @@
/*
* =====================================================================================
*
* 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/Utils.hpp>
#include <filesystem.hpp>
#include "Config.hpp"
#include "TitleScreenState.hpp"
#include "WorldDeletionState.hpp"
#include "WorldMenuState.hpp"
#include "WorldCreationState.hpp"
#include "WorldSelectionState.hpp"
namespace fs = ghc::filesystem;
WorldMenuState::WorldMenuState(const std::string &worldName, TitleScreenState *titleScreen) : InterfaceState(titleScreen) {
m_menuWidget.setScale(Config::guiScale, Config::guiScale);
m_menuWidget.addButton("Play", [worldName, titleScreen](TextButton &) {
titleScreen->startSingleplayer(true, worldName);
});
m_menuWidget.addButton("Rename", [this, titleScreen, worldName](TextButton &) {
m_stateStack->push<WorldCreationState>(titleScreen, worldName);
});
m_menuWidget.addButton("Delete", [this, worldName, titleScreen](TextButton &) {
m_stateStack->push<WorldDeletionState>(worldName, titleScreen);
});
m_cancelButton.setText("Cancel");
m_cancelButton.setScale(Config::guiScale, Config::guiScale);
m_cancelButton.setCallback([this](TextButton &) {
m_stateStack->pop();
});
updateWidgetPosition();
}
void WorldMenuState::onEvent(const sf::Event &event) {
InterfaceState::onEvent(event);
if (event.type == sf::Event::Resized) {
updateWidgetPosition();
if (!m_stateStack->empty() && &m_stateStack->top() != this)
m_menuWidget.onEvent(event);
}
if (!m_stateStack->empty() && &m_stateStack->top() == this) {
m_menuWidget.onEvent(event);
m_cancelButton.onEvent(event);
}
}
void WorldMenuState::update() {
}
void WorldMenuState::updateWidgetPosition() {
m_cancelButton.setPosition(Config::screenWidth / 2.0f - m_cancelButton.getGlobalBounds().sizeX / 2.0f, Config::screenHeight * 0.85);
}
void WorldMenuState::draw(gk::RenderTarget &target, gk::RenderStates states) const {
if (m_parent)
target.draw(*m_parent, states);
if (&m_stateStack->top() == this) {
prepareDraw(target, states);
target.draw(m_menuWidget, states);
target.draw(m_cancelButton, states);
}
}

View File

@ -24,18 +24,18 @@
*
* =====================================================================================
*/
#ifndef WORLDINFOSTATE_HPP_
#define WORLDINFOSTATE_HPP_
#ifndef WORLDMENUSTATE_HPP_
#define WORLDMENUSTATE_HPP_
#include "InterfaceState.hpp"
#include "MenuWidget.hpp"
#include "TextButton.hpp"
#include "TextInput.hpp"
class TitleScreenState;
class WorldInfoState : public InterfaceState {
class WorldMenuState : public InterfaceState {
public:
WorldInfoState(const std::string &worldName, TitleScreenState *titleScreen);
WorldMenuState(const std::string &worldName, TitleScreenState *titleScreen);
void onEvent(const sf::Event &event) override;
@ -46,14 +46,9 @@ class WorldInfoState : public InterfaceState {
void draw(gk::RenderTarget &target, gk::RenderStates states) const override;
TextInput m_textInput;
MenuWidget m_menuWidget{1, 4};
TextButton m_playButton;
TextButton m_renameButton;
TextButton m_cancelButton;
TextButton m_deleteButton;
Text m_errorText;
};
#endif // WORLDINFOSTATE_HPP_
#endif // WORLDMENUSTATE_HPP_

View File

@ -32,7 +32,7 @@
#include "Config.hpp"
#include "TitleScreenState.hpp"
#include "WorldCreationState.hpp"
#include "WorldInfoState.hpp"
#include "WorldMenuState.hpp"
#include "WorldSelectionState.hpp"
namespace fs = ghc::filesystem;
@ -97,9 +97,7 @@ void WorldSelectionState::loadSaveList() {
std::string saveFile = filename.substr(0, filename.find_last_of('.'));
std::string filesize = std::to_string(entry.file_size() / 1000.f / 1000.f);
m_menuWidget.addButton("- " + saveFile + " (" + filesize.substr(0, filesize.find_first_of('.') + 3) + " MB)" + " -", [&, saveFile](TextButton &) {
// m_stateStack->pop();
// m_titleScreen->startSingleplayer(true, saveFile);
m_stateStack->push<WorldInfoState>(saveFile, m_titleScreen);
m_stateStack->push<WorldMenuState>(saveFile, m_titleScreen);
});
}
}