2015-06-05 19:49:22 -06:00
|
|
|
#include <map>
|
2015-11-21 18:58:49 -07:00
|
|
|
#include <set>
|
|
|
|
#include <queue>
|
|
|
|
|
2015-06-05 19:49:22 -06:00
|
|
|
#include "Console.h"
|
|
|
|
#include "Core.h"
|
|
|
|
#include "DataDefs.h"
|
2022-06-03 14:18:16 -06:00
|
|
|
#include "Debug.h"
|
2015-11-21 18:58:49 -07:00
|
|
|
#include "Error.h"
|
2015-06-05 19:49:22 -06:00
|
|
|
#include "Export.h"
|
2015-11-21 16:59:30 -07:00
|
|
|
#include "LuaTools.h"
|
|
|
|
#include "LuaWrapper.h"
|
2015-11-21 18:58:49 -07:00
|
|
|
#include "PluginManager.h"
|
|
|
|
#include "VTableInterpose.h"
|
2015-06-06 08:31:12 -06:00
|
|
|
#include "modules/Gui.h"
|
2015-11-21 18:58:49 -07:00
|
|
|
#include "uicommon.h"
|
2015-06-06 08:31:12 -06:00
|
|
|
|
|
|
|
#include "df/building_tradedepotst.h"
|
2015-07-27 10:03:29 -06:00
|
|
|
#include "df/general_ref.h"
|
|
|
|
#include "df/general_ref_contained_in_itemst.h"
|
2022-06-02 07:24:13 -06:00
|
|
|
#include "df/interfacest.h"
|
2015-06-06 08:31:12 -06:00
|
|
|
#include "df/viewscreen_dwarfmodest.h"
|
2022-05-23 22:17:52 -06:00
|
|
|
#include "df/viewscreen_jobmanagementst.h"
|
2020-06-20 10:02:13 -06:00
|
|
|
#include "df/viewscreen_justicest.h"
|
2015-06-09 11:52:37 -06:00
|
|
|
#include "df/viewscreen_layer_militaryst.h"
|
2015-12-25 09:18:55 -07:00
|
|
|
#include "df/viewscreen_locationsst.h"
|
2015-06-05 19:49:22 -06:00
|
|
|
#include "df/viewscreen_tradegoodsst.h"
|
|
|
|
|
|
|
|
using namespace DFHack;
|
2015-06-06 08:31:12 -06:00
|
|
|
using namespace df::enums;
|
2017-05-26 22:26:14 -06:00
|
|
|
using std::map;
|
|
|
|
using std::queue;
|
2015-06-06 08:31:12 -06:00
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
2015-06-05 19:49:22 -06:00
|
|
|
|
|
|
|
DFHACK_PLUGIN("confirm");
|
|
|
|
DFHACK_PLUGIN_IS_ENABLED(is_enabled);
|
|
|
|
REQUIRE_GLOBAL(gps);
|
2015-06-06 08:31:12 -06:00
|
|
|
REQUIRE_GLOBAL(ui);
|
2015-06-05 19:49:22 -06:00
|
|
|
|
|
|
|
typedef std::set<df::interface_key> ikey_set;
|
2015-06-06 08:31:12 -06:00
|
|
|
command_result df_confirm (color_ostream &out, vector <string> & parameters);
|
2015-06-05 19:49:22 -06:00
|
|
|
|
2015-07-27 10:03:29 -06:00
|
|
|
struct conf_wrapper;
|
2017-05-26 22:26:14 -06:00
|
|
|
static map<string, conf_wrapper*> confirmations;
|
2015-11-21 18:58:49 -07:00
|
|
|
string active_id;
|
2017-05-26 22:26:14 -06:00
|
|
|
queue<string> cmds;
|
2015-06-05 19:49:22 -06:00
|
|
|
|
2022-06-02 07:24:13 -06:00
|
|
|
// true when confirm is paused
|
|
|
|
bool paused = false;
|
|
|
|
// if set, confirm will unpause when this screen is no longer on the stack
|
|
|
|
df::viewscreen *paused_screen = NULL;
|
|
|
|
|
2022-06-03 14:18:16 -06:00
|
|
|
namespace DFHack {
|
|
|
|
DBG_DECLARE(confirm,status);
|
|
|
|
}
|
2022-06-02 07:24:13 -06:00
|
|
|
|
2015-07-27 10:03:29 -06:00
|
|
|
template <typename VT, typename FT>
|
2015-11-11 21:00:20 -07:00
|
|
|
inline bool in_vector (std::vector<VT> &vec, FT item)
|
2015-07-27 10:03:29 -06:00
|
|
|
{
|
|
|
|
return std::find(vec.begin(), vec.end(), item) != vec.end();
|
|
|
|
}
|
|
|
|
|
2015-11-21 16:59:30 -07:00
|
|
|
string char_replace (string s, char a, char b)
|
|
|
|
{
|
|
|
|
string res = s;
|
|
|
|
size_t i = res.size();
|
|
|
|
while (i--)
|
|
|
|
if (res[i] == a)
|
|
|
|
res[i] = b;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-11-21 18:58:49 -07:00
|
|
|
bool set_conf_state (string name, bool state);
|
|
|
|
|
2017-05-26 22:26:14 -06:00
|
|
|
class confirmation_base {
|
|
|
|
public:
|
|
|
|
enum cstate { INACTIVE, ACTIVE, SELECTED };
|
|
|
|
virtual string get_id() = 0;
|
|
|
|
virtual bool set_state(cstate) = 0;
|
|
|
|
|
|
|
|
static bool set_state(string id, cstate state)
|
|
|
|
{
|
|
|
|
if (active && active->get_id() == id)
|
|
|
|
{
|
|
|
|
active->set_state(state);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
protected:
|
|
|
|
static confirmation_base *active;
|
|
|
|
};
|
|
|
|
confirmation_base *confirmation_base::active = nullptr;
|
|
|
|
|
2015-11-21 18:58:49 -07:00
|
|
|
struct conf_wrapper {
|
|
|
|
private:
|
|
|
|
bool enabled;
|
|
|
|
std::set<VMethodInterposeLinkBase*> hooks;
|
|
|
|
public:
|
|
|
|
conf_wrapper()
|
|
|
|
:enabled(false)
|
|
|
|
{}
|
|
|
|
void add_hook(VMethodInterposeLinkBase *hook)
|
|
|
|
{
|
|
|
|
if (!hooks.count(hook))
|
|
|
|
hooks.insert(hook);
|
|
|
|
}
|
|
|
|
bool apply (bool state) {
|
|
|
|
if (state == enabled)
|
|
|
|
return true;
|
2017-05-26 22:26:14 -06:00
|
|
|
for (auto hook : hooks)
|
2015-11-21 18:58:49 -07:00
|
|
|
{
|
2017-05-26 22:26:14 -06:00
|
|
|
if (!hook->apply(state))
|
2015-11-21 18:58:49 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
enabled = state;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
inline bool is_enabled() { return enabled; }
|
|
|
|
};
|
|
|
|
|
2015-11-11 21:00:20 -07:00
|
|
|
namespace trade {
|
|
|
|
static bool goods_selected (const std::vector<char> &selected)
|
|
|
|
{
|
2017-05-26 22:26:14 -06:00
|
|
|
for (char c : selected)
|
|
|
|
if (c)
|
2015-11-11 21:00:20 -07:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
inline bool trader_goods_selected (df::viewscreen_tradegoodsst *screen)
|
|
|
|
{
|
2015-11-21 18:58:49 -07:00
|
|
|
CHECK_NULL_POINTER(screen);
|
2015-11-11 21:00:20 -07:00
|
|
|
return goods_selected(screen->trader_selected);
|
|
|
|
}
|
|
|
|
inline bool broker_goods_selected (df::viewscreen_tradegoodsst *screen)
|
|
|
|
{
|
2015-11-21 18:58:49 -07:00
|
|
|
CHECK_NULL_POINTER(screen);
|
2015-11-11 21:00:20 -07:00
|
|
|
return goods_selected(screen->broker_selected);
|
|
|
|
}
|
2015-07-27 10:03:29 -06:00
|
|
|
|
2015-11-11 21:00:20 -07:00
|
|
|
static bool goods_all_selected(const std::vector<char> &selected, const std::vector<df::item*> &items) \
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < selected.size(); ++i)
|
|
|
|
{
|
|
|
|
if (!selected[i])
|
|
|
|
{
|
|
|
|
// check to see if item is in a container
|
|
|
|
// (if the container is not selected, it will be detected separately)
|
|
|
|
bool in_container = false;
|
2017-05-26 22:26:14 -06:00
|
|
|
for (auto ref : items[i]->general_refs)
|
2015-11-11 21:00:20 -07:00
|
|
|
{
|
2017-05-26 22:26:14 -06:00
|
|
|
if (virtual_cast<df::general_ref_contained_in_itemst>(ref))
|
2015-11-11 21:00:20 -07:00
|
|
|
{
|
|
|
|
in_container = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!in_container)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
inline bool trader_goods_all_selected(df::viewscreen_tradegoodsst *screen)
|
|
|
|
{
|
2015-11-21 18:58:49 -07:00
|
|
|
CHECK_NULL_POINTER(screen);
|
2015-11-11 21:00:20 -07:00
|
|
|
return goods_all_selected(screen->trader_selected, screen->trader_items);
|
|
|
|
}
|
|
|
|
inline bool broker_goods_all_selected(df::viewscreen_tradegoodsst *screen)
|
|
|
|
{
|
2015-11-21 18:58:49 -07:00
|
|
|
CHECK_NULL_POINTER(screen);
|
2015-11-11 21:00:20 -07:00
|
|
|
return goods_all_selected(screen->broker_selected, screen->broker_items);
|
|
|
|
}
|
2015-07-27 10:03:29 -06:00
|
|
|
}
|
2015-06-05 19:49:22 -06:00
|
|
|
|
2015-11-21 16:59:30 -07:00
|
|
|
namespace conf_lua {
|
|
|
|
static color_ostream_proxy *out;
|
|
|
|
static lua_State *l_state;
|
|
|
|
bool init (color_ostream &dfout)
|
|
|
|
{
|
|
|
|
out = new color_ostream_proxy(Core::getInstance().getConsole());
|
|
|
|
l_state = Lua::Open(*out);
|
|
|
|
return l_state;
|
|
|
|
}
|
|
|
|
void cleanup()
|
|
|
|
{
|
|
|
|
if (out)
|
|
|
|
{
|
|
|
|
delete out;
|
2017-05-26 22:26:14 -06:00
|
|
|
out = nullptr;
|
2015-11-21 16:59:30 -07:00
|
|
|
}
|
|
|
|
lua_close(l_state);
|
|
|
|
}
|
|
|
|
bool call (const char *func, int nargs = 0, int nres = 0)
|
|
|
|
{
|
|
|
|
if (!Lua::PushModulePublic(*out, l_state, "plugins.confirm", func))
|
|
|
|
return false;
|
|
|
|
if (nargs > 0)
|
|
|
|
lua_insert(l_state, lua_gettop(l_state) - nargs);
|
|
|
|
return Lua::SafeCall(*out, l_state, nargs, nres);
|
|
|
|
}
|
2015-11-21 18:58:49 -07:00
|
|
|
bool simple_call (const char *func)
|
|
|
|
{
|
|
|
|
Lua::StackUnwinder top(l_state);
|
|
|
|
return call(func, 0, 0);
|
|
|
|
}
|
2015-11-21 16:59:30 -07:00
|
|
|
template <typename T>
|
|
|
|
void push (T val)
|
|
|
|
{
|
|
|
|
Lua::Push(l_state, val);
|
|
|
|
}
|
|
|
|
namespace api {
|
|
|
|
int get_ids (lua_State *L)
|
|
|
|
{
|
|
|
|
lua_newtable(L);
|
2017-05-26 22:26:14 -06:00
|
|
|
for (auto item : confirmations)
|
|
|
|
Lua::TableInsert(L, item.first, true);
|
2015-11-21 16:59:30 -07:00
|
|
|
return 1;
|
|
|
|
}
|
2015-11-21 18:58:49 -07:00
|
|
|
int get_conf_data (lua_State *L)
|
|
|
|
{
|
|
|
|
lua_newtable(L);
|
|
|
|
int i = 1;
|
2017-05-26 22:26:14 -06:00
|
|
|
for (auto item : confirmations)
|
2015-11-21 18:58:49 -07:00
|
|
|
{
|
|
|
|
Lua::Push(L, i++);
|
|
|
|
lua_newtable(L);
|
2017-05-26 22:26:14 -06:00
|
|
|
Lua::TableInsert(L, "id", item.first);
|
|
|
|
Lua::TableInsert(L, "enabled", item.second->is_enabled());
|
2015-11-21 18:58:49 -07:00
|
|
|
lua_settable(L, -3);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
int get_active_id (lua_State *L)
|
|
|
|
{
|
|
|
|
if (active_id.size())
|
|
|
|
Lua::Push(L, active_id);
|
|
|
|
else
|
|
|
|
lua_pushnil(L);
|
|
|
|
return 1;
|
|
|
|
}
|
2022-06-02 07:24:13 -06:00
|
|
|
int unpause(lua_State *)
|
|
|
|
{
|
2022-06-03 14:18:16 -06:00
|
|
|
DEBUG(status).print("unpausing\n"),
|
2022-06-02 07:24:13 -06:00
|
|
|
paused = false;
|
|
|
|
paused_screen = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int get_paused (lua_State *L)
|
|
|
|
{
|
|
|
|
Lua::Push(L, paused);
|
|
|
|
return 1;
|
|
|
|
}
|
2015-11-21 16:59:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CONF_LUA_FUNC(ns, name) {#name, df::wrap_function(ns::name, true)}
|
|
|
|
DFHACK_PLUGIN_LUA_FUNCTIONS {
|
2015-11-21 18:58:49 -07:00
|
|
|
CONF_LUA_FUNC( , set_conf_state),
|
2015-11-21 16:59:30 -07:00
|
|
|
CONF_LUA_FUNC(trade, broker_goods_selected),
|
|
|
|
CONF_LUA_FUNC(trade, broker_goods_all_selected),
|
|
|
|
CONF_LUA_FUNC(trade, trader_goods_selected),
|
|
|
|
CONF_LUA_FUNC(trade, trader_goods_all_selected),
|
|
|
|
DFHACK_LUA_END
|
|
|
|
};
|
|
|
|
|
|
|
|
#define CONF_LUA_CMD(name) {#name, conf_lua::api::name}
|
|
|
|
DFHACK_PLUGIN_LUA_COMMANDS {
|
|
|
|
CONF_LUA_CMD(get_ids),
|
2015-11-21 18:58:49 -07:00
|
|
|
CONF_LUA_CMD(get_conf_data),
|
|
|
|
CONF_LUA_CMD(get_active_id),
|
2022-06-02 07:24:13 -06:00
|
|
|
CONF_LUA_CMD(unpause),
|
|
|
|
CONF_LUA_CMD(get_paused),
|
2015-11-21 16:59:30 -07:00
|
|
|
DFHACK_LUA_END
|
|
|
|
};
|
|
|
|
|
2015-11-21 18:58:49 -07:00
|
|
|
void show_options()
|
|
|
|
{
|
|
|
|
cmds.push("gui/confirm-opts");
|
|
|
|
}
|
|
|
|
|
2015-06-05 19:49:22 -06:00
|
|
|
template <class T>
|
2017-05-26 22:26:14 -06:00
|
|
|
class confirmation : public confirmation_base {
|
2015-06-05 19:49:22 -06:00
|
|
|
public:
|
|
|
|
typedef T screen_type;
|
|
|
|
screen_type *screen;
|
2017-05-26 22:26:14 -06:00
|
|
|
|
|
|
|
bool set_state (cstate s) override
|
2015-11-21 18:58:49 -07:00
|
|
|
{
|
2017-05-26 22:26:14 -06:00
|
|
|
if (confirmation_base::active && confirmation_base::active != this)
|
|
|
|
{
|
|
|
|
// Stop this confirmation from appearing over another one
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-21 18:58:49 -07:00
|
|
|
state = s;
|
2017-05-26 22:26:14 -06:00
|
|
|
if (s == INACTIVE) {
|
2015-11-21 18:58:49 -07:00
|
|
|
active_id = "";
|
2017-05-26 22:26:14 -06:00
|
|
|
confirmation_base::active = nullptr;
|
|
|
|
}
|
|
|
|
else {
|
2015-11-21 18:58:49 -07:00
|
|
|
active_id = get_id();
|
2017-05-26 22:26:14 -06:00
|
|
|
confirmation_base::active = this;
|
|
|
|
}
|
|
|
|
return true;
|
2015-11-21 18:58:49 -07:00
|
|
|
}
|
2015-06-05 19:49:22 -06:00
|
|
|
bool feed (ikey_set *input) {
|
2022-06-02 07:24:13 -06:00
|
|
|
if (paused)
|
|
|
|
{
|
|
|
|
// we can only detect that we've left the screen by intercepting the
|
|
|
|
// ESC key
|
|
|
|
if (!paused_screen && input->count(df::interface_key::LEAVESCREEN))
|
|
|
|
conf_lua::api::unpause(NULL);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if (state == INACTIVE)
|
2015-06-05 19:49:22 -06:00
|
|
|
{
|
2017-05-26 22:26:14 -06:00
|
|
|
for (df::interface_key key : *input)
|
2015-06-05 19:49:22 -06:00
|
|
|
{
|
2017-05-26 22:26:14 -06:00
|
|
|
if (intercept_key(key))
|
2015-06-05 19:49:22 -06:00
|
|
|
{
|
2017-05-26 22:26:14 -06:00
|
|
|
if (set_state(ACTIVE))
|
|
|
|
{
|
|
|
|
last_key = key;
|
|
|
|
return true;
|
|
|
|
}
|
2015-06-05 19:49:22 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if (state == ACTIVE)
|
|
|
|
{
|
|
|
|
if (input->count(df::interface_key::LEAVESCREEN))
|
2015-11-21 18:58:49 -07:00
|
|
|
set_state(INACTIVE);
|
2015-06-05 19:49:22 -06:00
|
|
|
else if (input->count(df::interface_key::SELECT))
|
2015-11-21 18:58:49 -07:00
|
|
|
set_state(SELECTED);
|
2022-06-02 07:24:13 -06:00
|
|
|
else if (input->count(df::interface_key::CUSTOM_P))
|
|
|
|
{
|
2022-06-03 14:18:16 -06:00
|
|
|
DEBUG(status).print("pausing\n"),
|
2022-06-02 07:24:13 -06:00
|
|
|
paused = true;
|
|
|
|
// only record the screen when we're not at the top viewscreen
|
|
|
|
// since this screen will *always* be on the stack. for
|
|
|
|
// dwarfmode screens, use ESC detection to discover when to
|
|
|
|
// unpause
|
|
|
|
if (!df::viewscreen_dwarfmodest::_identity.is_instance(screen))
|
|
|
|
paused_screen = screen;
|
|
|
|
set_state(INACTIVE);
|
|
|
|
}
|
2015-11-21 18:58:49 -07:00
|
|
|
else if (input->count(df::interface_key::CUSTOM_S))
|
|
|
|
show_options();
|
2015-06-05 19:49:22 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool key_conflict (df::interface_key key)
|
|
|
|
{
|
|
|
|
if (key == df::interface_key::SELECT || key == df::interface_key::LEAVESCREEN)
|
|
|
|
return false;
|
|
|
|
return state == ACTIVE;
|
|
|
|
}
|
|
|
|
void render() {
|
2015-06-06 08:31:12 -06:00
|
|
|
static vector<string> lines;
|
2022-06-02 07:24:13 -06:00
|
|
|
static const std::string pause_message =
|
|
|
|
"Pause confirmations until you exit this screen";
|
2015-06-05 19:49:22 -06:00
|
|
|
Screen::Pen corner_ul = Screen::Pen((char)201, COLOR_GREY, COLOR_BLACK);
|
|
|
|
Screen::Pen corner_ur = Screen::Pen((char)187, COLOR_GREY, COLOR_BLACK);
|
|
|
|
Screen::Pen corner_dl = Screen::Pen((char)200, COLOR_GREY, COLOR_BLACK);
|
|
|
|
Screen::Pen corner_dr = Screen::Pen((char)188, COLOR_GREY, COLOR_BLACK);
|
|
|
|
Screen::Pen border_ud = Screen::Pen((char)205, COLOR_GREY, COLOR_BLACK);
|
|
|
|
Screen::Pen border_lr = Screen::Pen((char)186, COLOR_GREY, COLOR_BLACK);
|
|
|
|
if (state == ACTIVE)
|
|
|
|
{
|
|
|
|
split_string(&lines, get_message(), "\n");
|
2015-11-21 18:58:49 -07:00
|
|
|
size_t max_length = 40;
|
2017-05-26 22:26:14 -06:00
|
|
|
for (string line : lines)
|
|
|
|
max_length = std::max(max_length, line.size());
|
2015-06-05 19:49:22 -06:00
|
|
|
int width = max_length + 4;
|
2022-06-02 07:24:13 -06:00
|
|
|
vector<string> pause_message_lines;
|
|
|
|
word_wrap(&pause_message_lines, pause_message, max_length - 3);
|
|
|
|
int height = lines.size() + pause_message_lines.size() + 5;
|
2015-06-05 19:49:22 -06:00
|
|
|
int x1 = (gps->dimx / 2) - (width / 2);
|
|
|
|
int x2 = x1 + width - 1;
|
|
|
|
int y1 = (gps->dimy / 2) - (height / 2);
|
|
|
|
int y2 = y1 + height - 1;
|
|
|
|
for (int x = x1; x <= x2; x++)
|
|
|
|
{
|
|
|
|
Screen::paintTile(border_ud, x, y1);
|
|
|
|
Screen::paintTile(border_ud, x, y2);
|
|
|
|
}
|
|
|
|
for (int y = y1; y <= y2; y++)
|
|
|
|
{
|
|
|
|
Screen::paintTile(border_lr, x1, y);
|
|
|
|
Screen::paintTile(border_lr, x2, y);
|
|
|
|
}
|
|
|
|
Screen::paintTile(corner_ul, x1, y1);
|
|
|
|
Screen::paintTile(corner_ur, x2, y1);
|
|
|
|
Screen::paintTile(corner_dl, x1, y2);
|
|
|
|
Screen::paintTile(corner_dr, x2, y2);
|
2015-06-06 08:31:12 -06:00
|
|
|
string title = " " + get_title() + " ";
|
|
|
|
Screen::paintString(Screen::Pen(' ', COLOR_DARKGREY, COLOR_BLACK),
|
|
|
|
x2 - 6, y1, "DFHack");
|
2015-06-05 19:49:22 -06:00
|
|
|
Screen::paintString(Screen::Pen(' ', COLOR_BLACK, COLOR_GREY),
|
|
|
|
(gps->dimx / 2) - (title.size() / 2), y1, title);
|
|
|
|
int x = x1 + 2;
|
2015-11-21 18:58:49 -07:00
|
|
|
int y = y2;
|
|
|
|
OutputString(COLOR_LIGHTRED, x, y, Screen::getKeyDisplay(df::interface_key::LEAVESCREEN));
|
|
|
|
OutputString(COLOR_WHITE, x, y, ": Cancel");
|
|
|
|
x = (gps->dimx - (Screen::getKeyDisplay(df::interface_key::CUSTOM_S) + ": Settings").size()) / 2 + 1;
|
|
|
|
OutputString(COLOR_LIGHTRED, x, y, Screen::getKeyDisplay(df::interface_key::CUSTOM_S));
|
|
|
|
OutputString(COLOR_WHITE, x, y, ": Settings");
|
2015-06-09 11:52:37 -06:00
|
|
|
x = x2 - 2 - 3 - Screen::getKeyDisplay(df::interface_key::SELECT).size();
|
2015-11-21 18:58:49 -07:00
|
|
|
OutputString(COLOR_LIGHTRED, x, y, Screen::getKeyDisplay(df::interface_key::SELECT));
|
|
|
|
OutputString(COLOR_WHITE, x, y, ": Ok");
|
2015-06-05 19:49:22 -06:00
|
|
|
Screen::fillRect(Screen::Pen(' ', COLOR_BLACK, COLOR_BLACK), x1 + 1, y1 + 1, x2 - 1, y2 - 1);
|
|
|
|
for (size_t i = 0; i < lines.size(); i++)
|
|
|
|
{
|
|
|
|
Screen::paintString(Screen::Pen(' ', get_color(), COLOR_BLACK), x1 + 2, y1 + 2 + i, lines[i]);
|
|
|
|
}
|
2022-06-02 07:24:13 -06:00
|
|
|
y = y1 + 3 + lines.size();
|
|
|
|
for (size_t i = 0; i < pause_message_lines.size(); i++)
|
|
|
|
{
|
|
|
|
Screen::paintString(Screen::Pen(' ', COLOR_WHITE, COLOR_BLACK), x1 + 5, y + i, pause_message_lines[i]);
|
|
|
|
}
|
|
|
|
x = x1 + 2;
|
|
|
|
OutputString(COLOR_LIGHTRED, x, y, Screen::getKeyDisplay(df::interface_key::CUSTOM_P));
|
|
|
|
OutputString(COLOR_WHITE, x, y, ":");
|
2015-06-05 19:49:22 -06:00
|
|
|
}
|
|
|
|
else if (state == SELECTED)
|
|
|
|
{
|
|
|
|
ikey_set tmp;
|
|
|
|
tmp.insert(last_key);
|
|
|
|
screen->feed(&tmp);
|
2015-11-21 18:58:49 -07:00
|
|
|
set_state(INACTIVE);
|
2015-06-05 19:49:22 -06:00
|
|
|
}
|
|
|
|
}
|
2017-05-26 22:26:14 -06:00
|
|
|
virtual string get_id() override = 0;
|
2015-11-21 16:59:30 -07:00
|
|
|
#define CONF_LUA_START using namespace conf_lua; Lua::StackUnwinder unwind(l_state); push(screen); push(get_id());
|
|
|
|
bool intercept_key (df::interface_key key)
|
|
|
|
{
|
|
|
|
CONF_LUA_START;
|
|
|
|
push(key);
|
|
|
|
if (call("intercept_key", 3, 1))
|
|
|
|
return lua_toboolean(l_state, -1);
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
string get_title()
|
|
|
|
{
|
|
|
|
CONF_LUA_START;
|
|
|
|
if (call("get_title", 2, 1) && lua_isstring(l_state, -1))
|
|
|
|
return lua_tostring(l_state, -1);
|
|
|
|
else
|
|
|
|
return "Confirm";
|
|
|
|
}
|
|
|
|
string get_message()
|
|
|
|
{
|
|
|
|
CONF_LUA_START;
|
|
|
|
if (call("get_message", 2, 1) && lua_isstring(l_state, -1))
|
|
|
|
return lua_tostring(l_state, -1);
|
|
|
|
else
|
|
|
|
return "<Message generation failed>";
|
|
|
|
};
|
|
|
|
UIColor get_color()
|
|
|
|
{
|
|
|
|
CONF_LUA_START;
|
|
|
|
if (call("get_color", 2, 1) && lua_isnumber(l_state, -1))
|
|
|
|
return lua_tointeger(l_state, -1) % 16;
|
|
|
|
else
|
|
|
|
return COLOR_YELLOW;
|
|
|
|
}
|
|
|
|
#undef CONF_LUA_START
|
2015-06-05 19:49:22 -06:00
|
|
|
protected:
|
|
|
|
cstate state;
|
|
|
|
df::interface_key last_key;
|
|
|
|
};
|
|
|
|
|
2015-11-11 21:00:20 -07:00
|
|
|
template<typename T>
|
2017-05-26 22:26:14 -06:00
|
|
|
int conf_register(confirmation<T> *c, const vector<VMethodInterposeLinkBase*> &hooks)
|
2015-11-11 21:00:20 -07:00
|
|
|
{
|
2015-11-21 18:58:49 -07:00
|
|
|
conf_wrapper *w = new conf_wrapper();
|
2015-11-11 21:00:20 -07:00
|
|
|
confirmations[c->get_id()] = w;
|
2017-05-26 22:26:14 -06:00
|
|
|
for (auto hook : hooks)
|
2015-11-11 21:00:20 -07:00
|
|
|
w->add_hook(hook);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-11-21 16:59:30 -07:00
|
|
|
#define IMPLEMENT_CONFIRMATION_HOOKS(cls, prio) \
|
2015-07-27 10:03:29 -06:00
|
|
|
static cls cls##_instance; \
|
|
|
|
struct cls##_hooks : cls::screen_type { \
|
|
|
|
typedef cls::screen_type interpose_base; \
|
|
|
|
DEFINE_VMETHOD_INTERPOSE(void, feed, (ikey_set *input)) \
|
|
|
|
{ \
|
|
|
|
cls##_instance.screen = this; \
|
|
|
|
if (!cls##_instance.feed(input)) \
|
|
|
|
INTERPOSE_NEXT(feed)(input); \
|
|
|
|
} \
|
|
|
|
DEFINE_VMETHOD_INTERPOSE(void, render, ()) \
|
|
|
|
{ \
|
|
|
|
cls##_instance.screen = this; \
|
|
|
|
INTERPOSE_NEXT(render)(); \
|
|
|
|
cls##_instance.render(); \
|
|
|
|
} \
|
|
|
|
DEFINE_VMETHOD_INTERPOSE(bool, key_conflict, (df::interface_key key)) \
|
|
|
|
{ \
|
|
|
|
return cls##_instance.key_conflict(key) || INTERPOSE_NEXT(key_conflict)(key); \
|
|
|
|
} \
|
|
|
|
}; \
|
|
|
|
IMPLEMENT_VMETHOD_INTERPOSE_PRIO(cls##_hooks, feed, prio); \
|
|
|
|
IMPLEMENT_VMETHOD_INTERPOSE_PRIO(cls##_hooks, render, prio); \
|
2015-11-11 21:00:20 -07:00
|
|
|
IMPLEMENT_VMETHOD_INTERPOSE_PRIO(cls##_hooks, key_conflict, prio); \
|
2017-05-26 22:26:14 -06:00
|
|
|
static int conf_register_##cls = conf_register(&cls##_instance, {\
|
2015-11-11 21:00:20 -07:00
|
|
|
&INTERPOSE_HOOK(cls##_hooks, feed), \
|
|
|
|
&INTERPOSE_HOOK(cls##_hooks, render), \
|
|
|
|
&INTERPOSE_HOOK(cls##_hooks, key_conflict), \
|
2017-05-26 22:26:14 -06:00
|
|
|
});
|
2015-07-27 10:03:29 -06:00
|
|
|
|
2017-05-26 22:26:14 -06:00
|
|
|
#define DEFINE_CONFIRMATION(cls, screen) \
|
2015-11-21 16:59:30 -07:00
|
|
|
class confirmation_##cls : public confirmation<df::screen> { \
|
|
|
|
virtual string get_id() { static string id = char_replace(#cls, '_', '-'); return id; } \
|
|
|
|
}; \
|
2017-05-26 22:26:14 -06:00
|
|
|
IMPLEMENT_CONFIRMATION_HOOKS(confirmation_##cls, 0);
|
2015-07-27 10:03:29 -06:00
|
|
|
|
2015-11-21 16:59:30 -07:00
|
|
|
/* This section defines stubs for all confirmation dialogs, with methods
|
|
|
|
implemented in plugins/lua/confirm.lua.
|
|
|
|
IDs (used in the "confirm enable/disable" command, by Lua, and in the docs)
|
|
|
|
are obtained by replacing '_' with '-' in the first argument to DEFINE_CONFIRMATION
|
|
|
|
*/
|
2017-05-26 22:26:14 -06:00
|
|
|
DEFINE_CONFIRMATION(trade, viewscreen_tradegoodsst);
|
|
|
|
DEFINE_CONFIRMATION(trade_cancel, viewscreen_tradegoodsst);
|
|
|
|
DEFINE_CONFIRMATION(trade_seize, viewscreen_tradegoodsst);
|
|
|
|
DEFINE_CONFIRMATION(trade_offer, viewscreen_tradegoodsst);
|
|
|
|
DEFINE_CONFIRMATION(trade_select_all, viewscreen_tradegoodsst);
|
|
|
|
DEFINE_CONFIRMATION(haul_delete, viewscreen_dwarfmodest);
|
|
|
|
DEFINE_CONFIRMATION(depot_remove, viewscreen_dwarfmodest);
|
|
|
|
DEFINE_CONFIRMATION(squad_disband, viewscreen_layer_militaryst);
|
|
|
|
DEFINE_CONFIRMATION(uniform_delete, viewscreen_layer_militaryst);
|
|
|
|
DEFINE_CONFIRMATION(note_delete, viewscreen_dwarfmodest);
|
|
|
|
DEFINE_CONFIRMATION(route_delete, viewscreen_dwarfmodest);
|
|
|
|
DEFINE_CONFIRMATION(location_retire, viewscreen_locationsst);
|
2020-06-20 10:02:13 -06:00
|
|
|
DEFINE_CONFIRMATION(convict, viewscreen_justicest);
|
2022-05-23 22:17:52 -06:00
|
|
|
DEFINE_CONFIRMATION(order_remove, viewscreen_jobmanagementst);
|
2015-06-09 11:52:37 -06:00
|
|
|
|
2015-06-06 08:31:12 -06:00
|
|
|
DFhackCExport command_result plugin_init (color_ostream &out, vector <PluginCommand> &commands)
|
2015-06-05 19:49:22 -06:00
|
|
|
{
|
2015-11-21 16:59:30 -07:00
|
|
|
if (!conf_lua::init(out))
|
|
|
|
return CR_FAILURE;
|
2015-06-05 19:49:22 -06:00
|
|
|
commands.push_back(PluginCommand(
|
|
|
|
"confirm",
|
|
|
|
"Confirmation dialogs",
|
|
|
|
df_confirm,
|
|
|
|
false, //allow non-interactive use
|
2015-07-27 10:03:29 -06:00
|
|
|
|
2022-03-23 20:45:13 -06:00
|
|
|
" confirm enable|disable option|all ...\n"
|
|
|
|
" confirm help|status\n"
|
2015-06-05 19:49:22 -06:00
|
|
|
));
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2015-07-27 10:03:29 -06:00
|
|
|
DFhackCExport command_result plugin_enable (color_ostream &out, bool enable)
|
2015-06-05 19:49:22 -06:00
|
|
|
{
|
|
|
|
if (is_enabled != enable)
|
|
|
|
{
|
2017-05-26 22:26:14 -06:00
|
|
|
for (auto c : confirmations)
|
2015-07-27 10:03:29 -06:00
|
|
|
{
|
2017-05-26 22:26:14 -06:00
|
|
|
if (!c.second->apply(enable))
|
2015-07-27 10:03:29 -06:00
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
2015-06-05 19:49:22 -06:00
|
|
|
is_enabled = enable;
|
|
|
|
}
|
2015-11-21 16:59:30 -07:00
|
|
|
if (is_enabled)
|
|
|
|
{
|
2015-11-21 18:58:49 -07:00
|
|
|
conf_lua::simple_call("check");
|
2015-11-21 16:59:30 -07:00
|
|
|
}
|
2015-06-05 19:49:22 -06:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
DFhackCExport command_result plugin_shutdown (color_ostream &out)
|
|
|
|
{
|
2015-07-27 10:03:29 -06:00
|
|
|
if (plugin_enable(out, false) != CR_OK)
|
|
|
|
return CR_FAILURE;
|
2015-11-21 16:59:30 -07:00
|
|
|
conf_lua::cleanup();
|
2017-05-26 22:26:14 -06:00
|
|
|
|
|
|
|
for (auto item : confirmations)
|
|
|
|
{
|
|
|
|
delete item.second;
|
|
|
|
}
|
|
|
|
confirmations.clear();
|
|
|
|
|
2015-06-05 19:49:22 -06:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2022-06-02 07:24:13 -06:00
|
|
|
static bool screen_found(df::viewscreen *target_screen)
|
|
|
|
{
|
|
|
|
if (!df::global::gview)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
df::viewscreen *screen = &df::global::gview->view;
|
|
|
|
while (screen)
|
|
|
|
{
|
|
|
|
if (screen == target_screen)
|
|
|
|
return true;
|
|
|
|
screen = screen->child;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-21 18:58:49 -07:00
|
|
|
DFhackCExport command_result plugin_onupdate (color_ostream &out)
|
|
|
|
{
|
|
|
|
while (!cmds.empty())
|
|
|
|
{
|
|
|
|
Core::getInstance().runCommand(out, cmds.front());
|
|
|
|
cmds.pop();
|
|
|
|
}
|
2022-06-02 07:24:13 -06:00
|
|
|
|
|
|
|
// if the screen that we paused on is no longer on the stack, unpause
|
|
|
|
if (paused_screen && !screen_found(paused_screen))
|
|
|
|
conf_lua::api::unpause(NULL);
|
|
|
|
|
2015-11-21 18:58:49 -07:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool set_conf_state (string name, bool state)
|
2015-06-05 19:49:22 -06:00
|
|
|
{
|
|
|
|
bool found = false;
|
2017-05-26 22:26:14 -06:00
|
|
|
for (auto it : confirmations)
|
2015-06-05 19:49:22 -06:00
|
|
|
{
|
2017-05-26 22:26:14 -06:00
|
|
|
if (it.first == name)
|
2015-06-05 19:49:22 -06:00
|
|
|
{
|
|
|
|
found = true;
|
2017-05-26 22:26:14 -06:00
|
|
|
it.second->apply(state);
|
2015-06-05 19:49:22 -06:00
|
|
|
}
|
|
|
|
}
|
2017-05-26 22:26:14 -06:00
|
|
|
|
|
|
|
if (state == false)
|
|
|
|
{
|
|
|
|
// dismiss the confirmation too
|
|
|
|
confirmation_base::set_state(name, confirmation_base::INACTIVE);
|
|
|
|
}
|
|
|
|
|
2015-11-21 18:58:49 -07:00
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
void enable_conf (color_ostream &out, string name, bool state)
|
|
|
|
{
|
|
|
|
if (!set_conf_state(name, state))
|
2015-06-05 19:49:22 -06:00
|
|
|
out.printerr("Unrecognized option: %s\n", name.c_str());
|
|
|
|
}
|
|
|
|
|
2015-06-06 08:31:12 -06:00
|
|
|
command_result df_confirm (color_ostream &out, vector <string> & parameters)
|
2015-06-05 19:49:22 -06:00
|
|
|
{
|
|
|
|
CoreSuspender suspend;
|
|
|
|
bool state = true;
|
2015-11-21 16:59:30 -07:00
|
|
|
if (parameters.empty() || in_vector(parameters, "help") || in_vector(parameters, "status"))
|
2015-07-27 10:03:29 -06:00
|
|
|
{
|
|
|
|
out << "Available options: \n";
|
2017-05-26 22:26:14 -06:00
|
|
|
for (auto it : confirmations)
|
|
|
|
out.print(" %20s: %s\n", it.first.c_str(), it.second->is_enabled() ? "enabled" : "disabled");
|
2015-07-27 10:03:29 -06:00
|
|
|
return CR_OK;
|
|
|
|
}
|
2017-05-26 22:26:14 -06:00
|
|
|
for (string param : parameters)
|
2015-06-05 19:49:22 -06:00
|
|
|
{
|
2017-05-26 22:26:14 -06:00
|
|
|
if (param == "enable")
|
2015-06-05 19:49:22 -06:00
|
|
|
state = true;
|
2017-05-26 22:26:14 -06:00
|
|
|
else if (param == "disable")
|
2015-06-05 19:49:22 -06:00
|
|
|
state = false;
|
2017-05-26 22:26:14 -06:00
|
|
|
else if (param == "all")
|
2015-06-05 19:49:22 -06:00
|
|
|
{
|
2017-05-26 22:26:14 -06:00
|
|
|
for (auto it : confirmations)
|
|
|
|
it.second->apply(state);
|
2015-06-05 19:49:22 -06:00
|
|
|
}
|
|
|
|
else
|
2017-05-26 22:26:14 -06:00
|
|
|
enable_conf(out, param, state);
|
2015-06-05 19:49:22 -06:00
|
|
|
}
|
|
|
|
return CR_OK;
|
|
|
|
}
|