rayfract
diff src/imtk/state.c @ 10:1496aae2e7d4
- simplified build by including dependences in the source tree
- added make dep tracking
- added mingw cross-build rules
- added readme & licence
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 31 Jul 2023 18:58:56 +0300 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/imtk/state.c Mon Jul 31 18:58:56 2023 +0300 1.3 @@ -0,0 +1,154 @@ 1.4 +#include "state.h" 1.5 +#include "imtk.h" 1.6 + 1.7 +struct key_node { 1.8 + int key; 1.9 + struct key_node *next; 1.10 +}; 1.11 + 1.12 + 1.13 +struct imtk_state st = { 1.14 + 1, 1, /* scr_width/scr_height */ 1.15 + 0, 0, 0, 0, 0, /* mousex/mousey, prevx, prevy, mouse_bnmask */ 1.16 + -1, -1, -1, -1 /* active, hot, input, prev_active */ 1.17 +}; 1.18 + 1.19 +static struct key_node *key_list, *key_tail; 1.20 + 1.21 + 1.22 + 1.23 +void imtk_inp_key(int key, int state) 1.24 +{ 1.25 + if(state == IMTK_DOWN) { 1.26 + struct key_node *node; 1.27 + 1.28 + if(!(node = malloc(sizeof *node))) { 1.29 + return; 1.30 + } 1.31 + node->key = key; 1.32 + node->next = 0; 1.33 + 1.34 + if(key_list) { 1.35 + key_tail->next = node; 1.36 + key_tail = node; 1.37 + } else { 1.38 + key_list = key_tail = node; 1.39 + } 1.40 + } 1.41 + 1.42 + imtk_post_redisplay(); 1.43 +} 1.44 + 1.45 +void imtk_inp_mouse(int bn, int state) 1.46 +{ 1.47 + if(state == IMTK_DOWN) { 1.48 + st.mouse_bnmask |= 1 << bn; 1.49 + } else { 1.50 + st.mouse_bnmask &= ~(1 << bn); 1.51 + } 1.52 + imtk_post_redisplay(); 1.53 +} 1.54 + 1.55 +void imtk_inp_motion(int x, int y) 1.56 +{ 1.57 + st.mousex = x; 1.58 + st.mousey = y; 1.59 + 1.60 + imtk_post_redisplay(); 1.61 +} 1.62 + 1.63 +void imtk_set_viewport(int x, int y) 1.64 +{ 1.65 + st.scr_width = x; 1.66 + st.scr_height = y; 1.67 +} 1.68 + 1.69 +void imtk_get_viewport(int *width, int *height) 1.70 +{ 1.71 + if(width) *width = st.scr_width; 1.72 + if(height) *height = st.scr_height; 1.73 +} 1.74 + 1.75 + 1.76 +void imtk_set_active(int id) 1.77 +{ 1.78 + if(id == -1 || st.hot == id) { 1.79 + st.prev_active = st.active; 1.80 + st.active = id; 1.81 + } 1.82 +} 1.83 + 1.84 +int imtk_is_active(int id) 1.85 +{ 1.86 + return st.active == id; 1.87 +} 1.88 + 1.89 +int imtk_set_hot(int id) 1.90 +{ 1.91 + if(st.active == -1) { 1.92 + st.hot = id; 1.93 + return 1; 1.94 + } 1.95 + return 0; 1.96 +} 1.97 + 1.98 +int imtk_is_hot(int id) 1.99 +{ 1.100 + return st.hot == id; 1.101 +} 1.102 + 1.103 +void imtk_set_focus(int id) 1.104 +{ 1.105 + st.input = id; 1.106 +} 1.107 + 1.108 +int imtk_has_focus(int id) 1.109 +{ 1.110 + return st.input == id; 1.111 +} 1.112 + 1.113 +int imtk_hit_test(int x, int y, int w, int h) 1.114 +{ 1.115 + return st.mousex >= x && st.mousex < (x + w) && 1.116 + st.mousey >= y && st.mousey < (y + h); 1.117 +} 1.118 + 1.119 +void imtk_get_mouse(int *xptr, int *yptr) 1.120 +{ 1.121 + if(xptr) *xptr = st.mousex; 1.122 + if(yptr) *yptr = st.mousey; 1.123 +} 1.124 + 1.125 +void imtk_set_prev_mouse(int x, int y) 1.126 +{ 1.127 + st.prevx = x; 1.128 + st.prevy = y; 1.129 +} 1.130 + 1.131 +void imtk_get_prev_mouse(int *xptr, int *yptr) 1.132 +{ 1.133 + if(xptr) *xptr = st.prevx; 1.134 + if(yptr) *yptr = st.prevy; 1.135 +} 1.136 + 1.137 +int imtk_button_state(int bn) 1.138 +{ 1.139 + return st.mouse_bnmask & (1 << bn); 1.140 +} 1.141 + 1.142 + 1.143 +int imtk_get_key(void) 1.144 +{ 1.145 + int key = -1; 1.146 + struct key_node *node = key_list; 1.147 + 1.148 + if(node) { 1.149 + key = node->key; 1.150 + key_list = node->next; 1.151 + if(!key_list) { 1.152 + key_tail = 0; 1.153 + } 1.154 + free(node); 1.155 + } 1.156 + return key; 1.157 +}