pane lua stuff

This commit is contained in:
Kae 2023-07-04 22:36:27 +10:00
parent 5df9adcd0e
commit c923f4f13b
20 changed files with 205 additions and 117 deletions

View File

@ -526,6 +526,10 @@ public:
template <typename Return, typename... Args, typename Function>
LuaFunction createFunctionWithSignature(Function&& func);
LuaFunction createWrappedFunction(LuaDetail::LuaWrappedFunction function);
LuaFunction createRawFunction(lua_CFunction func);
LuaThread createThread();
template <typename T>
@ -639,9 +643,6 @@ private:
int placeHandle();
LuaFunction createWrappedFunction(LuaDetail::LuaWrappedFunction function);
LuaFunction createRawFunction(lua_CFunction func);
void pushLuaValue(lua_State* state, LuaValue const& luaValue);
LuaValue popLuaValue(lua_State* state);

View File

@ -39,4 +39,16 @@ Maybe<Color> LuaConverter<Color>::to(LuaEngine& engine, LuaValue const& v) {
return {};
}
LuaValue LuaConverter<LuaCallbacks>::from(LuaEngine& engine, LuaCallbacks const& c) {
auto table = engine.createTable(0, c.callbacks().size());
for (auto& callback : c.callbacks())
table.set(callback.first, engine.createWrappedFunction(callback.second));
return table;
}
Maybe<LuaCallbacks> LuaConverter<LuaCallbacks>::to(LuaEngine& engine, LuaValue const& v) {
return {};
}
}

View File

@ -259,6 +259,12 @@ struct LuaConverter<Color> {
static Maybe<Color> to(LuaEngine& engine, LuaValue const& v);
};
template <>
struct LuaConverter<LuaCallbacks> {
static LuaValue from(LuaEngine& engine, LuaCallbacks const& c);
static Maybe<LuaCallbacks> to(LuaEngine& engine, LuaValue const& v);
};
}
#endif

View File

@ -55,7 +55,6 @@ SET (star_frontend_HEADERS
StarSongbookInterface.hpp
StarStatusPane.hpp
StarTeleportDialog.hpp
StarWidgetLuaBindings.hpp
StarWireInterface.hpp
)
@ -104,7 +103,6 @@ SET (star_frontend_SOURCES
StarSongbookInterface.cpp
StarStatusPane.cpp
StarTeleportDialog.cpp
StarWidgetLuaBindings.cpp
StarWireInterface.cpp
)

View File

