Add multiple GuiInventoryList callbacks, fix crash on resize.

master
Nicole Collings 2020-04-17 17:48:24 -07:00
parent 0359ada6b3
commit 83287cebf1
7 changed files with 37 additions and 22 deletions

View File

@ -34,11 +34,8 @@ void GameGui::update(double delta) {
void GameGui::winResized(glm::ivec2 win) {
this->win = win;
hudRoot->get<GuiRect>("crosshair")->setPos({win.x / 2 - 11, win.y / 2 - 9});
hudRoot->get<GuiRect>("viginette")->setScale(win);
menuBuilder.build(win);
hudBuilder.build(win);
}
void GameGui::showMenu(std::shared_ptr<LuaGuiElement> root) {

View File

@ -5,8 +5,9 @@
#include "GuiInventoryList.h"
#include "../basic/GuiInventoryItem.h"
#include "../../../inventory/LocalInventoryList.cpp"
#include "../../../../def/texture/Font.h"
#include "../../../inventory/LocalInventoryList.cpp"
GuiInventoryList::GuiInventoryList(const std::string &key) : GuiContainer(key) {}
std::shared_ptr<GuiInventoryList> GuiInventoryList::fromSerialized(const LuaGuiElement& elem, ClientGame &game,
@ -46,7 +47,8 @@ void GuiInventoryList::create(glm::vec2 scale, glm::vec4 padding, glm::ivec2 inn
});
drawContents();
list->setGuiCallback(std::bind(&GuiInventoryList::drawContents, this));
myCallback = std::make_shared<std::function<void()>>(std::bind(&GuiInventoryList::drawContents, this));
list->addGuiCallback(myCallback);
setCallback(CallbackType::PRIMARY, nullptr);
setCallback(CallbackType::SECONDARY, nullptr);
@ -146,5 +148,5 @@ void GuiInventoryList::drawContents() {
}
GuiInventoryList::~GuiInventoryList() {
if (list != nullptr) list->setGuiCallback(nullptr);
if (list != nullptr) list->removeGuiCallback(myCallback);
}

View File

@ -33,6 +33,7 @@ public:
void drawContents();
private:
std::shared_ptr<GuiRect> hoverRect = std::make_shared<GuiRect>("hover_rect");
std::shared_ptr<std::function<void()>> myCallback = nullptr;
std::shared_ptr<LocalInventoryList> list, hand;
glm::ivec2 innerPadding;

View File

@ -28,10 +28,22 @@ void LocalInventoryList::setData(unsigned int size, unsigned int width, std::vec
manipulated();
}
void LocalInventoryList::setGuiCallback(std::function<void()> cb) {
this->guiCallback = cb;
void LocalInventoryList::addGuiCallback(std::shared_ptr<std::function<void()>> cb) {
guiCallbacks.push_back(cb);
}
void LocalInventoryList::removeGuiCallback(std::shared_ptr<std::function<void()>> cb) {
for (auto it = guiCallbacks.begin(); it != guiCallbacks.end();) {
if (cb == (*it)) {
guiCallbacks.erase(it);
return;
}
it++;
}
std::cout << "Failed to remove callback" << std::endl;
}
void LocalInventoryList::manipulated() {
if (guiCallback != nullptr) guiCallback();
for (auto& cb : guiCallbacks) (*cb)();
}

View File

@ -4,6 +4,8 @@
#pragma once
#include <list>
#include "InventoryList.h"
class LocalInventoryList : public InventoryList {
@ -16,11 +18,13 @@ public:
void secondaryInteract(InventoryList& hand, unsigned short ind) override;
void setData(unsigned int size, unsigned int width, std::vector<ItemStack> stacks);
void setGuiCallback(std::function<void()> cb);
void addGuiCallback(std::shared_ptr<std::function<void()>> cb);
void removeGuiCallback(std::shared_ptr<std::function<void()>> cb);
private:
void manipulated() override;
std::function<void()> guiCallback = nullptr;
std::function<void(unsigned short)> primaryCallback = nullptr;
std::function<void(unsigned short)> secondaryCallback = nullptr;
std::list<std::shared_ptr<std::function<void()>>> guiCallbacks {};
};

View File

@ -17,13 +17,13 @@ root(function(e)
size = { 68, 68 },
background = "@aurailus:hot_wheel:hot_wheel_circle",
-- Gui.InventoryList {
-- position = { 7, 1 },
-- slot_spacing = { 2, 2 },
-- length = 1,
-- source = "current_player",
-- list = "main",
-- }
Gui.InventoryList {
position = { 7, 1 },
slot_spacing = { 2, 2 },
length = 1,
source = "current_player",
list = "main",
}
})
e:append(Gui.Rect {

View File

@ -17,9 +17,8 @@ zepha.register_block("@aurailus:tnt:tnt", {
for i = -amp, amp {
for j = -amp, amp {
for k = -amp, amp {
local offset = v(i, j, k)
if (vector.distance(v(), offset) < amp) {
zepha.set_block(vector.add(pos, offset), "air")
if (V(i, j, k):distance(V()) < amp) {
zepha.set_block(pos + offset, "air")
}
}
}