| 23 | namespace Star { |
| 24 | |
| 25 | ContainerPane::ContainerPane(WorldClientPtr worldClient, PlayerPtr player, ContainerInteractorPtr containerInteractor) { |
| 26 | m_worldClient = worldClient; |
| 27 | m_player = player; |
| 28 | m_containerInteractor = std::move(containerInteractor); |
| 29 | |
| 30 | auto container = m_containerInteractor->openContainer(); |
| 31 | auto guiConfig = container->containerGuiConfig(); |
| 32 | |
| 33 | if (auto scripts = guiConfig.opt("scripts").apply(jsonToStringList)) { |
| 34 | if (!m_script) { |
| 35 | m_script.emplace(); |
| 36 | m_script->setScripts(*scripts); |
| 37 | } |
| 38 | m_script->addCallbacks("widget", LuaBindings::makeWidgetCallbacks(this)); |
| 39 | m_script->addCallbacks("config", LuaBindings::makeConfigCallbacks( [guiConfig](String const& name, Json const& def) { |
| 40 | return guiConfig.query(name, def); |
| 41 | })); |
| 42 | m_script->addCallbacks("player", LuaBindings::makePlayerCallbacks(m_player.get())); |
| 43 | m_script->addCallbacks("status", LuaBindings::makeStatusControllerCallbacks(m_player->statusController())); |
| 44 | |
| 45 | LuaCallbacks containerPaneCallbacks; |
| 46 | containerPaneCallbacks.registerCallback("containerEntityId", [this]() -> Maybe<EntityId> { |
| 47 | return m_containerInteractor->openContainerId(); |
| 48 | }); |
| 49 | containerPaneCallbacks.registerCallback("playerEntityId", [this]() { return m_player->entityId(); }); |
| 50 | containerPaneCallbacks.registerCallback("dismiss", [this]() { dismiss(); }); |
| 51 | m_script->addCallbacks("pane", containerPaneCallbacks); |
| 52 | |
| 53 | m_script->setUpdateDelta(guiConfig.getUInt("scriptDelta", 1)); |
| 54 | } |
| 55 | |
| 56 | auto rightClickCallback = [this](size_t index) { |
| 57 | if (m_expectingSwap != ExpectingSwap::None) |
| 58 | return; |
| 59 | |
| 60 | if (ItemPtr slotItem = m_containerInteractor->openContainer()->itemBag()->at(index)) { |
| 61 | auto swapItem = m_player->inventory()->swapSlotItem(); |
| 62 | if (!swapItem || swapItem->empty() || swapItem->couldStack(slotItem)) { |
| 63 | size_t count = swapItem ? swapItem->couldStack(slotItem) : slotItem->maxStack(); |
| 64 | if (context()->shiftHeld()) |
| 65 | count = max<uint64_t>(1, min<uint64_t>(count, slotItem->count() / 2)); |
| 66 | else |
| 67 | count = 1; |
| 68 | |
| 69 | m_containerInteractor->takeFromContainerSlot(index, count); |
| 70 | m_expectingSwap = ExpectingSwap::SwapSlotStack; |
| 71 | } else if (is<AugmentItem>(swapItem)) { |
| 72 | m_containerInteractor->applyAugmentInContainer(index, swapItem); |
| 73 | m_player->inventory()->setSwapSlotItem({}); |
| 74 | m_expectingSwap = ExpectingSwap::SwapSlot; |
| 75 | } |
| 76 | } |
| 77 | }; |
| 78 | |
| 79 | m_reader.registerCallback("close", [this](Widget*) { dismiss(); }); |
| 80 | m_reader.registerCallback("itemGrid", [=](Widget* paneObj) { |
| 81 | if (auto itemGrid = as<ItemGridWidget>(paneObj)) |
| 82 | swapSlot(itemGrid); |
nothing calls this directly
no test coverage detected