vrshoot

view src/input.cc @ 3:c179c72369be

rename candy->vr
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 03 Feb 2014 08:52:13 +0200
parents
children
line source
1 #include <vector>
2 #include <bitset>
3 #include "input.h"
4 #include "logger.h"
6 #define NUM_KEYS 65536
7 static std::bitset<NUM_KEYS> keystate;
9 void set_key_state(int key, bool state)
10 {
11 if(key < 0 || key >= NUM_KEYS) {
12 error_log("set_key_state: key (%d) out of bounds\n", key);
13 return;
14 }
15 keystate[key] = state;
16 }
18 bool get_key_state(int key)
19 {
20 if(key < 0 || key >= NUM_KEYS) {
21 error_log("get_key_state: key (%d) out of bounds\n", key);
22 return false;
23 }
24 return keystate[key];
25 }
27 struct ButtonState {
28 bool pressed;
29 int press_x, press_y;
30 };
31 static std::vector<ButtonState> bnstate(16);
32 #define MAX_BNSTATE 128
33 static int mouse_x, mouse_y;
35 void set_mouse_state(int x, int y, int bn, bool state)
36 {
37 mouse_x = x;
38 mouse_y = y;
40 if(bn < 0 || bn >= MAX_BNSTATE) {
41 return;
42 }
43 if((size_t)bn >= bnstate.size()) {
44 bnstate.resize(bn + 1);
45 }
47 bnstate[bn].pressed = state;
48 if(state) {
49 bnstate[bn].press_x = x;
50 bnstate[bn].press_y = y;
51 }
52 }
54 bool mouse_button(int bn)
55 {
56 if(bn < 0 || (size_t)bn >= bnstate.size()) {
57 return false;
58 }
59 return bnstate[bn].pressed;
60 }
62 bool mouse_dragging(int bn)
63 {
64 if(bn < 0 || (size_t)bn >= bnstate.size()) {
65 return false;
66 }
68 return bnstate[bn].pressed && mouse_x != bnstate[bn].press_x &&
69 mouse_y != bnstate[bn].press_y;
70 }
72 bool mouse_drag_origin(int bn, int *xorig, int *yorig)
73 {
74 if(!mouse_dragging()) {
75 // this also handles bound issues, no need to recheck
76 *xorig = mouse_x;
77 *yorig = mouse_y;
78 return false;
79 }
81 *xorig = bnstate[bn].press_x;
82 *yorig = bnstate[bn].press_y;
83 return true;
84 }