[WorldCreationState|WorldDeletionState] Improved. Fixed #128.

This commit is contained in:
Quentin Bazin 2020-06-26 17:07:32 +02:00
parent 4e7e294527
commit 10bcbabdee
5 changed files with 89 additions and 49 deletions

View File

@ -37,6 +37,14 @@
namespace fs = ghc::filesystem;
WorldCreationState::WorldCreationState(TitleScreenState *titleScreen, const std::string &originalName) : InterfaceState(titleScreen) {
m_background.setScale(Config::guiScale * 2, Config::guiScale * 2);
m_filter.setFillColor(gk::Color(0, 0, 0, 192));
m_title.setScale(Config::guiScale, Config::guiScale);
m_title.setString(originalName.empty() ? "Create New World" : "Edit World");
m_title.updateVertexBuffer();
m_textInput.setString(originalName);
m_textInput.setCharacterLimit(32);
m_textInput.setBackgroundSize(150, 20);
@ -46,9 +54,8 @@ WorldCreationState::WorldCreationState(TitleScreenState *titleScreen, const std:
m_textInput.setFocus(false);
m_textInput.setPlaceholder("World Name");
m_createButton.setText(originalName.empty() ? "Create" : "Rename");
m_createButton.setScale(Config::guiScale, Config::guiScale);
m_createButton.setCallback([this, titleScreen, originalName](TextButton &) {
m_menuWidget.setScale(Config::guiScale, Config::guiScale);
m_menuWidget.addButton(originalName.empty() ? "Create New World" : "Save World", [this, titleScreen, originalName](TextButton &) {
std::string worldName = m_textInput.string();
if (!fs::exists("saves/" + worldName + ".dat")) {
if (gk::regexMatch(worldName, "^[A-Za-z0-9_]+$") && worldName[0] != '_') {
@ -79,13 +86,11 @@ WorldCreationState::WorldCreationState(TitleScreenState *titleScreen, const std:
m_errorText.updateVertexBuffer();
updateWidgetPosition();
}
});
}, 150);
m_cancelButton.setText("Cancel");
m_cancelButton.setScale(Config::guiScale, Config::guiScale);
m_cancelButton.setCallback([this](TextButton &) {
m_menuWidget.addButton("Cancel", [this](TextButton &) {
m_stateStack->pop();
});
}, 150);
m_errorText.setColor(gk::Color::Red);
m_errorText.setScale(Config::guiScale, Config::guiScale);
@ -99,8 +104,7 @@ void WorldCreationState::onEvent(const sf::Event &event) {
if (!m_stateStack->empty() && &m_stateStack->top() == this) {
m_textInput.onEvent(event);
m_createButton.onEvent(event);
m_cancelButton.onEvent(event);
m_menuWidget.onEvent(event);
}
}
@ -108,13 +112,25 @@ void WorldCreationState::update() {
}
void WorldCreationState::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_title.setPosition(
Config::screenWidth / 2.0f - m_title.getSize().x * Config::guiScale / 2.0f,
12.5f * Config::guiScale - m_title.getSize().y * Config::guiScale / 2.0f
);
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_createButton.setPosition(Config::screenWidth / 2.0f - m_createButton.getGlobalBounds().sizeX / 2, Config::screenHeight - 340);
m_cancelButton.setPosition(Config::screenWidth / 2.0f - m_cancelButton.getGlobalBounds().sizeX / 2, Config::screenHeight - 261);
m_menuWidget.setPosition(
Config::screenWidth / 2.0f - m_menuWidget.getGlobalBounds().sizeX / 2.0f,
Config::screenHeight - 12.5f * Config::guiScale - m_menuWidget.getGlobalBounds().sizeY / 2.0f
);
m_errorText.setPosition(
Config::screenWidth / 2.0f - m_errorText.getSize().x * Config::guiScale / 2.0f,
@ -123,16 +139,17 @@ void WorldCreationState::updateWidgetPosition() {
}
void WorldCreationState::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_background, states);
target.draw(m_filter, states);
target.draw(m_title, states);
target.draw(m_textInput, states);
target.draw(m_createButton, states);
target.draw(m_cancelButton, states);
target.draw(m_menuWidget, states);
if (!m_errorText.string().empty())
target.draw(m_errorText, states);

View File

@ -28,7 +28,7 @@
#define WORLDCREATIONSTATE_HPP_
#include "InterfaceState.hpp"
#include "TextButton.hpp"
#include "MenuWidget.hpp"
#include "TextInput.hpp"
class TitleScreenState;
@ -46,10 +46,14 @@ class WorldCreationState : public InterfaceState {
void draw(gk::RenderTarget &target, gk::RenderStates states) const override;
gk::Image m_background{"texture-menu_background"};
gk::RectangleShape m_filter;
Text m_title;
TextInput m_textInput;
TextButton m_createButton;
TextButton m_cancelButton;
MenuWidget m_menuWidget{2, 1};
Text m_errorText;
};

View File

@ -36,14 +36,20 @@
namespace fs = ghc::filesystem;
WorldDeletionState::WorldDeletionState(const std::string &worldName, TitleScreenState *titleScreen) : InterfaceState(titleScreen) {
m_text.setString("Are you sure you want to delete '" + worldName + "'?");
m_text.setColor(gk::Color::White);
m_text.updateVertexBuffer();
m_text.setScale(Config::guiScale * 2, Config::guiScale * 2);
m_background.setScale(Config::guiScale * 2, Config::guiScale * 2);
m_confirmButton.setText("Yes");
m_confirmButton.setScale(Config::guiScale, Config::guiScale);
m_confirmButton.setCallback([this, worldName, titleScreen](TextButton &) {
m_filter.setFillColor(gk::Color(0, 0, 0, 176));
m_text1.setString("Are you sure you want to delete this world?");
m_text1.updateVertexBuffer();
m_text1.setScale(Config::guiScale, Config::guiScale);
m_text2.setString("'" + worldName + "' will be lost forever! (A long time!)");
m_text2.updateVertexBuffer();
m_text2.setScale(Config::guiScale, Config::guiScale);
m_menuWidget.setScale(Config::guiScale, Config::guiScale);
m_menuWidget.addButton("Yes", [this, worldName, titleScreen](TextButton &) {
std::string saveFilepath = "saves/" + worldName + ".dat";
if (fs::exists(saveFilepath))
fs::remove(saveFilepath);
@ -52,13 +58,11 @@ WorldDeletionState::WorldDeletionState(const std::string &worldName, TitleScreen
// FIXME: This is needed because there's currently no way to refresh WorldSelectionState
m_stateStack->pop(); // WorldSelectionState
m_stateStack->push<WorldSelectionState>(titleScreen);
});
}, 150);
m_cancelButton.setText("No");
m_cancelButton.setScale(Config::guiScale, Config::guiScale);
m_cancelButton.setCallback([this](TextButton &) {
m_menuWidget.addButton("No", [this](TextButton &) {
m_stateStack->pop();
});
}, 150);
updateWidgetPosition();
}
@ -67,8 +71,7 @@ void WorldDeletionState::onEvent(const sf::Event &event) {
InterfaceState::onEvent(event);
if (&m_stateStack->top() == this) {
m_confirmButton.onEvent(event);
m_cancelButton.onEvent(event);
m_menuWidget.onEvent(event);
}
}
@ -76,11 +79,21 @@ void WorldDeletionState::update() {
}
void WorldDeletionState::updateWidgetPosition() {
m_text.setPosition(Config::screenWidth / 2 - m_text.getSize().x * Config::guiScale * 2 / 2,
Config::screenHeight / 2 - m_text.getSize().y * Config::guiScale * 2 / 2);
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_confirmButton.setPosition(Config::screenWidth / 2.0f - m_confirmButton.getGlobalBounds().sizeX / 2, Config::screenHeight - 340);
m_cancelButton.setPosition(Config::screenWidth / 2.0f - m_cancelButton.getGlobalBounds().sizeX / 2, Config::screenHeight - 261);
m_filter.setSize(Config::screenWidth, Config::screenHeight);
m_text1.setPosition(Config::screenWidth / 2 - m_text1.getSize().x * Config::guiScale / 2,
Config::screenHeight / 2 - 30 * Config::guiScale);
m_text2.setPosition(Config::screenWidth / 2 - m_text2.getSize().x * Config::guiScale / 2,
Config::screenHeight / 2 - m_text2.getSize().y * Config::guiScale / 2);
m_menuWidget.setPosition(
Config::screenWidth / 2.0f - m_menuWidget.getGlobalBounds().sizeX / 2,
Config::screenHeight - 110 * Config::guiScale
);
}
void WorldDeletionState::draw(gk::RenderTarget &target, gk::RenderStates states) const {
@ -90,10 +103,13 @@ void WorldDeletionState::draw(gk::RenderTarget &target, gk::RenderStates states)
if (&m_stateStack->top() == this) {
prepareDraw(target, states);
target.draw(m_text, states);
target.draw(m_background, states);
target.draw(m_filter, states);
target.draw(m_confirmButton, states);
target.draw(m_cancelButton, states);
target.draw(m_text1, states);
target.draw(m_text2, states);
target.draw(m_menuWidget, states);
}
}

View File

@ -28,7 +28,7 @@
#define WORLDDELETIONSTATE_HPP_
#include "InterfaceState.hpp"
#include "TextButton.hpp"
#include "MenuWidget.hpp"
class TitleScreenState;
@ -45,10 +45,13 @@ class WorldDeletionState : public InterfaceState {
void draw(gk::RenderTarget &target, gk::RenderStates states) const override;
Text m_text;
gk::Image m_background{"texture-menu_background"};
gk::RectangleShape m_filter;
TextButton m_confirmButton;
TextButton m_cancelButton;
Text m_text1;
Text m_text2;
MenuWidget m_menuWidget{2, 1};
};
#endif // WORLDDELETIONSTATE_HPP_

View File

@ -40,15 +40,15 @@ namespace fs = ghc::filesystem;
WorldSelectionState::WorldSelectionState(TitleScreenState *titleScreen)
: InterfaceState(titleScreen), m_titleScreen(titleScreen)
{
m_title.setScale(Config::guiScale, Config::guiScale);
m_title.setString("Select World");
m_title.updateVertexBuffer();
m_background.setScale(Config::guiScale * 2, Config::guiScale * 2);
m_filter1.setFillColor(gk::Color(0, 0, 0, 176));
m_filter2.setFillColor(gk::Color(0, 0, 0, 136));
m_title.setScale(Config::guiScale, Config::guiScale);
m_title.setString("Select World");
m_title.updateVertexBuffer();
m_worldList.setScale(Config::guiScale, Config::guiScale);
m_menuWidget1.setScale(Config::guiScale, Config::guiScale);