BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / input.js

input.js

Framework 3
timfa Tim Falken · pushed by timfa · 9 years ago
4a56d325 History
input.js 4.1 KiB Download Raw
1//<DOC> category GameInput
2
3var GameInput =
4{
5 g_keyStatus: [],
6 g_pKeyStatus: [],
7
8 g_pressKeyStatus: [],
9 g_ppressKeyStatus: [],
10
11 g_pressBlacklist: [],
12
13 g_mouseDown: false,
14 g_mouseDownThisFrame: false,
15
16 g_altMouseDown: false,
17 g_altMouseDownThisFrame: false,
18
19 g_middleMouseDown: false,
20 g_middleMouseDownThisFrame: false,
21
22 mousePosition: new vector(0, 0),
23
24 MouseButtons: {
25 Left: 0,
26 Middle: 1,
27 Right: 2
28 },
29
30 g_init: function()
31 {
32 document.onkeydown = GameInput.g_setKeyStatus;
33 document.onkeyup = GameInput.g_setKeyStatus;
34
35 document.oncontextmenu = function(){return false;}
36
37 document.onmousedown = function(e)
38 {
39 switch(e.button)
40 {
41 case GameInput.MouseButtons.Left: GameInput.g_mouseDownThisFrame = true; break;
42 case GameInput.MouseButtons.Middle: GameInput.g_middleMouseDownThisFrame = true; break;
43 case GameInput.MouseButtons.Right: GameInput.g_altMouseDownThisFrame = true; break;
44 }
45 }
46
47 document.onmouseup = function(e)
48 {
49 GameInput.g_mouseDownThisFrame = false;
50 GameInput.g_mouseDown = false;
51 }
52
53 document.onmousemove = function(e)
54 {
55 GameInput.mousePosition.x = e.x + GameWindow.x;
56 GameInput.mousePosition.y = e.y + GameWindow.y;
57 }
58
59
60 var el = document.getElementsByTagName("canvas")[0];
61 el.addEventListener("touchstart", function(){GameInput.g_mouseDownThisFrame = true;}, false);
62 el.addEventListener("touchend", function(){GameInput.g_mouseDownThisFrame = false;GameInput.g_mouseDown = false;}, false);
63 el.addEventListener("touchcancel", function(){GameInput.g_mouseDownThisFrame = false;GameInput.g_mouseDown = false;}, false);
64 el.addEventListener("touchmove", function(){return false}, false);
65 },
66
67 g_setKeyStatus: function(e)
68 {
69 GameInput.g_keyStatus[e.which] = (e.type == "keydown");
70
71 if(!GameInput.g_pressBlacklist[e.which])
72 GameInput.g_pressKeyStatus[e.which] = (e.type == "keydown");
73
74 GameInput.g_pressBlacklist[e.which] = (e.type == "keydown"); //only allow single presses if key has been released once
75 },
76
77 g_updateKeys: function()
78 {
79 GameInput.g_pKeyStatus = GameInput.g_keyStatus;
80 GameInput.g_ppressKeyStatus = GameInput.g_pressKeyStatus;
81 GameInput.g_pressKeyStatus = [];
82
83 if (GameInput.g_mouseDown && GameInput.g_mouseDownThisFrame)
84 {
85 GameInput.g_mouseDownThisFrame = false;
86 }
87
88 if(GameInput.g_mouseDownThisFrame && !GameInput.g_mouseDown)
89 {
90 GameInput.g_mouseDown = true;
91 }
92
93 if (GameInput.g_altMouseDown && GameInput.g_altMouseDownThisFrame)
94 {
95 GameInput.g_altMouseDownThisFrame = false;
96 }
97
98 if(GameInput.g_altMouseDownThisFrame && !GameInput.g_altMouseDown)
99 {
100 GameInput.g_altMouseDown = true;
101 }
102
103 if (GameInput.g_middleMouseDown && GameInput.g_middleMouseDownThisFrame)
104 {
105 GameInput.g_middleMouseDownThisFrame = false;
106 }
107
108 if(GameInput.g_middleMouseDownThisFrame && !GameInput.g_middleMouseDown)
109 {
110 GameInput.g_midleMouseDown = true;
111 }
112 },
113
114 //<DOC> <function> GameInput.IsPressed(keyCode)<br><br>Returns true if the key with given code 'keyCode' was pressed this frame. Only returns true the frame the key is pressed once until the key is released and pressed again.
115 isPressed: function(keyCode)
116 {
117 if(GameInput.g_ppressKeyStatus[keyCode] == null)
118 return false;
119 else
120 return GameInput.g_ppressKeyStatus[keyCode];
121 },
122
123 //<DOC> <function> GameInput.isHeld(keyCode)<br><br>returns true if the key with given code 'keyCode' is currently pressed.
124 isHeld: function(keyCode)
125 {
126 if(GameInput.g_pKeyStatus[keyCode] == null)
127 return false;
128 else
129 return GameInput.g_pKeyStatus[keyCode];
130 },
131
132 mousePressed: function(btn)
133 {
134 switch(btn)
135 {
136 case GameInput.MouseButtons.Left: return GameInput.g_mouseDownThisFrame;
137 case GameInput.MouseButtons.Middle: return GameInput.g_middleMouseDownThisFrame;
138 case GameInput.MouseButtons.Right: return GameInput.g_altMouseDownThisFrame;
139 }
140
141 return GameInput.g_mouseDownThisFrame;
142 },
143
144 mouseHeld: function(btn)
145 {
146 switch(btn)
147 {
148 case GameInput.MouseButtons.Left: return GameInput.g_mouseDown;
149 case GameInput.MouseButtons.Middle: return GameInput.g_middleMouseDown;
150 case GameInput.MouseButtons.Right: return GameInput.g_altMouseDown;
151 }
152
153 return GameInput.g_mouseDown;
154 }
155}//