]> git.lizzy.rs Git - dragonblocks.git/blob - engine/key_handler.js
Code style overhaul
[dragonblocks.git] / engine / key_handler.js
1 /*
2  * key_handler.js
3  *
4  * Copyright 2020 Elias Fleckenstein <eliasfleckenstein@web.de>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  * MA 02110-1301, USA.
20  *
21  *
22  */
23
24 dragonblocks.KeyHandler = class
25 {
26         constructor()
27         {
28                 this.locked = {};
29                 this.upHandlers = {};
30                 this.downHandlers = {};
31
32                 let self = this;
33
34                 addEventListener("keydown", event => {
35                         self.handle(event);
36                 });
37
38                 addEventListener("keyup", event => {
39                         self.handle(event);
40                 });
41
42                 addEventListener("wheel", event => {
43                         self.handle(event);
44                 });
45         }
46
47         lock(key)
48         {
49                 this.locked[key] = true;
50         }
51
52         lockAll()
53         {
54                 for(let key in this.upHandlers)
55                         this.lock(key);
56
57                 for(let key in this.downHandlers)
58                         this.lock(key);
59         }
60
61         unlock(key)
62         {
63                 dragonblocks.keyHandler.locked[key] = false;
64         }
65
66         unlockAll()
67         {
68                 for (let key in this.locked)
69                         this.unlock(key);
70         }
71
72         down(key, func)
73         {
74                 this.downHandlers[key] = func;
75                 this.lock(key);
76         }
77
78         up(key, func)
79         {
80                 this.upHandlers[key] = func;
81                 this.lock(key);
82         }
83
84         handle(event)
85         {
86                 switch (event.type) {
87                         case "keydown":
88                         case "keypress":
89                                 if (this.locked[event.key])
90                                         return;
91
92                                 if (this.downHandlers[event.key]) {
93                                         event.preventDefault();
94                                         (this.downHandlers[event.key])();
95                                 }
96                                 break;
97
98                         case "keyup":
99                                 if (this.locked[event.key])
100                                         return;
101
102                                 if (this.upHandlers[event.key]){
103                                         event.preventDefault();
104                                         (this.upHandlers[event.key])();
105                                 }
106                                 break;
107
108                         case "wheel":
109                                 if (this.locked["scroll"])
110                                         return;
111
112                                 if (event.deltaY > 0 && this.downHandlers["scroll"]) {
113                                         event.preventDefault();
114                                         (this.downHandlers["scroll"])();
115                                 } else if (this.upHandlers["scroll"]) {
116                                         event.preventDefault();
117                                         (this.upHandlers["scroll"])();
118                                 }
119                                 break;
120                 }
121         }
122 };
123
124 dragonblocks.keyHandler = new dragonblocks.KeyHandler();
125
126 dragonblocks.registerOnStarted(_ => {
127         dragonblocks.keyHandler.unlockAll();
128 });