home · contact · privacy
Strongly overhauled keybinding managemment. Window-specific keybindings and a window...
[plomrogue] / src / control.c
1 /* control.c */
2
3 #include "control.h"
4 #include <stdint.h> /* for uint8_t */
5 #include "windows.h" /* for cycle_active_win(), shift_active_win(), struct Win,
6                       *  struct WinMeta
7                       */
8 #include "keybindings.h" /* for get_keycode_to_action(), save_keybindings(),
9                           * move_keyb_mod_selection(), mod_selected_keyb()
10                           */
11 #include "map.h" /* for map_scroll(), map_center_player(), dir enum */
12 #include "main.h" /* for World struct */
13 #include "rexit.h" /* for exit_err() */
14 #include "wincontrol.h" /* for scroll_pad(), toggle_window(),
15                          * growshrink_active_window(), reload_win_config()
16                          * toggle_winconfig(), save_win_configs(),
17                          * toggle_win_height_type(), toggle_win_width_type()
18                          */
19 #include "map_object_actions.h" /* for player_wait(), move_player() */
20 #include "command_db.h" /* for is_command_id_shortdsc() */
21
22
23
24 extern uint16_t get_available_keycode_to_action(struct World * world,
25                                                 char * name)
26 {
27     uint16_t keycode = get_keycode_to_action(world->kb_global.kbs, name);
28     if (0 != keycode || 0 == world->wmeta->active)
29     {
30         return keycode;
31     }
32     struct WinConf * wc = get_winconf_by_win(world, world->wmeta->active);
33     if (0 == wc->view)
34     {
35         keycode = get_keycode_to_action(wc->kb.kbs, name);
36     }
37     else if (1 == wc->view)
38     {
39         keycode = get_keycode_to_action(world->kb_wingeom.kbs, name);
40     }
41     else if (2 == wc->view)
42     {
43         keycode = get_keycode_to_action(world->kb_winkeys.kbs, name);
44     }
45     return keycode;
46 }
47
48
49
50 extern void record_control(int action, struct World * world)
51 {
52     if      (is_command_id_shortdsc(world, action, "wait"))
53     {
54         player_wait(world);
55     }
56     else if (is_command_id_shortdsc(world, action, "player_u"))
57     {
58         move_player(world, NORTH);
59     }
60     else if (is_command_id_shortdsc(world, action, "player_r"))
61     {
62         move_player(world, EAST);
63     }
64     else if (is_command_id_shortdsc(world, action, "player_d"))
65     {
66         move_player(world, SOUTH);
67     }
68     else if (is_command_id_shortdsc(world, action, "player_l"))
69     {
70         move_player(world, WEST);
71     }
72 }
73
74
75
76 extern uint8_t player_control(int key, struct World * world)
77 {
78     if      (key == get_available_keycode_to_action(world, "player_u"))
79     {
80         move_player(world, NORTH);
81     }
82     else if (key == get_available_keycode_to_action(world, "player_r"))
83     {
84         move_player(world, EAST);
85     }
86     else if (key == get_available_keycode_to_action(world, "player_d"))
87     {
88         move_player(world, SOUTH);
89     }
90     else if (key == get_available_keycode_to_action(world, "player_l"))
91     {
92         move_player(world, WEST);
93     }
94     else if (key == get_available_keycode_to_action(world, "wait"))
95     {
96         player_wait(world);
97     }
98     else
99     {
100         return 0;
101     }
102     return 1;
103 }
104
105
106
107 extern uint8_t wingeom_control(int key, struct World * world)
108 {
109     char * err_shift  = "Trouble with shift_active_win() in wingeom_control().";
110     char * err_resize = "Trouble with growshrink_active_window() in "
111                         "wingeom_control().";
112     if      (key == get_available_keycode_to_action(world, "to_height_t"))
113     {
114         toggle_win_height_type(world, world->wmeta->active);
115     }
116     else if (key == get_available_keycode_to_action(world, "to_width_t"))
117     {
118         toggle_win_width_type(world, world->wmeta->active);
119     }
120     else if (key == get_available_keycode_to_action(world, "grow_h"))
121     {
122         exit_err(growshrink_active_window(world, '*'), world, err_resize);
123     }
124     else if (key == get_available_keycode_to_action(world, "shri_h"))
125     {
126         exit_err(growshrink_active_window(world, '_'), world, err_resize);
127     }
128     else if (key == get_available_keycode_to_action(world, "grow_v"))
129     {
130         exit_err(growshrink_active_window(world, '+'), world, err_resize);
131     }
132     else if (key == get_available_keycode_to_action(world, "shri_v"))
133     {
134         exit_err(growshrink_active_window(world, '-'), world, err_resize);
135     }
136     else if (key == get_available_keycode_to_action(world, "shift_f"))
137     {
138         exit_err(shift_active_win(world->wmeta, 'f'), world, err_shift);
139     }
140     else if (key == get_available_keycode_to_action(world, "shift_b"))
141     {
142         exit_err(shift_active_win(world->wmeta, 'b'), world, err_shift);
143     }
144     else
145     {
146         return 0;
147     }
148     return 1;
149 }
150
151
152
153 extern uint8_t winkeyb_control(int key, struct World * world)
154 {
155     struct WinConf * wc = get_winconf_by_win(world, world->wmeta->active);
156     if      (key == get_available_keycode_to_action(world, "w_keys_u"))
157     {
158         move_keyb_mod_selection(&wc->kb, 'u');
159     }
160     else if (key == get_available_keycode_to_action(world, "w_keys_d"))
161     {
162         move_keyb_mod_selection(&wc->kb, 'd');
163     }
164     else if (key == get_available_keycode_to_action(world, "w_keys_m"))
165     {
166         mod_selected_keyb(world, &wc->kb);
167     }
168     else
169     {
170         return 0;
171     }
172     return 1;
173 }
174
175
176
177 extern uint8_t meta_control(int key, struct World * world)
178 {
179     struct WinMeta * win_meta = world->wmeta;
180     struct Win * win_keys     = get_win_by_id(world, '0'); /* Bad hardcoding. */
181     struct Win * win_map      = get_win_by_id(world, 'm'); /* TODO: Replace.  */
182     struct Win * win_info     = get_win_by_id(world, 'i'); /*                 */
183     struct Win * win_log      = get_win_by_id(world, 'l'); /*                 */
184     char * err_toggle = "Trouble with toggle_window() in meta_control().";
185     if      (key == get_available_keycode_to_action(world, "quit"))
186     {
187         return 1;
188     }
189     else if (key == get_available_keycode_to_action(world, "winconf"))
190     {
191         toggle_winconfig(world, world->wmeta->active);
192     }
193     else if (key == get_available_keycode_to_action(world, "cyc_win_f"))
194     {
195         cycle_active_win(world->wmeta, 'f');
196     }
197     else if (key == get_available_keycode_to_action(world, "cyc_win_b"))
198     {
199         cycle_active_win(world->wmeta, 'b');
200     }
201     else if (key == get_available_keycode_to_action(world, "scrl_r"))
202     {
203         scroll_pad(win_meta, '+');
204     }
205     else if (key == get_available_keycode_to_action(world, "scrl_l"))
206     {
207         scroll_pad(win_meta, '-');
208     }
209     else if (key == get_available_keycode_to_action(world, "to_g_keywin"))
210     {
211         exit_err(toggle_window(win_meta, win_keys), world, err_toggle);
212     }
213     else if (key == get_available_keycode_to_action(world, "to_wg_keywin"))
214     {
215         uint8_t test = toggle_window(win_meta, get_win_by_id(world, '1'));
216         exit_err(test, world, err_toggle);
217     }
218     else if (key == get_available_keycode_to_action(world, "to_wk_keywin"))
219     {
220         uint8_t test = toggle_window(win_meta, get_win_by_id(world, '2'));
221         exit_err(test, world, err_toggle);
222     }
223     else if (key == get_available_keycode_to_action(world, "to_mapwin"))
224     {
225         exit_err(toggle_window(win_meta, win_map), world, err_toggle);
226     }
227     else if (key == get_available_keycode_to_action(world, "to_infowin"))
228     {
229         exit_err(toggle_window(win_meta, win_info), world, err_toggle);
230     }
231     else if (key == get_available_keycode_to_action(world, "to_logwin"))
232     {
233         exit_err(toggle_window(win_meta, win_log), world, err_toggle);
234     }
235     else if (key == get_available_keycode_to_action(world, "save_keys"))
236     {
237         save_keybindings(world, "config/keybindings_global",
238                          &world->kb_global);
239         save_keybindings(world, "config/keybindings_wingeom",
240                          &world->kb_wingeom);
241         save_keybindings(world, "config/keybindings_winkeys",
242                          &world->kb_winkeys);
243     }
244     else if (key == get_available_keycode_to_action(world, "g_keys_u"))
245     {
246         move_keyb_mod_selection(&world->kb_global, 'u');
247     }
248     else if (key == get_available_keycode_to_action(world, "g_keys_d"))
249     {
250         move_keyb_mod_selection(&world->kb_global, 'd');
251     }
252     else if (key == get_available_keycode_to_action(world, "g_keys_m"))
253     {
254         mod_selected_keyb(world, &world->kb_global);
255     }
256     else if (key == get_available_keycode_to_action(world, "wg_keys_u"))
257     {
258         move_keyb_mod_selection(&world->kb_wingeom, 'u');
259     }
260     else if (key == get_available_keycode_to_action(world, "wg_keys_d"))
261     {
262         move_keyb_mod_selection(&world->kb_wingeom, 'd');
263     }
264     else if (key == get_available_keycode_to_action(world, "wg_keys_m"))
265     {
266         mod_selected_keyb(world, &world->kb_wingeom);
267     }
268     else if (key == get_available_keycode_to_action(world, "wk_keys_u"))
269     {
270         move_keyb_mod_selection(&world->kb_winkeys, 'u');
271     }
272     else if (key == get_available_keycode_to_action(world, "wk_keys_d"))
273     {
274         move_keyb_mod_selection(&world->kb_winkeys, 'd');
275     }
276     else if (key == get_available_keycode_to_action(world, "wk_keys_m"))
277     {
278         mod_selected_keyb(world, &world->kb_winkeys);
279     }
280     else if (key == get_available_keycode_to_action(world, "map_u"))
281     {
282         map_scroll(world->map, NORTH, win_map->frame.size);
283      }
284     else if (key == get_available_keycode_to_action(world, "map_d"))
285     {
286         map_scroll(world->map, SOUTH, win_map->frame.size);
287     }
288     else if (key == get_available_keycode_to_action(world, "map_r"))
289     {
290         map_scroll(world->map, EAST, win_map->frame.size);
291     }
292     else if (key == get_available_keycode_to_action(world, "map_l"))
293     {
294         map_scroll(world->map, WEST, win_map->frame.size);
295     }
296     else if (key == get_available_keycode_to_action(world, "map_c"))
297     {
298         map_center_player(world->map, world->player, win_map->frame.size);
299     }
300     else if (key == get_available_keycode_to_action(world, "reload_wins"))
301     {
302         reload_win_config(world);
303     }
304     else if (key == get_available_keycode_to_action(world, "winconf"))
305     {
306         toggle_winconfig(world, world->wmeta->active);
307     }
308     else if (key == get_available_keycode_to_action(world, "save_winconf"))
309     {
310         save_win_configs(world);
311     }
312     return 0;
313 }