osb/source/frontend/StarScriptPane.cpp

100 lines
3.1 KiB
C++
Raw Normal View History

2023-06-20 04:33:09 +00:00
#include "StarScriptPane.hpp"
#include "StarRoot.hpp"
#include "StarAssets.hpp"
#include "StarGuiReader.hpp"
#include "StarJsonExtra.hpp"
#include "StarConfigLuaBindings.hpp"
#include "StarPlayerLuaBindings.hpp"
#include "StarStatusControllerLuaBindings.hpp"
#include "StarCelestialLuaBindings.hpp"
#include "StarLuaGameConverters.hpp"
#include "StarWorldClient.hpp"
#include "StarPlayer.hpp"
#include "StarUniverseClient.hpp"
#include "StarWidgetLuaBindings.hpp"
#include "StarInterfaceLuaBindings.hpp"
2023-06-20 04:33:09 +00:00
#include "StarCanvasWidget.hpp"
#include "StarItemTooltip.hpp"
#include "StarItemGridWidget.hpp"
#include "StarSimpleTooltip.hpp"
#include "StarImageWidget.hpp"
namespace Star {
2023-07-02 07:19:54 +00:00
ScriptPane::ScriptPane(UniverseClientPtr client, Json config, EntityId sourceEntityId) : BaseScriptPane(config) {
2023-06-20 04:33:09 +00:00
auto& root = Root::singleton();
auto assets = root.assets();
m_client = std::move(client);
2023-06-20 04:33:09 +00:00
m_sourceEntityId = sourceEntityId;
m_script.addCallbacks("player", LuaBindings::makePlayerCallbacks(m_client->mainPlayer().get()));
m_script.addCallbacks("status", LuaBindings::makeStatusControllerCallbacks(m_client->mainPlayer()->statusController()));
m_script.addCallbacks("celestial", LuaBindings::makeCelestialCallbacks(m_client.get()));
}
void ScriptPane::displayed() {
auto world = m_client->worldClient();
2023-07-02 07:19:54 +00:00
if (world && world->inWorld()) {
m_script.setLuaRoot(world->luaRoot());
m_script.addCallbacks("world", LuaBindings::makeWorldCallbacks(world.get()));
}
BaseScriptPane::displayed();
2023-06-20 04:33:09 +00:00
}
void ScriptPane::dismissed() {
2023-07-02 07:19:54 +00:00
BaseScriptPane::dismissed();
m_script.removeCallbacks("world");
2023-06-20 04:33:09 +00:00
}
void ScriptPane::tick(float dt) {
2023-06-20 04:33:09 +00:00
if (m_sourceEntityId != NullEntityId && !m_client->worldClient()->playerCanReachEntity(m_sourceEntityId))
dismiss();
BaseScriptPane::tick(dt);
2023-06-20 04:33:09 +00:00
}
PanePtr ScriptPane::createTooltip(Vec2I const& screenPosition) {
auto result = m_script.invoke<Json>("createTooltip", screenPosition);
if (result && !result.value().isNull()) {
if (result->type() == Json::Type::String) {
return SimpleTooltipBuilder::buildTooltip(result->toString());
} else {
PanePtr tooltip = make_shared<Pane>();
2023-07-04 12:36:27 +00:00
m_reader->construct(*result, tooltip.get());
2023-06-20 04:33:09 +00:00
return tooltip;
}
} else {
ItemPtr item;
if (auto child = getChildAt(screenPosition)) {
if (auto itemSlot = as<ItemSlotWidget>(child))
item = itemSlot->item();
if (auto itemGrid = as<ItemGridWidget>(child))
item = itemGrid->itemAt(screenPosition);
}
if (item)
return ItemTooltipBuilder::buildItemTooltip(item, m_client->mainPlayer());
return {};
}
}
LuaCallbacks ScriptPane::makePaneCallbacks() {
2023-07-02 07:19:54 +00:00
LuaCallbacks callbacks = BaseScriptPane::makePaneCallbacks();
2023-06-20 04:33:09 +00:00
callbacks.registerCallback("sourceEntity", [this]() { return m_sourceEntityId; });
return callbacks;
}
bool ScriptPane::openWithInventory() const {
return m_config.getBool("openWithInventory", false);
}
bool ScriptPane::closeWithInventory() const {
return m_config.getBool("closeWithInventory", openWithInventory());
}
2023-07-28 16:12:03 +00:00
EntityId ScriptPane::sourceEntityId() const {
return m_sourceEntityId;
}
2023-06-20 04:33:09 +00:00
}