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