InventoryView now uses items instead of blocks (part I)
This commit is contained in:
parent
dd5cd55aeb
commit
eae00dbb11
@ -4,6 +4,7 @@
|
||||
#include <string>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include "../graphics/UVRegion.h"
|
||||
#include "../typedefs.h"
|
||||
|
||||
#define BLOCK_ITEM_SUFFIX ".item"
|
||||
@ -13,7 +14,9 @@ struct item_funcs_set {
|
||||
};
|
||||
|
||||
enum class item_icon_type {
|
||||
sprite, block,
|
||||
none, // invisible (core:empty) must not be rendered
|
||||
sprite, // textured quad: icon is `atlas_name:texture_name`
|
||||
block, // block preview: icon is string block id
|
||||
};
|
||||
|
||||
class ItemDef {
|
||||
@ -30,6 +33,7 @@ public:
|
||||
struct {
|
||||
itemid_t id;
|
||||
item_funcs_set funcsset {};
|
||||
UVRegion iconRegion {0, 0, 1, 1};
|
||||
} rt;
|
||||
|
||||
ItemDef(std::string name);
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include "content/ItemDef.h"
|
||||
#include "content/Content.h"
|
||||
#include "window/Window.h"
|
||||
#include "window/Events.h"
|
||||
@ -21,6 +22,9 @@ void setup_definitions(ContentBuilder* builder) { // Strange function, need to R
|
||||
block->selectable = false;
|
||||
block->model = BlockModel::none;
|
||||
builder->add(block);
|
||||
|
||||
ItemDef* item = builder->createItem("core:empty");
|
||||
item->iconType = item_icon_type::none;
|
||||
}
|
||||
|
||||
void setup_bindings() {
|
||||
|
||||
@ -10,17 +10,19 @@
|
||||
#include "../graphics/Batch2D.h"
|
||||
#include "../graphics/GfxContext.h"
|
||||
#include "../content/Content.h"
|
||||
#include "../content/ItemDef.h"
|
||||
#include "../maths/voxmaths.h"
|
||||
#include "../objects/Player.h"
|
||||
#include "../voxels/Block.h"
|
||||
|
||||
InventoryView::InventoryView(
|
||||
int columns,
|
||||
const ContentIndices* indices,
|
||||
const Content* content,
|
||||
LevelFrontend* frontend,
|
||||
std::vector<blockid_t> blocks)
|
||||
: indices(indices),
|
||||
blocks(blocks),
|
||||
std::vector<itemid_t> items)
|
||||
: content(content),
|
||||
indices(content->indices),
|
||||
items(items),
|
||||
frontend(frontend),
|
||||
columns(columns) {
|
||||
}
|
||||
@ -38,7 +40,7 @@ int InventoryView::getWidth() const {
|
||||
}
|
||||
|
||||
int InventoryView::getHeight() const {
|
||||
uint inv_rows = ceildiv(blocks.size(), columns);
|
||||
uint inv_rows = ceildiv(items.size(), columns);
|
||||
return inv_rows * iconSize + (inv_rows-1) * interval + padding.y * 2;
|
||||
}
|
||||
|
||||
@ -82,8 +84,8 @@ void InventoryView::actAndDraw(const GfxContext* ctx) {
|
||||
subctx.depthTest(true);
|
||||
subctx.cullFace(true);
|
||||
uint index = 0;
|
||||
for (uint i = 0; i < blocks.size(); i++) {
|
||||
Block* cblock = indices->getBlockDef(blocks[i]);
|
||||
for (uint i = 0; i < items.size(); i++) {
|
||||
ItemDef* item = indices->getItemDef(items[i]);
|
||||
int x = xs + (iconSize+interval) * (index % columns);
|
||||
int y = ys + (iconSize+interval) * (index / columns) - scroll;
|
||||
if (y < -int(iconSize+interval) || y >= int(viewport.getHeight())) {
|
||||
@ -96,13 +98,19 @@ void InventoryView::actAndDraw(const GfxContext* ctx) {
|
||||
tint.b *= 1.2f;
|
||||
if (Events::jclicked(mousecode::BUTTON_1)) {
|
||||
if (consumer) {
|
||||
consumer(blocks[i]);
|
||||
consumer(items[i]);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
tint = glm::vec4(1.0f);
|
||||
}
|
||||
blocksPreview->draw(cblock, x, y, iconSize, tint);
|
||||
switch (item->iconType) {
|
||||
case item_icon_type::block: {
|
||||
Block* cblock = content->requireBlock(item->icon);
|
||||
blocksPreview->draw(cblock, x, y, iconSize, tint);
|
||||
break;
|
||||
}
|
||||
}
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
@ -9,14 +9,16 @@
|
||||
|
||||
class Assets;
|
||||
class GfxContext;
|
||||
class Content;
|
||||
class ContentIndices;
|
||||
class LevelFrontend;
|
||||
|
||||
typedef std::function<void(blockid_t)> slotconsumer;
|
||||
typedef std::function<void(itemid_t)> slotconsumer;
|
||||
|
||||
class InventoryView {
|
||||
const Content* content;
|
||||
const ContentIndices* indices;
|
||||
std::vector<blockid_t> blocks;
|
||||
std::vector<itemid_t> items;
|
||||
slotconsumer consumer = nullptr;
|
||||
LevelFrontend* frontend;
|
||||
|
||||
@ -29,9 +31,9 @@ class InventoryView {
|
||||
public:
|
||||
InventoryView(
|
||||
int columns,
|
||||
const ContentIndices* indices,
|
||||
const Content* content,
|
||||
LevelFrontend* frontend,
|
||||
std::vector<blockid_t> blocks);
|
||||
std::vector<itemid_t> items);
|
||||
|
||||
virtual ~InventoryView();
|
||||
|
||||
|
||||
@ -182,14 +182,11 @@ HudRenderer::HudRenderer(Engine* engine, LevelFrontend* frontend)
|
||||
auto menu = gui->getMenu();
|
||||
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);
|
||||
std::vector<itemid_t> items;
|
||||
for (itemid_t id = 0; id < indices->countItemDefs(); id++) {
|
||||
items.push_back(id);
|
||||
}
|
||||
contentAccess.reset(new InventoryView(8, indices, frontend, blocks));
|
||||
contentAccess.reset(new InventoryView(8, content, frontend, items));
|
||||
contentAccess->setSlotConsumer([=](blockid_t id) {
|
||||
level->player->chosenBlock = id;
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user