]> git.lizzy.rs Git - dragonblocks.git/blob - engine/chat.js
Code style overhaul
[dragonblocks.git] / engine / chat.js
1 /*
2  * chat.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.Chat = class
25 {
26         constructor()
27         {
28                 this.input = [""];
29                 this.history = -1;
30                 this.lines = dragonblocks.settings.chat.lines;
31
32                 this.addGraphics();
33                 this.clear();
34
35                 dragonblocks.keyHandler.down("t", _ => {
36                         dragonblocks.chat.open();
37                 });
38
39                 dragonblocks.keyHandler.down("/", event => {
40                         dragonblocks.chat.open();
41                         document.getElementById("dragonblocks.chat.input").value = "/";
42                 });
43         }
44
45         addGraphics()
46         {
47                 let display = document.body.appendChild(document.createElement("div"));
48                 display.id = "dragonblocks.chat";
49                 display.style.position = "fixed";
50                 display.style.top = "0px";
51                 display.style.left = "0px";
52                 display.style.backgroundColor = "black";
53                 display.style.opacity = "0.7";
54                 display.style.height = 23 * this.lines + "px";
55                 display.style.fontSize = "20px";
56                 display.style.color = "white";
57                 display.style.width = "100%";
58                 display.style.fontFamily = "monospace";
59                 display.style.overflowY = "scroll";
60                 display.style.scrollbarWidth = "none";
61                 display.style.visibility = "hidden";
62
63                 let input = document.body.appendChild(document.createElement("input"));
64                 input.id = "dragonblocks.chat.input";
65                 input.style.position = "fixed";
66                 input.style.top = 23 * this.lines + "px";
67                 input.style.left = "0px";
68                 input.style.backgroundColor = "black";
69                 input.style.border = "none";
70                 input.style.opacity = "0.7";
71                 input.style.fontSize = "20px";
72                 input.style.color = "white";
73                 input.style.height = "23px";
74                 input.style.width = "100%";
75                 input.style.caretWidth = "30px";
76                 input.style.caretHeight = "20px";
77                 input.style.fontFamily = "monospace";
78                 input.style.visibility = "hidden";
79
80                 let self = this;
81
82                 input.addEventListener("keydown", event => {
83                         switch (event.key) {
84                                 case "Enter":
85                                         let message = event.srcElement.value;
86                                         event.srcElement.value = "";
87
88                                         if (message == "")
89                                                 return;
90
91                                         self.input[self.input.length - 1] = message;
92                                         self.send(message);
93                                         self.input.push("");
94
95                                         self.history = -1;
96                                         break;
97
98                                 case "Escape":
99                                         self.close();
100                                         break;
101
102                                 case "ArrowUp":
103                                         event.srcElement.value = self.historyUp();
104                                         break;
105
106                                 case "ArrowDown":
107                                         event.srcElement.value = self.historyDown();
108                                         break;
109                         }
110                 });
111
112                 input.addEventListener("input", event => {
113                         self.input[self.input.length - 1] = event.srcElement.value;
114                 });
115         }
116
117         open()
118         {
119                 dragonblocks.keyHandler.lockAll();
120
121                 document.getElementById("dragonblocks.chat").style.visibility = "visible";
122
123                 let input = document.getElementById("dragonblocks.chat.input");
124                 input.style.visibility = "visible";
125                 input.focus();
126         }
127
128         close()
129         {
130                 setTimeout(_ => {
131                         dragonblocks.keyHandler.unlockAll();
132                 });
133
134                 document.getElementById("dragonblocks.chat").style.visibility = "hidden";
135
136                 let input = document.getElementById("dragonblocks.chat.input");
137                 input.style.visibility = "hidden";
138                 input.blur();
139         }
140
141         write(text)
142         {
143                 text = text || "";
144
145                 if (text.startsWith("!HTML"))
146                         text = text.replace("!HTML", "");
147                 else
148                         text = dblib.htmlEntities(text);
149
150                 text += "<br>";
151
152                 let display = document.getElementById("dragonblocks.chat");
153                 display.innerHTML += text;
154                 display.lastChild.scrollIntoView();
155         }
156
157         send(input)
158         {
159                 for (let func of dragonblocks.onChatMessageCallbacks)
160                         if (func(input) == false)
161                                 return false;
162
163                 this.write(input);
164         }
165
166         historyUp()
167         {
168                 this.history--;
169
170                 if (this.input[this.input.length + this.history] == undefined)
171                         this.history++;
172
173                 return this.input[this.input.length + this.history];
174         }
175
176         historyDown()
177         {
178                 this.history++;
179
180                 if (this.input[this.input.length + this.history] == undefined)
181                         this.history--;
182
183                 return this.input[this.input.length + this.history];
184         }
185
186         clear()
187         {
188                 document.getElementById("dragonblocks.chat").innerHTML = "<br>".repeat(this.lines);
189         }
190 };
191
192 dragonblocks.chat = new dragonblocks.Chat();
193
194 dragonblocks.chatMessage = msg => {
195         dragonblocks.chat.write(msg);
196 };
197
198 dragonblocks.onChatMessageCallbacks = [];
199
200 dragonblocks.registerOnChatMessage = func => {
201         dragonblocks.onChatMessageCallbacks.push(func);
202 };