dfhack/plugins/overlay.cpp

194 lines
6.4 KiB
C++

2022-09-16 14:38:05 -06:00
#include "df/viewscreen_adopt_regionst.h"
2022-12-20 21:18:00 -07:00
#include "df/viewscreen_choose_game_typest.h"
2022-09-16 14:38:05 -06:00
#include "df/viewscreen_choose_start_sitest.h"
#include "df/viewscreen_dwarfmodest.h"
#include "df/viewscreen_export_regionst.h"
#include "df/viewscreen_game_cleanerst.h"
2022-12-20 21:18:00 -07:00
#include "df/viewscreen_initial_prepst.h"
2022-09-16 14:38:05 -06:00
#include "df/viewscreen_legendsst.h"
#include "df/viewscreen_loadgamest.h"
2022-10-31 10:36:54 -06:00
#include "df/viewscreen_new_regionst.h"
2022-09-16 14:38:05 -06:00
#include "df/viewscreen_savegamest.h"
#include "df/viewscreen_setupdwarfgamest.h"
2022-09-16 13:15:01 -06:00
#include "df/viewscreen_titlest.h"
2022-09-16 14:38:05 -06:00
#include "df/viewscreen_update_regionst.h"
2022-12-20 21:18:00 -07:00
#include "df/viewscreen_worldst.h"
#include "df/world.h"
2022-09-16 13:15:01 -06:00
2022-09-16 17:19:21 -06:00
#include "Debug.h"
2022-10-31 10:36:54 -06:00
#include "LuaTools.h"
2022-09-16 13:15:01 -06:00
#include "PluginManager.h"
#include "VTableInterpose.h"
2022-09-16 21:59:02 -06:00
#include "modules/Gui.h"
2022-10-31 13:22:00 -06:00
#include "modules/Screen.h"
2022-09-16 13:15:01 -06:00
using namespace DFHack;
DFHACK_PLUGIN("overlay");
DFHACK_PLUGIN_IS_ENABLED(is_enabled);
REQUIRE_GLOBAL(world);
2022-09-16 17:19:21 -06:00
namespace DFHack {
2022-10-31 10:36:54 -06:00
DBG_DECLARE(overlay, control, DebugCategory::LINFO);
DBG_DECLARE(overlay, event, DebugCategory::LINFO);
2022-09-16 13:15:01 -06:00
}
2022-10-31 13:22:00 -06:00
static df::coord2d screenSize;
static void call_overlay_lua(color_ostream *out, const char *fn_name,
int nargs = 0, int nres = 0,
Lua::LuaLambda && args_lambda = Lua::DEFAULT_LUA_LAMBDA,
Lua::LuaLambda && res_lambda = Lua::DEFAULT_LUA_LAMBDA) {
2022-10-31 13:22:00 -06:00
DEBUG(event).print("calling overlay lua function: '%s'\n", fn_name);
CoreSuspender guard;
auto L = Lua::Core::State;
Lua::StackUnwinder top(L);
2022-11-02 13:33:06 -06:00
if (!out)
out = &Core::getInstance().getConsole();
2022-10-31 13:22:00 -06:00
Lua::CallLuaModuleFunction(*out, L, "plugins.overlay", fn_name, nargs, nres,
std::forward<Lua::LuaLambda&&>(args_lambda),
std::forward<Lua::LuaLambda&&>(res_lambda));
2022-10-31 13:22:00 -06:00
}
2022-09-16 14:38:05 -06:00
template<class T>
struct viewscreen_overlay : T {
typedef T interpose_base;
2022-09-16 13:15:01 -06:00
2022-10-31 10:36:54 -06:00
DEFINE_VMETHOD_INTERPOSE(void, logic, ()) {
INTERPOSE_NEXT(logic)();
2022-11-02 13:33:06 -06:00
call_overlay_lua(NULL, "update_viewscreen_widgets", 2, 0,
[&](lua_State *L) {
Lua::Push(L, T::_identity.getName());
Lua::Push(L, this);
});
2022-10-31 10:36:54 -06:00
}
DEFINE_VMETHOD_INTERPOSE(void, feed, (std::set<df::interface_key> *input)) {
2022-10-31 13:22:00 -06:00
bool input_is_handled = false;
// don't send input to the overlays if there is a modal dialog up
if (!world->status.popups.size())
call_overlay_lua(NULL, "feed_viewscreen_widgets", 3, 1,
[&](lua_State *L) {
Lua::Push(L, T::_identity.getName());
Lua::Push(L, this);
Lua::PushInterfaceKeys(L, *input);
}, [&](lua_State *L) {
input_is_handled = lua_toboolean(L, -1);
});
2022-10-31 13:22:00 -06:00
if (!input_is_handled)
INTERPOSE_NEXT(feed)(input);
2022-09-16 13:15:01 -06:00
}
DEFINE_VMETHOD_INTERPOSE(void, render, ()) {
INTERPOSE_NEXT(render)();
2022-11-02 13:33:06 -06:00
call_overlay_lua(NULL, "render_viewscreen_widgets", 2, 0,
[&](lua_State *L) {
Lua::Push(L, T::_identity.getName());
Lua::Push(L, this);
});
2022-09-16 13:15:01 -06:00
}
};
2022-09-16 14:38:05 -06:00
#define IMPLEMENT_HOOKS(screen) \
typedef viewscreen_overlay<df::viewscreen_##screen##st> screen##_overlay; \
2022-10-31 10:36:54 -06:00
template<> IMPLEMENT_VMETHOD_INTERPOSE_PRIO(screen##_overlay, logic, 100); \
template<> IMPLEMENT_VMETHOD_INTERPOSE_PRIO(screen##_overlay, feed, 100); \
template<> IMPLEMENT_VMETHOD_INTERPOSE_PRIO(screen##_overlay, render, 100);
2022-09-16 14:38:05 -06:00
IMPLEMENT_HOOKS(adopt_region)
2022-12-20 21:18:00 -07:00
IMPLEMENT_HOOKS(choose_game_type)
IMPLEMENT_HOOKS(choose_start_site)
IMPLEMENT_HOOKS(dwarfmode)
IMPLEMENT_HOOKS(export_region)
IMPLEMENT_HOOKS(game_cleaner)
2022-12-20 21:18:00 -07:00
IMPLEMENT_HOOKS(initial_prep)
IMPLEMENT_HOOKS(legends)
IMPLEMENT_HOOKS(loadgame)
IMPLEMENT_HOOKS(new_region)
IMPLEMENT_HOOKS(savegame)
IMPLEMENT_HOOKS(setupdwarfgame)
IMPLEMENT_HOOKS(title)
IMPLEMENT_HOOKS(update_region)
2022-12-20 21:18:00 -07:00
IMPLEMENT_HOOKS(world)
2022-09-16 14:38:05 -06:00
#undef IMPLEMENT_HOOKS
2022-09-16 13:15:01 -06:00
2022-09-16 14:38:05 -06:00
#define INTERPOSE_HOOKS_FAILED(screen) \
2022-10-31 10:36:54 -06:00
!INTERPOSE_HOOK(screen##_overlay, logic).apply(enable) || \
2022-09-16 14:38:05 -06:00
!INTERPOSE_HOOK(screen##_overlay, feed).apply(enable) || \
!INTERPOSE_HOOK(screen##_overlay, render).apply(enable)
DFhackCExport command_result plugin_enable(color_ostream &out, bool enable) {
2022-09-16 13:15:01 -06:00
if (is_enabled == enable)
return CR_OK;
2022-09-16 14:38:05 -06:00
if (enable) {
screenSize = Screen::getWindowSize();
call_overlay_lua(&out, "reload");
}
2022-10-31 10:36:54 -06:00
DEBUG(control).print("%sing interpose hooks\n", enable ? "enabl" : "disabl");
2022-09-16 13:15:01 -06:00
if (INTERPOSE_HOOKS_FAILED(adopt_region) ||
INTERPOSE_HOOKS_FAILED(choose_start_site) ||
2022-12-20 21:18:00 -07:00
INTERPOSE_HOOKS_FAILED(choose_game_type) ||
INTERPOSE_HOOKS_FAILED(dwarfmode) ||
INTERPOSE_HOOKS_FAILED(export_region) ||
INTERPOSE_HOOKS_FAILED(game_cleaner) ||
2022-12-20 21:18:00 -07:00
INTERPOSE_HOOKS_FAILED(initial_prep) ||
INTERPOSE_HOOKS_FAILED(legends) ||
INTERPOSE_HOOKS_FAILED(loadgame) ||
INTERPOSE_HOOKS_FAILED(new_region) ||
INTERPOSE_HOOKS_FAILED(savegame) ||
INTERPOSE_HOOKS_FAILED(setupdwarfgame) ||
INTERPOSE_HOOKS_FAILED(title) ||
2022-12-20 21:18:00 -07:00
INTERPOSE_HOOKS_FAILED(update_region) ||
INTERPOSE_HOOKS_FAILED(world))
2022-10-31 10:36:54 -06:00
return CR_FAILURE;
is_enabled = enable;
2022-09-16 13:15:01 -06:00
return CR_OK;
}
2022-09-16 14:38:05 -06:00
#undef INTERPOSE_HOOKS_FAILED
2022-10-31 10:36:54 -06:00
static command_result overlay_cmd(color_ostream &out, std::vector <std::string> & parameters) {
2022-11-02 13:33:06 -06:00
bool show_help = false;
call_overlay_lua(&out, "overlay_command", 1, 1, [&](lua_State *L) {
Lua::PushVector(L, parameters);
}, [&](lua_State *L) {
show_help = !lua_toboolean(L, -1);
});
2022-10-31 10:36:54 -06:00
2022-11-02 13:33:06 -06:00
return show_help ? CR_WRONG_USAGE : CR_OK;
2022-10-31 10:36:54 -06:00
}
DFhackCExport command_result plugin_init(color_ostream &out, std::vector <PluginCommand> &commands) {
commands.push_back(
PluginCommand(
"overlay",
"Manage onscreen widgets.",
overlay_cmd,
Gui::anywhere_hotkey));
2022-10-31 10:36:54 -06:00
return CR_OK;
2022-09-16 13:15:01 -06:00
}
DFhackCExport command_result plugin_shutdown(color_ostream &out) {
return plugin_enable(out, false);
}
2022-10-31 10:36:54 -06:00
DFhackCExport command_result plugin_onupdate (color_ostream &out) {
2022-10-31 13:22:00 -06:00
df::coord2d newScreenSize = Screen::getWindowSize();
if (newScreenSize != screenSize) {
2022-11-02 13:33:06 -06:00
call_overlay_lua(&out, "reposition_widgets");
2022-10-31 13:22:00 -06:00
screenSize = newScreenSize;
}
2022-11-02 13:33:06 -06:00
call_overlay_lua(&out, "update_hotspot_widgets");
2022-10-31 10:36:54 -06:00
return CR_OK;
}