Stop embark-tools from crashing on unload

Also enable sand indicator and mouse controls by default and refactor
develop
lethosor 2015-07-27 21:38:53 -04:00
parent 7eb4c33cb1
commit 5ca5feb855
2 changed files with 47 additions and 32 deletions

@ -222,6 +222,9 @@ enable \
# allow the fortress bookkeeper to queue jobs through the manager # allow the fortress bookkeeper to queue jobs through the manager
enable stockflow enable stockflow
# enable mouse controls and sand indicator in embark screen
embark-tools enable sand mouse
########### ###########
# Scripts # # Scripts #
########### ###########

@ -8,6 +8,7 @@
#include "modules/Screen.h" #include "modules/Screen.h"
#include "modules/Gui.h" #include "modules/Gui.h"
#include <algorithm> #include <algorithm>
#include <map>
#include <set> #include <set>
#include <VTableInterpose.h> #include <VTableInterpose.h>
@ -85,7 +86,7 @@ public:
virtual void after_feed(start_sitest* screen, ikey_set* input) { }; virtual void after_feed(start_sitest* screen, ikey_set* input) { };
virtual void after_mouse_event(start_sitest* screen) { }; virtual void after_mouse_event(start_sitest* screen) { };
}; };
std::vector<EmbarkTool*> tools; std::map<std::string, EmbarkTool*> tools;
/* /*
@ -575,6 +576,8 @@ public:
max_y = min_y + height; max_y = min_y + height;
Screen::fillRect(Screen::Pen(' ', COLOR_BLACK, COLOR_DARKGREY), min_x, min_y, max_x, max_y); Screen::fillRect(Screen::Pen(' ', COLOR_BLACK, COLOR_DARKGREY), min_x, min_y, max_x, max_y);
Screen::fillRect(Screen::Pen(' ', COLOR_BLACK, COLOR_BLACK), min_x + 1, min_y + 1, max_x - 1, max_y - 1); Screen::fillRect(Screen::Pen(' ', COLOR_BLACK, COLOR_BLACK), min_x + 1, min_y + 1, max_x - 1, max_y - 1);
std::string title = " Embark tools (DFHack) ";
Screen::paintString(Screen::Pen(' ', COLOR_BLACK, COLOR_GREY), (max_x - min_x + title.size()) / 2, min_y, title);
x = min_x + 2; x = min_x + 2;
y = max_y - 2; y = max_y - 2;
OutputString(COLOR_LIGHTRED, x, y, Screen::getKeyDisplay(df::interface_key::SELECT)); OutputString(COLOR_LIGHTRED, x, y, Screen::getKeyDisplay(df::interface_key::SELECT));
@ -584,7 +587,7 @@ public:
y = min_y + 2; y = min_y + 2;
FOR_ITER_TOOLS(iter) FOR_ITER_TOOLS(iter)
{ {
EmbarkTool* t = *iter; EmbarkTool* t = iter->second;
x = min_x + 2; x = min_x + 2;
OutputString(COLOR_LIGHTRED, x, y, Screen::getKeyDisplay(t->getToggleKey())); OutputString(COLOR_LIGHTRED, x, y, Screen::getKeyDisplay(t->getToggleKey()));
OutputString(COLOR_WHITE, x, y, ": " + t->getName() + ": "); OutputString(COLOR_WHITE, x, y, ": " + t->getName() + ": ");
@ -605,7 +608,7 @@ public:
df::interface_key key = *iter; df::interface_key key = *iter;
FOR_ITER_TOOLS(iter) FOR_ITER_TOOLS(iter)
{ {
EmbarkTool* t = *iter; EmbarkTool* t = iter->second;
if (t->getToggleKey() == key) if (t->getToggleKey() == key)
{ {
t->toggleEnabled(); t->toggleEnabled();
@ -615,25 +618,20 @@ public:
}; };
}; };
void add_tool (EmbarkTool *t)
{
tools[t->getId()] = t;
}
bool tool_exists (std::string tool_name) bool tool_exists (std::string tool_name)
{ {
FOR_ITER_TOOLS(iter) return tools.find(tool_name) != tools.end();
{
EmbarkTool* tool = *iter;
if (tool->getId() == tool_name)
return true;
}
return false;
} }
bool tool_enabled (std::string tool_name) bool tool_enabled (std::string tool_name)
{ {
FOR_ITER_TOOLS(iter) if (tool_exists(tool_name))
{ return tools[tool_name]->getEnabled();
EmbarkTool* tool = *iter;
if (tool->getId() == tool_name)
return tool->getEnabled();
}
return false; return false;
} }
@ -642,7 +640,7 @@ bool tool_enable (std::string tool_name, bool enable_state)
int n = 0; int n = 0;
FOR_ITER_TOOLS(iter) FOR_ITER_TOOLS(iter)
{ {
EmbarkTool* tool = *iter; EmbarkTool* tool = iter->second;
if (tool->getId() == tool_name || tool_name == "all") if (tool->getId() == tool_name || tool_name == "all")
{ {
tool->setEnabled(enable_state); tool->setEnabled(enable_state);
@ -666,8 +664,9 @@ struct choose_start_site_hook : df::viewscreen_choose_start_sitest
std::vector<std::string> parts; std::vector<std::string> parts;
FOR_ITER_TOOLS(it) FOR_ITER_TOOLS(it)
{ {
if ((*it)->getEnabled()) EmbarkTool *t = it->second;
parts.push_back((*it)->getName()); if (t->getEnabled())
parts.push_back(t->getName());
} }
if (parts.size()) if (parts.size())
{ {
@ -698,7 +697,7 @@ struct choose_start_site_hook : df::viewscreen_choose_start_sitest
bool cancel = false; bool cancel = false;
FOR_ITER_TOOLS(iter) FOR_ITER_TOOLS(iter)
{ {
EmbarkTool* tool = *iter; EmbarkTool* tool = iter->second;
if (tool->getEnabled()) if (tool->getEnabled())
tool->before_feed(this, input, cancel); tool->before_feed(this, input, cancel);
} }
@ -709,7 +708,7 @@ struct choose_start_site_hook : df::viewscreen_choose_start_sitest
display_settings(); display_settings();
FOR_ITER_TOOLS(iter) FOR_ITER_TOOLS(iter)
{ {
EmbarkTool* tool = *iter; EmbarkTool* tool = iter->second;
if (tool->getEnabled()) if (tool->getEnabled())
tool->after_feed(this, input); tool->after_feed(this, input);
} }
@ -718,7 +717,7 @@ struct choose_start_site_hook : df::viewscreen_choose_start_sitest
{ {
FOR_ITER_TOOLS(iter) FOR_ITER_TOOLS(iter)
{ {
EmbarkTool* tool = *iter; EmbarkTool* tool = iter->second;
if (tool->getEnabled()) if (tool->getEnabled())
tool->before_render(this); tool->before_render(this);
} }
@ -726,7 +725,7 @@ struct choose_start_site_hook : df::viewscreen_choose_start_sitest
display_tool_status(); display_tool_status();
FOR_ITER_TOOLS(iter) FOR_ITER_TOOLS(iter)
{ {
EmbarkTool* tool = *iter; EmbarkTool* tool = iter->second;
if (tool->getEnabled()) if (tool->getEnabled())
tool->after_render(this); tool->after_render(this);
} }
@ -742,16 +741,16 @@ command_result embark_tools_cmd (color_ostream &out, std::vector <std::string> &
DFhackCExport command_result plugin_init (color_ostream &out, std::vector <PluginCommand> &commands) DFhackCExport command_result plugin_init (color_ostream &out, std::vector <PluginCommand> &commands)
{ {
tools.push_back(new EmbarkAnywhere); add_tool(new EmbarkAnywhere);
tools.push_back(new MouseControl); add_tool(new MouseControl);
tools.push_back(new SandIndicator); add_tool(new SandIndicator);
tools.push_back(new StablePosition); add_tool(new StablePosition);
std::string help = ""; std::string help = "";
help += "embark-tools (enable/disable) tool [tool...]\n" help += "embark-tools (enable/disable) tool [tool...]\n"
"Tools:\n"; "Tools:\n";
FOR_ITER_TOOLS(iter) FOR_ITER_TOOLS(iter)
{ {
help += (" " + (*iter)->getId() + ": " + (*iter)->getDesc() + "\n"); help += (" " + iter->second->getId() + ": " + iter->second->getDesc() + "\n");
} }
commands.push_back(PluginCommand( commands.push_back(PluginCommand(
"embark-tools", "embark-tools",
@ -782,6 +781,19 @@ DFhackCExport command_result plugin_enable (color_ostream &out, bool enable)
return CR_OK; return CR_OK;
} }
DFhackCExport command_result plugin_onstatechange (color_ostream &out, state_change_event evt)
{
if (evt == SC_BEGIN_UNLOAD)
{
if (Gui::getCurFocus() == "dfhack/embark-tools/options")
{
out.printerr("Settings screen active.\n");
return CR_NOT_FOUND;
}
}
return CR_OK;
}
DFhackCExport command_result plugin_onupdate (color_ostream &out) DFhackCExport command_result plugin_onupdate (color_ostream &out)
{ {
static int8_t mask = 0; static int8_t mask = 0;
@ -801,8 +813,8 @@ DFhackCExport command_result plugin_onupdate (color_ostream &out)
{ {
FOR_ITER_TOOLS(iter) FOR_ITER_TOOLS(iter)
{ {
if ((*iter)->getEnabled()) if (iter->second->getEnabled())
(*iter)->after_mouse_event(screen); iter->second->after_mouse_event(screen);
} }
} }
mask = new_mask; mask = new_mask;
@ -842,8 +854,8 @@ command_result embark_tools_cmd (color_ostream &out, std::vector <std::string> &
out << "Tool status:" << std::endl; out << "Tool status:" << std::endl;
FOR_ITER_TOOLS(iter) FOR_ITER_TOOLS(iter)
{ {
EmbarkTool* t = *iter; EmbarkTool* t = iter->second;
out << t->getName() << " (" << t->getId() << "): " out << " " << t->getName() << " (" << t->getId() << "): "
<< (t->getEnabled() ? "Enabled" : "Disabled") << std::endl; << (t->getEnabled() ? "Enabled" : "Disabled") << std::endl;
} }
} }