From 2055cff0be779cabd920b6fc2be17d4e865994fa Mon Sep 17 00:00:00 2001 From: MihailRis Date: Thu, 7 Dec 2023 13:18:28 +0300 Subject: [PATCH] Added gui_util --- src/frontend/BlocksPreview.cpp | 18 +++++----- src/frontend/BlocksPreview.h | 4 ++- src/frontend/WorldRenderer.cpp | 2 +- src/frontend/gui/GUI.cpp | 2 +- src/frontend/gui/GUI.h | 2 ++ src/frontend/gui/gui_util.cpp | 60 ++++++++++++++++++++++++++++++++++ src/frontend/gui/gui_util.h | 18 ++++++++++ src/frontend/hud.cpp | 4 +-- src/frontend/menu.cpp | 30 ++++++----------- src/frontend/screens.cpp | 7 ++-- src/voxels/voxel.h | 10 ++++++ 11 files changed, 122 insertions(+), 35 deletions(-) create mode 100644 src/frontend/gui/gui_util.cpp create mode 100644 src/frontend/gui/gui_util.h diff --git a/src/frontend/BlocksPreview.cpp b/src/frontend/BlocksPreview.cpp index 1798af87..c94134b0 100644 --- a/src/frontend/BlocksPreview.cpp +++ b/src/frontend/BlocksPreview.cpp @@ -2,13 +2,13 @@ #include +#include "../graphics/Viewport.h" #include "../graphics/Shader.h" #include "../graphics/Texture.h" #include "../graphics/Atlas.h" #include "../graphics/Batch3D.h" #include "../window/Camera.h" #include "../voxels/Block.h" -#include "../window/Window.h" #include "ContentGfxCache.h" using glm::vec4; @@ -25,13 +25,12 @@ BlocksPreview::~BlocksPreview() { delete batch; } -void BlocksPreview::begin() { +void BlocksPreview::begin(const Viewport* viewport) { + this->viewport = viewport; shader->use(); shader->uniformMatrix("u_projview", - glm::ortho(0.0f, - float(Window::width), - 0.0f, - float(Window::height), + glm::ortho(0.0f, float(viewport->getWidth()), + 0.0f, float(viewport->getHeight()), -1000.0f, 1000.0f) * glm::lookAt(vec3(2, 2, 2), vec3(0.0f), vec3(0, 1, 0))); atlas->getTexture()->bind(); @@ -39,10 +38,13 @@ void BlocksPreview::begin() { /* Draw one block preview at given screen position */ void BlocksPreview::draw(const Block* def, int x, int y, int size, vec4 tint) { - y = Window::height - y - 1; + uint width = viewport->getWidth(); + uint height = viewport->getHeight(); + + y = height - y - 1; x += 2; y -= 35; - shader->uniformMatrix("u_apply", glm::translate(glm::mat4(1.0f), vec3(x/float(Window::width) * 2, y/float(Window::height) * 2, 0.0f))); + shader->uniformMatrix("u_apply", glm::translate(glm::mat4(1.0f), vec3(x/float(width) * 2, y/float(height) * 2, 0.0f))); blockid_t id = def->rt.id; const UVRegion texfaces[6]{ cache->getRegion(id, 0), cache->getRegion(id, 1), cache->getRegion(id, 2), cache->getRegion(id, 3), diff --git a/src/frontend/BlocksPreview.h b/src/frontend/BlocksPreview.h index 5c75004f..2fe494a0 100644 --- a/src/frontend/BlocksPreview.h +++ b/src/frontend/BlocksPreview.h @@ -4,6 +4,7 @@ #include "../typedefs.h" #include +class Viewport; class Shader; class Atlas; class Batch3D; @@ -15,11 +16,12 @@ class BlocksPreview { Atlas* atlas; Batch3D* batch; const ContentGfxCache* const cache; + const Viewport* viewport; public: BlocksPreview(Shader* shader, Atlas* atlas, const ContentGfxCache* cache); ~BlocksPreview(); - void begin(); + void begin(const Viewport* viewport); void draw(const Block* block, int x, int y, int size, glm::vec4 tint); }; diff --git a/src/frontend/WorldRenderer.cpp b/src/frontend/WorldRenderer.cpp index 8ab9f0ec..6893ffda 100644 --- a/src/frontend/WorldRenderer.cpp +++ b/src/frontend/WorldRenderer.cpp @@ -148,7 +148,7 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera){ backShader->use(); backShader->uniformMatrix("u_view", camera->getView(false)); backShader->uniform1f("u_zoom", camera->zoom*camera->getFov()/(3.141592*0.5f)); - backShader->uniform1f("u_ar", (float)Window::width/(float)Window::height); + backShader->uniform1f("u_ar", float(displayWidth)/float(displayHeight)); skybox->draw(backShader); { diff --git a/src/frontend/gui/GUI.cpp b/src/frontend/gui/GUI.cpp index 0027b2f2..569ede8c 100644 --- a/src/frontend/gui/GUI.cpp +++ b/src/frontend/gui/GUI.cpp @@ -19,7 +19,7 @@ using std::shared_ptr; using namespace gui; GUI::GUI() { - container = new Container(vec2(0, 0), vec2(Window::width, Window::height)); + container = new Container(vec2(0, 0), vec2(1000)); uicamera = new Camera(vec3(), Window::height); uicamera->perspective = false; diff --git a/src/frontend/gui/GUI.h b/src/frontend/gui/GUI.h index 6e5fc5f6..ca13fe82 100644 --- a/src/frontend/gui/GUI.h +++ b/src/frontend/gui/GUI.h @@ -44,6 +44,8 @@ class Camera; */ namespace gui { + typedef std::function runnable; + class UINode; class Container; class PagesControl; diff --git a/src/frontend/gui/gui_util.cpp b/src/frontend/gui/gui_util.cpp new file mode 100644 index 00000000..df902c32 --- /dev/null +++ b/src/frontend/gui/gui_util.cpp @@ -0,0 +1,60 @@ +#include "gui_util.h" +#include "controls.h" +#include "panels.h" + +#include + +using namespace gui; +using glm::vec2; +using glm::vec4; +using std::string; +using std::wstring; + +Button* guiutil::backButton(PagesControl* menu) { + return (new Button(L"Back", vec4(10.f)))->listenAction([=](GUI* gui) { + menu->back(); + }); +} + +Button* guiutil::gotoButton(wstring text, string page, PagesControl* menu) { + return (new Button(text, vec4(10.f)))->listenAction([=](GUI* gui) { + menu->set(page); + }); +} + +void guiutil::alert(GUI* gui, wstring text, gui::runnable on_hidden) { + PagesControl* menu = gui->getMenu(); + Panel* panel = new Panel(vec2(500, 200), vec4(8.0f), 8.0f); + panel->color(vec4(0.0f, 0.0f, 0.0f, 0.5f)); + panel->add(new Label(text)); + panel->add((new Button(L"Ok", vec4(10.f)))->listenAction([=](GUI* gui) { + if (on_hidden) + on_hidden(); + menu->back(); + })); + panel->refresh(); + menu->add("", panel); + menu->set(""); +} + +void guiutil::confirm(GUI* gui, wstring text, gui::runnable on_confirm) { + PagesControl* menu = gui->getMenu(); + Panel* panel = new Panel(vec2(500, 200), vec4(8.0f), 8.0f); + panel->color(vec4(0.0f, 0.0f, 0.0f, 0.5f)); + panel->add(new Label(text)); + Panel* subpanel = new Panel(vec2(500, 53)); + subpanel->color(vec4(0)); + subpanel->add((new Button(L"Yes", vec4(8.0f)))->listenAction([=](GUI*){ + if (on_confirm) + on_confirm(); + menu->back(); + })); + subpanel->add((new Button(L"No", vec4(8.0f)))->listenAction([=](GUI*){ + menu->back(); + })); + panel->add(subpanel); + + panel->refresh(); + menu->add("", panel); + menu->set(""); +} diff --git a/src/frontend/gui/gui_util.h b/src/frontend/gui/gui_util.h new file mode 100644 index 00000000..3bfb50a5 --- /dev/null +++ b/src/frontend/gui/gui_util.h @@ -0,0 +1,18 @@ +#ifndef FRONTEND_GUI_GUI_UTIL_H_ +#define FRONTEND_GUI_GUI_UTIL_H_ + +#include +#include "GUI.h" + +namespace gui { + class Button; +} + +namespace guiutil { + gui::Button* backButton(gui::PagesControl* menu); + gui::Button* gotoButton(std::wstring text, std::string page, gui::PagesControl* menu); + void alert(gui::GUI* gui, std::wstring text, gui::runnable on_hidden=nullptr); + void confirm(gui::GUI* gui, std::wstring text, gui::runnable on_confirm=nullptr); +} + +#endif // FRONTEND_GUI_GUI_UTIL_H_ diff --git a/src/frontend/hud.cpp b/src/frontend/hud.cpp index 6a8ea2b5..9e11e063 100644 --- a/src/frontend/hud.cpp +++ b/src/frontend/hud.cpp @@ -239,7 +239,7 @@ void HudRenderer::drawContentAccess(const GfxContext& ctx, Player* player) { batch->render(); // blocks & items - blocksPreview->begin(); + blocksPreview->begin(&ctx.getViewport()); { Window::clearDepth(); GfxContext subctx = ctx.sub(); @@ -328,7 +328,7 @@ void HudRenderer::draw(const GfxContext& ctx){ batch->color = vec4(1.0f); batch->render(); - blocksPreview->begin(); + blocksPreview->begin(&ctx.getViewport()); { Window::clearDepth(); GfxContext subctx = ctx.sub(); diff --git a/src/frontend/menu.cpp b/src/frontend/menu.cpp index ab7ee152..31302d7c 100644 --- a/src/frontend/menu.cpp +++ b/src/frontend/menu.cpp @@ -19,6 +19,8 @@ #include "../engine.h" #include "../settings.h" +#include "gui/gui_util.h" + using glm::vec2; using glm::vec4; using std::string; @@ -29,25 +31,13 @@ using std::filesystem::u8path; using std::filesystem::directory_iterator; using namespace gui; -inline Button* gotoButton(wstring text, string page, PagesControl* menu) { - return (new Button(text, vec4(10.f)))->listenAction([=](GUI* gui) { - menu->set(page); - }); -} - -inline Button* backButton(PagesControl* menu) { - return (new Button(L"Back", vec4(10.f)))->listenAction([=](GUI* gui) { - menu->back(); - }); -} - Panel* create_main_menu_panel(Engine* engine, PagesControl* menu) { EnginePaths* paths = engine->getPaths(); Panel* panel = new Panel(vec2(400, 200), vec4(5.0f), 1.0f); panel->color(vec4(0.0f)); - panel->add(gotoButton(L"New World", "new-world", menu)); + panel->add(guiutil::gotoButton(L"New World", "new-world", menu)); Panel* worldsPanel = new Panel(vec2(390, 200), vec4(5.0f)); worldsPanel->color(vec4(0.1f)); @@ -75,8 +65,8 @@ Panel* create_main_menu_panel(Engine* engine, PagesControl* menu) { } } panel->add(worldsPanel); - panel->add(gotoButton(L"Settings", "settings", menu)); - panel->add((new Button(L"Quit", vec4(10.f)))->listenAction([](GUI*) { + panel->add(guiutil::gotoButton(L"Settings", "settings", menu)); + panel->add((new Button(L"Quit", vec4(10.f)))->listenAction([](GUI* gui) { Window::setShouldClose(true); })); panel->refresh(); @@ -163,7 +153,7 @@ Panel* create_new_world_panel(Engine* engine, PagesControl* menu) { panel->add(button); } - panel->add(backButton(menu)); + panel->add(guiutil::backButton(menu)); panel->refresh(); return panel; } @@ -209,7 +199,7 @@ Panel* create_controls_panel(Engine* engine, PagesControl* menu) { } panel->add(scrollPanel); - panel->add(backButton(menu)); + panel->add(guiutil::backButton(menu)); panel->refresh(); return panel; } @@ -323,8 +313,8 @@ Panel* create_settings_panel(Engine* engine, PagesControl* menu) { panel->add(checkpanel); } - panel->add(gotoButton(L"Controls", "controls", menu)); - panel->add(backButton(menu)); + panel->add(guiutil::gotoButton(L"Controls", "controls", menu)); + panel->add(guiutil::backButton(menu)); panel->refresh(); return panel; } @@ -339,7 +329,7 @@ Panel* create_pause_panel(Engine* engine, PagesControl* menu) { }); panel->add(shared_ptr(button)); } - panel->add(gotoButton(L"Settings", "settings", menu)); + panel->add(guiutil::gotoButton(L"Settings", "settings", menu)); { Button* button = new Button(L"Save and Quit to Menu", vec4(10.f)); button->listenAction([engine](GUI*){ diff --git a/src/frontend/screens.cpp b/src/frontend/screens.cpp index 6419efc8..eec335e2 100644 --- a/src/frontend/screens.cpp +++ b/src/frontend/screens.cpp @@ -81,11 +81,14 @@ void MenuScreen::draw(float delta) { uishader->use(); uishader->uniformMatrix("u_projview", uicamera->getProjView()); + uint width = Window::width; + uint height = Window::height; + batch->begin(); batch->texture(engine->getAssets()->getTexture("menubg")); batch->rect(0, 0, - Window::width, Window::height, 0, 0, 0, - UVRegion(0, 0, Window::width/64, Window::height/64), + width, height, 0, 0, 0, + UVRegion(0, 0, width/64, height/64), false, false, vec4(1.0f)); batch->render(); } diff --git a/src/voxels/voxel.h b/src/voxels/voxel.h index 934d6b45..905c4996 100644 --- a/src/voxels/voxel.h +++ b/src/voxels/voxel.h @@ -12,10 +12,20 @@ const int BLOCK_DIR_DOWN = 0x5; // limited to 16 block orientations const int BLOCK_ROT_MASK = 0xF; +// limited to 16 block variants +const int BLOCK_VARIANT_MASK = 0xF0; struct voxel { blockid_t id; uint8_t states; + + inline uint8_t rotation() { + return states & BLOCK_ROT_MASK; + } + + inline int8_t variant() { + return (states & BLOCK_VARIANT_MASK) >> 4; + } }; #endif /* VOXELS_VOXEL_H_ */