home · contact · privacy
Client: reload_interface_conf() calls map_center() to re-focus map on player.
[plomrogue] / src / client / misc.c
1 /* src/client/misc.c */
2
3 #include "misc.h"
4 #include <ncurses.h> /* delwin(), getmaxy(), getmaxx(), newpad() */
5 #include <stdint.h> /* uint8_t, uint16_t, uint32_t */
6 #include "../common/rexit.h" /* exit_err() */
7 #include "cleanup.h" /* for set_cleanup_flag() */
8 #include "keybindings.h" /* init_keybindings(), free_keybindings(),
9                           * save_keybindings()
10                           */
11 #include "map_window.h" /* for map_center() */
12 #include "wincontrol.h" /* init_winconfs(), init_wins(),
13                          * sorted_wintoggle_and_activate()
14                          */
15 #include "windows.h" /* suspend_win() */
16 #include "world.h" /* global world */
17
18
19
20 extern void save_interface_conf()
21 {
22     save_keybindings("confclient/keybindings_global", &world.kb_global);
23     save_keybindings("confclient/keybindings_wingeom", &world.kb_wingeom);
24     save_keybindings("confclient/keybindings_winkeys", &world.kb_winkeys);
25     save_win_configs();
26 }
27
28
29
30 extern void load_interface_conf()
31 {
32     init_keybindings("confclient/keybindings_global",  &world.kb_global);
33     init_keybindings("confclient/keybindings_wingeom", &world.kb_wingeom);
34     init_keybindings("confclient/keybindings_winkeys", &world.kb_winkeys);
35     init_winconfs();
36     char * err_s = "load_interface_conf() makes illegaly large virtual screen.";
37     char * err_m = "load_interface_conf(): memory alloc error via newpad().";
38     uint32_t maxy_test = getmaxy(world.wmeta.screen);
39     uint32_t maxx_test = getmaxx(world.wmeta.screen);
40     exit_err(maxy_test > UINT16_MAX || maxx_test > UINT16_MAX, err_s);
41     world.wmeta.padsize.y = maxy_test;
42     world.wmeta.padsize.x = maxx_test;
43     world.wmeta.pad = newpad(world.wmeta.padsize.y, 1);
44     exit_err(NULL == world.wmeta.pad, err_m);
45     init_wins();
46     sorted_wintoggle_and_activate();
47     set_cleanup_flag(CLEANUP_INTERFACE);
48 }
49
50
51
52 extern void unload_interface_conf()
53 {
54     free_keybindings(world.kb_global.kbs);
55     free_keybindings(world.kb_wingeom.kbs);
56     free_keybindings(world.kb_winkeys.kbs);
57     while (0 != world.wmeta.active)
58     {
59         suspend_win(world.wmeta.active);
60     }
61     free_winconfs();
62     delwin(world.wmeta.pad);
63 }
64
65
66
67 extern void reload_interface_conf()
68 {
69     unload_interface_conf();
70     load_interface_conf();
71     map_center();
72 }
73
74
75
76 extern uint16_t center_offset(uint16_t position, uint16_t mapsize,
77                               uint16_t framesize)
78 {
79     uint16_t offset = 0;
80     if (mapsize > framesize)
81     {
82         if (position > framesize / 2)
83         {
84             if (position < mapsize - (framesize / 2))
85             {
86                 offset = position - (framesize / 2);
87             }
88             else
89             {
90                 offset = mapsize - framesize;
91             }
92         }
93     }
94     return offset;
95 }
96
97
98
99 extern void nav_inventory(char dir)
100 {
101     if ('u' == dir)
102     {
103         world.player_inventory_select = world.player_inventory_select
104                                         - (world.player_inventory_select > 0);
105         return;
106     }
107     uint8_t n_elems = 0;
108     uint8_t i;
109     for (i = 0; '\0' != world.player_inventory[i]; i++)
110     {
111         n_elems = n_elems + ('\n' == world.player_inventory[i]);
112     }
113     world.player_inventory_select = world.player_inventory_select
114                                     + (world.player_inventory_select < n_elems);
115 }
116