Re-add jungles, apple trees
[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 "IGUIFont.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         // load the font
96         // FIXME should a custom texture_path be searched too?
97         #if USE_FREETYPE
98         std::string font_name = g_settings->get("mono_font_path");
99         u16 font_size = g_settings->getU16("mono_font_size");
100         m_font = gui::CGUITTFont::createTTFont(env, font_name.c_str(), font_size);
101         #else
102         std::string font_name = "fontdejavusansmono.png";
103         m_font = env->getFont(getTexturePath(font_name).c_str());
104         #endif
105         if (m_font == NULL)
106         {
107                 dstream << "Unable to load font: " << font_name << std::endl;
108         }
109         else
110         {
111                 core::dimension2d<u32> dim = m_font->getDimension(L"M");
112                 m_fontsize = v2u32(dim.Width, dim.Height);
113                 dstream << "Font size: " << m_fontsize.X << " " << m_fontsize.Y << std::endl;
114         }
115         m_fontsize.X = MYMAX(m_fontsize.X, 1);
116         m_fontsize.Y = MYMAX(m_fontsize.Y, 1);
117
118         // set default cursor options
119         setCursor(true, true, 2.0, 0.1);
120 }
121
122 GUIChatConsole::~GUIChatConsole()
123 {
124 }
125
126 void GUIChatConsole::openConsole(f32 height)
127 {
128         m_open = true;
129         m_desired_height_fraction = height;
130         m_desired_height = height * m_screensize.Y;
131         reformatConsole();
132 }
133
134 bool GUIChatConsole::isOpenInhibited() const
135 {
136         return m_open_inhibited > 0;
137 }
138
139 void GUIChatConsole::closeConsole()
140 {
141         m_open = false;
142 }
143
144 void GUIChatConsole::closeConsoleAtOnce()
145 {
146         m_open = false;
147         m_height = 0;
148         recalculateConsolePosition();
149 }
150
151 f32 GUIChatConsole::getDesiredHeight() const
152 {
153         return m_desired_height_fraction;
154 }
155
156 void GUIChatConsole::setCursor(
157         bool visible, bool blinking, f32 blink_speed, f32 relative_height)
158 {
159         if (visible)
160         {
161                 if (blinking)
162                 {
163                         // leave m_cursor_blink unchanged
164                         m_cursor_blink_speed = blink_speed;
165                 }
166                 else
167                 {
168                         m_cursor_blink = 0x8000;  // on
169                         m_cursor_blink_speed = 0.0;
170                 }
171         }
172         else
173         {
174                 m_cursor_blink = 0;  // off
175                 m_cursor_blink_speed = 0.0;
176         }
177         m_cursor_height = relative_height;
178 }
179
180 void GUIChatConsole::draw()
181 {
182         if(!IsVisible)
183                 return;
184
185         video::IVideoDriver* driver = Environment->getVideoDriver();
186
187         // Check screen size
188         v2u32 screensize = driver->getScreenSize();
189         if (screensize != m_screensize)
190         {
191                 // screen size has changed
192                 // scale current console height to new window size
193                 if (m_screensize.Y != 0)
194                         m_height = m_height * screensize.Y / m_screensize.Y;
195                 m_desired_height = m_desired_height_fraction * m_screensize.Y;
196                 m_screensize = screensize;
197                 reformatConsole();
198         }
199
200         // Animation
201         u32 now = getTimeMs();
202         animate(now - m_animate_time_old);
203         m_animate_time_old = now;
204
205         // Draw console elements if visible
206         if (m_height > 0)
207         {
208                 drawBackground();
209                 drawText();
210                 drawPrompt();
211         }
212
213         gui::IGUIElement::draw();
214 }
215
216 void GUIChatConsole::reformatConsole()
217 {
218         s32 cols = m_screensize.X / m_fontsize.X - 2; // make room for a margin (looks better)
219         s32 rows = m_desired_height / m_fontsize.Y - 1; // make room for the input prompt
220         if (cols <= 0 || rows <= 0)
221                 cols = rows = 0;
222         m_chat_backend->reformat(cols, rows);
223 }
224
225 void GUIChatConsole::recalculateConsolePosition()
226 {
227         core::rect<s32> rect(0, 0, m_screensize.X, m_height);
228         DesiredRect = rect;
229         recalculateAbsolutePosition(false);
230 }
231
232 void GUIChatConsole::animate(u32 msec)
233 {
234         // animate the console height
235         s32 goal = m_open ? m_desired_height : 0;
236         if (m_height != goal)
237         {
238                 s32 max_change = msec * m_screensize.Y * (m_height_speed / 1000.0);
239                 if (max_change == 0)
240                         max_change = 1;
241
242                 if (m_height < goal)
243                 {
244                         // increase height
245                         if (m_height + max_change < goal)
246                                 m_height += max_change;
247                         else
248                                 m_height = goal;
249                 }
250                 else
251                 {
252                         // decrease height
253                         if (m_height > goal + max_change)
254                                 m_height -= max_change;
255                         else
256                                 m_height = goal;
257                 }
258
259                 recalculateConsolePosition();
260         }
261
262         // blink the cursor
263         if (m_cursor_blink_speed != 0.0)
264         {
265                 u32 blink_increase = 0x10000 * msec * (m_cursor_blink_speed / 1000.0);
266                 if (blink_increase == 0)
267                         blink_increase = 1;
268                 m_cursor_blink = ((m_cursor_blink + blink_increase) & 0xffff);
269         }
270
271         // decrease open inhibit counter
272         if (m_open_inhibited > msec)
273                 m_open_inhibited -= msec;
274         else
275                 m_open_inhibited = 0;
276 }
277
278 void GUIChatConsole::drawBackground()
279 {
280         video::IVideoDriver* driver = Environment->getVideoDriver();
281         if (m_background != NULL)
282         {
283                 core::rect<s32> sourcerect(0, -m_height, m_screensize.X, 0);
284                 driver->draw2DImage(
285                         m_background,
286                         v2s32(0, 0),
287                         sourcerect,
288                         &AbsoluteClippingRect,
289                         m_background_color,
290                         false);
291         }
292         else
293         {
294                 driver->draw2DRectangle(
295                         m_background_color,
296                         core::rect<s32>(0, 0, m_screensize.X, m_height),
297                         &AbsoluteClippingRect);
298         }
299 }
300
301 void GUIChatConsole::drawText()
302 {
303         if (m_font == NULL)
304                 return;
305
306         ChatBuffer& buf = m_chat_backend->getConsoleBuffer();
307         for (u32 row = 0; row < buf.getRows(); ++row)
308         {
309                 const ChatFormattedLine& line = buf.getFormattedLine(row);
310                 if (line.fragments.empty())
311                         continue;
312
313                 s32 line_height = m_fontsize.Y;
314                 s32 y = row * line_height + m_height - m_desired_height;
315                 if (y + line_height < 0)
316                         continue;
317
318                 for (u32 i = 0; i < line.fragments.size(); ++i)
319                 {
320                         const ChatFormattedFragment& fragment = line.fragments[i];
321                         s32 x = (fragment.column + 1) * m_fontsize.X;
322                         core::rect<s32> destrect(
323                                 x, y, x + m_fontsize.X * fragment.text.size(), y + m_fontsize.Y);
324                         m_font->draw(
325                                 fragment.text.c_str(),
326                                 destrect,
327                                 video::SColor(255, 255, 255, 255),
328                                 false,
329                                 false,
330                                 &AbsoluteClippingRect);
331                 }
332         }
333 }
334
335 void GUIChatConsole::drawPrompt()
336 {
337         if (m_font == NULL)
338                 return;
339
340         u32 row = m_chat_backend->getConsoleBuffer().getRows();
341         s32 line_height = m_fontsize.Y;
342         s32 y = row * line_height + m_height - m_desired_height;
343
344         ChatPrompt& prompt = m_chat_backend->getPrompt();
345         std::wstring prompt_text = prompt.getVisiblePortion();
346
347         // FIXME Draw string at once, not character by character
348         // That will only work with the cursor once we have a monospace font
349         for (u32 i = 0; i < prompt_text.size(); ++i)
350         {
351                 wchar_t ws[2] = {prompt_text[i], 0};
352                 s32 x = (1 + i) * m_fontsize.X;
353                 core::rect<s32> destrect(
354                         x, y, x + m_fontsize.X, y + m_fontsize.Y);
355                 m_font->draw(
356                         ws,
357                         destrect,
358                         video::SColor(255, 255, 255, 255),
359                         false,
360                         false,
361                         &AbsoluteClippingRect);
362         }
363
364         // Draw the cursor during on periods
365         if ((m_cursor_blink & 0x8000) != 0)
366         {
367                 s32 cursor_pos = prompt.getVisibleCursorPosition();
368                 if (cursor_pos >= 0)
369                 {
370                         video::IVideoDriver* driver = Environment->getVideoDriver();
371                         s32 x = (1 + cursor_pos) * m_fontsize.X;
372                         core::rect<s32> destrect(
373                                 x,
374                                 y + (1.0-m_cursor_height) * m_fontsize.Y,
375                                 x + m_fontsize.X,
376                                 y + m_fontsize.Y);
377                         video::SColor cursor_color(255,255,255,255);
378                         driver->draw2DRectangle(
379                                 cursor_color,
380                                 destrect,
381                                 &AbsoluteClippingRect);
382                 }
383         }
384
385 }
386
387 bool GUIChatConsole::OnEvent(const SEvent& event)
388 {
389         if(event.EventType == EET_KEY_INPUT_EVENT && event.KeyInput.PressedDown)
390         {
391                 // Key input
392                 if(KeyPress(event.KeyInput) == getKeySetting("keymap_console"))
393                 {
394                         closeConsole();
395                         Environment->removeFocus(this);
396
397                         // inhibit open so the_game doesn't reopen immediately
398                         m_open_inhibited = 50;
399                         return true;
400                 }
401                 else if(event.KeyInput.Key == KEY_ESCAPE)
402                 {
403                         closeConsoleAtOnce();
404                         Environment->removeFocus(this);
405                         // the_game will open the pause menu
406                         return true;
407                 }
408                 else if(event.KeyInput.Key == KEY_PRIOR)
409                 {
410                         m_chat_backend->scrollPageUp();
411                         return true;
412                 }
413                 else if(event.KeyInput.Key == KEY_NEXT)
414                 {
415                         m_chat_backend->scrollPageDown();
416                         return true;
417                 }
418                 else if(event.KeyInput.Key == KEY_RETURN)
419                 {
420                         std::wstring text = m_chat_backend->getPrompt().submit();
421                         m_client->typeChatMessage(text);
422                         return true;
423                 }
424                 else if(event.KeyInput.Key == KEY_UP)
425                 {
426                         // Up pressed
427                         // Move back in history
428                         m_chat_backend->getPrompt().historyPrev();
429                         return true;
430                 }
431                 else if(event.KeyInput.Key == KEY_DOWN)
432                 {
433                         // Down pressed
434                         // Move forward in history
435                         m_chat_backend->getPrompt().historyNext();
436                         return true;
437                 }
438                 else if(event.KeyInput.Key == KEY_LEFT)
439                 {
440                         // Left or Ctrl-Left pressed
441                         // move character / word to the left
442                         ChatPrompt::CursorOpScope scope =
443                                 event.KeyInput.Control ?
444                                 ChatPrompt::CURSOROP_SCOPE_WORD :
445                                 ChatPrompt::CURSOROP_SCOPE_CHARACTER;
446                         m_chat_backend->getPrompt().cursorOperation(
447                                 ChatPrompt::CURSOROP_MOVE,
448                                 ChatPrompt::CURSOROP_DIR_LEFT,
449                                 scope);
450                         return true;
451                 }
452                 else if(event.KeyInput.Key == KEY_RIGHT)
453                 {
454                         // Right or Ctrl-Right pressed
455                         // move character / word to the right
456                         ChatPrompt::CursorOpScope scope =
457                                 event.KeyInput.Control ?
458                                 ChatPrompt::CURSOROP_SCOPE_WORD :
459                                 ChatPrompt::CURSOROP_SCOPE_CHARACTER;
460                         m_chat_backend->getPrompt().cursorOperation(
461                                 ChatPrompt::CURSOROP_MOVE,
462                                 ChatPrompt::CURSOROP_DIR_RIGHT,
463                                 scope);
464                         return true;
465                 }
466                 else if(event.KeyInput.Key == KEY_HOME)
467                 {
468                         // Home pressed
469                         // move to beginning of line
470                         m_chat_backend->getPrompt().cursorOperation(
471                                 ChatPrompt::CURSOROP_MOVE,
472                                 ChatPrompt::CURSOROP_DIR_LEFT,
473                                 ChatPrompt::CURSOROP_SCOPE_LINE);
474                         return true;
475                 }
476                 else if(event.KeyInput.Key == KEY_END)
477                 {
478                         // End pressed
479                         // move to end of line
480                         m_chat_backend->getPrompt().cursorOperation(
481                                 ChatPrompt::CURSOROP_MOVE,
482                                 ChatPrompt::CURSOROP_DIR_RIGHT,
483                                 ChatPrompt::CURSOROP_SCOPE_LINE);
484                         return true;
485                 }
486                 else if(event.KeyInput.Key == KEY_BACK)
487                 {
488                         // Backspace or Ctrl-Backspace pressed
489                         // delete character / word to the left
490                         ChatPrompt::CursorOpScope scope =
491                                 event.KeyInput.Control ?
492                                 ChatPrompt::CURSOROP_SCOPE_WORD :
493                                 ChatPrompt::CURSOROP_SCOPE_CHARACTER;
494                         m_chat_backend->getPrompt().cursorOperation(
495                                 ChatPrompt::CURSOROP_DELETE,
496                                 ChatPrompt::CURSOROP_DIR_LEFT,
497                                 scope);
498                         return true;
499                 }
500                 else if(event.KeyInput.Key == KEY_DELETE)
501                 {
502                         // Delete or Ctrl-Delete pressed
503                         // delete character / word to the right
504                         ChatPrompt::CursorOpScope scope =
505                                 event.KeyInput.Control ?
506                                 ChatPrompt::CURSOROP_SCOPE_WORD :
507                                 ChatPrompt::CURSOROP_SCOPE_CHARACTER;
508                         m_chat_backend->getPrompt().cursorOperation(
509                                 ChatPrompt::CURSOROP_DELETE,
510                                 ChatPrompt::CURSOROP_DIR_RIGHT,
511                                 scope);
512                         return true;
513                 }
514                 else if(event.KeyInput.Key == KEY_KEY_U && event.KeyInput.Control)
515                 {
516                         // Ctrl-U pressed
517                         // kill line to left end
518                         m_chat_backend->getPrompt().cursorOperation(
519                                 ChatPrompt::CURSOROP_DELETE,
520                                 ChatPrompt::CURSOROP_DIR_LEFT,
521                                 ChatPrompt::CURSOROP_SCOPE_LINE);
522                         return true;
523                 }
524                 else if(event.KeyInput.Key == KEY_KEY_K && event.KeyInput.Control)
525                 {
526                         // Ctrl-K pressed
527                         // kill line to right end
528                         m_chat_backend->getPrompt().cursorOperation(
529                                 ChatPrompt::CURSOROP_DELETE,
530                                 ChatPrompt::CURSOROP_DIR_RIGHT,
531                                 ChatPrompt::CURSOROP_SCOPE_LINE);
532                         return true;
533                 }
534                 else if(event.KeyInput.Key == KEY_TAB)
535                 {
536                         // Tab or Shift-Tab pressed
537                         // Nick completion
538                         std::list<std::wstring> names = m_client->getConnectedPlayerNames();
539                         bool backwards = event.KeyInput.Shift;
540                         m_chat_backend->getPrompt().nickCompletion(names, backwards);
541                         return true;
542                 }
543                 else if(event.KeyInput.Char != 0 && !event.KeyInput.Control)
544                 {
545                         m_chat_backend->getPrompt().input(event.KeyInput.Char);
546                         return true;
547                 }
548         }
549         else if(event.EventType == EET_MOUSE_INPUT_EVENT)
550         {
551                 if(event.MouseInput.Event == EMIE_MOUSE_WHEEL)
552                 {
553                         s32 rows = myround(-3.0 * event.MouseInput.Wheel);
554                         m_chat_backend->scroll(rows);
555                 }
556         }
557
558         return Parent ? Parent->OnEvent(event) : false;
559 }
560