home · contact · privacy
All control action is moved from main() to the control library.
[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_action_key(), save_keybindings(),
9                           * keyswin_move_selection(), keyswin_mod_key()
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 "misc.h" /* for scroll_pad(), toggle_window(),
15                    * growshrink_active_window()
16                    */
17 #include "map_object_actions.h" /* for player_wait(), move_player() */
18
19
20
21 extern void record_control(int action, struct World * world)
22 {
23     if (0 == action)
24     {
25         player_wait(world);
26     }
27     else if (NORTH == action)
28     {
29         move_player(world, NORTH);
30     }
31     else if (EAST  == action)
32     {
33         move_player(world, EAST);
34     }
35     else if (SOUTH == action)
36     {
37         move_player(world, SOUTH);
38     }
39     else if (WEST == action)
40     {
41         move_player(world, WEST);
42     }
43 }
44
45
46
47 extern uint8_t player_control(int key, struct World * world)
48 {
49     if      (key == get_action_key(world->keybindings, "player up"))
50     {
51         move_player(world, NORTH);
52     }
53     else if (key == get_action_key(world->keybindings, "player right"))
54     {
55         move_player(world, EAST);
56     }
57     else if (key == get_action_key(world->keybindings, "player down"))
58     {
59         move_player(world, SOUTH);
60     }
61     else if (key == get_action_key(world->keybindings, "player left"))
62     {
63         move_player(world, WEST);
64     }
65     else if (key == get_action_key(world->keybindings, "wait / next turn"))
66     {
67         player_wait(world);
68     }
69     else
70     {
71         return 1;
72     }
73     return 0;
74 }
75
76
77
78 extern uint8_t meta_control(int key, struct World * world)
79 {
80     struct WinMeta * win_meta = world->wins.meta;
81     struct Win * win_keys     = world->wins.keys;
82     struct Win * win_map      = world->wins.map;
83     struct Win * win_info     = world->wins.info;
84     struct Win * win_log      = world->wins.log;
85     char * err_toggle = "Trouble with toggle_window() in meta_keys().";
86     char * err_shift  = "Trouble with shift_active_win() in meta_keys().";
87     char * err_resize = "Trouble with growshrink_active_window() in "
88                         "meta_keys().";
89     if (key == get_action_key(world->keybindings, "quit"))
90     {
91         return 1;
92     }
93     else if (key == get_action_key(world->keybindings, "scroll pad right"))
94     {
95         scroll_pad(win_meta, '+');
96     }
97     else if (key == get_action_key(world->keybindings, "scroll pad left"))
98     {
99         scroll_pad(win_meta, '-');
100     }
101     else if (key == get_action_key(world->keybindings, "toggle keys window"))
102     {
103         exit_err(toggle_window(win_meta, win_keys), world, err_toggle);
104     }
105     else if (key == get_action_key(world->keybindings, "toggle map window"))
106     {
107         exit_err(toggle_window(win_meta, win_map), world, err_toggle);
108     }
109     else if (key == get_action_key(world->keybindings, "toggle info window"))
110     {
111         exit_err(toggle_window(win_meta, win_info), world, err_toggle);
112     }
113     else if (key == get_action_key(world->keybindings, "toggle log window"))
114     {
115         exit_err(toggle_window(win_meta, win_log), world, err_toggle);
116     }
117     else if (key == get_action_key(world->keybindings, "cycle forwards"))
118     {
119         cycle_active_win(win_meta, 'f');
120     }
121     else if (key == get_action_key(world->keybindings, "cycle backwards"))
122     {
123         cycle_active_win(win_meta, 'b');
124     }
125     else if (key == get_action_key(world->keybindings, "shift forwards"))
126     {
127         exit_err(shift_active_win(win_meta, 'f'), world, err_shift);
128     }
129     else if (key == get_action_key(world->keybindings, "shift backwards"))
130     {
131         exit_err(shift_active_win(win_meta, 'b'), world, err_shift);
132     }
133     else if (key == get_action_key(world->keybindings, "grow horizontally"))
134     {
135         exit_err(growshrink_active_window(win_meta, '*'), world, err_resize);
136     }
137     else if (key == get_action_key(world->keybindings, "shrink horizontally"))
138     {
139         exit_err(growshrink_active_window(win_meta, '_'), world, err_resize);
140     }
141     else if (key == get_action_key(world->keybindings, "grow vertically"))
142     {
143         exit_err(growshrink_active_window(win_meta, '+'), world, err_resize);
144     }
145     else if (key == get_action_key(world->keybindings, "shrink vertically"))
146     {
147         exit_err(growshrink_active_window(win_meta, '-'), world, err_resize);
148     }
149     else if (key == get_action_key(world->keybindings, "save keys"))
150     {
151         save_keybindings(world);
152     }
153     else if (key == get_action_key(world->keybindings, "keys nav up"))
154     {
155         keyswin_move_selection (world, 'u');
156     }
157     else if (key == get_action_key(world->keybindings, "keys nav down"))
158     {
159         keyswin_move_selection (world, 'd');
160     }
161     else if (key == get_action_key(world->keybindings, "keys mod"))
162     {
163         keyswin_mod_key (world, win_meta);
164     }
165     else if (key == get_action_key(world->keybindings, "map up"))
166     {
167         map_scroll(world->map, NORTH, win_map->frame.size);
168      }
169     else if (key == get_action_key(world->keybindings, "map down"))
170     {
171         map_scroll(world->map, SOUTH, win_map->frame.size);
172     }
173     else if (key == get_action_key(world->keybindings, "map right"))
174     {
175         map_scroll(world->map, EAST, win_map->frame.size);
176     }
177     else if (key == get_action_key(world->keybindings, "map left"))
178     {
179         map_scroll(world->map, WEST, win_map->frame.size);
180     }
181     else if (key == get_action_key(world->keybindings, "map center player"))
182     {
183         map_center_player (world->map, world->player, win_map->frame.size);
184     }
185     return 0;
186 }