Skip to content

Commit ad7de34

Browse files
committed
add clickable overlay to title screen
1 parent b735720 commit ad7de34

2 files changed

Lines changed: 81 additions & 0 deletions

File tree

plugins/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ if(BUILD_SUPPORTED)
144144
dfhack_plugin(mousequery mousequery.cpp)
145145
dfhack_plugin(nestboxes nestboxes.cpp)
146146
dfhack_plugin(orders orders.cpp LINK_LIBRARIES jsoncpp_static)
147+
dfhack_plugin(overlay overlay.cpp)
147148
dfhack_plugin(pathable pathable.cpp LINK_LIBRARIES lua)
148149
dfhack_plugin(petcapRemover petcapRemover.cpp)
149150
dfhack_plugin(plants plants.cpp)

plugins/overlay.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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

Comments
 (0)