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