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