Added world creation menu

This commit is contained in:
MihailRis 2023-11-17 13:57:45 +03:00
parent 03a6ed0f96
commit 4e1f19c911
12 changed files with 227 additions and 52 deletions

View File

@ -4,14 +4,14 @@
#include <sstream>
#include "../typedefs.h"
namespace filesystem = std::filesystem;
namespace fs = std::filesystem;
using std::string;
using filesystem::path;
using fs::path;
path enginefs::get_screenshot_file(string ext) {
path folder = SCREENSHOTS_FOLDER;
if (!filesystem::is_directory(folder)) {
filesystem::create_directory(folder);
if (!fs::is_directory(folder)) {
fs::create_directory(folder);
}
auto t = std::time(nullptr);
@ -23,15 +23,19 @@ path enginefs::get_screenshot_file(string ext) {
ss << std::put_time(&tm, format);
string datetimestr = ss.str();
path filename = folder/("screenshot-"+datetimestr+"."+ext);
path filename = folder/path("screenshot-"+datetimestr+"."+ext);
uint index = 0;
while (filesystem::exists(filename)) {
filename = folder/("screenshot-"+datetimestr+"-"+std::to_string(index)+"."+ext);
while (fs::exists(filename)) {
filename = folder/path("screenshot-"+datetimestr+"-"+std::to_string(index)+"."+ext);
index++;
}
return filename;
}
path enginefs::get_worlds_folder() {
return "worlds";
return path("worlds");
}
bool enginefs::is_world_name_used(std::string name) {
return fs::exists(enginefs::get_worlds_folder()/fs::u8path(name));
}

View File

@ -9,6 +9,7 @@
namespace enginefs {
extern std::filesystem::path get_screenshot_file(std::string ext);
extern std::filesystem::path get_worlds_folder();
extern bool is_world_name_used(std::string name);
}
#endif // FILES_ENGINE_FILES_H_

View File

@ -84,8 +84,11 @@ void Button::listenAction(onaction action) {
actions.push_back(action);
}
TextBox::TextBox(wstring text, vec4 padding) : Panel(vec2(200,32), padding, 0, false) {
label = new Label(text);
TextBox::TextBox(wstring placeholder, vec4 padding)
: Panel(vec2(200,32), padding, 0, false),
input(L""),
placeholder(placeholder) {
label = new Label(L"");
label->align(Align::center);
add(shared_ptr<UINode>(label));
}
@ -96,20 +99,28 @@ void TextBox::drawBackground(Batch2D* batch, Assets* assets) {
batch->color = (isfocused() ? focusedColor : (hover_ ? hoverColor : color_));
batch->rect(coord.x, coord.y, size_.x, size_.y);
if (!focused_ && supplier) {
label->text(supplier());
input = supplier();
}
if (input.empty()) {
label->color(vec4(0.5f));
label->text(placeholder);
} else {
label->color(vec4(1.0f));
label->text(input);
}
}
void TextBox::typed(unsigned int codepoint) {
label->text(label->text() + wstring({(wchar_t)codepoint}));
input += wstring({(wchar_t)codepoint});
}
void TextBox::keyPressed(int key) {
wstring src = label->text();
switch (key) {
case KEY_BACKSPACE:
if (src.length())
label->text(src.substr(0, src.length()-1));
if (!input.empty()){
input = input.substr(0, input.length()-1);
}
break;
case KEY_ENTER:
if (consumer) {
@ -130,4 +141,10 @@ void TextBox::textSupplier(wstringsupplier supplier) {
void TextBox::textConsumer(wstringconsumer consumer) {
this->consumer = consumer;
}
wstring TextBox::text() const {
if (input.empty())
return placeholder;
return input;
}

View File

@ -53,10 +53,12 @@ namespace gui {
glm::vec4 hoverColor {0.05f, 0.1f, 0.2f, 0.75f};
glm::vec4 focusedColor {0.0f, 0.0f, 0.0f, 1.0f};
Label* label;
std::wstring input;
std::wstring placeholder;
wstringsupplier supplier = nullptr;
wstringconsumer consumer = nullptr;
public:
TextBox(std::wstring text, glm::vec4 padding=glm::vec4(2.0f));
TextBox(std::wstring placeholder, glm::vec4 padding=glm::vec4(2.0f));
virtual std::shared_ptr<UINode> getAt(glm::vec2 pos, std::shared_ptr<UINode> self) override;
@ -66,6 +68,7 @@ namespace gui {
virtual void textSupplier(wstringsupplier supplier);
virtual void textConsumer(wstringconsumer consumer);
virtual bool isfocuskeeper() const override {return true;}
virtual std::wstring text() const;
};
}

View File

@ -35,11 +35,22 @@ void Container::act(float delta) {
if (event.timer > event.interval) {
event.callback();
event.timer = fmod(event.timer, event.interval);
if (event.repeat > 0) {
event.repeat--;
}
}
}
intervalEvents.erase(std::remove_if(
intervalEvents.begin(), intervalEvents.end(),
[](const IntervalEvent& event) {
return event.repeat == 0;
}
), intervalEvents.end());
for (auto node : nodes) {
if (node->visible())
if (node->visible()) {
node->act(delta);
}
}
}
@ -66,18 +77,23 @@ void Container::add(shared_ptr<UINode> node) {
void Container::remove(shared_ptr<UINode> selected) {
selected->setParent(nullptr);
nodes.erase(std::remove_if(nodes.begin(), nodes.end(), [selected](const shared_ptr<UINode> node) {
return node == selected;
}), nodes.end());
nodes.erase(std::remove_if(nodes.begin(), nodes.end(),
[selected](const shared_ptr<UINode> node) {
return node == selected;
}
), nodes.end());
refresh();
}
void Container::listenInterval(float interval, ontimeout callback) {
intervalEvents.push_back({callback, interval, 0.0f});
void Container::listenInterval(float interval, ontimeout callback, int repeat) {
intervalEvents.push_back({callback, interval, 0.0f, repeat});
}
Panel::Panel(vec2 size, glm::vec4 padding, float interval, bool resizing)
: Container(vec2(), size), padding(padding), interval(interval), resizing_(resizing) {
: Container(vec2(), size),
padding(padding),
interval(interval),
resizing_(resizing) {
color_ = vec4(0.0f, 0.0f, 0.0f, 0.75f);
}
@ -103,13 +119,13 @@ void Panel::refresh() {
y += margin.y;
float ex;
float spacex = size.x - margin.z - padding.z;
switch (node->align()) {
case Align::center:
ex = x + fmax(0.0f, (size.x - margin.z - padding.z) - node->size().x) / 2.0f;
ex = x + fmax(0.0f, spacex - node->size().x) / 2.0f;
break;
case Align::right:
ex = x + size.x - margin.z - padding.z - node->size().x;
ex = x + spacex - node->size().x;
break;
default:
ex = x + margin.x;
@ -131,7 +147,7 @@ void Panel::refresh() {
x += margin.x;
node->setCoord(vec2(x, y+margin.y));
x += nodesize.x + margin.z + interval;
float height = size.y - padding.y - padding.w - margin.y - margin.w;
node->size(vec2(nodesize.x, height));
maxh = fmax(maxh, y+margin.y+node->size().y+margin.w+padding.w);

View File

@ -15,6 +15,8 @@ namespace gui {
ontimeout callback;
float interval;
float timer;
// -1 - infinity, 1 - one time event
int repeat;
};
enum class Orientation { vertical, horizontal };
@ -32,7 +34,7 @@ namespace gui {
virtual std::shared_ptr<UINode> getAt(glm::vec2 pos, std::shared_ptr<UINode> self) override;
virtual void add(std::shared_ptr<UINode> node);
virtual void remove(std::shared_ptr<UINode> node);
void listenInterval(float interval, ontimeout callback);
void listenInterval(float interval, ontimeout callback, int repeat=-1);
};
class Panel : public Container {

View File

@ -57,23 +57,27 @@ HudRenderer::HudRenderer(Engine* engine, Level* level) : level(level), assets(en
fpsMax = fps;
});
panel->setCoord(vec2(10, 10));
panel->add(shared_ptr<Label>(create_label([this, level]() {
return L"chunks: "+std::to_wstring(this->level->chunks->chunksCount)+L" visible: "+std::to_wstring(level->chunks->visible);
})));
panel->add(shared_ptr<Label>(create_label([this](){
return L"fps: "+this->fpsString;
})));
panel->add(shared_ptr<Label>(create_label([this](){
return L"meshes: " + std::to_wstring(Mesh::meshesCount);
})));
panel->add(shared_ptr<Label>(create_label([this](){
return L"occlusion: "+wstring(this->occlusion ? L"on" : L"off");
})));
panel->add(shared_ptr<Label>(create_label([this, level]() {
return L"chunks: "+std::to_wstring(this->level->chunks->chunksCount)+L" visible: "+std::to_wstring(level->chunks->visible);
})));
panel->add(shared_ptr<Label>(create_label([this](){
std::wstringstream stream;
stream << std::hex << this->level->player->selectedVoxel.states;
return L"block-selected: "+std::to_wstring(this->level->player->selectedVoxel.id)+L" "+stream.str();
})));
panel->add(shared_ptr<Label>(create_label([this](){
return L"meshes: " + std::to_wstring(Mesh::meshesCount);
return L"seed: "+std::to_wstring(this->level->world->seed);
})));
for (int ax = 0; ax < 3; ax++){
Panel* sub = new Panel(vec2(10, 27), vec4(0.0f));
sub->orientation(Orientation::horizontal);

View File

@ -4,6 +4,7 @@
#include <memory>
#include <glm/glm.hpp>
#include <filesystem>
#include <stdexcept>
#include "../window/Camera.h"
#include "../window/Events.h"
@ -26,42 +27,45 @@
#include "../files/engine_files.h"
#include "../util/stringutil.h"
using std::string;
using std::wstring;
using glm::vec3;
using glm::vec4;
using std::shared_ptr;
using std::filesystem::path;
using std::filesystem::u8path;
using std::filesystem::directory_iterator;
using namespace gui;
MenuScreen::MenuScreen(Engine* engine_) : Screen(engine_) {
// Replace this ugly piece of code
// With some normal pages system
Panel* create_main_menu_panel(Engine* engine, shared_ptr<UINode>* newWorldPanel) {
Panel* panel = new Panel(vec2(400, 200), vec4(5.0f), 1.0f);
panel->color(vec4(0.0f));
panel->setCoord(vec2(10, 10));
{
auto button = new Button(L"New World", vec4(12.0f, 10.0f, 12.0f, 10.0f));
button->listenAction([this, panel](GUI*) {
std::cout << "-- loading world" << std::endl;
EngineSettings& settings = engine->getSettings();
path folder = enginefs::get_worlds_folder()/path("world");
World* world = new World("world", folder, 42, settings);
auto screen = new LevelScreen(engine, world->load(settings));
engine->setScreen(shared_ptr<Screen>(screen));
button->listenAction([engine, panel, newWorldPanel](GUI*) {
panel->visible(false);
(*newWorldPanel)->visible(true);
});
panel->add(shared_ptr<UINode>(button));
}
Panel* worldsPanel = new Panel(vec2(390, 200), vec4(5.0f));
worldsPanel->color(vec4(0.1f));
for (auto const& entry : directory_iterator(enginefs::get_worlds_folder())) {
std::string name = entry.path().filename();
Button* button = new Button(util::str2wstr_utf8(name), vec4(10.0f, 8.0f, 10.0f, 8.0f));
string name = entry.path().filename();
Button* button = new Button(util::str2wstr_utf8(name),
vec4(10.0f, 8.0f, 10.0f, 8.0f));
button->color(vec4(0.5f));
button->listenAction([this, panel, name](GUI*) {
button->listenAction([engine, panel, name](GUI*) {
EngineSettings& settings = engine->getSettings();
World* world = new World(name, enginefs::get_worlds_folder()/name, 42, settings);
engine->setScreen(shared_ptr<Screen>(new LevelScreen(engine, world->load(settings))));
auto folder = enginefs::get_worlds_folder()/name;
World* world = new World(name, folder, 42, settings);
auto screen = new LevelScreen(engine, world->load(settings));
engine->setScreen(shared_ptr<Screen>(screen));
});
worldsPanel->add(shared_ptr<UINode>(button));
}
@ -69,13 +73,113 @@ MenuScreen::MenuScreen(Engine* engine_) : Screen(engine_) {
{
Button* button = new Button(L"Quit", vec4(12.0f, 10.0f, 12.0f, 10.0f));
button->listenAction([this](GUI*) {
button->listenAction([](GUI*) {
Window::setShouldClose(true);
});
panel->add(shared_ptr<UINode>(button));
}
this->panel = shared_ptr<UINode>(panel);
engine->getGUI()->add(this->panel);
return panel;
}
Panel* create_new_world_panel(Engine* engine, shared_ptr<UINode>* mainPanel) {
Panel* panel = new Panel(vec2(400, 200), vec4(5.0f), 1.0f);
panel->color(vec4(0.0f));
panel->setCoord(vec2(10, 10));
TextBox* worldNameInput;
{
Label* label = new Label(L"World Name");
panel->add(shared_ptr<UINode>(label));
TextBox* input = new TextBox(L"New World", vec4(6.0f));
panel->add(shared_ptr<UINode>(input));
worldNameInput = input;
}
TextBox* seedInput;
{
Label* label = new Label(L"Seed");
panel->add(shared_ptr<UINode>(label));
uint64_t randseed = rand() ^ (rand() << 8) ^
(rand() << 16) ^ (rand() << 24) ^
((uint64_t)rand() << 32) ^ ((uint64_t)rand() << 40) ^
((uint64_t)rand() << 56);
TextBox* input = new TextBox(std::to_wstring(randseed), vec4(6.0f));
panel->add(shared_ptr<UINode>(input));
seedInput = input;
}
{
Button* button = new Button(L"Create World", vec4(10.0f));
button->margin(vec4(0, 20, 0, 0));
vec4 basecolor = worldNameInput->color();
button->listenAction([=](GUI*) {
wstring name = worldNameInput->text();
string nameutf8 = util::wstr2str_utf8(name);
// Basic validation
if (!util::is_valid_filename(name) ||
enginefs::is_world_name_used(nameutf8)) {
// blink red two times
panel->listenInterval(0.1f, [worldNameInput, basecolor]() {
static bool flag = true;
if (flag) {
worldNameInput->color(vec4(0.3f, 0.0f, 0.0f, 0.5f));
} else {
worldNameInput->color(basecolor);
}
flag = !flag;
}, 4);
return;
}
wstring seedstr = seedInput->text();
uint64_t seed;
if (util::is_integer(seedstr)) {
try {
seed = std::stoull(seedstr);
} catch (const std::out_of_range& err) {
std::hash<wstring> hash;
seed = hash(seedstr);
}
} else {
std::hash<wstring> hash;
seed = hash(seedstr);
}
std::cout << "world seed: " << seed << std::endl;
EngineSettings& settings = engine->getSettings();
auto folder = enginefs::get_worlds_folder()/u8path(nameutf8);
std::filesystem::create_directories(folder);
World* world = new World(nameutf8, folder, seed, settings);
auto screen = new LevelScreen(engine, world->load(settings));
engine->setScreen(shared_ptr<Screen>(screen));
});
panel->add(shared_ptr<UINode>(button));
}
{
Button* button = new Button(L"Back", vec4(10.0f));
button->listenAction([panel, mainPanel](GUI*) {
panel->visible(false);
(*mainPanel)->visible(true);
});
panel->add(shared_ptr<UINode>(button));
}
return panel;
}
MenuScreen::MenuScreen(Engine* engine_) : Screen(engine_) {
GUI* gui = engine->getGUI();
panel = shared_ptr<UINode>(create_main_menu_panel(engine, &newWorldPanel));
newWorldPanel = shared_ptr<UINode>(create_new_world_panel(engine, &panel));
newWorldPanel->visible(false);
gui->add(panel);
gui->add(newWorldPanel);
batch = new Batch2D(1024);
uicamera = new Camera(vec3(), Window::height);
@ -84,7 +188,9 @@ MenuScreen::MenuScreen(Engine* engine_) : Screen(engine_) {
}
MenuScreen::~MenuScreen() {
engine->getGUI()->remove(panel);
GUI* gui = engine->getGUI();
gui->remove(newWorldPanel);
gui->remove(panel);
delete batch;
delete uicamera;
}
@ -94,6 +200,7 @@ void MenuScreen::update(float delta) {
void MenuScreen::draw(float delta) {
panel->setCoord((Window::size() - panel->size()) / 2.0f);
newWorldPanel->setCoord((Window::size() - newWorldPanel->size()) / 2.0f);
Window::clear();
Window::setBgColor(vec3(0.2f, 0.2f, 0.2f));
@ -108,7 +215,7 @@ void MenuScreen::draw(float delta) {
batch->rect(0, 0,
Window::width, Window::height, 0, 0, 0,
UVRegion(0, 0, Window::width/64, Window::height/64),
false, false, glm::vec4(1.0f));
false, false, vec4(1.0f));
batch->render();
}

View File

@ -30,6 +30,7 @@ public:
class MenuScreen : public Screen {
std::shared_ptr<gui::UINode> panel;
std::shared_ptr<gui::UINode> newWorldPanel;
Batch2D* batch;
Camera* uicamera;
public:

View File

@ -131,4 +131,22 @@ bool util::is_integer(string text) {
return false;
}
return true;
}
bool util::is_integer(wstring text) {
for (wchar_t c : text) {
if (c < L'0' || c > L'9')
return false;
}
return true;
}
bool util::is_valid_filename(std::wstring name) {
for (wchar_t c : name) {
if (c < 31 || c == '/' || c == '\\' || c == '<' || c == '>' ||
c == ':' || c == '"' || c == '|' || c == '?' || c == '*'){
return false;
}
}
return true;
}

View File

@ -13,6 +13,8 @@ namespace util {
extern std::string wstr2str_utf8(const std::wstring ws);
extern std::wstring str2wstr_utf8(const std::string s);
extern bool is_integer(std::string text);
extern bool is_integer(std::wstring text);
extern bool is_valid_filename(std::wstring name);
}
#endif // UTIL_STRINGUTIL_H_

View File

@ -48,7 +48,7 @@ Level* World::load(EngineSettings& settings) {
seed = info.seed;
name = info.name;
vec3 playerPosition = vec3(0, 64, 0);
vec3 playerPosition = vec3(0, 100, 0);
Camera* camera = new Camera(playerPosition, glm::radians(90.0f));
Player* player = new Player(playerPosition, 4.0f, camera);
Level* level = new Level(this, player, settings);