diff options
| author | Alexander Gavrilov | 2011-12-31 13:25:46 +0400 |
|---|---|---|
| committer | Alexander Gavrilov | 2011-12-31 13:25:46 +0400 |
| commit | b652ec4132ad97b88dfd648303bf7a99267f1742 (patch) | |
| tree | 90b27e73785ee8f2082b4339ffcbfddacc6a8df6 /plugins/stockpiles.cpp | |
| parent | 4aa77f55308236189f157f65ad9ce82c35b9dabc (diff) | |
| download | dfhack-b652ec4132ad97b88dfd648303bf7a99267f1742.tar.gz dfhack-b652ec4132ad97b88dfd648303bf7a99267f1742.tar.bz2 dfhack-b652ec4132ad97b88dfd648303bf7a99267f1742.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.cpp | 37 |
1 files changed, 24 insertions, 13 deletions
diff --git a/plugins/stockpiles.cpp b/plugins/stockpiles.cpp index a12819e1..1e0b46d7 100644 --- a/plugins/stockpiles.cpp +++ b/plugins/stockpiles.cpp @@ -21,7 +21,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 +33,16 @@ 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, + " - In 'q' or 't' mode: select a stockpile and invoke in order\n" + " to switch to the 'p' stockpile creation mode, and initialize\n" + " the custom settings from the selected stockpile.\n" + " - In 'p': invoke in order to switch back to 'q'.\n" + ) + ); } std::cerr << "world: " << sizeof(df::world) << " ui: " << sizeof(df::ui) << " b_stock: " << sizeof(building_stockpilest) << endl; @@ -44,21 +54,27 @@ DFhackCExport command_result plugin_shutdown ( Core * c ) return CR_OK; } -bool inSelectMode() { +static bool copystock_guard(Core *c, df::viewscreen *top) +{ using namespace ui_sidebar_mode; + if (!dwarfmode_hotkey(c,top)) + return false; + 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 +86,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_WRONG_USAGE; } ui->stockpile.custom_settings = sp->settings; |
