]> git.lizzy.rs Git - dragonfireclient.git/blob - src/guiChatConsole.cpp
Handle particle spawners in env and delete expired ids
[dragonfireclient.git] / src / guiChatConsole.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "guiChatConsole.h"
21 #include "chat.h"
22 #include "client.h"
23 #include "debug.h"
24 #include "gettime.h"
25 #include "keycode.h"
26 #include "settings.h"
27 #include "porting.h"
28 #include "client/tile.h"
29 #include "fontengine.h"
30 #include "log.h"
31 #include "gettext.h"
32 #include <string>
33
34 #if USE_FREETYPE
35 #include "xCGUITTFont.h"
36 #endif
37
38 inline u32 clamp_u8(s32 value)
39 {
40         return (u32) MYMIN(MYMAX(value, 0), 255);
41 }
42
43
44 GUIChatConsole::GUIChatConsole(
45                 gui::IGUIEnvironment* env,
46                 gui::IGUIElement* parent,
47                 s32 id,
48                 ChatBackend* backend,
49                 Client* client,
50                 IMenuManager* menumgr
51 ):
52         IGUIElement(gui::EGUIET_ELEMENT, env, parent, id,
53                         core::rect<s32>(0,0,100,100)),
54         m_chat_backend(backend),
55         m_client(client),
56         m_menumgr(menumgr),
57         m_screensize(v2u32(0,0)),
58         m_animate_time_old(0),
59         m_open(false),
60         m_close_on_enter(false),
61         m_height(0),
62         m_desired_height(0),
63         m_desired_height_fraction(0.0),
64         m_height_speed(5.0),
65         m_open_inhibited(0),
66         m_cursor_blink(0.0),
67         m_cursor_blink_speed(0.0),
68         m_cursor_height(0.0),
69         m_background(NULL),
70         m_background_color(255, 0, 0, 0),
71         m_font(NULL),
72         m_fontsize(0, 0)
73 {
74         m_animate_time_old = getTimeMs();
75
76         // load background settings
77         s32 console_alpha = g_settings->getS32("console_alpha");
78         m_background_color.setAlpha(clamp_u8(console_alpha));
79
80         // load the background texture depending on settings
81         ITextureSource *tsrc = client->getTextureSource();
82         if (tsrc->isKnownSourceImage("background_chat.jpg")) {
83                 m_background = tsrc->getTexture("background_chat.jpg");
84                 m_background_color.setRed(255);
85                 m_background_color.setGreen(255);
86                 m_background_color.setBlue(255);
87         } else {
88                 v3f console_color = g_settings->getV3F("console_color");
89                 m_background_color.setRed(clamp_u8(myround(console_color.X)));
90                 m_background_color.setGreen(clamp_u8(myround(console_color.Y)));
91                 m_background_color.setBlue(clamp_u8(myround(console_color.Z)));
92         }
93
94         m_font = g_fontengine->getFont(FONT_SIZE_UNSPECIFIED, FM_Mono);
95
96         if (m_font == NULL)
97         {
98                 errorstream << "GUIChatConsole: Unable to load mono font ";
99         }
100         else
101         {
102                 core::dimension2d<u32> dim = m_font->getDimension(L"M");
103                 m_fontsize = v2u32(dim.Width, dim.Height);
104                 m_font->grab();
105         }
106         m_fontsize.X = MYMAX(m_fontsize.X, 1);
107         m_fontsize.Y = MYMAX(m_fontsize.Y, 1);
108
109         // set default cursor options
110         setCursor(true, true, 2.0, 0.1);
111 }
112
113 GUIChatConsole::~GUIChatConsole()
114 {
115         if (m_font)
116                 m_font->drop();
117 }
118
119 void GUIChatConsole::openConsole(f32 height)
120 {
121         m_open = true;
122         m_desired_height_fraction = height;
123         m_desired_height = height * m_screensize.Y;
124         reformatConsole();
125         m_animate_time_old = getTimeMs();
126         IGUIElement::setVisible(true);
127         Environment->setFocus(this);
128         m_menumgr->createdMenu(this);
129 }
130
131 bool GUIChatConsole::isOpen() const
132 {
133         return m_open;
134 }
135
136 bool GUIChatConsole::isOpenInhibited() const
137 {
138         return m_open_inhibited > 0;
139 }
140
141 void GUIChatConsole::closeConsole()
142 {
143         m_open = false;
144         Environment->removeFocus(this);
145         m_menumgr->deletingMenu(this);
146 }
147
148 void GUIChatConsole::closeConsoleAtOnce()
149 {
150         closeConsole();
151         m_height = 0;
152         recalculateConsolePosition();
153 }
154
155 f32 GUIChatConsole::getDesiredHeight() const
156 {
157         return m_desired_height_fraction;
158 }
159
160 void GUIChatConsole::replaceAndAddToHistory(std::wstring line)
161 {
162         ChatPrompt& prompt = m_chat_backend->getPrompt();
163         prompt.addToHistory(prompt.getLine());
164         prompt.replace(line);
165 }
166
167
168 void GUIChatConsole::setCursor(
169         bool visible, bool blinking, f32 blink_speed, f32 relative_height)
170 {
171         if (visible)
172         {
173                 if (blinking)
174                 {
175                         // leave m_cursor_blink unchanged
176                         m_cursor_blink_speed = blink_speed;
177                 }
178                 else
179                 {
180                         m_cursor_blink = 0x8000;  // on
181                         m_cursor_blink_speed = 0.0;
182                 }
183         }
184         else
185         {
186                 m_cursor_blink = 0;  // off
187                 m_cursor_blink_speed = 0.0;
188         }
189         m_cursor_height = relative_height;
190 }
191
192 void GUIChatConsole::draw()
193 {
194         if(!IsVisible)
195                 return;
196
197         video::IVideoDriver* driver = Environment->getVideoDriver();
198
199         // Check screen size
200         v2u32 screensize = driver->getScreenSize();
201         if (screensize != m_screensize)
202         {
203                 // screen size has changed
204                 // scale current console height to new window size
205                 if (m_screensize.Y != 0)
206                         m_height = m_height * screensize.Y / m_screensize.Y;
207                 m_desired_height = m_desired_height_fraction * m_screensize.Y;
208                 m_screensize = screensize;
209                 reformatConsole();
210         }
211
212         // Animation
213         u32 now = getTimeMs();
214         animate(now - m_animate_time_old);
215         m_animate_time_old = now;
216
217         // Draw console elements if visible
218         if (m_height > 0)
219         {
220                 drawBackground();
221                 drawText();
222                 drawPrompt();
223         }
224
225         gui::IGUIElement::draw();
226 }
227
228 void GUIChatConsole::reformatConsole()
229 {
230         s32 cols = m_screensize.X / m_fontsize.X - 2; // make room for a margin (looks better)
231         s32 rows = m_desired_height / m_fontsize.Y - 1; // make room for the input prompt
232         if (cols <= 0 || rows <= 0)
233                 cols = rows = 0;
234         m_chat_backend->reformat(cols, rows);
235 }
236
237 void GUIChatConsole::recalculateConsolePosition()
238 {
239         core::rect<s32> rect(0, 0, m_screensize.X, m_height);
240         DesiredRect = rect;
241         recalculateAbsolutePosition(false);
242 }
243
244 void GUIChatConsole::animate(u32 msec)
245 {
246         // animate the console height
247         s32 goal = m_open ? m_desired_height : 0;
248
249         // Set invisible if close animation finished (reset by openConsole)
250         if (!m_open && m_height == 0)
251                 IGUIElement::setVisible(false);
252
253         if (m_height != goal)
254         {
255                 s32 max_change = msec * m_screensize.Y * (m_height_speed / 1000.0);
256                 if (max_change == 0)
257                         max_change = 1;
258
259                 if (m_height < goal)
260                 {
261                         // increase height
262                         if (m_height + max_change < goal)
263                                 m_height += max_change;
264                         else
265                                 m_height = goal;
266                 }
267                 else
268                 {
269                         // decrease height
270                         if (m_height > goal + max_change)
271                                 m_height -= max_change;
272                         else
273                                 m_height = goal;
274                 }
275
276                 recalculateConsolePosition();
277         }
278
279         // blink the cursor
280         if (m_cursor_blink_speed != 0.0)
281         {
282                 u32 blink_increase = 0x10000 * msec * (m_cursor_blink_speed / 1000.0);
283                 if (blink_increase == 0)
284                         blink_increase = 1;
285                 m_cursor_blink = ((m_cursor_blink + blink_increase) & 0xffff);
286         }
287
288         // decrease open inhibit counter
289         if (m_open_inhibited > msec)
290                 m_open_inhibited -= msec;
291         else
292                 m_open_inhibited = 0;
293 }
294
295 void GUIChatConsole::drawBackground()
296 {
297         video::IVideoDriver* driver = Environment->getVideoDriver();
298         if (m_background != NULL)
299         {
300                 core::rect<s32> sourcerect(0, -m_height, m_screensize.X, 0);
301                 driver->draw2DImage(
302                         m_background,
303                         v2s32(0, 0),
304                         sourcerect,
305                         &AbsoluteClippingRect,
306                         m_background_color,
307                         false);
308         }
309         else
310         {
311                 driver->draw2DRectangle(
312                         m_background_color,
313                         core::rect<s32>(0, 0, m_screensize.X, m_height),
314                         &AbsoluteClippingRect);
315         }
316 }
317
318 void GUIChatConsole::drawText()
319 {
320         if (m_font == NULL)
321                 return;
322
323         ChatBuffer& buf = m_chat_backend->getConsoleBuffer();
324         for (u32 row = 0; row < buf.getRows(); ++row)
325         {
326                 const ChatFormattedLine& line = buf.getFormattedLine(row);
327                 if (line.fragments.empty())
328                         continue;
329
330                 s32 line_height = m_fontsize.Y;
331                 s32 y = row * line_height + m_height - m_desired_height;
332                 if (y + line_height < 0)
333                         continue;
334
335                 for (u32 i = 0; i < line.fragments.size(); ++i)
336                 {
337                         const ChatFormattedFragment& fragment = line.fragments[i];
338                         s32 x = (fragment.column + 1) * m_fontsize.X;
339                         core::rect<s32> destrect(
340                                 x, y, x + m_fontsize.X * fragment.text.size(), y + m_fontsize.Y);
341                         m_font->draw(
342                                 fragment.text.c_str(),
343                                 destrect,
344                                 video::SColor(255, 255, 255, 255),
345                                 false,
346                                 false,
347                                 &AbsoluteClippingRect);
348                 }
349         }
350 }
351
352 void GUIChatConsole::drawPrompt()
353 {
354         if (m_font == NULL)
355                 return;
356
357         u32 row = m_chat_backend->getConsoleBuffer().getRows();
358         s32 line_height = m_fontsize.Y;
359         s32 y = row * line_height + m_height - m_desired_height;
360
361         ChatPrompt& prompt = m_chat_backend->getPrompt();
362         std::wstring prompt_text = prompt.getVisiblePortion();
363
364         // FIXME Draw string at once, not character by character
365         // That will only work with the cursor once we have a monospace font
366         for (u32 i = 0; i < prompt_text.size(); ++i)
367         {
368                 wchar_t ws[2] = {prompt_text[i], 0};
369                 s32 x = (1 + i) * m_fontsize.X;
370                 core::rect<s32> destrect(
371                         x, y, x + m_fontsize.X, y + m_fontsize.Y);
372                 m_font->draw(
373                         ws,
374                         destrect,
375                         video::SColor(255, 255, 255, 255),
376                         false,
377                         false,
378                         &AbsoluteClippingRect);
379         }
380
381         // Draw the cursor during on periods
382         if ((m_cursor_blink & 0x8000) != 0)
383         {
384                 s32 cursor_pos = prompt.getVisibleCursorPosition();
385                 if (cursor_pos >= 0)
386                 {
387                         s32 cursor_len = prompt.getCursorLength();
388                         video::IVideoDriver* driver = Environment->getVideoDriver();
389                         s32 x = (1 + cursor_pos) * m_fontsize.X;
390                         core::rect<s32> destrect(
391                                 x,
392                                 y + m_fontsize.Y * (1.0 - m_cursor_height),
393                                 x + m_fontsize.X * MYMAX(cursor_len, 1),
394                                 y + m_fontsize.Y * (cursor_len ? m_cursor_height+1 : 1)
395                         );
396                         video::SColor cursor_color(255,255,255,255);
397                         driver->draw2DRectangle(
398                                 cursor_color,
399                                 destrect,
400                                 &AbsoluteClippingRect);
401                 }
402         }
403
404 }
405
406 bool GUIChatConsole::OnEvent(const SEvent& event)
407 {
408
409         ChatPrompt &prompt = m_chat_backend->getPrompt();
410
411         if(event.EventType == EET_KEY_INPUT_EVENT && event.KeyInput.PressedDown)
412         {
413                 // Key input
414                 if(KeyPress(event.KeyInput) == getKeySetting("keymap_console"))
415                 {
416                         closeConsole();
417
418                         // inhibit open so the_game doesn't reopen immediately
419                         m_open_inhibited = 50;
420                         m_close_on_enter = false;
421                         return true;
422                 }
423                 else if(event.KeyInput.Key == KEY_ESCAPE)
424                 {
425                         closeConsoleAtOnce();
426                         m_close_on_enter = false;
427                         // inhibit open so the_game doesn't reopen immediately
428                         m_open_inhibited = 1; // so the ESCAPE button doesn't open the "pause menu"
429                         return true;
430                 }
431                 else if(event.KeyInput.Key == KEY_PRIOR)
432                 {
433                         m_chat_backend->scrollPageUp();
434                         return true;
435                 }
436                 else if(event.KeyInput.Key == KEY_NEXT)
437                 {
438                         m_chat_backend->scrollPageDown();
439                         return true;
440                 }
441                 else if(event.KeyInput.Key == KEY_RETURN)
442                 {
443                         prompt.addToHistory(prompt.getLine());
444                         std::wstring text = prompt.replace(L"");
445                         m_client->typeChatMessage(text);
446                         if (m_close_on_enter) {
447                                 closeConsoleAtOnce();
448                                 m_close_on_enter = false;
449                         }
450                         return true;
451                 }
452                 else if(event.KeyInput.Key == KEY_UP)
453                 {
454                         // Up pressed
455                         // Move back in history
456                         prompt.historyPrev();
457                         return true;
458                 }
459                 else if(event.KeyInput.Key == KEY_DOWN)
460                 {
461                         // Down pressed
462                         // Move forward in history
463                         prompt.historyNext();
464                         return true;
465                 }
466                 else if(event.KeyInput.Key == KEY_LEFT || event.KeyInput.Key == KEY_RIGHT)
467                 {
468                         // Left/right pressed
469                         // Move/select character/word to the left depending on control and shift keys
470                         ChatPrompt::CursorOp op = event.KeyInput.Shift ?
471                                 ChatPrompt::CURSOROP_SELECT :
472                                 ChatPrompt::CURSOROP_MOVE;
473                         ChatPrompt::CursorOpDir dir = event.KeyInput.Key == KEY_LEFT ?
474                                 ChatPrompt::CURSOROP_DIR_LEFT :
475                                 ChatPrompt::CURSOROP_DIR_RIGHT;
476                         ChatPrompt::CursorOpScope scope = event.KeyInput.Control ?
477                                 ChatPrompt::CURSOROP_SCOPE_WORD :
478                                 ChatPrompt::CURSOROP_SCOPE_CHARACTER;
479                         prompt.cursorOperation(op, dir, scope);
480                         return true;
481                 }
482                 else if(event.KeyInput.Key == KEY_HOME)
483                 {
484                         // Home pressed
485                         // move to beginning of line
486                         prompt.cursorOperation(
487                                 ChatPrompt::CURSOROP_MOVE,
488                                 ChatPrompt::CURSOROP_DIR_LEFT,
489                                 ChatPrompt::CURSOROP_SCOPE_LINE);
490                         return true;
491                 }
492                 else if(event.KeyInput.Key == KEY_END)
493                 {
494                         // End pressed
495                         // move to end of line
496                         prompt.cursorOperation(
497                                 ChatPrompt::CURSOROP_MOVE,
498                                 ChatPrompt::CURSOROP_DIR_RIGHT,
499                                 ChatPrompt::CURSOROP_SCOPE_LINE);
500                         return true;
501                 }
502                 else if(event.KeyInput.Key == KEY_BACK)
503                 {
504                         // Backspace or Ctrl-Backspace pressed
505                         // delete character / word to the left
506                         ChatPrompt::CursorOpScope scope =
507                                 event.KeyInput.Control ?
508                                 ChatPrompt::CURSOROP_SCOPE_WORD :
509                                 ChatPrompt::CURSOROP_SCOPE_CHARACTER;
510                         prompt.cursorOperation(
511                                 ChatPrompt::CURSOROP_DELETE,
512                                 ChatPrompt::CURSOROP_DIR_LEFT,
513                                 scope);
514                         return true;
515                 }
516                 else if(event.KeyInput.Key == KEY_DELETE)
517                 {
518                         // Delete or Ctrl-Delete pressed
519                         // delete character / word to the right
520                         ChatPrompt::CursorOpScope scope =
521                                 event.KeyInput.Control ?
522                                 ChatPrompt::CURSOROP_SCOPE_WORD :
523                                 ChatPrompt::CURSOROP_SCOPE_CHARACTER;
524                         prompt.cursorOperation(
525                                 ChatPrompt::CURSOROP_DELETE,
526                                 ChatPrompt::CURSOROP_DIR_RIGHT,
527                                 scope);
528                         return true;
529                 }
530                 else if(event.KeyInput.Key == KEY_KEY_A && event.KeyInput.Control)
531                 {
532                         // Ctrl-A pressed
533                         // Select all text
534                         prompt.cursorOperation(
535                                 ChatPrompt::CURSOROP_SELECT,
536                                 ChatPrompt::CURSOROP_DIR_LEFT, // Ignored
537                                 ChatPrompt::CURSOROP_SCOPE_LINE);
538                         return true;
539                 }
540                 else if(event.KeyInput.Key == KEY_KEY_C && event.KeyInput.Control)
541                 {
542                         // Ctrl-C pressed
543                         // Copy text to clipboard
544                         if (prompt.getCursorLength() <= 0)
545                                 return true;
546                         std::wstring wselected = prompt.getSelection();
547                         std::string selected(wselected.begin(), wselected.end());
548                         Environment->getOSOperator()->copyToClipboard(selected.c_str());
549                         return true;
550                 }
551                 else if(event.KeyInput.Key == KEY_KEY_V && event.KeyInput.Control)
552                 {
553                         // Ctrl-V pressed
554                         // paste text from clipboard
555                         if (prompt.getCursorLength() > 0) {
556                                 // Delete selected section of text
557                                 prompt.cursorOperation(
558                                         ChatPrompt::CURSOROP_DELETE,
559                                         ChatPrompt::CURSOROP_DIR_LEFT, // Ignored
560                                         ChatPrompt::CURSOROP_SCOPE_SELECTION);
561                         }
562                         IOSOperator *os_operator = Environment->getOSOperator();
563                         const c8 *text = os_operator->getTextFromClipboard();
564                         if (!text)
565                                 return true;
566                         std::basic_string<unsigned char> str((const unsigned char*)text);
567                         prompt.input(std::wstring(str.begin(), str.end()));
568                         return true;
569                 }
570                 else if(event.KeyInput.Key == KEY_KEY_X && event.KeyInput.Control)
571                 {
572                         // Ctrl-X pressed
573                         // Cut text to clipboard
574                         if (prompt.getCursorLength() <= 0)
575                                 return true;
576                         std::wstring wselected = prompt.getSelection();
577                         std::string selected(wselected.begin(), wselected.end());
578                         Environment->getOSOperator()->copyToClipboard(selected.c_str());
579                         prompt.cursorOperation(
580                                 ChatPrompt::CURSOROP_DELETE,
581                                 ChatPrompt::CURSOROP_DIR_LEFT, // Ignored
582                                 ChatPrompt::CURSOROP_SCOPE_SELECTION);
583                         return true;
584                 }
585                 else if(event.KeyInput.Key == KEY_KEY_U && event.KeyInput.Control)
586                 {
587                         // Ctrl-U pressed
588                         // kill line to left end
589                         prompt.cursorOperation(
590                                 ChatPrompt::CURSOROP_DELETE,
591                                 ChatPrompt::CURSOROP_DIR_LEFT,
592                                 ChatPrompt::CURSOROP_SCOPE_LINE);
593                         return true;
594                 }
595                 else if(event.KeyInput.Key == KEY_KEY_K && event.KeyInput.Control)
596                 {
597                         // Ctrl-K pressed
598                         // kill line to right end
599                         prompt.cursorOperation(
600                                 ChatPrompt::CURSOROP_DELETE,
601                                 ChatPrompt::CURSOROP_DIR_RIGHT,
602                                 ChatPrompt::CURSOROP_SCOPE_LINE);
603                         return true;
604                 }
605                 else if(event.KeyInput.Key == KEY_TAB)
606                 {
607                         // Tab or Shift-Tab pressed
608                         // Nick completion
609                         std::list<std::string> names = m_client->getConnectedPlayerNames();
610                         bool backwards = event.KeyInput.Shift;
611                         prompt.nickCompletion(names, backwards);
612                         return true;
613                 }
614                 else if(event.KeyInput.Char != 0 && !event.KeyInput.Control)
615                 {
616                         #if (defined(linux) || defined(__linux))
617                                 wchar_t wc = L'_';
618                                 mbtowc( &wc, (char *) &event.KeyInput.Char, sizeof(event.KeyInput.Char) );
619                                 prompt.input(wc);
620                         #else
621                                 prompt.input(event.KeyInput.Char);
622                         #endif
623                         return true;
624                 }
625         }
626         else if(event.EventType == EET_MOUSE_INPUT_EVENT)
627         {
628                 if(event.MouseInput.Event == EMIE_MOUSE_WHEEL)
629                 {
630                         s32 rows = myround(-3.0 * event.MouseInput.Wheel);
631                         m_chat_backend->scroll(rows);
632                 }
633         }
634
635         return Parent ? Parent->OnEvent(event) : false;
636 }
637
638 void GUIChatConsole::setVisible(bool visible)
639 {
640         m_open = visible;
641         IGUIElement::setVisible(visible);
642         if (!visible) {
643                 m_height = 0;
644                 recalculateConsolePosition();
645         }
646 }
647