|
9 | 9 | #include "RemoteTools.h" |
10 | 10 |
|
11 | 11 | #include "modules/Gui.h" |
| 12 | +#include "modules/Hotkey.h" |
12 | 13 | #include "modules/World.h" |
13 | 14 |
|
14 | 15 | #include "df/viewscreen_new_regionst.h" |
@@ -282,56 +283,62 @@ namespace DFHack |
282 | 283 |
|
283 | 284 | command_result Commands::keybinding(color_ostream& con, Core& core, const std::string& first, const std::vector<std::string>& parts) |
284 | 285 | { |
285 | | - if (parts.size() >= 3 && (parts[0] == "set" || parts[0] == "add")) |
286 | | - { |
287 | | - std::string keystr = parts[1]; |
| 286 | + using Hotkey::KeySpec; |
| 287 | + auto hotkey_mgr = core.getHotkeyManager(); |
| 288 | + std::string parse_error; |
| 289 | + if (parts.size() >= 3 && (parts[0] == "set" || parts[0] == "add")) { |
| 290 | + const std::string& keystr = parts[1]; |
288 | 291 | if (parts[0] == "set") |
289 | | - core.ClearKeyBindings(keystr); |
290 | | - // for (int i = parts.size()-1; i >= 2; i--) |
291 | | - for (const auto& part : parts | std::views::drop(2) | std::views::reverse) |
292 | | - { |
293 | | - if (!core.AddKeyBinding(keystr, part)) |
294 | | - { |
295 | | - con.printerr("Invalid key spec: %s\n", keystr.c_str()); |
296 | | - return CR_FAILURE; |
| 292 | + hotkey_mgr->removeKeybind(keystr); |
| 293 | + for (const auto& part : parts | std::views::drop(2) | std::views::reverse) { |
| 294 | + auto spec = KeySpec::parse(keystr, &parse_error); |
| 295 | + if (!spec.has_value()) { |
| 296 | + con.printerr("%s\n", parse_error.c_str()); |
| 297 | + break; |
| 298 | + } |
| 299 | + if (!hotkey_mgr->addKeybind(spec.value(), part)) { |
| 300 | + con.printerr("Invalid command: '%s'\n", part.c_str()); |
| 301 | + break; |
297 | 302 | } |
298 | 303 | } |
299 | 304 | } |
300 | | - else if (parts.size() >= 2 && parts[0] == "clear") |
301 | | - { |
302 | | - // for (size_t i = 1; i < parts.size(); i++) |
303 | | - for (const auto& part : parts | std::views::drop(1)) |
304 | | - { |
305 | | - if (!core.ClearKeyBindings(part)) |
306 | | - { |
307 | | - con.printerr("Invalid key spec: %s\n", part.c_str()); |
308 | | - return CR_FAILURE; |
| 305 | + else if (parts.size() >= 2 && parts[0] == "clear") { |
| 306 | + for (const auto& part : parts | std::views::drop(1)) { |
| 307 | + auto spec = KeySpec::parse(part, &parse_error); |
| 308 | + if (!spec.has_value()) { |
| 309 | + con.printerr("%s\n", parse_error.c_str()); |
| 310 | + } |
| 311 | + if (!hotkey_mgr->removeKeybind(spec.value())) { |
| 312 | + con.printerr("No matching keybinds to remove\n"); |
| 313 | + break; |
309 | 314 | } |
310 | 315 | } |
311 | 316 | } |
312 | | - else if (parts.size() == 2 && parts[0] == "list") |
313 | | - { |
314 | | - std::vector<std::string> list = core.ListKeyBindings(parts[1]); |
| 317 | + else if (parts.size() == 2 && parts[0] == "list") { |
| 318 | + auto spec = KeySpec::parse(parts[1], &parse_error); |
| 319 | + if (!spec.has_value()) { |
| 320 | + con.printerr("%s\n", parse_error.c_str()); |
| 321 | + return CR_FAILURE; |
| 322 | + } |
| 323 | + std::vector<std::string> list = hotkey_mgr->listKeybinds(spec.value()); |
315 | 324 | if (list.empty()) |
316 | 325 | con << "No bindings." << std::endl; |
317 | 326 | for (const auto& kb : list) |
318 | 327 | con << " " << kb << std::endl; |
319 | 328 | } |
320 | | - else |
321 | | - { |
322 | | - con << "Usage:" << std::endl |
323 | | - << " keybinding list <key>" << std::endl |
324 | | - << " keybinding clear <key>[@context]..." << std::endl |
325 | | - << " keybinding set <key>[@context] \"cmdline\" \"cmdline\"..." << std::endl |
326 | | - << " keybinding add <key>[@context] \"cmdline\" \"cmdline\"..." << std::endl |
327 | | - << "Later adds, and earlier items within one command have priority." << std::endl |
328 | | - << "Supported keys: [Ctrl-][Alt-][Shift-](A-Z, 0-9, F1-F12, `, or Enter)." << std::endl |
329 | | - << "Context may be used to limit the scope of the binding, by" << std::endl |
330 | | - << "requiring the current context to have a certain prefix." << std::endl |
331 | | - << "Current UI context is: " << std::endl |
| 329 | + else { |
| 330 | + con << "Usage:\n" |
| 331 | + << " keybinding list <key>\n" |
| 332 | + << " keybinding clear <key>[@context]...\n" |
| 333 | + << " keybinding set <key>[@context] \"cmdline\" \"cmdline\"...\n" |
| 334 | + << " keybinding add <key>[@context] \"cmdline\" \"cmdline\"...\n" |
| 335 | + << "Later adds, and earlier items within one command have priority.\n" |
| 336 | + << "Key format: [Ctrl-][Alt-][Super-][Shift-](A-Z, 0-9, F1-F12, `, etc.).\n" |
| 337 | + << "Context may be used to limit the scope of the binding, by\n" |
| 338 | + << "requiring the current context to have a certain prefix.\n" |
| 339 | + << "Current UI context is: \n" |
332 | 340 | << join_strings("\n", Gui::getCurFocus(true)) << std::endl; |
333 | 341 | } |
334 | | - |
335 | 342 | return CR_OK; |
336 | 343 | } |
337 | 344 |
|
|
0 commit comments