home · contact · privacy
Minor refactoring of meta_control().
[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_map      = get_win_by_id(world, 'm');
183     char * err_toggle = "Trouble with toggle_window() in meta_control().";
184     if      (key == get_available_keycode_to_action(world, "quit"))
185     {
186         return 1;
187     }
188     else if (key == get_available_keycode_to_action(world, "winconf"))
189     {
190         toggle_winconfig(world, world->wmeta->active);
191     }
192     else if (key == get_available_keycode_to_action(world, "cyc_win_f"))
193     {
194         cycle_active_win(world->wmeta, 'f');
195     }
196     else if (key == get_available_keycode_to_action(world, "cyc_win_b"))
197     {
198         cycle_active_win(world->wmeta, 'b');
199     }
200     else if (key == get_available_keycode_to_action(world, "scrl_r"))
201     {
202         scroll_pad(win_meta, '+');
203     }
204     else if (key == get_available_keycode_to_action(world, "scrl_l"))
205     {
206         scroll_pad(win_meta, '-');
207     }
208     else if (key == get_available_keycode_to_action(world, "to_a_keywin"))
209     {
210         uint8_t test = toggle_window(win_meta, get_win_by_id(world, 'k'));
211         exit_err(test, world, err_toggle);
212     }
213     else if (key == get_available_keycode_to_action(world, "to_g_keywin"))
214     {
215         uint8_t test = toggle_window(win_meta, get_win_by_id(world, '0'));
216         exit_err(test, world, err_toggle);
217     }
218     else if (key == get_available_keycode_to_action(world, "to_wg_keywin"))
219     {
220         uint8_t test = toggle_window(win_meta, get_win_by_id(world, '1'));
221         exit_err(test, world, err_toggle);
222     }
223     else if (key == get_available_keycode_to_action(world, "to_wk_keywin"))
224     {
225         uint8_t test = toggle_window(win_meta, get_win_by_id(world, '2'));
226         exit_err(test, world, err_toggle);
227     }
228     else if (key == get_available_keycode_to_action(world, "to_mapwin"))
229     {
230         exit_err(toggle_window(win_meta, win_map), world, err_toggle);
231     }
232     else if (key == get_available_keycode_to_action(world, "to_infowin"))
233     {
234         uint8_t test = toggle_window(win_meta, get_win_by_id(world, 'i'));
235         exit_err(test, world, err_toggle);
236     }
237     else if (key == get_available_keycode_to_action(world, "to_inv"))
238     {
239         uint8_t test = toggle_window(win_meta, get_win_by_id(world, 'c'));
240         exit_err(test, world, err_toggle);
241     }
242     else if (key == get_available_keycode_to_action(world, "to_logwin"))
243     {
244         uint8_t test = toggle_window(win_meta, get_win_by_id(world, 'l'));
245         exit_err(test, 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 }