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