X-Git-Url: https://plomlompom.com/repos/feed.xml?a=blobdiff_plain;f=src%2Fcontrol.c;h=cb5ed3bebd116cb34823f3ded1a99f2b41e0598b;hb=42f6cd9789e06f0257a078a33fa13aaea0714fce;hp=c1e295f2adfdbbf8f024c6067acd1f73ef65c964;hpb=abdd94745a788288c022a017233014a1eedcaf80;p=plomrogue diff --git a/src/control.c b/src/control.c index c1e295f..cb5ed3b 100644 --- a/src/control.c +++ b/src/control.c @@ -5,314 +5,253 @@ #include "windows.h" /* for cycle_active_win(), shift_active_win(), struct Win, * struct WinMeta */ -#include "keybindings.h" /* for get_keycode_to_action(), save_keybindings(), - * move_keyb_mod_selection(), mod_selected_keyb() +#include "keybindings.h" /* for get_keycode_to_action(), mod_selected_keyb(), + * move_keyb_mod_selection(), get_func_to_keycode() */ -#include "map.h" /* for map_scroll(), map_center_player(), dir enum */ -#include "main.h" /* for World struct */ +#include "map.h" /* for map_scroll(), map_center() */ +#include "main.h" /* for world global */ #include "rexit.h" /* for exit_err() */ -#include "wincontrol.h" /* for scroll_pad(), toggle_window(), - * growshrink_active_window(), reload_win_config() - * toggle_winconfig(), save_win_configs(), +#include "wincontrol.h" /* for struct WinConf, scroll_pad(), toggle_window(), + * growshrink_active_window(),toggle_winconfig(), * toggle_win_height_type(), toggle_win_width_type() */ -#include "map_object_actions.h" /* for player_wait(), move_player() */ +#include "map_object_actions.h" /* for player_wait(), move_player(), + * player_drop(), player_pick() + */ #include "command_db.h" /* for is_command_id_shortdsc() */ +#include "misc.h" /* for reload_interface_conf(), save_interface_conf(), + * nav_inventory() + */ -extern uint16_t get_available_keycode_to_action(struct World * world, - char * name) +/* If "cmd" (either (type = "i") command or (type = "k") keybinding identifier) + * matches "match" in is_cmd_id_shortdsc() or get_available_keycode_to_action(), + * execute "f" with provided char arguments and return 1; else only return 0. + */ +static uint8_t try_cmd_0args(char type, int cmd, char * match, void (* f) ()); +static uint8_t try_cmd_1args(char type, int cmd, char * match, + void (* f) (char), char c); +static uint8_t try_cmd_2args(char type, int cmd, char * match, + void (* f) (char, char), char c1, char c2); + +/* Return pointer to global keybindings or to keybindings for wingeometry config + * (c = "g") or winkeys config (c = "k") or active window's keybindings ("w"). + */ +static struct KeyBiData * select_keybidata_pointer(char c); + +/* Wrappers to make some functions compatible to try_cmd_* single char args. */ +static void wrap_mod_selected_keyb(char c); +static void wrap_mv_kb_mod(char c1, char c2); + + + +static uint8_t try_cmd_0args(char type, int cmd, char * match, void (* f) ()) { - uint16_t keycode = get_keycode_to_action(world->kb_global.kbs, name); - if (0 != keycode || 0 == world->wmeta->active) + if ( ('k' == type && cmd == get_available_keycode_to_action(match)) + || ('i' == type && is_command_id_shortdsc(cmd, match))) { - return keycode; - } - struct WinConf * wc = get_winconf_by_win(world, world->wmeta->active); - if (0 == wc->view) - { - keycode = get_keycode_to_action(wc->kb.kbs, name); - } - else if (1 == wc->view) - { - keycode = get_keycode_to_action(world->kb_wingeom.kbs, name); - } - else if (2 == wc->view) - { - keycode = get_keycode_to_action(world->kb_winkeys.kbs, name); + f(); + return 1; } - return keycode; + return 0; } -extern void record_control(int action, struct World * world) +static uint8_t try_cmd_1args(char type, int cmd, char * match, + void (* f) (char), char c) { - if (is_command_id_shortdsc(world, action, "wait")) + if ( ('k' == type && cmd == get_available_keycode_to_action(match)) + || ('i' == type && is_command_id_shortdsc(cmd, match))) { - player_wait(world); - } - else if (is_command_id_shortdsc(world, action, "player_u")) - { - move_player(world, NORTH); - } - else if (is_command_id_shortdsc(world, action, "player_r")) - { - move_player(world, EAST); - } - else if (is_command_id_shortdsc(world, action, "player_d")) - { - move_player(world, SOUTH); - } - else if (is_command_id_shortdsc(world, action, "player_l")) - { - move_player(world, WEST); + f(c); + return 1; } + return 0; } -extern uint8_t player_control(int key, struct World * world) +static uint8_t try_cmd_2args(char type, int cmd, char * match, + void (* f) (char, char), char c1, char c2) { - if (key == get_available_keycode_to_action(world, "player_u")) - { - move_player(world, NORTH); - } - else if (key == get_available_keycode_to_action(world, "player_r")) - { - move_player(world, EAST); - } - else if (key == get_available_keycode_to_action(world, "player_d")) - { - move_player(world, SOUTH); - } - else if (key == get_available_keycode_to_action(world, "player_l")) - { - move_player(world, WEST); - } - else if (key == get_available_keycode_to_action(world, "wait")) + if ( ('k' == type && cmd == get_available_keycode_to_action(match)) + || ('i' == type && is_command_id_shortdsc(cmd, match))) { - player_wait(world); - } - else - { - return 0; + f(c1, c2); + return 1; } - return 1; + return 0; } -extern uint8_t wingeom_control(int key, struct World * world) +static struct KeyBiData * select_keybidata_pointer(char c) { - char * err_shift = "Trouble with shift_active_win() in wingeom_control()."; - char * err_resize = "Trouble with growshrink_active_window() in " - "wingeom_control()."; - if (key == get_available_keycode_to_action(world, "to_height_t")) + struct KeyBiData * kbd; + kbd = &world.kb_global; + if ('g' == c) { - toggle_win_height_type(world, world->wmeta->active); + kbd = &world.kb_wingeom; } - else if (key == get_available_keycode_to_action(world, "to_width_t")) + else if ('k' == c) { - toggle_win_width_type(world, world->wmeta->active); + kbd = &world.kb_winkeys; } - else if (key == get_available_keycode_to_action(world, "grow_h")) + else if ('w' == c) { - exit_err(growshrink_active_window(world, '*'), world, err_resize); + struct WinConf * wc = get_winconf_by_win(world.wmeta->active); + kbd = &wc->kb; } - else if (key == get_available_keycode_to_action(world, "shri_h")) - { - exit_err(growshrink_active_window(world, '_'), world, err_resize); - } - else if (key == get_available_keycode_to_action(world, "grow_v")) - { - exit_err(growshrink_active_window(world, '+'), world, err_resize); - } - else if (key == get_available_keycode_to_action(world, "shri_v")) + return kbd; +} + + + +static void wrap_mod_selected_keyb(char c) +{ + mod_selected_keyb(select_keybidata_pointer(c)); +} + + + +static void wrap_mv_kb_mod(char c1, char c2) +{ + move_keyb_mod_selection(select_keybidata_pointer(c1), c2); +} + + + +extern uint16_t get_available_keycode_to_action(char * name) +{ + uint16_t keycode = get_keycode_to_action(world.kb_global.kbs, name); + if (0 != keycode || 0 == world.wmeta->active) { - exit_err(growshrink_active_window(world, '-'), world, err_resize); + return keycode; } - else if (key == get_available_keycode_to_action(world, "shift_f")) + struct WinConf * wc = get_winconf_by_win(world.wmeta->active); + if (0 == wc->view) { - exit_err(shift_active_win(world->wmeta, 'f'), world, err_shift); + keycode = get_keycode_to_action(wc->kb.kbs, name); } - else if (key == get_available_keycode_to_action(world, "shift_b")) + else if (1 == wc->view) { - exit_err(shift_active_win(world->wmeta, 'b'), world, err_shift); + keycode = get_keycode_to_action(world.kb_wingeom.kbs, name); } - else + else if (2 == wc->view) { - return 0; + keycode = get_keycode_to_action(world.kb_winkeys.kbs, name); } - return 1; + return keycode; } -extern uint8_t winkeyb_control(int key, struct World * world) +extern uint8_t player_control_by_key(int key) { - struct WinConf * wc = get_winconf_by_win(world, world->wmeta->active); - if (key == get_available_keycode_to_action(world, "w_keys_u")) + char * action_name = get_func_to_keycode(world.kb_global.kbs, key); + if (NULL == action_name && 0 != world.wmeta->active) { - move_keyb_mod_selection(&wc->kb, 'u'); + struct WinConf * wc = get_winconf_by_win(world.wmeta->active); + action_name = get_func_to_keycode(wc->kb.kbs, key); } - else if (key == get_available_keycode_to_action(world, "w_keys_d")) + if (NULL != action_name) { - move_keyb_mod_selection(&wc->kb, 'd'); + uint8_t action_id = get_command_id(action_name); + return player_control_by_id(action_id); } - else if (key == get_available_keycode_to_action(world, "w_keys_m")) - { - mod_selected_keyb(world, &wc->kb); - } - else - { - return 0; - } - return 1; + return 0; } -extern uint8_t meta_control(int key, struct World * world) +extern uint8_t player_control_by_id(int action) { - struct WinMeta * win_meta = world->wmeta; - struct Win * win_keys = get_win_by_id(world, '0'); /* Bad hardcoding. */ - struct Win * win_map = get_win_by_id(world, 'm'); /* TODO: Replace. */ - struct Win * win_info = get_win_by_id(world, 'i'); /* */ - struct Win * win_log = get_win_by_id(world, 'l'); /* */ - char * err_toggle = "Trouble with toggle_window() in meta_control()."; - if (key == get_available_keycode_to_action(world, "quit")) + if ( try_cmd_0args('i', action, "wait", player_wait) + || try_cmd_0args('i', action, "drop", player_drop) + || try_cmd_0args('i', action, "pick", player_pick) + || try_cmd_0args('i', action, "use", player_use) + || try_cmd_1args('i', action, "player_u", move_player, 'N') + || try_cmd_1args('i', action, "player_d", move_player, 'S') + || try_cmd_1args('i', action, "player_r", move_player, 'E') + || try_cmd_1args('i', action, "player_l", move_player, 'W')) { return 1; } - else if (key == get_available_keycode_to_action(world, "winconf")) - { - toggle_winconfig(world, world->wmeta->active); - } - else if (key == get_available_keycode_to_action(world, "cyc_win_f")) - { - cycle_active_win(world->wmeta, 'f'); - } - else if (key == get_available_keycode_to_action(world, "cyc_win_b")) - { - cycle_active_win(world->wmeta, 'b'); - } - else if (key == get_available_keycode_to_action(world, "scrl_r")) - { - scroll_pad(win_meta, '+'); - } - else if (key == get_available_keycode_to_action(world, "scrl_l")) - { - scroll_pad(win_meta, '-'); - } - else if (key == get_available_keycode_to_action(world, "to_a_keywin")) - { - uint8_t test = toggle_window(win_meta, get_win_by_id(world, 'k')); - exit_err(test, world, err_toggle); - } - else if (key == get_available_keycode_to_action(world, "to_g_keywin")) - { - exit_err(toggle_window(win_meta, win_keys), world, err_toggle); - } - else if (key == get_available_keycode_to_action(world, "to_wg_keywin")) - { - uint8_t test = toggle_window(win_meta, get_win_by_id(world, '1')); - exit_err(test, world, err_toggle); - } - else if (key == get_available_keycode_to_action(world, "to_wk_keywin")) - { - uint8_t test = toggle_window(win_meta, get_win_by_id(world, '2')); - exit_err(test, world, err_toggle); - } - else if (key == get_available_keycode_to_action(world, "to_mapwin")) - { - exit_err(toggle_window(win_meta, win_map), world, err_toggle); - } - else if (key == get_available_keycode_to_action(world, "to_infowin")) - { - exit_err(toggle_window(win_meta, win_info), world, err_toggle); - } - else if (key == get_available_keycode_to_action(world, "to_logwin")) - { - exit_err(toggle_window(win_meta, win_log), world, err_toggle); - } - else if (key == get_available_keycode_to_action(world, "save_keys")) - { - save_keybindings(world, "config/keybindings_global", - &world->kb_global); - save_keybindings(world, "config/keybindings_wingeom", - &world->kb_wingeom); - save_keybindings(world, "config/keybindings_winkeys", - &world->kb_winkeys); - } - else if (key == get_available_keycode_to_action(world, "g_keys_u")) - { - move_keyb_mod_selection(&world->kb_global, 'u'); - } - else if (key == get_available_keycode_to_action(world, "g_keys_d")) - { - move_keyb_mod_selection(&world->kb_global, 'd'); - } - else if (key == get_available_keycode_to_action(world, "g_keys_m")) - { - mod_selected_keyb(world, &world->kb_global); - } - else if (key == get_available_keycode_to_action(world, "wg_keys_u")) - { - move_keyb_mod_selection(&world->kb_wingeom, 'u'); - } - else if (key == get_available_keycode_to_action(world, "wg_keys_d")) - { - move_keyb_mod_selection(&world->kb_wingeom, 'd'); - } - else if (key == get_available_keycode_to_action(world, "wg_keys_m")) - { - mod_selected_keyb(world, &world->kb_wingeom); - } - else if (key == get_available_keycode_to_action(world, "wk_keys_u")) - { - move_keyb_mod_selection(&world->kb_winkeys, 'u'); - } - else if (key == get_available_keycode_to_action(world, "wk_keys_d")) - { - move_keyb_mod_selection(&world->kb_winkeys, 'd'); - } - else if (key == get_available_keycode_to_action(world, "wk_keys_m")) - { - mod_selected_keyb(world, &world->kb_winkeys); - } - else if (key == get_available_keycode_to_action(world, "map_u")) - { - map_scroll(world->map, NORTH, win_map->frame.size); - } - else if (key == get_available_keycode_to_action(world, "map_d")) - { - map_scroll(world->map, SOUTH, win_map->frame.size); - } - else if (key == get_available_keycode_to_action(world, "map_r")) - { - map_scroll(world->map, EAST, win_map->frame.size); - } - else if (key == get_available_keycode_to_action(world, "map_l")) - { - map_scroll(world->map, WEST, win_map->frame.size); - } - else if (key == get_available_keycode_to_action(world, "map_c")) - { - map_center_player(world->map, world->player, win_map->frame.size); - } - else if (key == get_available_keycode_to_action(world, "reload_wins")) - { - reload_win_config(world); - } - else if (key == get_available_keycode_to_action(world, "winconf")) + return 0; +} + + + +extern uint8_t wingeom_control(int key) +{ + if ( try_cmd_0args('k', key, "to_height_t", toggle_win_height_type) + || try_cmd_0args('k', key, "to_width_t", toggle_win_width_type) + || try_cmd_1args('k', key, "grow_h", growshrink_active_window, '*') + || try_cmd_1args('k', key, "shri_h", growshrink_active_window, '_') + || try_cmd_1args('k', key, "grow_v", growshrink_active_window, '+') + || try_cmd_1args('k', key, "shri_v", growshrink_active_window, '-') + || try_cmd_1args('k', key, "shift_f", shift_active_win, 'f') + || try_cmd_1args('k', key, "shift_b", shift_active_win, 'b')) { - toggle_winconfig(world, world->wmeta->active); + return 1; } - else if (key == get_available_keycode_to_action(world, "save_winconf")) + return 0; +} + + + +extern uint8_t winkeyb_control(int key) +{ + if ( try_cmd_1args('k', key, "w_keys_m", wrap_mod_selected_keyb, 'w') + || try_cmd_2args('k', key, "w_keys_u", wrap_mv_kb_mod, 'w', 'u') + || try_cmd_2args('k', key, "w_keys_d", wrap_mv_kb_mod, 'w', 'd')) { - save_win_configs(world); + return 1; } return 0; } + + + +extern uint8_t meta_control(int key) +{ + uint8_t ret = (key == get_available_keycode_to_action("quit")); + if ( (0 == ret) + && ( try_cmd_0args('k', key, "winconf", toggle_winconfig) + || try_cmd_0args('k', key, "reload_conf", reload_interface_conf) + || try_cmd_0args('k', key, "save_conf", save_interface_conf) + || try_cmd_0args('k', key, "map_c", map_center) + || try_cmd_1args('k', key, "scrl_r", scroll_pad, '+') + || try_cmd_1args('k', key, "scrl_l", scroll_pad, '-') + || try_cmd_1args('k', key, "to_a_keywin", toggle_window, 'k') + || try_cmd_1args('k', key, "to_g_keywin", toggle_window, '0') + || try_cmd_1args('k', key, "to_wg_keywin", toggle_window, '1') + || try_cmd_1args('k', key, "to_wk_keywin", toggle_window, '2') + || try_cmd_1args('k', key, "to_mapwin", toggle_window, 'm') + || try_cmd_1args('k', key, "to_infowin", toggle_window, 'i') + || try_cmd_1args('k', key, "to_inv", toggle_window, 'c') + || try_cmd_1args('k', key, "to_logwin", toggle_window, 'l') + || try_cmd_1args('k', key, "cyc_win_f", cycle_active_win, 'f') + || try_cmd_1args('k', key, "cyc_win_b", cycle_active_win, 'b') + || try_cmd_1args('k', key, "g_keys_m", wrap_mod_selected_keyb, 'G') + || try_cmd_1args('k', key, "wg_keys_m", wrap_mod_selected_keyb, 'g') + || try_cmd_1args('k', key, "wk_keys_m", wrap_mod_selected_keyb, 'k') + || try_cmd_1args('k', key, "inv_u", nav_inventory, 'u') + || try_cmd_1args('k', key, "inv_d", nav_inventory, 'd') + || try_cmd_1args('k', key, "map_u", map_scroll, 'N') + || try_cmd_1args('k', key, "map_d", map_scroll, 'S') + || try_cmd_1args('k', key, "map_r", map_scroll, 'E') + || try_cmd_1args('k', key, "map_l", map_scroll, 'W') + || try_cmd_2args('k', key, "g_keys_u", wrap_mv_kb_mod, 'G', 'u') + || try_cmd_2args('k', key, "g_keys_d", wrap_mv_kb_mod, 'G', 'd') + || try_cmd_2args('k', key, "wg_keys_u", wrap_mv_kb_mod, 'g', 'u') + || try_cmd_2args('k', key, "wg_keys_d", wrap_mv_kb_mod, 'g', 'd') + || try_cmd_2args('k', key, "wk_keys_u", wrap_mv_kb_mod, 'k', 'u') + || try_cmd_2args('k', key, "wk_keys_d", wrap_mv_kb_mod, 'k', 'd'))) + { + ; + } + return ret; +}