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

View File

@ -15,34 +15,36 @@
InventoryView::InventoryView(
int columns,
Player* player,
const Assets* assets,
const ContentIndices* indices,
const ContentGfxCache* cache,
std::vector<blockid_t> 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);

View File

@ -2,44 +2,46 @@
#define FRONTEND_INVENTORY_VIEW_H_
#include <vector>
#include <functional>
#include <glm/glm.hpp>
#include "../typedefs.h"
class Player;
class Assets;
class GfxContext;
class ContentIndices;
class BlocksPreview;
class ContentGfxCache;
typedef std::function<void(blockid_t)> slotconsumer;
class InventoryView {
Player* player;
const Assets* assets;
const ContentIndices* indices;
const ContentGfxCache* const cache;
std::vector<blockid_t> 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<blockid_t> 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_

View File

@ -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<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);
void HudRenderer::createDebugPanel(Engine* engine) {
Panel* panel = new Panel(vec2(250, 200), vec4(5.0f), 1.0f);
debugPanel = shared_ptr<UINode>(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<Label>(create_label([this, level]() {
return L"chunks: "+std::to_wstring(this->level->chunks->chunksCount)+
panel->add(shared_ptr<Label>(create_label([this]() {
return L"chunks: "+std::to_wstring(level->chunks->chunksCount)+
L" visible: "+std::to_wstring(level->chunks->visible);
})));
panel->add(shared_ptr<Label>(create_label([this](){
auto player = this->level->player;
auto indices = this->level->content->indices;
auto player = level->player;
auto indices = level->content->indices;
auto def = indices->getBlockDef(player->selectedVoxel.id);
std::wstringstream stream;
stream << std::hex << this->level->player->selectedVoxel.states;
stream << std::hex << level->player->selectedVoxel.states;
if (def) {
stream << L" (" << util::str2wstr_utf8(def->name) << L")";
}
@ -123,7 +92,7 @@ HudRenderer::HudRenderer(Engine* engine,
L" "+stream.str();
})));
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++){
@ -203,6 +172,38 @@ HudRenderer::HudRenderer(Engine* engine,
panel->add(checkpanel);
}
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();
gui->add(this->debugPanel);

View File

@ -41,6 +41,8 @@ class HudRenderer {
std::shared_ptr<gui::UINode> debugPanel;
gui::GUI* gui;
const ContentGfxCache* const cache;
void createDebugPanel(Engine* engine);
public:
HudRenderer(Engine* engine,
Level* level,