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