summaryrefslogtreecommitdiff
path: root/plugins/stockpiles.cpp
diff options
context:
space:
mode:
authorAlexander Gavrilov2011-12-30 23:25:50 +0400
committerAlexander Gavrilov2011-12-30 23:25:50 +0400
commit2222757e77d4c7f2a337c8b6a0f663414702d111 (patch)
tree57f47ae94b7be0c8585de0adc23635a4df095b59 /plugins/stockpiles.cpp
parent4aa77f55308236189f157f65ad9ce82c35b9dabc (diff)
downloaddfhack-2222757e77d4c7f2a337c8b6a0f663414702d111.tar.gz
dfhack-2222757e77d4c7f2a337c8b6a0f663414702d111.tar.bz2
dfhack-2222757e77d4c7f2a337c8b6a0f663414702d111.tar.xz
Implement context-sensitive keybinding support.
Allow defining commands with guard conditions, and binding one or more commands to alphabetic and function keys. When the relevant key is pressed, the first listed command with successfully evaluated guard is chosen. For consistency, the guard is also checked when the command is invoked from the console; this requires suspending the core inside PluginManager, before invoking plugin code.
Diffstat (limited to 'plugins/stockpiles.cpp')
-rw-r--r--plugins/stockpiles.cpp33
1 files changed, 19 insertions, 14 deletions
diff --git a/plugins/stockpiles.cpp b/plugins/stockpiles.cpp
index a12819e1..5a4de463 100644
--- a/plugins/stockpiles.cpp
+++ b/plugins/stockpiles.cpp
@@ -8,6 +8,7 @@
#include <dfhack/df/ui.h>
#include <dfhack/df/building_stockpilest.h>
#include <dfhack/df/selection_rect.h>
+#include <dfhack/df/viewscreen_dwarfmodest.h>
using std::vector;
using std::string;
@@ -21,7 +22,8 @@ using df::global::selection_rect;
using df::building_stockpilest;
-DFhackCExport command_result copystock(Core * c, vector <string> & parameters);
+static command_result copystock(Core *c, vector <string> & parameters);
+static bool copystock_guard(Core *c, df::viewscreen *top);
DFhackCExport const char * plugin_name ( void )
{
@@ -32,7 +34,12 @@ DFhackCExport command_result plugin_init (Core *c, std::vector <PluginCommand> &
{
commands.clear();
if (world && ui) {
- commands.push_back(PluginCommand("copystock", "Copy stockpile under cursor.", copystock));
+ commands.push_back(
+ PluginCommand(
+ "copystock", "Copy stockpile under cursor.", copystock,
+ copystock_guard, &df::viewscreen_dwarfmodest::_identity
+ )
+ );
}
std::cerr << "world: " << sizeof(df::world) << " ui: " << sizeof(df::ui)
<< " b_stock: " << sizeof(building_stockpilest) << endl;
@@ -44,21 +51,24 @@ DFhackCExport command_result plugin_shutdown ( Core * c )
return CR_OK;
}
-bool inSelectMode() {
+static bool copystock_guard(Core *c, df::viewscreen *)
+{
using namespace ui_sidebar_mode;
switch (ui->main.mode) {
+ case Stockpiles:
+ return true;
case BuildingItems:
case QueryBuilding:
- return true;
+ return !!virtual_cast<building_stockpilest>(world->selected_building);
default:
return false;
- }
+ }
}
-DFhackCExport command_result copystock(Core * c, vector <string> & parameters)
+static command_result copystock(Core * c, vector <string> & parameters)
{
- CoreSuspender suspend(c);
+ /* HOTKEY COMMAND: CORE ALREADY SUSPENDED */
// For convenience: when used in the stockpiles mode, switch to 'q'
if (ui->main.mode == ui_sidebar_mode::Stockpiles) {
@@ -70,15 +80,10 @@ DFhackCExport command_result copystock(Core * c, vector <string> & parameters)
return CR_OK;
}
- if (!inSelectMode()) {
- c->con << "Cannot copy stockpile in mode " << ENUM_KEY_STR(ui_sidebar_mode, ui->main.mode) << endl;
- return CR_OK;
- }
-
building_stockpilest *sp = virtual_cast<building_stockpilest>(world->selected_building);
if (!sp) {
- c->con << "Selected building isn't a stockpile." << endl;
- return CR_OK;
+ c->con.printerr("Selected building isn't a stockpile.\n");
+ return CR_FAILURE;
}
ui->stockpile.custom_settings = sp->settings;