4 * Copyright 2020 Elias Fleckenstein <eliasfleckenstein@web.de>
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.
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.
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,
24 dragonblocks.Chat = class
30 this.lines = dragonblocks.settings.chat.lines;
35 dragonblocks.keyHandler.down("t", _ => {
36 dragonblocks.chat.open();
39 dragonblocks.keyHandler.down("/", event => {
40 dragonblocks.chat.open();
41 this.inputDisplay.value = "/";
47 this.display = document.body.appendChild(document.createElement("div"));
48 this.display.style.position = "fixed";
49 this.display.style.top = "0px";
50 this.display.style.left = "0px";
51 this.display.style.backgroundColor = "black";
52 this.display.style.opacity = "0.7";
53 this.display.style.height = 23 * this.lines + "px";
54 this.display.style.fontSize = "20px";
55 this.display.style.color = "white";
56 this.display.style.width = "100%";
57 this.display.style.fontFamily = "monospace";
58 this.display.style.overflowY = "scroll";
59 this.display.style.scrollbarWidth = "none";
60 this.display.style.visibility = "hidden";
62 this.inputDisplay = document.body.appendChild(document.createElement("input"));
63 this.inputDisplay.style.position = "fixed";
64 this.inputDisplay.style.top = 23 * this.lines + "px";
65 this.inputDisplay.style.left = "0px";
66 this.inputDisplay.style.backgroundColor = "black";
67 this.inputDisplay.style.border = "none";
68 this.inputDisplay.style.outline = "none";
69 this.inputDisplay.style.opacity = "0.7";
70 this.inputDisplay.style.fontSize = "20px";
71 this.inputDisplay.style.color = "white";
72 this.inputDisplay.style.height = "23px";
73 this.inputDisplay.style.width = "100%";
74 this.inputDisplay.style.caretWidth = "30px";
75 this.inputDisplay.style.caretHeight = "20px";
76 this.inputDisplay.style.fontFamily = "monospace";
77 this.inputDisplay.style.visibility = "hidden";
81 this.inputDisplay.addEventListener("keydown", event => {
84 let message = self.inputDisplay.value;
85 event.srcElement.value = "";
90 self.input[self.input.length - 1] = message;
102 self.inputDisplay.value = self.historyUp();
106 self.inputDisplay.value = self.historyDown();
111 this.inputDisplay.addEventListener("input", event => {
112 self.input[self.input.length - 1] = self.inputDisplay;
118 dragonblocks.keyHandler.lockAll();
120 this.display.style.visibility = "visible";
121 this.inputDisplay.style.visibility = "visible";
122 this.inputDisplay.focus();
128 dragonblocks.keyHandler.unlockAll();
131 this.display.style.visibility = "hidden";
132 this.inputDisplay.style.visibility = "hidden";
133 this.inputDisplay.blur();
140 if (text.startsWith("!HTML"))
141 text = text.replace("!HTML", "");
143 text = dblib.htmlEntities(text);
147 this.display.innerHTML += text;
148 this.display.lastChild.scrollIntoView();
153 for (let func of dragonblocks.onChatMessageCallbacks)
154 if (func(input) == false)
164 if (this.input[this.input.length + this.history] == undefined)
167 return this.input[this.input.length + this.history];
174 if (this.input[this.input.length + this.history] == undefined)
177 return this.input[this.input.length + this.history];
182 this.display.innerHTML = "<br>".repeat(this.lines);
186 dragonblocks.chat = new dragonblocks.Chat();
188 dragonblocks.chatMessage = msg => {
189 dragonblocks.chat.write(msg);
192 dragonblocks.onChatMessageCallbacks = [];
194 dragonblocks.registerOnChatMessage = func => {
195 dragonblocks.onChatMessageCallbacks.push(func);