inventory render optimized
This commit is contained in:
+170
-170
@@ -52,106 +52,106 @@ using glm::vec4;
|
||||
using namespace gui;
|
||||
|
||||
inline std::shared_ptr<Label> create_label(gui::wstringsupplier supplier) {
|
||||
auto label = std::make_shared<Label>(L"-");
|
||||
label->textSupplier(supplier);
|
||||
return label;
|
||||
auto label = std::make_shared<Label>(L"-");
|
||||
label->textSupplier(supplier);
|
||||
return label;
|
||||
}
|
||||
|
||||
void HudRenderer::createDebugPanel(Engine* engine) {
|
||||
auto level = frontend->getLevel();
|
||||
|
||||
Panel* panel = new Panel(vec2(250, 200), vec4(5.0f), 1.0f);
|
||||
debugPanel = std::shared_ptr<UINode>(panel);
|
||||
panel->listenInterval(1.0f, [this]() {
|
||||
fpsString = std::to_wstring(fpsMax)+L" / "+std::to_wstring(fpsMin);
|
||||
fpsMin = fps;
|
||||
fpsMax = fps;
|
||||
});
|
||||
panel->setCoord(vec2(10, 10));
|
||||
panel->add(create_label([this](){ return L"fps: "+this->fpsString;}));
|
||||
panel->add(create_label([this](){
|
||||
return L"meshes: " + std::to_wstring(Mesh::meshesCount);
|
||||
}));
|
||||
panel->add(create_label([=](){
|
||||
auto& settings = engine->getSettings();
|
||||
bool culling = settings.graphics.frustumCulling;
|
||||
return L"frustum-culling: "+std::wstring(culling ? L"on" : L"off");
|
||||
}));
|
||||
panel->add(create_label([=]() {
|
||||
return L"chunks: "+std::to_wstring(level->chunks->chunksCount)+
|
||||
L" visible: "+std::to_wstring(level->chunks->visible);
|
||||
}));
|
||||
panel->add(create_label([=](){
|
||||
auto player = level->player;
|
||||
auto* indices = level->content->getIndices();
|
||||
auto def = indices->getBlockDef(player->selectedVoxel.id);
|
||||
std::wstringstream stream;
|
||||
stream << std::hex << level->player->selectedVoxel.states;
|
||||
if (def) {
|
||||
stream << L" (" << util::str2wstr_utf8(def->name) << L")";
|
||||
}
|
||||
return L"block: "+std::to_wstring(player->selectedVoxel.id)+
|
||||
L" "+stream.str();
|
||||
}));
|
||||
panel->add(create_label([=](){
|
||||
return L"seed: "+std::to_wstring(level->world->seed);
|
||||
}));
|
||||
debugPanel = std::shared_ptr<UINode>(panel);
|
||||
panel->listenInterval(1.0f, [this]() {
|
||||
fpsString = std::to_wstring(fpsMax)+L" / "+std::to_wstring(fpsMin);
|
||||
fpsMin = fps;
|
||||
fpsMax = fps;
|
||||
});
|
||||
panel->setCoord(vec2(10, 10));
|
||||
panel->add(create_label([this](){ return L"fps: "+this->fpsString;}));
|
||||
panel->add(create_label([this](){
|
||||
return L"meshes: " + std::to_wstring(Mesh::meshesCount);
|
||||
}));
|
||||
panel->add(create_label([=](){
|
||||
auto& settings = engine->getSettings();
|
||||
bool culling = settings.graphics.frustumCulling;
|
||||
return L"frustum-culling: "+std::wstring(culling ? L"on" : L"off");
|
||||
}));
|
||||
panel->add(create_label([=]() {
|
||||
return L"chunks: "+std::to_wstring(level->chunks->chunksCount)+
|
||||
L" visible: "+std::to_wstring(level->chunks->visible);
|
||||
}));
|
||||
panel->add(create_label([=](){
|
||||
auto player = level->player;
|
||||
auto* indices = level->content->getIndices();
|
||||
auto def = indices->getBlockDef(player->selectedVoxel.id);
|
||||
std::wstringstream stream;
|
||||
stream << std::hex << level->player->selectedVoxel.states;
|
||||
if (def) {
|
||||
stream << L" (" << util::str2wstr_utf8(def->name) << L")";
|
||||
}
|
||||
return L"block: "+std::to_wstring(player->selectedVoxel.id)+
|
||||
L" "+stream.str();
|
||||
}));
|
||||
panel->add(create_label([=](){
|
||||
return L"seed: "+std::to_wstring(level->world->seed);
|
||||
}));
|
||||
|
||||
for (int ax = 0; ax < 3; ax++){
|
||||
Panel* sub = new Panel(vec2(10, 27), vec4(0.0f));
|
||||
sub->orientation(Orientation::horizontal);
|
||||
for (int ax = 0; ax < 3; ax++){
|
||||
Panel* sub = new Panel(vec2(10, 27), vec4(0.0f));
|
||||
sub->orientation(Orientation::horizontal);
|
||||
|
||||
std::wstring str = L"x: ";
|
||||
str[0] += ax;
|
||||
Label* label = new Label(str);
|
||||
label->margin(vec4(2, 3, 2, 3));
|
||||
sub->add(label);
|
||||
sub->color(vec4(0.0f));
|
||||
std::wstring str = L"x: ";
|
||||
str[0] += ax;
|
||||
Label* label = new Label(str);
|
||||
label->margin(vec4(2, 3, 2, 3));
|
||||
sub->add(label);
|
||||
sub->color(vec4(0.0f));
|
||||
|
||||
// Coord input
|
||||
TextBox* box = new TextBox(L"");
|
||||
box->textSupplier([=]() {
|
||||
Hitbox* hitbox = level->player->hitbox.get();
|
||||
return util::to_wstring(hitbox->position[ax], 2);
|
||||
});
|
||||
box->textConsumer([=](std::wstring text) {
|
||||
try {
|
||||
vec3 position = level->player->hitbox->position;
|
||||
position[ax] = std::stoi(text);
|
||||
level->player->teleport(position);
|
||||
} catch (std::invalid_argument& _){
|
||||
}
|
||||
});
|
||||
box->setOnEditStart([=](){
|
||||
Hitbox* hitbox = level->player->hitbox.get();
|
||||
box->text(std::to_wstring(int(hitbox->position[ax])));
|
||||
});
|
||||
// Coord input
|
||||
TextBox* box = new TextBox(L"");
|
||||
box->textSupplier([=]() {
|
||||
Hitbox* hitbox = level->player->hitbox.get();
|
||||
return util::to_wstring(hitbox->position[ax], 2);
|
||||
});
|
||||
box->textConsumer([=](std::wstring text) {
|
||||
try {
|
||||
vec3 position = level->player->hitbox->position;
|
||||
position[ax] = std::stoi(text);
|
||||
level->player->teleport(position);
|
||||
} catch (std::invalid_argument& _){
|
||||
}
|
||||
});
|
||||
box->setOnEditStart([=](){
|
||||
Hitbox* hitbox = level->player->hitbox.get();
|
||||
box->text(std::to_wstring(int(hitbox->position[ax])));
|
||||
});
|
||||
|
||||
sub->add(box);
|
||||
panel->add(sub);
|
||||
}
|
||||
panel->add(create_label([=](){
|
||||
int hour, minute, second;
|
||||
timeutil::from_value(level->world->daytime, hour, minute, second);
|
||||
sub->add(box);
|
||||
panel->add(sub);
|
||||
}
|
||||
panel->add(create_label([=](){
|
||||
int hour, minute, second;
|
||||
timeutil::from_value(level->world->daytime, hour, minute, second);
|
||||
|
||||
std::wstring timeString =
|
||||
util::lfill(std::to_wstring(hour), 2, L'0') + L":" +
|
||||
util::lfill(std::to_wstring(minute), 2, L'0');
|
||||
return L"time: "+timeString;
|
||||
}));
|
||||
{
|
||||
TrackBar* bar = new TrackBar(0.0f, 1.0f, 1.0f, 0.005f, 8);
|
||||
bar->supplier([=]() {return level->world->daytime;});
|
||||
bar->consumer([=](double val) {level->world->daytime = val;});
|
||||
panel->add(bar);
|
||||
}
|
||||
{
|
||||
TrackBar* bar = new TrackBar(0.0f, 1.0f, 0.0f, 0.005f, 8);
|
||||
bar->supplier([=]() {return WorldRenderer::fog;});
|
||||
bar->consumer([=](double val) {WorldRenderer::fog = val;});
|
||||
panel->add(bar);
|
||||
}
|
||||
{
|
||||
std::wstring timeString =
|
||||
util::lfill(std::to_wstring(hour), 2, L'0') + L":" +
|
||||
util::lfill(std::to_wstring(minute), 2, L'0');
|
||||
return L"time: "+timeString;
|
||||
}));
|
||||
{
|
||||
TrackBar* bar = new TrackBar(0.0f, 1.0f, 1.0f, 0.005f, 8);
|
||||
bar->supplier([=]() {return level->world->daytime;});
|
||||
bar->consumer([=](double val) {level->world->daytime = val;});
|
||||
panel->add(bar);
|
||||
}
|
||||
{
|
||||
TrackBar* bar = new TrackBar(0.0f, 1.0f, 0.0f, 0.005f, 8);
|
||||
bar->supplier([=]() {return WorldRenderer::fog;});
|
||||
bar->consumer([=](double val) {WorldRenderer::fog = val;});
|
||||
panel->add(bar);
|
||||
}
|
||||
{
|
||||
auto checkbox = new FullCheckBox(L"Show Chunk Borders", vec2(400, 32));
|
||||
checkbox->supplier([=]() {
|
||||
return engine->getSettings().debug.showChunkBorders;
|
||||
@@ -160,8 +160,8 @@ void HudRenderer::createDebugPanel(Engine* engine) {
|
||||
engine->getSettings().debug.showChunkBorders = checked;
|
||||
});
|
||||
panel->add(checkbox);
|
||||
}
|
||||
panel->refresh();
|
||||
}
|
||||
panel->refresh();
|
||||
}
|
||||
|
||||
std::shared_ptr<InventoryView> HudRenderer::createContentAccess() {
|
||||
@@ -192,10 +192,10 @@ std::shared_ptr<InventoryView> HudRenderer::createContentAccess() {
|
||||
|
||||
auto contentAccess = std::make_shared<InventoryView>(
|
||||
content,
|
||||
frontend,
|
||||
interaction.get(),
|
||||
accessInventory,
|
||||
std::move(layout)
|
||||
frontend,
|
||||
interaction.get(),
|
||||
accessInventory,
|
||||
std::move(layout)
|
||||
);
|
||||
contentAccess->build();
|
||||
return contentAccess;
|
||||
@@ -215,10 +215,10 @@ std::shared_ptr<InventoryView> HudRenderer::createHotbar() {
|
||||
layout->setOrigin(glm::vec2(layout->getSize().x/2, 0));
|
||||
auto view = std::make_shared<InventoryView>(
|
||||
content,
|
||||
frontend,
|
||||
interaction.get(),
|
||||
inventory,
|
||||
std::move(layout)
|
||||
frontend,
|
||||
interaction.get(),
|
||||
inventory,
|
||||
std::move(layout)
|
||||
);
|
||||
view->build();
|
||||
view->setInteractive(false);
|
||||
@@ -255,13 +255,13 @@ HudRenderer::HudRenderer(Engine* engine, LevelFrontend* frontend)
|
||||
gui(engine->getGUI()),
|
||||
frontend(frontend)
|
||||
{
|
||||
auto menu = gui->getMenu();
|
||||
auto menu = gui->getMenu();
|
||||
|
||||
interaction = std::make_unique<InventoryInteraction>();
|
||||
grabbedItemView = std::make_shared<SlotView>(
|
||||
interaction->getGrabbedItem(),
|
||||
frontend,
|
||||
interaction.get(),
|
||||
interaction.get(),
|
||||
frontend->getLevel()->content,
|
||||
SlotLayout(glm::vec2(), false, false, nullptr, nullptr)
|
||||
);
|
||||
@@ -279,14 +279,14 @@ HudRenderer::HudRenderer(Engine* engine, LevelFrontend* frontend)
|
||||
hotbarView = createHotbar();
|
||||
inventoryView = createInventory();
|
||||
|
||||
uicamera = new Camera(vec3(), 1);
|
||||
uicamera->perspective = false;
|
||||
uicamera->flipped = true;
|
||||
uicamera = new Camera(vec3(), 1);
|
||||
uicamera->perspective = false;
|
||||
uicamera->flipped = true;
|
||||
|
||||
createDebugPanel(engine);
|
||||
menu->reset();
|
||||
|
||||
gui->add(debugPanel);
|
||||
menu->reset();
|
||||
|
||||
gui->add(debugPanel);
|
||||
gui->add(contentAccessPanel);
|
||||
gui->add(hotbarView);
|
||||
gui->add(inventoryView);
|
||||
@@ -298,48 +298,48 @@ HudRenderer::~HudRenderer() {
|
||||
gui->remove(inventoryView);
|
||||
gui->remove(hotbarView);
|
||||
gui->remove(contentAccessPanel);
|
||||
gui->remove(debugPanel);
|
||||
delete uicamera;
|
||||
gui->remove(debugPanel);
|
||||
delete uicamera;
|
||||
}
|
||||
|
||||
void HudRenderer::drawDebug(int fps){
|
||||
this->fps = fps;
|
||||
fpsMin = min(fps, fpsMin);
|
||||
fpsMax = max(fps, fpsMax);
|
||||
this->fps = fps;
|
||||
fpsMin = min(fps, fpsMin);
|
||||
fpsMax = max(fps, fpsMax);
|
||||
}
|
||||
|
||||
void HudRenderer::update(bool visible) {
|
||||
auto level = frontend->getLevel();
|
||||
auto player = level->player;
|
||||
auto menu = gui->getMenu();
|
||||
auto menu = gui->getMenu();
|
||||
|
||||
menu->visible(pause);
|
||||
|
||||
if (!visible && inventoryOpen) {
|
||||
inventoryOpen = false;
|
||||
}
|
||||
if (pause && menu->current().panel == nullptr) {
|
||||
pause = false;
|
||||
}
|
||||
if (Events::jpressed(keycode::ESCAPE) && !gui->isFocusCaught()) {
|
||||
if (pause) {
|
||||
pause = false;
|
||||
menu->reset();
|
||||
} else if (inventoryOpen) {
|
||||
inventoryOpen = false;
|
||||
} else {
|
||||
pause = true;
|
||||
menu->set("pause");
|
||||
}
|
||||
}
|
||||
if (visible && Events::jactive(BIND_HUD_INVENTORY)) {
|
||||
if (!pause) {
|
||||
inventoryOpen = !inventoryOpen;
|
||||
}
|
||||
}
|
||||
if ((pause || inventoryOpen) == Events::_cursor_locked) {
|
||||
Events::toggleCursor();
|
||||
}
|
||||
if (pause && menu->current().panel == nullptr) {
|
||||
pause = false;
|
||||
}
|
||||
if (Events::jpressed(keycode::ESCAPE) && !gui->isFocusCaught()) {
|
||||
if (pause) {
|
||||
pause = false;
|
||||
menu->reset();
|
||||
} else if (inventoryOpen) {
|
||||
inventoryOpen = false;
|
||||
} else {
|
||||
pause = true;
|
||||
menu->set("pause");
|
||||
}
|
||||
}
|
||||
if (visible && Events::jactive(BIND_HUD_INVENTORY)) {
|
||||
if (!pause) {
|
||||
inventoryOpen = !inventoryOpen;
|
||||
}
|
||||
}
|
||||
if ((pause || inventoryOpen) == Events::_cursor_locked) {
|
||||
Events::toggleCursor();
|
||||
}
|
||||
|
||||
glm::vec2 invSize = contentAccessPanel->size();
|
||||
inventoryView->visible(inventoryOpen);
|
||||
@@ -365,10 +365,10 @@ void HudRenderer::update(bool visible) {
|
||||
}
|
||||
|
||||
void HudRenderer::drawOverlay(const GfxContext& ctx) {
|
||||
if (pause) {
|
||||
Shader* uishader = assets->getShader("ui");
|
||||
uishader->use();
|
||||
uishader->uniformMatrix("u_projview", uicamera->getProjView());
|
||||
if (pause) {
|
||||
Shader* uishader = assets->getShader("ui");
|
||||
uishader->use();
|
||||
uishader->uniformMatrix("u_projview", uicamera->getProjView());
|
||||
|
||||
const Viewport& viewport = ctx.getViewport();
|
||||
const uint width = viewport.getWidth();
|
||||
@@ -376,48 +376,48 @@ void HudRenderer::drawOverlay(const GfxContext& ctx) {
|
||||
auto batch = ctx.getBatch2D();
|
||||
batch->begin();
|
||||
|
||||
// draw fullscreen dark overlay
|
||||
batch->texture(nullptr);
|
||||
batch->color = vec4(0.0f, 0.0f, 0.0f, 0.5f);
|
||||
batch->rect(0, 0, width, height);
|
||||
// draw fullscreen dark overlay
|
||||
batch->texture(nullptr);
|
||||
batch->color = vec4(0.0f, 0.0f, 0.0f, 0.5f);
|
||||
batch->rect(0, 0, width, height);
|
||||
batch->render();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void HudRenderer::draw(const GfxContext& ctx){
|
||||
auto level = frontend->getLevel();
|
||||
|
||||
const Viewport& viewport = ctx.getViewport();
|
||||
const uint width = viewport.getWidth();
|
||||
const uint height = viewport.getHeight();
|
||||
const Viewport& viewport = ctx.getViewport();
|
||||
const uint width = viewport.getWidth();
|
||||
const uint height = viewport.getHeight();
|
||||
|
||||
Player* player = level->player;
|
||||
debugPanel->visible(player->debug);
|
||||
Player* player = level->player;
|
||||
debugPanel->visible(player->debug);
|
||||
|
||||
uicamera->setFov(height);
|
||||
uicamera->setFov(height);
|
||||
|
||||
auto batch = ctx.getBatch2D();
|
||||
batch->begin();
|
||||
batch->begin();
|
||||
|
||||
Shader* uishader = assets->getShader("ui");
|
||||
uishader->use();
|
||||
uishader->uniformMatrix("u_projview", uicamera->getProjView());
|
||||
|
||||
// Draw selected item preview
|
||||
Shader* uishader = assets->getShader("ui");
|
||||
uishader->use();
|
||||
uishader->uniformMatrix("u_projview", uicamera->getProjView());
|
||||
|
||||
// Draw selected item preview
|
||||
hotbarView->setCoord(glm::vec2(width/2, height-65));
|
||||
hotbarView->setSelected(player->getChosenSlot());
|
||||
|
||||
// Crosshair
|
||||
batch->begin();
|
||||
if (!pause && Events::_cursor_locked && !level->player->debug) {
|
||||
batch->lineWidth(2);
|
||||
batch->line(width/2, height/2-6, width/2, height/2+6, 0.2f, 0.2f, 0.2f, 1.0f);
|
||||
batch->line(width/2+6, height/2, width/2-6, height/2, 0.2f, 0.2f, 0.2f, 1.0f);
|
||||
batch->line(width/2-5, height/2-5, width/2+5, height/2+5, 0.9f, 0.9f, 0.9f, 1.0f);
|
||||
batch->line(width/2+5, height/2-5, width/2-5, height/2+5, 0.9f, 0.9f, 0.9f, 1.0f);
|
||||
}
|
||||
// Crosshair
|
||||
batch->begin();
|
||||
if (!pause && Events::_cursor_locked && !level->player->debug) {
|
||||
batch->lineWidth(2);
|
||||
batch->line(width/2, height/2-6, width/2, height/2+6, 0.2f, 0.2f, 0.2f, 1.0f);
|
||||
batch->line(width/2+6, height/2, width/2-6, height/2, 0.2f, 0.2f, 0.2f, 1.0f);
|
||||
batch->line(width/2-5, height/2-5, width/2+5, height/2+5, 0.9f, 0.9f, 0.9f, 1.0f);
|
||||
batch->line(width/2+5, height/2-5, width/2-5, height/2+5, 0.9f, 0.9f, 0.9f, 1.0f);
|
||||
}
|
||||
|
||||
if (inventoryOpen) {
|
||||
if (inventoryOpen) {
|
||||
auto caLayout = contentAccess->getLayout();
|
||||
auto invLayout = inventoryView->getLayout();
|
||||
float caWidth = caLayout->getSize().x;
|
||||
@@ -430,15 +430,15 @@ void HudRenderer::draw(const GfxContext& ctx){
|
||||
height/2-invSize.y/2
|
||||
));
|
||||
contentAccessPanel->setCoord(glm::vec2(width-caWidth, 0));
|
||||
}
|
||||
}
|
||||
grabbedItemView->setCoord(glm::vec2(Events::cursor));
|
||||
batch->render();
|
||||
batch->render();
|
||||
}
|
||||
|
||||
bool HudRenderer::isInventoryOpen() const {
|
||||
return inventoryOpen;
|
||||
return inventoryOpen;
|
||||
}
|
||||
|
||||
bool HudRenderer::isPause() const {
|
||||
return pause;
|
||||
return pause;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user