]> git.lizzy.rs Git - dragonblocks.git/blob - engine/key_handler.js
750d37f20254222bb0b52c7700929ee6241196d6
[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 dragonblocks.KeyHandler = class{
24         constructor(){
25                 this.locked = {};
26                 this.upHandlers = {};
27                 this.downHandlers = {};
28                 addEventListener("keydown", event => {dragonblocks.keyHandler.handler(event)});
29                 addEventListener("keyup", event => {dragonblocks.keyHandler.handler(event)});
30                 addEventListener("wheel", event => {dragonblocks.keyHandler.handler(event)});
31         }
32         lock(key){
33                 this.locked[key] = true;
34         }
35         lockAll(){
36                 for(let key in this.upHandlers)
37                         this.lock(key);
38                 for(let key in this.downHandlers)
39                         this.lock(key);
40         }
41         unlock(key){
42                 dragonblocks.keyHandler.locked[key] = false;
43         }
44         unlockAll(){
45                 for(let key in this.locked)
46                         this.unlock(key);
47         }
48         down(key, func){
49                 this.downHandlers[key] = func;
50                 this.lock(key);
51         }
52         up(key, func){
53                 this.upHandlers[key] = func;
54                 this.lock(key);
55         }
56         handler(event){
57                 switch(event.type){
58                         case "keydown":
59                         case "keypress":
60                                 if(this.locked[event.key])
61                                         return;
62                                 if(this.downHandlers[event.key]){
63                                         event.preventDefault();
64                                         (this.downHandlers[event.key])();
65                                 }
66                                 break;
67                         case "keyup":
68                                 if(this.locked[event.key])
69                                         return;
70                                 if(this.upHandlers[event.key]){
71                                         event.preventDefault();
72                                         (this.upHandlers[event.key])();
73                                 }
74                                 break;
75                         case "wheel":
76                                 if(this.locked["scroll"])
77                                         return;
78                                 if(event.deltaY > 0 && this.downHandlers["scroll"]){
79                                                 event.preventDefault();
80                                                 (this.downHandlers["scroll"])();
81                                 }
82                                 else if(this.upHandlers["scroll"]){
83                                                 event.preventDefault();
84                                                 (this.upHandlers["scroll"])();
85                                 }
86                                 break;
87                 }
88         }
89 }
90 dragonblocks.keyHandler = new dragonblocks.KeyHandler();
91 dragonblocks.registerOnStarted(_ => {
92         dragonblocks.keyHandler.unlockAll();
93 });