From d4c4c4d399b63c7aab22d8efc8030b3413197154 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 7 Jan 2024 14:25:00 +0300 Subject: [PATCH] hud refactor --- src/frontend/InventoryView.cpp | 40 +++++++++-------- src/frontend/InventoryView.h | 20 +++++---- src/frontend/hud.cpp | 79 +++++++++++++++++----------------- src/frontend/hud.h | 2 + 4 files changed, 75 insertions(+), 66 deletions(-) diff --git a/src/frontend/InventoryView.cpp b/src/frontend/InventoryView.cpp index 6685535f..1ee28db7 100644 --- a/src/frontend/InventoryView.cpp +++ b/src/frontend/InventoryView.cpp @@ -15,34 +15,36 @@ InventoryView::InventoryView( int columns, - Player* player, const Assets* assets, const ContentIndices* indices, const ContentGfxCache* cache, std::vector blocks) - : player(player), - assets(assets), + : assets(assets), indices(indices), cache(cache), blocks(blocks), - invColumns(columns) { + columns(columns) { blocksPreview = new BlocksPreview(assets->getShader("ui3d"), assets->getAtlas("blocks"), cache); } void InventoryView::setPosition(int x, int y) { - this->invX = x; - this->invY = y; + position.x = x; + position.y = y; } 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 { - uint inv_rows = ceildiv(blocks.size(), invColumns); - return inv_rows * iconSize + (inv_rows-1) * interval + padY * 2; + uint inv_rows = ceildiv(blocks.size(), columns); + 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) { @@ -51,8 +53,8 @@ void InventoryView::actAndDraw(const GfxContext* ctx) { auto viewport = ctx->getViewport(); uint inv_w = getWidth(); uint inv_h = getHeight(); - int xs = invX + padX; - int ys = invY + padY; + int xs = position.x + padding.x; + int ys = position.y + padding.y; glm::vec4 tint (1.0f); int mx = Events::cursor.x; @@ -62,15 +64,15 @@ void InventoryView::actAndDraw(const GfxContext* ctx) { auto batch = ctx->getBatch2D(); batch->texture(nullptr); 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(); // blocks & items if (Events::scroll) { - inventoryScroll -= Events::scroll * (iconSize+interval); + scroll -= Events::scroll * (iconSize+interval); } - inventoryScroll = std::min(inventoryScroll, int(inv_h-viewport.getHeight())); - inventoryScroll = std::max(inventoryScroll, 0); + scroll = std::min(scroll, int(inv_h-viewport.getHeight())); + scroll = std::max(scroll, 0); blocksPreview->begin(&ctx->getViewport()); { Window::clearDepth(); @@ -80,8 +82,8 @@ void InventoryView::actAndDraw(const GfxContext* ctx) { uint index = 0; for (uint i = 0; i < blocks.size(); i++) { Block* cblock = indices->getBlockDef(blocks[i]); - int x = xs + (iconSize+interval) * (index % invColumns); - int y = ys + (iconSize+interval) * (index / invColumns) - inventoryScroll; + int x = xs + (iconSize+interval) * (index % columns); + int y = ys + (iconSize+interval) * (index / columns) - scroll; if (y < -int(iconSize+interval) || y >= int(viewport.getHeight())) { index++; continue; @@ -91,7 +93,9 @@ void InventoryView::actAndDraw(const GfxContext* ctx) { tint.g *= 1.2f; tint.b *= 1.2f; if (Events::jclicked(mousecode::BUTTON_1)) { - player->chosenBlock = blocks[i]; + if (consumer) { + consumer(blocks[i]); + } } } else { tint = glm::vec4(1.0f); diff --git a/src/frontend/InventoryView.h b/src/frontend/InventoryView.h index 792fec42..fe85be22 100644 --- a/src/frontend/InventoryView.h +++ b/src/frontend/InventoryView.h @@ -2,44 +2,46 @@ #define FRONTEND_INVENTORY_VIEW_H_ #include +#include +#include #include "../typedefs.h" -class Player; class Assets; class GfxContext; class ContentIndices; class BlocksPreview; class ContentGfxCache; +typedef std::function slotconsumer; + class InventoryView { - Player* player; const Assets* assets; const ContentIndices* indices; const ContentGfxCache* const cache; std::vector blocks; BlocksPreview* blocksPreview; + slotconsumer consumer = nullptr; - int inventoryScroll = 0; - int invColumns; + int scroll = 0; + int columns; uint iconSize = 48; uint interval = 4; - int padX = interval; - int padY = interval; - int invX = 0; - int invY = 0; + glm::ivec2 padding {interval, interval}; + glm::ivec2 position {0, 0}; public: InventoryView( int columns, - Player* player, const Assets* assets, const ContentIndices* indices, const ContentGfxCache* cache, std::vector blocks); + void setPosition(int x, int y); void actAndDraw(const GfxContext* ctx); int getWidth() const; int getHeight() const; + void setSlotConsumer(slotconsumer consumer); }; #endif // FRONTEND_INVENTORY_VIEW_H_ diff --git a/src/frontend/hud.cpp b/src/frontend/hud.cpp index 2e8b7b3c..be5004fc 100644 --- a/src/frontend/hud.cpp +++ b/src/frontend/hud.cpp @@ -55,39 +55,8 @@ inline Label* create_label(gui::wstringsupplier supplier) { return label; } -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 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); +void HudRenderer::createDebugPanel(Engine* engine) { + Panel* panel = new Panel(vec2(250, 200), vec4(5.0f), 1.0f); debugPanel = shared_ptr(panel); panel->listenInterval(1.0f, [this]() { fpsString = std::to_wstring(fpsMax)+L" / "+std::to_wstring(fpsMin); @@ -106,16 +75,16 @@ HudRenderer::HudRenderer(Engine* engine, bool culling = settings.graphics.frustumCulling; return L"frustum-culling: "+wstring(culling ? L"on" : L"off"); }))); - panel->add(shared_ptr