@ -24,15 +24,17 @@ BaseScriptPane::BaseScriptPane(Json config) : Pane() {
} else {
m_config = assets->fetchJson(config);
}
m_reader.registerCallback("close", [this](Widget*) { dismiss(); });
m_reader = make_shared<GuiReader>();
m_reader->registerCallback("close", [this](Widget*) { dismiss(); });
for (auto const& callbackName : jsonToStringList(m_config.get("scriptWidgetCallbacks", JsonArray{}))) {
m_reader.registerCallback(callbackName, [this, callbackName](Widget* widget) {
m_reader->registerCallback(callbackName, [this, callbackName](Widget* widget) {
m_script.invoke(callbackName, widget->name(), widget->data());
});
}
m_reader.construct(assets->fetchJson(m_config.get("gui")), this);
m_reader->construct(assets->fetchJson(m_config.get("gui")), this);
for (auto pair : m_config.getObject("canvasClickCallbacks", {}))
m_canvasClickCallbacks.set(findChild<CanvasWidget>(pair.first), pair.second.toString());
@ -53,7 +55,7 @@ void BaseScriptPane::displayed() {
Pane::displayed();
if (!m_callbacksAdded) {
m_script.addCallbacks("pane", makePaneCallbacks());
m_script.addCallbacks("widget", LuaBindings::makeWidgetCallbacks(this, &m_reader));
m_script.addCallbacks("widget", LuaBindings::makeWidgetCallbacks(this, m_reader));
m_script.addCallbacks("config", LuaBindings::makeConfigCallbacks( [this](String const& name, Json const& def) {
return m_config.query(name, def);
}));
@ -81,10 +83,6 @@ void BaseScriptPane::tick() {
m_script.invoke(p.second, (int)keyEvent.key, keyEvent.keyDown);
}
m_playingSounds.filter([](pair<String, AudioInstancePtr> const& p) {
return p.second->finished() == false;
});
m_script.update(m_script.updateDt());
}
@ -106,7 +104,7 @@ PanePtr BaseScriptPane::createTooltip(Vec2I const& screenPosition) {
return SimpleTooltipBuilder::buildTooltip(result->toString());
} else {
PanePtr tooltip = make_shared<Pane>();
m_reader.construct(*result, tooltip.get());
m_reader->construct(*result, tooltip.get());
return tooltip;
}
} else {
@ -131,53 +129,8 @@ Maybe<String> BaseScriptPane::cursorOverride(Vec2I const& screenPosition) {
return {};
}
LuaCallbacks BaseScriptPane::makePaneCallbacks() {
LuaCallbacks callbacks;
callbacks.registerCallback("dismiss", [this]() { dismiss(); });
callbacks.registerCallback("playSound",
[this](String const& audio, Maybe<int> loops, Maybe<float> volume) {
auto assets = Root::singleton().assets();
auto config = Root::singleton().configuration();
auto audioInstance = make_shared<AudioInstance>(*assets->audio(audio));
audioInstance->setVolume(volume.value(1.0));
audioInstance->setLoops(loops.value(0));
auto& guiContext = GuiContext::singleton();
guiContext.playAudio(audioInstance);
m_playingSounds.append({audio, move(audioInstance)});
});
callbacks.registerCallback("stopAllSounds", [this](String const& audio) {
m_playingSounds.filter([audio](pair<String, AudioInstancePtr> const& p) {
if (p.first == audio) {
p.second->stop();
return false;
}
return true;
});
});
callbacks.registerCallback("setTitle", [this](String const& title, String const& subTitle) {
setTitleString(title, subTitle);
});
callbacks.registerCallback("setTitleIcon", [this](String const& image) {
if (auto icon = as<ImageWidget>(titleIcon()))
icon->setImage(image);
});
callbacks.registerCallback("addWidget", [this](Json const& newWidgetConfig, Maybe<String> const& newWidgetName) {
String name = newWidgetName.value(strf("{}", Random::randu64()));
WidgetPtr newWidget = m_reader.makeSingle(name, newWidgetConfig);
this->addChild(name, newWidget);
});
callbacks.registerCallback("removeWidget", [this](String const& widgetName) {
this->removeChild(widgetName);
});
return callbacks;
GuiReaderPtr BaseScriptPane::reader() {
return m_reader;
}
}

View File

@ -12,6 +12,8 @@ STAR_CLASS(BaseScriptPane);
// A more 'raw' script pane that doesn't depend on a world being present.
// Requires a derived class to provide a Lua root.
// Should maybe move into windowing?
class BaseScriptPane : public Pane {
public:
BaseScriptPane(Json config);
@ -27,17 +29,16 @@ public:
PanePtr createTooltip(Vec2I const& screenPosition) override;
Maybe<String> cursorOverride(Vec2I const& screenPosition) override;
protected:
virtual LuaCallbacks makePaneCallbacks();
virtual GuiReaderPtr reader();
Json m_config;
GuiReader m_reader;
GuiReaderPtr m_reader;
Map<CanvasWidgetPtr, String> m_canvasClickCallbacks;
Map<CanvasWidgetPtr, String> m_canvasKeyCallbacks;
bool m_callbacksAdded;
LuaUpdatableComponent<LuaBaseComponent> m_script;
List<pair<String, AudioInstancePtr>> m_playingSounds;
};
}

View File

@ -34,7 +34,7 @@ ContainerPane::ContainerPane(WorldClientPtr worldClient, PlayerPtr player, Conta
m_script.emplace();
m_script->setScripts(*scripts);
}
m_script->addCallbacks("widget", LuaBindings::makeWidgetCallbacks(this, &m_reader));
m_script->addCallbacks("widget", LuaBindings::makeWidgetCallbacks(this));
m_script->addCallbacks("config", LuaBindings::makeConfigCallbacks( [guiConfig](String const& name, Json const& def) {
return guiConfig.query(name, def);
}));

View File

@ -3,6 +3,7 @@
#include "StarJsonExtra.hpp"
#include "StarLuaGameConverters.hpp"
#include "StarMainInterface.hpp"
#include "StarGuiContext.hpp"
namespace Star {
@ -15,6 +16,17 @@ LuaCallbacks LuaBindings::makeInterfaceCallbacks(MainInterface* mainInterface) {
return {};
});
callbacks.registerCallback("bindRegisteredPane", [mainInterface](String const& registeredPaneName) -> Maybe<LuaCallbacks> {
if (auto pane = mainInterface->paneManager()->maybeRegisteredPane(MainInterfacePanesNames.getLeft(registeredPaneName)))
return pane->makePaneCallbacks();
return {};
});
callbacks.registerCallback("scale", [mainInterface]() -> int {
return GuiContext::singleton().interfaceScale();
});
return callbacks;
}

View File

@ -6,6 +6,32 @@
namespace Star {
EnumMap<MainInterfacePanes> const MainInterfacePanesNames{
{MainInterfacePanes::EscapeDialog, "EscapeDialog"},
{MainInterfacePanes::Inventory, "Inventory"},
{MainInterfacePanes::Codex, "Codex"},
{MainInterfacePanes::Cockpit, "Cockpit"},
{MainInterfacePanes::Tech, "Tech"},
{MainInterfacePanes::Songbook, "Songbook"},
{MainInterfacePanes::Ai, "Ai"},
{MainInterfacePanes::Popup, "Popup"},
{MainInterfacePanes::Confirmation, "Confirmation"},
{MainInterfacePanes::JoinRequest, "JoinRequest"},
{MainInterfacePanes::Options, "Options"},
{MainInterfacePanes::QuestLog, "QuestLog"},
{MainInterfacePanes::ActionBar, "ActionBar"},
{MainInterfacePanes::TeamBar, "TeamBar"},
{MainInterfacePanes::StatusPane, "StatusPane"},
{MainInterfacePanes::Chat, "Chat"},
{MainInterfacePanes::WireInterface, "WireInterface"},
{MainInterfacePanes::PlanetText, "PlanetText"},
{MainInterfacePanes::RadioMessagePopup, "RadioMessagePopup"},
{MainInterfacePanes::CraftingPlain, "CraftingPlain"},
{MainInterfacePanes::QuestTracker, "QuestTracker"},
{MainInterfacePanes::MmUpgrade, "MmUpgrade"},
{MainInterfacePanes::Collections, "Collections"}
};
MainInterfaceConfigPtr MainInterfaceConfig::loadFromAssets() {
auto& root = Root::singleton();
auto assets = root.assets();

View File

@ -37,6 +37,8 @@ enum class MainInterfacePanes {
Collections
};
extern EnumMap<MainInterfacePanes> const MainInterfacePanesNames;
typedef RegisteredPaneManager<MainInterfacePanes> MainInterfacePaneManager;
struct MainInterfaceConfig {

View File

@ -48,25 +48,10 @@ void ScriptPane::dismissed() {
}
void ScriptPane::tick() {
BaseScriptPane::tick();
if (m_sourceEntityId != NullEntityId && !m_client->worldClient()->playerCanReachEntity(m_sourceEntityId))
dismiss();
for (auto p : m_canvasClickCallbacks) {
for (auto const& clickEvent : p.first->pullClickEvents())
m_script.invoke(p.second, jsonFromVec2I(clickEvent.position), (uint8_t)clickEvent.button, clickEvent.buttonDown);
}
for (auto p : m_canvasKeyCallbacks) {
for (auto const& keyEvent : p.first->pullKeyEvents())
m_script.invoke(p.second, (int)keyEvent.key, keyEvent.keyDown);
}
m_playingSounds.filter([](pair<String, AudioInstancePtr> const& p) {
return p.second->finished() == false;
});
m_script.update(m_script.updateDt());
BaseScriptPane::tick();
}
PanePtr ScriptPane::createTooltip(Vec2I const& screenPosition) {
@ -76,7 +61,7 @@ PanePtr ScriptPane::createTooltip(Vec2I const& screenPosition) {
return SimpleTooltipBuilder::buildTooltip(result->toString());
} else {
PanePtr tooltip = make_shared<Pane>();
m_reader.construct(*result, tooltip.get());
m_reader->construct(*result, tooltip.get());
return tooltip;
}
} else {

View File

@ -22,9 +22,8 @@ public:
bool openWithInventory() const;
private:
LuaCallbacks makePaneCallbacks() override;
private:
UniverseClientPtr m_client;
EntityId m_sourceEntityId;
};

View File

@ -38,6 +38,7 @@ SET (star_windowing_HEADERS
StarTextBoxWidget.hpp
StarVerticalLayout.hpp
StarWidget.hpp
StarWidgetLuaBindings.hpp
StarWidgetParsing.hpp
)
@ -70,6 +71,7 @@ SET (star_windowing_SOURCES
StarTextBoxWidget.cpp
StarVerticalLayout.cpp
StarWidget.cpp
StarWidgetLuaBindings.cpp
StarWidgetParsing.cpp
)

View File

@ -6,6 +6,7 @@
namespace Star {
STAR_EXCEPTION(GUIBuilderException, StarException);
STAR_CLASS(GuiReader);
class GuiReader : public WidgetParser {
public:

View File

@ -6,7 +6,6 @@
namespace Star {
STAR_CLASS(GuiReader);
STAR_CLASS(ListWidget);
class ListWidget : public Widget {

View File

@ -2,6 +2,10 @@
#include "StarRoot.hpp"
#include "StarJsonExtra.hpp"
#include "StarAssets.hpp"
#include "StarWidgetLuaBindings.hpp"
#include "StarLuaConverters.hpp"
#include "StarImageWidget.hpp"
#include "StarGuiReader.hpp"
namespace Star {
@ -187,35 +191,6 @@ Pane* Pane::window() {
return this;
}
void Pane::renderImpl() {
if (m_bgFooter != "")
m_context->drawInterfaceQuad(m_bgFooter, Vec2F(position()));
if (m_bgBody != "")
m_context->drawInterfaceQuad(m_bgBody, Vec2F(position()) + Vec2F(0, m_footerSize[1]));
if (m_bgHeader != "") {
auto headerPos = Vec2F(position()) + Vec2F(0, m_footerSize[1] + m_bodySize[1]);
m_context->drawInterfaceQuad(m_bgHeader, headerPos);
if (m_icon) {
m_icon->setPosition(Vec2I(0, m_footerSize[1] + m_bodySize[1]) + m_iconOffset);
m_icon->render(m_drawingArea);
m_context->resetInterfaceScissorRect();
}
m_context->setFont(m_font);
m_context->setFontSize(m_fontSize);
m_context->setFontColor(m_titleColor.toRgba());
m_context->setFontMode(FontMode::Shadow);
m_context->renderInterfaceText(m_title, {headerPos + Vec2F(m_titleOffset)});
m_context->setFontColor(m_subTitleColor.toRgba());
m_context->renderInterfaceText(m_subTitle, {headerPos + Vec2F(m_subTitleOffset)});
m_context->setFontMode(FontMode::Normal);
m_context->setDefaultFont();
}
}
void Pane::update() {
if (m_visible) {
for (auto const& widget : m_members) {
@ -228,7 +203,11 @@ void Pane::update() {
}
}
void Pane::tick() {}
void Pane::tick() {
m_playingSounds.filter([](pair<String, AudioInstancePtr> const& p) {
return p.second->finished() == false;
});
}
bool Pane::dragActive() const {
return m_dragActive;
@ -369,4 +348,96 @@ Maybe<String> Pane::cursorOverride(Vec2I const&) {
return {};
}
LuaCallbacks Pane::makePaneCallbacks() {
LuaCallbacks callbacks;
callbacks.registerCallback("toWidget", [this]() -> LuaCallbacks {
return LuaBindings::makeWidgetCallbacks(this, reader());
});
callbacks.registerCallback("dismiss", [this]() { dismiss(); });
callbacks.registerCallback("playSound",
[this](String const& audio, Maybe<int> loops, Maybe<float> volume) {
auto assets = Root::singleton().assets();
auto config = Root::singleton().configuration();
auto audioInstance = make_shared<AudioInstance>(*assets->audio(audio));
audioInstance->setVolume(volume.value(1.0));
audioInstance->setLoops(loops.value(0));
auto& guiContext = GuiContext::singleton();
guiContext.playAudio(audioInstance);
m_playingSounds.append({audio, move(audioInstance)});
});
callbacks.registerCallback("stopAllSounds", [this](Maybe<String> const& audio) {
m_playingSounds.filter([audio](pair<String, AudioInstancePtr> const& p) {
if (!audio || p.first == *audio) {
p.second->stop();
return false;
}
return true;
});
});
callbacks.registerCallback("setTitle", [this](String const& title, String const& subTitle) {
setTitleString(title, subTitle);
});
callbacks.registerCallback("setTitleIcon", [this](String const& image) {
if (auto icon = as<ImageWidget>(titleIcon()))
icon->setImage(image);
});
callbacks.registerCallback("getPosition", [this]() -> Vec2I { return relativePosition(); });
callbacks.registerCallback("setPosition", [this](Vec2I const& position) { setPosition(position); });
callbacks.registerCallback("getSize", [this]() -> Vec2I { return size(); });
callbacks.registerCallback("setSize", [this](Vec2I const& size) { setSize(size); });
callbacks.registerCallback("addWidget", [this](Json const& newWidgetConfig, Maybe<String> const& newWidgetName) -> LuaCallbacks {
String name = newWidgetName.value(toString(Random::randu64()));
WidgetPtr newWidget = reader()->makeSingle(name, newWidgetConfig);
this->addChild(name, newWidget);
return LuaBindings::makeWidgetCallbacks(newWidget.get(), reader());
});
callbacks.registerCallback("removeWidget", [this](String const& widgetName) -> bool {
return this->removeChild(widgetName);
});
return callbacks;
}
GuiReaderPtr Pane::reader() {
return make_shared<GuiReader>();
}
void Pane::renderImpl() {
if (m_bgFooter != "")
m_context->drawInterfaceQuad(m_bgFooter, Vec2F(position()));
if (m_bgBody != "")
m_context->drawInterfaceQuad(m_bgBody, Vec2F(position()) + Vec2F(0, m_footerSize[1]));
if (m_bgHeader != "") {
auto headerPos = Vec2F(position()) + Vec2F(0, m_footerSize[1] + m_bodySize[1]);
m_context->drawInterfaceQuad(m_bgHeader, headerPos);
if (m_icon) {
m_icon->setPosition(Vec2I(0, m_footerSize[1] + m_bodySize[1]) + m_iconOffset);
m_icon->render(m_drawingArea);
m_context->resetInterfaceScissorRect();
}
m_context->setFont(m_font);
m_context->setFontSize(m_fontSize);
m_context->setFontColor(m_titleColor.toRgba());
m_context->setFontMode(FontMode::Shadow);
m_context->renderInterfaceText(m_title, {headerPos + Vec2F(m_titleOffset)});
m_context->setFontColor(m_subTitleColor.toRgba());
m_context->renderInterfaceText(m_subTitle, {headerPos + Vec2F(m_subTitleOffset)});
m_context->setFontMode(FontMode::Normal);
m_context->setDefaultFont();
}
}
}

View File

@ -7,6 +7,9 @@
namespace Star {
STAR_CLASS(Pane);
STAR_CLASS(LuaCallbacks);
STAR_CLASS(AudioInstance);
STAR_CLASS(GuiReader);
enum class PaneAnchor {
None,
@ -89,7 +92,9 @@ public:
virtual PanePtr createTooltip(Vec2I const& screenPosition);
virtual Maybe<String> cursorOverride(Vec2I const& screenPosition);
virtual LuaCallbacks makePaneCallbacks();
protected:
virtual GuiReaderPtr reader();
virtual void renderImpl();
String m_bgHeader;
@ -124,6 +129,8 @@ protected:
PaneAnchor m_anchor;
Vec2I m_anchorOffset;
bool m_hasDisplayed;
List<pair<String, AudioInstancePtr>> m_playingSounds;
};
}

View File

@ -19,6 +19,8 @@ public:
template <typename T = Pane>
shared_ptr<T> registeredPane(KeyT const& paneId) const;
template <typename T = Pane>
shared_ptr<T> maybeRegisteredPane(KeyT const& paneId) const;
// Displays a registred pane if it is not already displayed. Returns true
// if it is newly displayed.
@ -53,6 +55,14 @@ shared_ptr<T> RegisteredPaneManager<KeyT>::registeredPane(KeyT const& paneId) co
throw GuiException(strf("No pane named '{}' found in RegisteredPaneManager", outputAny(paneId)));
}
template <typename KeyT>
template <typename T>
shared_ptr<T> RegisteredPaneManager<KeyT>::maybeRegisteredPane(KeyT const& paneId) const {
if (auto v = m_registeredPanes.ptr(paneId))
return convert<T>(v->pane);
return {};
}
template <typename KeyT>
void RegisteredPaneManager<KeyT>::registerPane(
KeyT paneId, PaneLayer paneLayer, PanePtr pane, DismissCallback onDismiss) {

View File

@ -89,7 +89,10 @@ LuaMethods<CanvasWidgetPtr> LuaUserDataMethods<CanvasWidgetPtr>::make() {
return methods;
}
LuaCallbacks LuaBindings::makeWidgetCallbacks(Widget* parentWidget, GuiReader* reader) {
LuaCallbacks LuaBindings::makeWidgetCallbacks(Widget* parentWidget, GuiReaderPtr reader) {
if (!reader)
reader = make_shared<GuiReader>();
LuaCallbacks callbacks;
// a bit miscellaneous, but put this here since widgets have access to gui context

View File

@ -18,7 +18,7 @@ struct LuaUserDataMethods<CanvasWidgetPtr> {
};
namespace LuaBindings {
LuaCallbacks makeWidgetCallbacks(Widget* parentWidget, GuiReader* reader);
LuaCallbacks makeWidgetCallbacks(Widget* parentWidget, GuiReaderPtr reader = {});
}
}