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