Added gui_util

This commit is contained in:
MihailRis
2023-12-07 13:18:28 +03:00
parent e3f7f7218f
commit 2055cff0be
11 changed files with 122 additions and 35 deletions
+10 -8
View File
@@ -2,13 +2,13 @@
#include <glm/ext.hpp> #include <glm/ext.hpp>
#include "../graphics/Viewport.h"
#include "../graphics/Shader.h" #include "../graphics/Shader.h"
#include "../graphics/Texture.h" #include "../graphics/Texture.h"
#include "../graphics/Atlas.h" #include "../graphics/Atlas.h"
#include "../graphics/Batch3D.h" #include "../graphics/Batch3D.h"
#include "../window/Camera.h" #include "../window/Camera.h"
#include "../voxels/Block.h" #include "../voxels/Block.h"
#include "../window/Window.h"
#include "ContentGfxCache.h" #include "ContentGfxCache.h"
using glm::vec4; using glm::vec4;
@@ -25,13 +25,12 @@ BlocksPreview::~BlocksPreview() {
delete batch; delete batch;
} }
void BlocksPreview::begin() { void BlocksPreview::begin(const Viewport* viewport) {
this->viewport = viewport;
shader->use(); shader->use();
shader->uniformMatrix("u_projview", shader->uniformMatrix("u_projview",
glm::ortho(0.0f, glm::ortho(0.0f, float(viewport->getWidth()),
float(Window::width), 0.0f, float(viewport->getHeight()),
0.0f,
float(Window::height),
-1000.0f, 1000.0f) * -1000.0f, 1000.0f) *
glm::lookAt(vec3(2, 2, 2), vec3(0.0f), vec3(0, 1, 0))); glm::lookAt(vec3(2, 2, 2), vec3(0.0f), vec3(0, 1, 0)));
atlas->getTexture()->bind(); atlas->getTexture()->bind();
@@ -39,10 +38,13 @@ void BlocksPreview::begin() {
/* Draw one block preview at given screen position */ /* Draw one block preview at given screen position */
void BlocksPreview::draw(const Block* def, int x, int y, int size, vec4 tint) { 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; x += 2;
y -= 35; 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; blockid_t id = def->rt.id;
const UVRegion texfaces[6]{ cache->getRegion(id, 0), cache->getRegion(id, 1), const UVRegion texfaces[6]{ cache->getRegion(id, 0), cache->getRegion(id, 1),
cache->getRegion(id, 2), cache->getRegion(id, 3), cache->getRegion(id, 2), cache->getRegion(id, 3),
+3 -1
View File
@@ -4,6 +4,7 @@
#include "../typedefs.h" #include "../typedefs.h"
#include <glm/glm.hpp> #include <glm/glm.hpp>
class Viewport;
class Shader; class Shader;
class Atlas; class Atlas;
class Batch3D; class Batch3D;
@@ -15,11 +16,12 @@ class BlocksPreview {
Atlas* atlas; Atlas* atlas;
Batch3D* batch; Batch3D* batch;
const ContentGfxCache* const cache; const ContentGfxCache* const cache;
const Viewport* viewport;
public: public:
BlocksPreview(Shader* shader, Atlas* atlas, const ContentGfxCache* cache); BlocksPreview(Shader* shader, Atlas* atlas, const ContentGfxCache* cache);
~BlocksPreview(); ~BlocksPreview();
void begin(); void begin(const Viewport* viewport);
void draw(const Block* block, int x, int y, int size, glm::vec4 tint); void draw(const Block* block, int x, int y, int size, glm::vec4 tint);
}; };
+1 -1
View File
@@ -148,7 +148,7 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera){
backShader->use(); backShader->use();
backShader->uniformMatrix("u_view", camera->getView(false)); backShader->uniformMatrix("u_view", camera->getView(false));
backShader->uniform1f("u_zoom", camera->zoom*camera->getFov()/(3.141592*0.5f)); 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); skybox->draw(backShader);
{ {
+1 -1
View File
@@ -19,7 +19,7 @@ using std::shared_ptr;
using namespace gui; using namespace gui;
GUI::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 = new Camera(vec3(), Window::height);
uicamera->perspective = false; uicamera->perspective = false;
+2
View File
@@ -44,6 +44,8 @@ class Camera;
*/ */
namespace gui { namespace gui {
typedef std::function<void()> runnable;
class UINode; class UINode;
class Container; class Container;
class PagesControl; class PagesControl;
+60
View File
@@ -0,0 +1,60 @@
#include "gui_util.h"
#include "controls.h"
#include "panels.h"
#include <glm/glm.hpp>
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("<alert>", panel);
menu->set("<alert>");
}
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("<confirm>", panel);
menu->set("<confirm>");
}
+18
View File
@@ -0,0 +1,18 @@
#ifndef FRONTEND_GUI_GUI_UTIL_H_
#define FRONTEND_GUI_GUI_UTIL_H_
#include <string>
#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_
+2 -2
View File
@@ -239,7 +239,7 @@ void HudRenderer::drawContentAccess(const GfxContext& ctx, Player* player) {
batch->render(); batch->render();
// blocks & items // blocks & items
blocksPreview->begin(); blocksPreview->begin(&ctx.getViewport());
{ {
Window::clearDepth(); Window::clearDepth();
GfxContext subctx = ctx.sub(); GfxContext subctx = ctx.sub();
@@ -328,7 +328,7 @@ void HudRenderer::draw(const GfxContext& ctx){
batch->color = vec4(1.0f); batch->color = vec4(1.0f);
batch->render(); batch->render();
blocksPreview->begin(); blocksPreview->begin(&ctx.getViewport());
{ {
Window::clearDepth(); Window::clearDepth();
GfxContext subctx = ctx.sub(); GfxContext subctx = ctx.sub();
+10 -20
View File
@@ -19,6 +19,8 @@
#include "../engine.h" #include "../engine.h"
#include "../settings.h" #include "../settings.h"
#include "gui/gui_util.h"
using glm::vec2; using glm::vec2;
using glm::vec4; using glm::vec4;
using std::string; using std::string;
@@ -29,25 +31,13 @@ using std::filesystem::u8path;
using std::filesystem::directory_iterator; using std::filesystem::directory_iterator;
using namespace gui; 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) { Panel* create_main_menu_panel(Engine* engine, PagesControl* menu) {
EnginePaths* paths = engine->getPaths(); EnginePaths* paths = engine->getPaths();
Panel* panel = new Panel(vec2(400, 200), vec4(5.0f), 1.0f); Panel* panel = new Panel(vec2(400, 200), vec4(5.0f), 1.0f);
panel->color(vec4(0.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)); Panel* worldsPanel = new Panel(vec2(390, 200), vec4(5.0f));
worldsPanel->color(vec4(0.1f)); worldsPanel->color(vec4(0.1f));
@@ -75,8 +65,8 @@ Panel* create_main_menu_panel(Engine* engine, PagesControl* menu) {
} }
} }
panel->add(worldsPanel); panel->add(worldsPanel);
panel->add(gotoButton(L"Settings", "settings", menu)); panel->add(guiutil::gotoButton(L"Settings", "settings", menu));
panel->add((new Button(L"Quit", vec4(10.f)))->listenAction([](GUI*) { panel->add((new Button(L"Quit", vec4(10.f)))->listenAction([](GUI* gui) {
Window::setShouldClose(true); Window::setShouldClose(true);
})); }));
panel->refresh(); panel->refresh();
@@ -163,7 +153,7 @@ Panel* create_new_world_panel(Engine* engine, PagesControl* menu) {
panel->add(button); panel->add(button);
} }
panel->add(backButton(menu)); panel->add(guiutil::backButton(menu));
panel->refresh(); panel->refresh();
return panel; return panel;
} }
@@ -209,7 +199,7 @@ Panel* create_controls_panel(Engine* engine, PagesControl* menu) {
} }
panel->add(scrollPanel); panel->add(scrollPanel);
panel->add(backButton(menu)); panel->add(guiutil::backButton(menu));
panel->refresh(); panel->refresh();
return panel; return panel;
} }
@@ -323,8 +313,8 @@ Panel* create_settings_panel(Engine* engine, PagesControl* menu) {
panel->add(checkpanel); panel->add(checkpanel);
} }
panel->add(gotoButton(L"Controls", "controls", menu)); panel->add(guiutil::gotoButton(L"Controls", "controls", menu));
panel->add(backButton(menu)); panel->add(guiutil::backButton(menu));
panel->refresh(); panel->refresh();
return panel; return panel;
} }
@@ -339,7 +329,7 @@ Panel* create_pause_panel(Engine* engine, PagesControl* menu) {
}); });
panel->add(shared_ptr<UINode>(button)); panel->add(shared_ptr<UINode>(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* button = new Button(L"Save and Quit to Menu", vec4(10.f));
button->listenAction([engine](GUI*){ button->listenAction([engine](GUI*){
+5 -2
View File
@@ -81,11 +81,14 @@ void MenuScreen::draw(float delta) {
uishader->use(); uishader->use();
uishader->uniformMatrix("u_projview", uicamera->getProjView()); uishader->uniformMatrix("u_projview", uicamera->getProjView());
uint width = Window::width;
uint height = Window::height;
batch->begin(); batch->begin();
batch->texture(engine->getAssets()->getTexture("menubg")); batch->texture(engine->getAssets()->getTexture("menubg"));
batch->rect(0, 0, batch->rect(0, 0,
Window::width, Window::height, 0, 0, 0, width, height, 0, 0, 0,
UVRegion(0, 0, Window::width/64, Window::height/64), UVRegion(0, 0, width/64, height/64),
false, false, vec4(1.0f)); false, false, vec4(1.0f));
batch->render(); batch->render();
} }
+10
View File
@@ -12,10 +12,20 @@ const int BLOCK_DIR_DOWN = 0x5;
// limited to 16 block orientations // limited to 16 block orientations
const int BLOCK_ROT_MASK = 0xF; const int BLOCK_ROT_MASK = 0xF;
// limited to 16 block variants
const int BLOCK_VARIANT_MASK = 0xF0;
struct voxel { struct voxel {
blockid_t id; blockid_t id;
uint8_t states; 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_ */ #endif /* VOXELS_VOXEL_H_ */