ui callbacks update

This commit is contained in:
MihailRis 2024-02-12 19:59:58 +03:00
parent e9cbb04090
commit 2de5f5c646
7 changed files with 62 additions and 16 deletions

View File

@ -1,4 +1,3 @@
<inventory color="#1F1F1FE0" size="400,0">
<slots-grid cols="4" count="40"/>
<button padding="10,10,10,10" coord="300,10">Test</button>
<slots-grid rows="4" count="40" sharefunc="inventory_share_func"/>
</inventory>

View File

@ -5,3 +5,7 @@ end
function on_close(inv)
print("CLOSE", inv)
end
function inventory_share_func(invid, slotid)
inventory.set(invid, slotid, 0, 0)
end

View File

@ -23,6 +23,7 @@
#include "../frontend/gui/controls.h"
#include "../util/stringutil.h"
#include "../world/Level.h"
#include "../logic/scripting/scripting.h"
SlotLayout::SlotLayout(
int index,
@ -204,7 +205,7 @@ void SlotView::clicked(gui::GUI* gui, int button) {
if (button == mousecode::BUTTON_1) {
if (Events::pressed(keycode::LEFT_SHIFT)) {
if (layout.shareFunc) {
layout.shareFunc(stack);
layout.shareFunc(layout.index, stack);
}
return;
}
@ -344,6 +345,17 @@ void InventoryView::setInventory(std::shared_ptr<Inventory> inventory) {
#include "../coders/xml.h"
#include "gui/gui_xml.h"
static itemsharefunc readShareFunc(InventoryView* view, gui::UiXmlReader& reader, xml::xmlelement& element) {
auto consumer = scripting::create_int_array_consumer(
reader.getEnvironment().getId(),
element->attr("sharefunc").getText()
);
return [=](uint slot, ItemStack& stack) {
int args[] {int(view->getInventory()->getId()), int(slot)};
consumer(args, 2);
};
}
static void readSlot(InventoryView* view, gui::UiXmlReader& reader, xml::xmlelement element) {
int index = element->attr("index", "0").asInt();
bool itemSource = element->attr("item-source", "false").asBool();
@ -351,6 +363,9 @@ static void readSlot(InventoryView* view, gui::UiXmlReader& reader, xml::xmlelem
if (element->has("coord")) {
layout.position = element->attr("coord").asVec2();
}
if (element->has("sharefunc")) {
layout.shareFunc = readShareFunc(view, reader, element);
}
auto slot = view->addSlot(layout);
reader.readUINode(reader, element, *slot);
view->add(slot);
@ -382,6 +397,9 @@ static void readSlotsGrid(InventoryView* view, gui::UiXmlReader& reader, xml::xm
if (element->has("coord")) {
layout.position = element->attr("coord").asVec2();
}
if (element->has("sharefunc")) {
layout.shareFunc = readShareFunc(view, reader, element);
}
layout.padding = padding;
glm::vec2 size (

View File

@ -26,7 +26,7 @@ namespace scripting {
class Environment;
}
using itemsharefunc = std::function<void(ItemStack&)>;
using itemsharefunc = std::function<void(uint, ItemStack&)>;
using slotcallback = std::function<void(ItemStack&, ItemStack&)>;
class InventoryInteraction {

View File

@ -182,7 +182,7 @@ std::shared_ptr<InventoryView> HudRenderer::createContentAccess() {
}
SlotLayout slotLayout(-1, glm::vec2(), false, true,
[=](ItemStack& item) {
[=](uint, ItemStack& item) {
auto copy = ItemStack(item);
inventory->move(copy, indices);
},

View File

@ -74,20 +74,37 @@ wstringconsumer scripting::create_wstring_consumer(
const std::string& src,
const std::string& file
) {
try {
if (state->eval(env, src, file) == 0)
return [](const std::wstring& _) {};
} catch (const lua::luaerror& err) {
std::cerr << err.what() << std::endl;
return [](const std::wstring& _) {};
}
auto funcName = state->storeAnonymous();
return [=](const std::wstring& x){
if (state->getglobal(funcName)) {
state->pushstring(util::wstr2str_utf8(x));
state->callNoThrow(1);
try {
if (state->eval(env, src, file) == 0)
return;
} catch (lua::luaerror err) {
std::cerr << err.what() << std::endl;
return;
}
state->pushstring(util::wstr2str_utf8(x));
state->callNoThrow(1);
};
}
int_array_consumer scripting::create_int_array_consumer(
int env,
const std::string& src,
const std::string& file
) {
return [=](const int arr[], size_t len){
try {
if (state->eval(env, src, file) == 0)
return;
} catch (lua::luaerror err) {
std::cerr << err.what() << std::endl;
return;
}
for (uint i = 0; i < len; i++) {
state->pushinteger(arr[i]);
}
state->callNoThrow(len);
};
}

View File

@ -23,6 +23,8 @@ struct uidocscript;
class BlocksController;
namespace scripting {
using int_array_consumer = std::function<void(const int[], size_t)>;
extern Engine* engine;
extern const Content* content;
extern const ContentIndices* indices;
@ -53,6 +55,12 @@ namespace scripting {
const std::string& file="<string>"
);
int_array_consumer create_int_array_consumer(
int env,
const std::string& src,
const std::string& file="<string>"
);
std::unique_ptr<Environment> create_environment(int parent=0);
std::unique_ptr<Environment> create_pack_environment(const ContentPack& pack);