|
| 1 | +#include "df/viewscreen_titlest.h" |
| 2 | + |
| 3 | +#include "modules/Gui.h" |
| 4 | + |
| 5 | +#include "PluginManager.h" |
| 6 | +#include "VTableInterpose.h" |
| 7 | +#include "uicommon.h" |
| 8 | + |
| 9 | +using namespace DFHack; |
| 10 | + |
| 11 | +DFHACK_PLUGIN("overlay"); |
| 12 | +DFHACK_PLUGIN_IS_ENABLED(is_enabled); |
| 13 | +REQUIRE_GLOBAL(enabler); |
| 14 | +REQUIRE_GLOBAL(gps); |
| 15 | + |
| 16 | +static const std::string button_text = "[ DFHack Launcher ]"; |
| 17 | +static bool clicked = false; |
| 18 | + |
| 19 | +static bool handle_click() { |
| 20 | + int32_t x, y; |
| 21 | + if (!enabler->tracking_on || !enabler->mouse_lbut || clicked || |
| 22 | + !Gui::getMousePos(x, y)) |
| 23 | + return false; |
| 24 | + if (y == gps->dimy - 1 && x >= 1 && (size_t)x <= button_text.size()) { |
| 25 | + clicked = true; |
| 26 | + Core::getInstance().setHotkeyCmd("gui/launcher"); |
| 27 | + return true; |
| 28 | + } |
| 29 | + return false; |
| 30 | +} |
| 31 | + |
| 32 | +static void draw_widgets() { |
| 33 | + int x = 1; |
| 34 | + int y = gps->dimy - 1; |
| 35 | + OutputString(COLOR_GREEN, x, y, button_text); |
| 36 | +} |
| 37 | + |
| 38 | +struct title_hook : df::viewscreen_titlest { |
| 39 | + typedef df::viewscreen_titlest interpose_base; |
| 40 | + |
| 41 | + DEFINE_VMETHOD_INTERPOSE(void, feed, (set<df::interface_key> *input)) { |
| 42 | + if (!handle_click()) |
| 43 | + INTERPOSE_NEXT(feed)(input); |
| 44 | + } |
| 45 | + DEFINE_VMETHOD_INTERPOSE(void, render, ()) { |
| 46 | + INTERPOSE_NEXT(render)(); |
| 47 | + draw_widgets(); |
| 48 | + } |
| 49 | +}; |
| 50 | +IMPLEMENT_VMETHOD_INTERPOSE(title_hook, feed); |
| 51 | +IMPLEMENT_VMETHOD_INTERPOSE(title_hook, render); |
| 52 | + |
| 53 | +DFhackCExport command_result plugin_onstatechange(color_ostream &, |
| 54 | + state_change_event evt) { |
| 55 | + if (evt == SC_VIEWSCREEN_CHANGED) { |
| 56 | + clicked = false; |
| 57 | + } |
| 58 | + return CR_OK; |
| 59 | +} |
| 60 | + |
| 61 | +DFhackCExport command_result plugin_enable(color_ostream &, bool enable) { |
| 62 | + if (is_enabled == enable) |
| 63 | + return CR_OK; |
| 64 | + if (enable != is_enabled) { |
| 65 | + if (!INTERPOSE_HOOK(title_hook, feed).apply(enable) || |
| 66 | + !INTERPOSE_HOOK(title_hook, render).apply(enable)) |
| 67 | + return CR_FAILURE; |
| 68 | + |
| 69 | + is_enabled = enable; |
| 70 | + } |
| 71 | + return CR_OK; |
| 72 | +} |
| 73 | + |
| 74 | +DFhackCExport command_result plugin_init(color_ostream &out, std::vector <PluginCommand> &) { |
| 75 | + return plugin_enable(out, true); |
| 76 | +} |
| 77 | + |
| 78 | +DFhackCExport command_result plugin_shutdown(color_ostream &out) { |
| 79 | + return plugin_enable(out, false); |
| 80 | +} |
0 commit comments