hud refactor

This commit is contained in:
MihailRis
2024-01-07 14:25:00 +03:00
parent 85d85dada7
commit d4c4c4d399
4 changed files with 75 additions and 66 deletions
+22 -18
View File
@@ -15,34 +15,36 @@
InventoryView::InventoryView( InventoryView::InventoryView(
int columns, int columns,
Player* player,
const Assets* assets, const Assets* assets,
const ContentIndices* indices, const ContentIndices* indices,
const ContentGfxCache* cache, const ContentGfxCache* cache,
std::vector<blockid_t> blocks) std::vector<blockid_t> blocks)
: player(player), : assets(assets),
assets(assets),
indices(indices), indices(indices),
cache(cache), cache(cache),
blocks(blocks), blocks(blocks),
invColumns(columns) { columns(columns) {
blocksPreview = new BlocksPreview(assets->getShader("ui3d"), blocksPreview = new BlocksPreview(assets->getShader("ui3d"),
assets->getAtlas("blocks"), assets->getAtlas("blocks"),
cache); cache);
} }
void InventoryView::setPosition(int x, int y) { void InventoryView::setPosition(int x, int y) {
this->invX = x; position.x = x;
this->invY = y; position.y = y;
} }
int InventoryView::getWidth() const { int InventoryView::getWidth() const {
return invColumns * iconSize + (invColumns-1) * interval + padX * 2; return columns * iconSize + (columns-1) * interval + padding.x * 2;
} }
int InventoryView::getHeight() const { int InventoryView::getHeight() const {
uint inv_rows = ceildiv(blocks.size(), invColumns); uint inv_rows = ceildiv(blocks.size(), columns);
return inv_rows * iconSize + (inv_rows-1) * interval + padY * 2; return inv_rows * iconSize + (inv_rows-1) * interval + padding.y * 2;
}
void InventoryView::setSlotConsumer(slotconsumer consumer) {
this->consumer = consumer;
} }
void InventoryView::actAndDraw(const GfxContext* ctx) { void InventoryView::actAndDraw(const GfxContext* ctx) {
@@ -51,8 +53,8 @@ void InventoryView::actAndDraw(const GfxContext* ctx) {
auto viewport = ctx->getViewport(); auto viewport = ctx->getViewport();
uint inv_w = getWidth(); uint inv_w = getWidth();
uint inv_h = getHeight(); uint inv_h = getHeight();
int xs = invX + padX; int xs = position.x + padding.x;
int ys = invY + padY; int ys = position.y + padding.y;
glm::vec4 tint (1.0f); glm::vec4 tint (1.0f);
int mx = Events::cursor.x; int mx = Events::cursor.x;
@@ -62,15 +64,15 @@ void InventoryView::actAndDraw(const GfxContext* ctx) {
auto batch = ctx->getBatch2D(); auto batch = ctx->getBatch2D();
batch->texture(nullptr); batch->texture(nullptr);
batch->color = glm::vec4(0.0f, 0.0f, 0.0f, 0.5f); batch->color = glm::vec4(0.0f, 0.0f, 0.0f, 0.5f);
batch->rect(invX, invY, inv_w, inv_h); batch->rect(position.x, position.y, inv_w, inv_h);
batch->render(); batch->render();
// blocks & items // blocks & items
if (Events::scroll) { if (Events::scroll) {
inventoryScroll -= Events::scroll * (iconSize+interval); scroll -= Events::scroll * (iconSize+interval);
} }
inventoryScroll = std::min(inventoryScroll, int(inv_h-viewport.getHeight())); scroll = std::min(scroll, int(inv_h-viewport.getHeight()));
inventoryScroll = std::max(inventoryScroll, 0); scroll = std::max(scroll, 0);
blocksPreview->begin(&ctx->getViewport()); blocksPreview->begin(&ctx->getViewport());
{ {
Window::clearDepth(); Window::clearDepth();
@@ -80,8 +82,8 @@ void InventoryView::actAndDraw(const GfxContext* ctx) {
uint index = 0; uint index = 0;
for (uint i = 0; i < blocks.size(); i++) { for (uint i = 0; i < blocks.size(); i++) {
Block* cblock = indices->getBlockDef(blocks[i]); Block* cblock = indices->getBlockDef(blocks[i]);
int x = xs + (iconSize+interval) * (index % invColumns); int x = xs + (iconSize+interval) * (index % columns);
int y = ys + (iconSize+interval) * (index / invColumns) - inventoryScroll; int y = ys + (iconSize+interval) * (index / columns) - scroll;
if (y < -int(iconSize+interval) || y >= int(viewport.getHeight())) { if (y < -int(iconSize+interval) || y >= int(viewport.getHeight())) {
index++; index++;
continue; continue;
@@ -91,7 +93,9 @@ void InventoryView::actAndDraw(const GfxContext* ctx) {
tint.g *= 1.2f; tint.g *= 1.2f;
tint.b *= 1.2f; tint.b *= 1.2f;
if (Events::jclicked(mousecode::BUTTON_1)) { if (Events::jclicked(mousecode::BUTTON_1)) {
player->chosenBlock = blocks[i]; if (consumer) {
consumer(blocks[i]);
}
} }
} else { } else {
tint = glm::vec4(1.0f); tint = glm::vec4(1.0f);
+11 -9
View File
@@ -2,44 +2,46 @@
#define FRONTEND_INVENTORY_VIEW_H_ #define FRONTEND_INVENTORY_VIEW_H_
#include <vector> #include <vector>
#include <functional>
#include <glm/glm.hpp>
#include "../typedefs.h" #include "../typedefs.h"
class Player;
class Assets; class Assets;
class GfxContext; class GfxContext;
class ContentIndices; class ContentIndices;
class BlocksPreview; class BlocksPreview;
class ContentGfxCache; class ContentGfxCache;
typedef std::function<void(blockid_t)> slotconsumer;
class InventoryView { class InventoryView {
Player* player;
const Assets* assets; const Assets* assets;
const ContentIndices* indices; const ContentIndices* indices;
const ContentGfxCache* const cache; const ContentGfxCache* const cache;
std::vector<blockid_t> blocks; std::vector<blockid_t> blocks;
BlocksPreview* blocksPreview; BlocksPreview* blocksPreview;
slotconsumer consumer = nullptr;
int inventoryScroll = 0; int scroll = 0;
int invColumns; int columns;
uint iconSize = 48; uint iconSize = 48;
uint interval = 4; uint interval = 4;
int padX = interval; glm::ivec2 padding {interval, interval};
int padY = interval; glm::ivec2 position {0, 0};
int invX = 0;
int invY = 0;
public: public:
InventoryView( InventoryView(
int columns, int columns,
Player* player,
const Assets* assets, const Assets* assets,
const ContentIndices* indices, const ContentIndices* indices,
const ContentGfxCache* cache, const ContentGfxCache* cache,
std::vector<blockid_t> blocks); std::vector<blockid_t> blocks);
void setPosition(int x, int y); void setPosition(int x, int y);
void actAndDraw(const GfxContext* ctx); void actAndDraw(const GfxContext* ctx);
int getWidth() const; int getWidth() const;
int getHeight() const; int getHeight() const;
void setSlotConsumer(slotconsumer consumer);
}; };
#endif // FRONTEND_INVENTORY_VIEW_H_ #endif // FRONTEND_INVENTORY_VIEW_H_
+40 -39
View File
@@ -55,39 +55,8 @@ inline Label* create_label(gui::wstringsupplier supplier) {
return label; return label;
} }
HudRenderer::HudRenderer(Engine* engine, void HudRenderer::createDebugPanel(Engine* engine) {
Level* level, Panel* panel = new Panel(vec2(250, 200), vec4(5.0f), 1.0f);
const ContentGfxCache* cache)
: level(level),
assets(engine->getAssets()),
gui(engine->getGUI()),
cache(cache) {
auto menu = gui->getMenu();
blocksPreview = new BlocksPreview(assets->getShader("ui3d"),
assets->getAtlas("blocks"),
cache);
auto content = level->content;
auto indices = content->indices;
std::vector<blockid_t> blocks;
for (blockid_t id = 1; id < indices->countBlockDefs(); id++) {
const Block* def = indices->getBlockDef(id);
if (def->hidden)
continue;
blocks.push_back(id);
}
contentAccess.reset(new InventoryView(
8,
level->player,
assets,
indices,
cache,
blocks));
uicamera = new Camera(vec3(), 1);
uicamera->perspective = false;
uicamera->flipped = true;
Panel* panel = new Panel(vec2(250, 200), vec4(5.0f), 1.0f);
debugPanel = shared_ptr<UINode>(panel); debugPanel = shared_ptr<UINode>(panel);
panel->listenInterval(1.0f, [this]() { panel->listenInterval(1.0f, [this]() {
fpsString = std::to_wstring(fpsMax)+L" / "+std::to_wstring(fpsMin); fpsString = std::to_wstring(fpsMax)+L" / "+std::to_wstring(fpsMin);
@@ -106,16 +75,16 @@ HudRenderer::HudRenderer(Engine* engine,
bool culling = settings.graphics.frustumCulling; bool culling = settings.graphics.frustumCulling;
return L"frustum-culling: "+wstring(culling ? L"on" : L"off"); return L"frustum-culling: "+wstring(culling ? L"on" : L"off");
}))); })));
panel->add(shared_ptr<Label>(create_label([this, level]() { panel->add(shared_ptr<Label>(create_label([this]() {
return L"chunks: "+std::to_wstring(this->level->chunks->chunksCount)+ return L"chunks: "+std::to_wstring(level->chunks->chunksCount)+
L" visible: "+std::to_wstring(level->chunks->visible); L" visible: "+std::to_wstring(level->chunks->visible);
}))); })));
panel->add(shared_ptr<Label>(create_label([this](){ panel->add(shared_ptr<Label>(create_label([this](){
auto player = this->level->player; auto player = level->player;
auto indices = this->level->content->indices; auto indices = level->content->indices;
auto def = indices->getBlockDef(player->selectedVoxel.id); auto def = indices->getBlockDef(player->selectedVoxel.id);
std::wstringstream stream; std::wstringstream stream;
stream << std::hex << this->level->player->selectedVoxel.states; stream << std::hex << level->player->selectedVoxel.states;
if (def) { if (def) {
stream << L" (" << util::str2wstr_utf8(def->name) << L")"; stream << L" (" << util::str2wstr_utf8(def->name) << L")";
} }
@@ -123,7 +92,7 @@ HudRenderer::HudRenderer(Engine* engine,
L" "+stream.str(); L" "+stream.str();
}))); })));
panel->add(shared_ptr<Label>(create_label([this](){ panel->add(shared_ptr<Label>(create_label([this](){
return L"seed: "+std::to_wstring(this->level->world->seed); return L"seed: "+std::to_wstring(level->world->seed);
}))); })));
for (int ax = 0; ax < 3; ax++){ for (int ax = 0; ax < 3; ax++){
@@ -203,6 +172,38 @@ HudRenderer::HudRenderer(Engine* engine,
panel->add(checkpanel); panel->add(checkpanel);
} }
panel->refresh(); panel->refresh();
}
HudRenderer::HudRenderer(Engine* engine,
Level* level,
const ContentGfxCache* cache)
: level(level),
assets(engine->getAssets()),
gui(engine->getGUI()),
cache(cache) {
auto menu = gui->getMenu();
blocksPreview = new BlocksPreview(assets->getShader("ui3d"),
assets->getAtlas("blocks"),
cache);
auto content = level->content;
auto indices = content->indices;
std::vector<blockid_t> blocks;
for (blockid_t id = 1; id < indices->countBlockDefs(); id++) {
const Block* def = indices->getBlockDef(id);
if (def->hidden)
continue;
blocks.push_back(id);
}
contentAccess.reset(new InventoryView(8, assets, indices, cache, blocks));
contentAccess->setSlotConsumer([=](blockid_t id) {
level->player->chosenBlock = id;
});
uicamera = new Camera(vec3(), 1);
uicamera->perspective = false;
uicamera->flipped = true;
createDebugPanel(engine);
menu->reset(); menu->reset();
gui->add(this->debugPanel); gui->add(this->debugPanel);
+2
View File
@@ -41,6 +41,8 @@ class HudRenderer {
std::shared_ptr<gui::UINode> debugPanel; std::shared_ptr<gui::UINode> debugPanel;
gui::GUI* gui; gui::GUI* gui;
const ContentGfxCache* const cache; const ContentGfxCache* const cache;
void createDebugPanel(Engine* engine);
public: public:
HudRenderer(Engine* engine, HudRenderer(Engine* engine,
Level* level, Level* level,