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