]> git.lizzy.rs Git - dragonblocks.git/blob - engine/chat.js
Prevent chat console outline on chromium
[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.outline = "none";
71                 input.style.opacity = "0.7";
72                 input.style.fontSize = "20px";
73                 input.style.color = "white";
74                 input.style.height = "23px";
75                 input.style.width = "100%";
76                 input.style.caretWidth = "30px";
77                 input.style.caretHeight = "20px";
78                 input.style.fontFamily = "monospace";
79                 input.style.visibility = "hidden";
80
81                 let self = this;
82
83                 input.addEventListener("keydown", event => {
84                         switch (event.key) {
85                                 case "Enter":
86                                         let message = event.srcElement.value;
87                                         event.srcElement.value = "";
88
89                                         if (message == "")
90                                                 return;
91
92                                         self.input[self.input.length - 1] = message;
93                                         self.send(message);
94                                         self.input.push("");
95
96                                         self.history = -1;
97                                         break;
98
99                                 case "Escape":
100                                         self.close();
101                                         break;
102
103                                 case "ArrowUp":
104                                         event.srcElement.value = self.historyUp();
105                                         break;
106
107                                 case "ArrowDown":
108                                         event.srcElement.value = self.historyDown();
109                                         break;
110                         }
111                 });
112
113                 input.addEventListener("input", event => {
114                         self.input[self.input.length - 1] = event.srcElement.value;
115                 });
116         }
117
118         open()
119         {
120                 dragonblocks.keyHandler.lockAll();
121
122                 document.getElementById("dragonblocks.chat").style.visibility = "visible";
123
124                 let input = document.getElementById("dragonblocks.chat.input");
125                 input.style.visibility = "visible";
126                 input.focus();
127         }
128
129         close()
130         {
131                 setTimeout(_ => {
132                         dragonblocks.keyHandler.unlockAll();
133                 });
134
135                 document.getElementById("dragonblocks.chat").style.visibility = "hidden";
136
137                 let input = document.getElementById("dragonblocks.chat.input");
138                 input.style.visibility = "hidden";
139                 input.blur();
140         }
141
142         write(text)
143         {
144                 text = text || "";
145
146                 if (text.startsWith("!HTML"))
147                         text = text.replace("!HTML", "");
148                 else
149                         text = dblib.htmlEntities(text);
150
151                 text += "<br>";
152
153                 let display = document.getElementById("dragonblocks.chat");
154                 display.innerHTML += text;
155                 display.lastChild.scrollIntoView();
156         }
157
158         send(input)
159         {
160                 for (let func of dragonblocks.onChatMessageCallbacks)
161                         if (func(input) == false)
162                                 return false;
163
164                 this.write(input);
165         }
166
167         historyUp()
168         {
169                 this.history--;
170
171                 if (this.input[this.input.length + this.history] == undefined)
172                         this.history++;
173
174                 return this.input[this.input.length + this.history];
175         }
176
177         historyDown()
178         {
179                 this.history++;
180
181                 if (this.input[this.input.length + this.history] == undefined)
182                         this.history--;
183
184                 return this.input[this.input.length + this.history];
185         }
186
187         clear()
188         {
189                 document.getElementById("dragonblocks.chat").innerHTML = "<br>".repeat(this.lines);
190         }
191 };
192
193 dragonblocks.chat = new dragonblocks.Chat();
194
195 dragonblocks.chatMessage = msg => {
196         dragonblocks.chat.write(msg);
197 };
198
199 dragonblocks.onChatMessageCallbacks = [];
200
201 dragonblocks.registerOnChatMessage = func => {
202         dragonblocks.onChatMessageCallbacks.push(func);
203 };