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