From 1d8b2d8cea8b7d4d9c3d423f439a4c44dfaaae60 Mon Sep 17 00:00:00 2001 From: lethosor Date: Sun, 18 Jan 2015 13:03:42 -0500 Subject: [PATCH] Expose manipulator columns to Lua --- plugins/CMakeLists.txt | 2 +- plugins/lua/manipulator.lua | 17 +++++++ plugins/manipulator.cpp | 89 ++++++++++++++++++++++++++++++++++++- 3 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 plugins/lua/manipulator.lua diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index 063e8a90e..caa03cfba 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -132,7 +132,7 @@ if (BUILD_SUPPORTED) DFHACK_PLUGIN(jobutils jobutils.cpp) DFHACK_PLUGIN(lair lair.cpp) DFHACK_PLUGIN(liquids liquids.cpp Brushes.h LINK_LIBRARIES lua) - DFHACK_PLUGIN(manipulator manipulator.cpp) + DFHACK_PLUGIN(manipulator manipulator.cpp LINK_LIBRARIES lua) DFHACK_PLUGIN(mode mode.cpp) #DFHACK_PLUGIN(misery misery.cpp) DFHACK_PLUGIN(mousequery mousequery.cpp) diff --git a/plugins/lua/manipulator.lua b/plugins/lua/manipulator.lua new file mode 100644 index 000000000..a6dec63b0 --- /dev/null +++ b/plugins/lua/manipulator.lua @@ -0,0 +1,17 @@ +local _ENV = mkmodule('plugins.manipulator') + +--[[ + + Native functions: + + * isEnabled() + * setEnabled(bool) + * has_column(name) + * add_column(name, allow_column, allow_format, column_header, format_spec, format_desc) + * remove_column(name) + * list_columns() + +--]] + + +return _ENV diff --git a/plugins/manipulator.cpp b/plugins/manipulator.cpp index 489c8f35a..b9c6af838 100644 --- a/plugins/manipulator.cpp +++ b/plugins/manipulator.cpp @@ -5,6 +5,8 @@ #include #include #include +#include +#include #include #include #include @@ -407,6 +409,91 @@ bool sortBySelected (const UnitInfo *d1, const UnitInfo *d2) return descending ? (d1->selected > d2->selected) : (d1->selected < d2->selected); } +class ManipulatorColumn { +public: + typedef string(*callback)(df::unit*); + ManipulatorColumn(bool allow_column, bool allow_format, + string column_header = "", + string format_spec = "", + string format_desc = "") + :allow_column(allow_column), allow_format(allow_format), + column_header(column_header), format_spec(format_spec), format_desc(format_desc) + { } + bool allow_column; + bool allow_format; + string column_header; + string format_spec; + string format_desc; +}; + +class ManipulatorColumnList : public std::map { +public: + bool contains(string key) { return find(key) != end(); } + void remove(string key) { if (contains(key)) erase(key); } +}; + +ManipulatorColumnList column_list; + +bool has_column (string id) { return column_list.contains(id); } + +bool add_column (string id, bool allow_column, bool allow_format, + string column_header, string format_spec, string format_desc) +{ + if (!column_list.contains(id)) + { + ManipulatorColumn col(allow_column, allow_format, column_header, format_spec, format_desc); + column_list.insert(std::pair(id, col)); + return true; + } + return false; +} + +bool remove_column (string id) +{ + if (column_list.contains(id)) + { + column_list.remove(id); + return true; + } + return false; +} + +static int list_columns (lua_State *L) +{ + lua_newtable(L); + int clist_idx = lua_gettop(L); + int i = 1; + for (auto it = column_list.begin(); it != column_list.end(); ++it) + { + lua_newtable(L); + int col_idx = lua_gettop(L); + std::string foo = it->first; + std::string format_spec = it->second.format_spec; + #define set_field(key) Lua::SetField(L, it->second.key, col_idx, #key) + set_field(allow_column); + set_field(allow_format); + set_field(column_header); + set_field(format_spec); + set_field(format_desc); + #undef set_field + lua_rawseti(L, -2, i); + i++; + } + return 1; +} + +DFHACK_PLUGIN_LUA_FUNCTIONS { + DFHACK_LUA_FUNCTION(has_column), + DFHACK_LUA_FUNCTION(add_column), + DFHACK_LUA_FUNCTION(remove_column), + DFHACK_LUA_END +}; + +DFHACK_PLUGIN_LUA_COMMANDS { + DFHACK_LUA_COMMAND(list_columns), + DFHACK_LUA_END +}; + template class StringFormatter { public: @@ -1285,7 +1372,7 @@ void viewscreen_unitlaborsst::feed(set *events) case DISP_COLUMN_SELECTED: // left-click to select, right-click or shift-click to extend selection - if (enabler->mouse_rbut || (enabler->mouse_lbut && (modstate & MOD_SHIFT))) + if (enabler->mouse_rbut || (enabler->mouse_lbut && (modstate & DFH_MOD_SHIFT))) { input_row = click_unit; events->insert(interface_key::CUSTOM_SHIFT_X);