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