c9f41c76fd47fab2eadf31165441f2e343868793
[oweals/minetest.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 "main.h"  // for g_settings
28 #include "porting.h"
29 #include "tile.h"
30 #include "fontengine.h"
31 #include <string>
32
33 #include "gettext.h"
34
35 #if USE_FREETYPE
36 #include "xCGUITTFont.h"
37 #endif
38
39 inline u32 clamp_u8(s32 value)
40 {
41         return (u32) MYMIN(MYMAX(value, 0), 255);
42 }
43
44
45 GUIChatConsole::GUIChatConsole(
46                 gui::IGUIEnvironment* env,
47                 gui::IGUIElement* parent,
48                 s32 id,
49                 ChatBackend* backend,
50                 Client* client
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_screensize(v2u32(0,0)),
57         m_animate_time_old(0),
58         m_open(false),
59         m_height(0),
60         m_desired_height(0),
61         m_desired_height_fraction(0.0),
62         m_height_speed(5.0),
63         m_open_inhibited(0),
64         m_cursor_blink(0.0),
65         m_cursor_blink_speed(0.0),
66         m_cursor_height(0.0),
67         m_background(NULL),
68         m_background_color(255, 0, 0, 0),
69         m_font(NULL),
70         m_fontsize(0, 0)
71 {
72         m_animate_time_old = getTimeMs();
73
74         // load background settings
75         bool console_color_set = !g_settings->get("console_color").empty();
76         s32 console_alpha = g_settings->getS32("console_alpha");
77
78         // load the background texture depending on settings
79         m_background_color.setAlpha(clamp_u8(console_alpha));
80         if (console_color_set)
81         {
82                 v3f console_color = g_settings->getV3F("console_color");
83                 m_background_color.setRed(clamp_u8(myround(console_color.X)));
84                 m_background_color.setGreen(clamp_u8(myround(console_color.Y)));
85                 m_background_color.setBlue(clamp_u8(myround(console_color.Z)));
86         }
87         else
88         {
89                 m_background = env->getVideoDriver()->getTexture(getTexturePath("background_chat.jpg").c_str());
90                 m_background_color.setRed(255);
91                 m_background_color.setGreen(255);
92                 m_background_color.setBlue(255);
93         }
94
95         m_font = glb_fontengine->getFont(FONT_SIZE_UNSPECIFIED, FM_Mono);
96
97         if (m_font == NULL)
98         {
99                 errorstream << "GUIChatConsole: Unable to load mono font ";
100         }
101         else
102         {
103                 core::dimension2d<u32> dim = m_font->getDimension(L"M");
104                 m_fontsize = v2u32(dim.Width, dim.Height);
105                 dstream << "Font size: " << m_fontsize.X << " " << m_fontsize.Y << std::endl;
106         }
107         m_fontsize.X = MYMAX(m_fontsize.X, 1);
108         m_fontsize.Y = MYMAX(m_fontsize.Y, 1);
109
110         // set default cursor options
111         setCursor(true, true, 2.0, 0.1);
112 }
113
114 GUIChatConsole::~GUIChatConsole()
115 {}
116
117 void GUIChatConsole::openConsole(f32 height)
118 {
119         m_open = true;
120         m_desired_height_fraction = height;
121         m_desired_height = height * m_screensize.Y;
122         reformatConsole();
123 }
124
125 bool GUIChatConsole::isOpen() const
126 {
127         return m_open;
128 }
129
130 bool GUIChatConsole::isOpenInhibited() const
131 {
132         return m_open_inhibited > 0;
133 }
134
135 void GUIChatConsole::closeConsole()
136 {
137         m_open = false;
138 }
139
140 void GUIChatConsole::closeConsoleAtOnce()
141 {
142         m_open = false;
143         m_height = 0;
144         recalculateConsolePosition();
145 }
146
147 f32 GUIChatConsole::getDesiredHeight() const
148 {
149         return m_desired_height_fraction;
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_desired_height = m_desired_height_fraction * m_screensize.Y;
192                 m_screensize = screensize;
193                 reformatConsole();
194         }
195
196         // Animation
197         u32 now = 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         m_chat_backend->reformat(cols, rows);
219 }
220
221 void GUIChatConsole::recalculateConsolePosition()
222 {
223         core::rect<s32> rect(0, 0, m_screensize.X, m_height);
224         DesiredRect = rect;
225         recalculateAbsolutePosition(false);
226 }
227
228 void GUIChatConsole::animate(u32 msec)
229 {
230         // animate the console height
231         s32 goal = m_open ? m_desired_height : 0;
232         if (m_height != goal)
233         {
234                 s32 max_change = msec * m_screensize.Y * (m_height_speed / 1000.0);
235                 if (max_change == 0)
236                         max_change = 1;
237
238                 if (m_height < goal)
239                 {
240                         // increase height
241                         if (m_height + max_change < goal)
242                                 m_height += max_change;
243                         else
244                                 m_height = goal;
245                 }
246                 else
247                 {
248                         // decrease height
249                         if (m_height > goal + max_change)
250                                 m_height -= max_change;
251                         else
252                                 m_height = goal;
253                 }
254
255                 recalculateConsolePosition();
256         }
257
258         // blink the cursor
259         if (m_cursor_blink_speed != 0.0)
260         {
261                 u32 blink_increase = 0x10000 * msec * (m_cursor_blink_speed / 1000.0);
262                 if (blink_increase == 0)
263                         blink_increase = 1;
264                 m_cursor_blink = ((m_cursor_blink + blink_increase) & 0xffff);
265         }
266
267         // decrease open inhibit counter
268         if (m_open_inhibited > msec)
269                 m_open_inhibited -= msec;
270         else
271                 m_open_inhibited = 0;
272 }
273
274 void GUIChatConsole::drawBackground()
275 {
276         video::IVideoDriver* driver = Environment->getVideoDriver();
277         if (m_background != NULL)
278         {
279                 core::rect<s32> sourcerect(0, -m_height, m_screensize.X, 0);
280                 driver->draw2DImage(
281                         m_background,
282                         v2s32(0, 0),
283                         sourcerect,
284                         &AbsoluteClippingRect,
285                         m_background_color,
286                         false);
287         }
288         else
289         {
290                 driver->draw2DRectangle(
291                         m_background_color,
292                         core::rect<s32>(0, 0, m_screensize.X, m_height),
293                         &AbsoluteClippingRect);
294         }
295 }
296
297 void GUIChatConsole::drawText()
298 {
299         if (m_font == NULL)
300                 return;
301
302         ChatBuffer& buf = m_chat_backend->getConsoleBuffer();
303         for (u32 row = 0; row < buf.getRows(); ++row)
304         {
305                 const ChatFormattedLine& line = buf.getFormattedLine(row);
306                 if (line.fragments.empty())
307                         continue;
308
309                 s32 line_height = m_fontsize.Y;
310                 s32 y = row * line_height + m_height - m_desired_height;
311                 if (y + line_height < 0)
312                         continue;
313
314                 for (u32 i = 0; i < line.fragments.size(); ++i)
315                 {
316                         const ChatFormattedFragment& fragment = line.fragments[i];
317                         s32 x = (fragment.column + 1) * m_fontsize.X;
318                         core::rect<s32> destrect(
319                                 x, y, x + m_fontsize.X * fragment.text.size(), y + m_fontsize.Y);
320                         m_font->draw(
321                                 fragment.text.c_str(),
322                                 destrect,
323                                 video::SColor(255, 255, 255, 255),
324                                 false,
325                                 false,
326                                 &AbsoluteClippingRect);
327                 }
328         }
329 }
330
331 void GUIChatConsole::drawPrompt()
332 {
333         if (m_font == NULL)
334                 return;
335
336         u32 row = m_chat_backend->getConsoleBuffer().getRows();
337         s32 line_height = m_fontsize.Y;
338         s32 y = row * line_height + m_height - m_desired_height;
339
340         ChatPrompt& prompt = m_chat_backend->getPrompt();
341         std::wstring prompt_text = prompt.getVisiblePortion();
342
343         // FIXME Draw string at once, not character by character
344         // That will only work with the cursor once we have a monospace font
345         for (u32 i = 0; i < prompt_text.size(); ++i)
346         {
347                 wchar_t ws[2] = {prompt_text[i], 0};
348                 s32 x = (1 + i) * m_fontsize.X;
349                 core::rect<s32> destrect(
350                         x, y, x + m_fontsize.X, y + m_fontsize.Y);
351                 m_font->draw(
352                         ws,
353                         destrect,
354                         video::SColor(255, 255, 255, 255),
355                         false,
356                         false,
357                         &AbsoluteClippingRect);
358         }
359
360         // Draw the cursor during on periods
361         if ((m_cursor_blink & 0x8000) != 0)
362         {
363                 s32 cursor_pos = prompt.getVisibleCursorPosition();
364                 if (cursor_pos >= 0)
365                 {
366                         video::IVideoDriver* driver = Environment->getVideoDriver();
367                         s32 x = (1 + cursor_pos) * m_fontsize.X;
368                         core::rect<s32> destrect(
369                                 x,
370                                 y + (1.0-m_cursor_height) * m_fontsize.Y,
371                                 x + m_fontsize.X,
372                                 y + m_fontsize.Y);
373                         video::SColor cursor_color(255,255,255,255);
374                         driver->draw2DRectangle(
375                                 cursor_color,
376                                 destrect,
377                                 &AbsoluteClippingRect);
378                 }
379         }
380
381 }
382
383 bool GUIChatConsole::OnEvent(const SEvent& event)
384 {
385         if(event.EventType == EET_KEY_INPUT_EVENT && event.KeyInput.PressedDown)
386         {
387                 // Key input
388                 if(KeyPress(event.KeyInput) == getKeySetting("keymap_console"))
389                 {
390                         closeConsole();
391                         Environment->removeFocus(this);
392
393                         // inhibit open so the_game doesn't reopen immediately
394                         m_open_inhibited = 50;
395                         return true;
396                 }
397                 else if(event.KeyInput.Key == KEY_ESCAPE)
398                 {
399                         closeConsoleAtOnce();
400                         Environment->removeFocus(this);
401                         // the_game will open the pause menu
402                         return true;
403                 }
404                 else if(event.KeyInput.Key == KEY_PRIOR)
405                 {
406                         m_chat_backend->scrollPageUp();
407                         return true;
408                 }
409                 else if(event.KeyInput.Key == KEY_NEXT)
410                 {
411                         m_chat_backend->scrollPageDown();
412                         return true;
413                 }
414                 else if(event.KeyInput.Key == KEY_RETURN)
415                 {
416                         std::wstring text = m_chat_backend->getPrompt().submit();
417                         m_client->typeChatMessage(text);
418                         return true;
419                 }
420                 else if(event.KeyInput.Key == KEY_UP)
421                 {
422                         // Up pressed
423                         // Move back in history
424                         m_chat_backend->getPrompt().historyPrev();
425                         return true;
426                 }
427                 else if(event.KeyInput.Key == KEY_DOWN)
428                 {
429                         // Down pressed
430                         // Move forward in history
431                         m_chat_backend->getPrompt().historyNext();
432                         return true;
433                 }
434                 else if(event.KeyInput.Key == KEY_LEFT)
435                 {
436                         // Left or Ctrl-Left pressed
437                         // move character / word to the left
438                         ChatPrompt::CursorOpScope scope =
439                                 event.KeyInput.Control ?
440                                 ChatPrompt::CURSOROP_SCOPE_WORD :
441                                 ChatPrompt::CURSOROP_SCOPE_CHARACTER;
442                         m_chat_backend->getPrompt().cursorOperation(
443                                 ChatPrompt::CURSOROP_MOVE,
444                                 ChatPrompt::CURSOROP_DIR_LEFT,
445                                 scope);
446                         return true;
447                 }
448                 else if(event.KeyInput.Key == KEY_RIGHT)
449                 {
450                         // Right or Ctrl-Right pressed
451                         // move character / word to the right
452                         ChatPrompt::CursorOpScope scope =
453                                 event.KeyInput.Control ?
454                                 ChatPrompt::CURSOROP_SCOPE_WORD :
455                                 ChatPrompt::CURSOROP_SCOPE_CHARACTER;
456                         m_chat_backend->getPrompt().cursorOperation(
457                                 ChatPrompt::CURSOROP_MOVE,
458                                 ChatPrompt::CURSOROP_DIR_RIGHT,
459                                 scope);
460                         return true;
461                 }
462                 else if(event.KeyInput.Key == KEY_HOME)
463                 {
464                         // Home pressed
465                         // move to beginning of line
466                         m_chat_backend->getPrompt().cursorOperation(
467                                 ChatPrompt::CURSOROP_MOVE,
468                                 ChatPrompt::CURSOROP_DIR_LEFT,
469                                 ChatPrompt::CURSOROP_SCOPE_LINE);
470                         return true;
471                 }
472                 else if(event.KeyInput.Key == KEY_END)
473                 {
474                         // End pressed
475                         // move to end of line
476                         m_chat_backend->getPrompt().cursorOperation(
477                                 ChatPrompt::CURSOROP_MOVE,
478                                 ChatPrompt::CURSOROP_DIR_RIGHT,
479                                 ChatPrompt::CURSOROP_SCOPE_LINE);
480                         return true;
481                 }
482                 else if(event.KeyInput.Key == KEY_BACK)
483                 {
484                         // Backspace or Ctrl-Backspace pressed
485                         // delete character / word to the left
486                         ChatPrompt::CursorOpScope scope =
487                                 event.KeyInput.Control ?
488                                 ChatPrompt::CURSOROP_SCOPE_WORD :
489                                 ChatPrompt::CURSOROP_SCOPE_CHARACTER;
490                         m_chat_backend->getPrompt().cursorOperation(
491                                 ChatPrompt::CURSOROP_DELETE,
492                                 ChatPrompt::CURSOROP_DIR_LEFT,
493                                 scope);
494                         return true;
495                 }
496                 else if(event.KeyInput.Key == KEY_DELETE)
497                 {
498                         // Delete or Ctrl-Delete pressed
499                         // delete character / word to the right
500                         ChatPrompt::CursorOpScope scope =
501                                 event.KeyInput.Control ?
502                                 ChatPrompt::CURSOROP_SCOPE_WORD :
503                                 ChatPrompt::CURSOROP_SCOPE_CHARACTER;
504                         m_chat_backend->getPrompt().cursorOperation(
505                                 ChatPrompt::CURSOROP_DELETE,
506                                 ChatPrompt::CURSOROP_DIR_RIGHT,
507                                 scope);
508                         return true;
509                 }
510                 else if(event.KeyInput.Key == KEY_KEY_U && event.KeyInput.Control)
511                 {
512                         // Ctrl-U pressed
513                         // kill line to left end
514                         m_chat_backend->getPrompt().cursorOperation(
515                                 ChatPrompt::CURSOROP_DELETE,
516                                 ChatPrompt::CURSOROP_DIR_LEFT,
517                                 ChatPrompt::CURSOROP_SCOPE_LINE);
518                         return true;
519                 }
520                 else if(event.KeyInput.Key == KEY_KEY_K && event.KeyInput.Control)
521                 {
522                         // Ctrl-K pressed
523                         // kill line to right end
524                         m_chat_backend->getPrompt().cursorOperation(
525                                 ChatPrompt::CURSOROP_DELETE,
526                                 ChatPrompt::CURSOROP_DIR_RIGHT,
527                                 ChatPrompt::CURSOROP_SCOPE_LINE);
528                         return true;
529                 }
530                 else if(event.KeyInput.Key == KEY_TAB)
531                 {
532                         // Tab or Shift-Tab pressed
533                         // Nick completion
534                         std::list<std::string> names = m_client->getConnectedPlayerNames();
535                         bool backwards = event.KeyInput.Shift;
536                         m_chat_backend->getPrompt().nickCompletion(names, backwards);
537                         return true;
538                 }
539                 else if(event.KeyInput.Char != 0 && !event.KeyInput.Control)
540                 {
541                         #if (defined(linux) || defined(__linux))
542                                 wchar_t wc = L'_';
543                                 mbtowc( &wc, (char *) &event.KeyInput.Char, sizeof(event.KeyInput.Char) );
544                                 m_chat_backend->getPrompt().input(wc);
545                         #else
546                                 m_chat_backend->getPrompt().input(event.KeyInput.Char);
547                         #endif
548                         return true;
549                 }
550         }
551         else if(event.EventType == EET_MOUSE_INPUT_EVENT)
552         {
553                 if(event.MouseInput.Event == EMIE_MOUSE_WHEEL)
554                 {
555                         s32 rows = myround(-3.0 * event.MouseInput.Wheel);
556                         m_chat_backend->scroll(rows);
557                 }
558         }
559
560         return Parent ? Parent->OnEvent(event) : false;
561 }
562