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