3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
20 #include "guiChatConsole.h"
22 #include "client/client.h"
25 #include "client/keycode.h"
28 #include "client/tile.h"
29 #include "client/fontengine.h"
35 #include "irrlicht_changes/CGUITTFont.h"
38 inline u32 clamp_u8(s32 value)
40 return (u32) MYMIN(MYMAX(value, 0), 255);
44 GUIChatConsole::GUIChatConsole(
45 gui::IGUIEnvironment* env,
46 gui::IGUIElement* parent,
52 IGUIElement(gui::EGUIET_ELEMENT, env, parent, id,
53 core::rect<s32>(0,0,100,100)),
54 m_chat_backend(backend),
57 m_animate_time_old(porting::getTimeMs())
59 // load background settings
60 s32 console_alpha = g_settings->getS32("console_alpha");
61 m_background_color.setAlpha(clamp_u8(console_alpha));
63 // load the background texture depending on settings
64 ITextureSource *tsrc = client->getTextureSource();
65 if (tsrc->isKnownSourceImage("background_chat.jpg")) {
66 m_background = tsrc->getTexture("background_chat.jpg");
67 m_background_color.setRed(255);
68 m_background_color.setGreen(255);
69 m_background_color.setBlue(255);
71 v3f console_color = g_settings->getV3F("console_color");
72 m_background_color.setRed(clamp_u8(myround(console_color.X)));
73 m_background_color.setGreen(clamp_u8(myround(console_color.Y)));
74 m_background_color.setBlue(clamp_u8(myround(console_color.Z)));
77 u16 chat_font_size = g_settings->getU16("chat_font_size");
78 m_font = g_fontengine->getFont(chat_font_size != 0 ?
79 chat_font_size : FONT_SIZE_UNSPECIFIED, FM_Mono);
82 errorstream << "GUIChatConsole: Unable to load mono font" << std::endl;
84 core::dimension2d<u32> dim = m_font->getDimension(L"M");
85 m_fontsize = v2u32(dim.Width, dim.Height);
88 m_fontsize.X = MYMAX(m_fontsize.X, 1);
89 m_fontsize.Y = MYMAX(m_fontsize.Y, 1);
91 // set default cursor options
92 setCursor(true, true, 2.0, 0.1);
95 GUIChatConsole::~GUIChatConsole()
101 void GUIChatConsole::openConsole(f32 scale)
103 assert(scale > 0.0f && scale <= 1.0f);
106 m_desired_height_fraction = scale;
107 m_desired_height = scale * m_screensize.Y;
109 m_animate_time_old = porting::getTimeMs();
110 IGUIElement::setVisible(true);
111 Environment->setFocus(this);
112 m_menumgr->createdMenu(this);
115 bool GUIChatConsole::isOpen() const
120 bool GUIChatConsole::isOpenInhibited() const
122 return m_open_inhibited > 0;
125 void GUIChatConsole::closeConsole()
128 Environment->removeFocus(this);
129 m_menumgr->deletingMenu(this);
132 void GUIChatConsole::closeConsoleAtOnce()
136 recalculateConsolePosition();
139 f32 GUIChatConsole::getDesiredHeight() const
141 return m_desired_height_fraction;
144 void GUIChatConsole::replaceAndAddToHistory(const std::wstring &line)
146 ChatPrompt& prompt = m_chat_backend->getPrompt();
147 prompt.addToHistory(prompt.getLine());
148 prompt.replace(line);
152 void GUIChatConsole::setCursor(
153 bool visible, bool blinking, f32 blink_speed, f32 relative_height)
159 // leave m_cursor_blink unchanged
160 m_cursor_blink_speed = blink_speed;
164 m_cursor_blink = 0x8000; // on
165 m_cursor_blink_speed = 0.0;
170 m_cursor_blink = 0; // off
171 m_cursor_blink_speed = 0.0;
173 m_cursor_height = relative_height;
176 void GUIChatConsole::draw()
181 video::IVideoDriver* driver = Environment->getVideoDriver();
184 v2u32 screensize = driver->getScreenSize();
185 if (screensize != m_screensize)
187 // screen size has changed
188 // scale current console height to new window size
189 if (m_screensize.Y != 0)
190 m_height = m_height * screensize.Y / m_screensize.Y;
191 m_screensize = screensize;
192 m_desired_height = m_desired_height_fraction * m_screensize.Y;
197 u64 now = porting::getTimeMs();
198 animate(now - m_animate_time_old);
199 m_animate_time_old = now;
201 // Draw console elements if visible
209 gui::IGUIElement::draw();
212 void GUIChatConsole::reformatConsole()
214 s32 cols = m_screensize.X / m_fontsize.X - 2; // make room for a margin (looks better)
215 s32 rows = m_desired_height / m_fontsize.Y - 1; // make room for the input prompt
216 if (cols <= 0 || rows <= 0)
218 recalculateConsolePosition();
219 m_chat_backend->reformat(cols, rows);
222 void GUIChatConsole::recalculateConsolePosition()
224 core::rect<s32> rect(0, 0, m_screensize.X, m_height);
226 recalculateAbsolutePosition(false);
229 void GUIChatConsole::animate(u32 msec)
231 // animate the console height
232 s32 goal = m_open ? m_desired_height : 0;
234 // Set invisible if close animation finished (reset by openConsole)
235 // This function (animate()) is never called once its visibility becomes false so do not
236 // actually set visible to false before the inhibited period is over
237 if (!m_open && m_height == 0 && m_open_inhibited == 0)
238 IGUIElement::setVisible(false);
240 if (m_height != goal)
242 s32 max_change = msec * m_screensize.Y * (m_height_speed / 1000.0);
249 if (m_height + max_change < goal)
250 m_height += max_change;
257 if (m_height > goal + max_change)
258 m_height -= max_change;
263 recalculateConsolePosition();
267 if (m_cursor_blink_speed != 0.0)
269 u32 blink_increase = 0x10000 * msec * (m_cursor_blink_speed / 1000.0);
270 if (blink_increase == 0)
272 m_cursor_blink = ((m_cursor_blink + blink_increase) & 0xffff);
275 // decrease open inhibit counter
276 if (m_open_inhibited > msec)
277 m_open_inhibited -= msec;
279 m_open_inhibited = 0;
282 void GUIChatConsole::drawBackground()
284 video::IVideoDriver* driver = Environment->getVideoDriver();
285 if (m_background != NULL)
287 core::rect<s32> sourcerect(0, -m_height, m_screensize.X, 0);
292 &AbsoluteClippingRect,
298 driver->draw2DRectangle(
300 core::rect<s32>(0, 0, m_screensize.X, m_height),
301 &AbsoluteClippingRect);
305 void GUIChatConsole::drawText()
310 ChatBuffer& buf = m_chat_backend->getConsoleBuffer();
311 for (u32 row = 0; row < buf.getRows(); ++row)
313 const ChatFormattedLine& line = buf.getFormattedLine(row);
314 if (line.fragments.empty())
317 s32 line_height = m_fontsize.Y;
318 s32 y = row * line_height + m_height - m_desired_height;
319 if (y + line_height < 0)
322 for (const ChatFormattedFragment &fragment : line.fragments) {
323 s32 x = (fragment.column + 1) * m_fontsize.X;
324 core::rect<s32> destrect(
325 x, y, x + m_fontsize.X * fragment.text.size(), y + m_fontsize.Y);
328 if (m_font->getType() == irr::gui::EGFT_CUSTOM) {
329 // Draw colored text if FreeType is enabled
330 irr::gui::CGUITTFont *tmp = dynamic_cast<irr::gui::CGUITTFont *>(m_font);
334 video::SColor(255, 255, 255, 255),
337 &AbsoluteClippingRect);
341 // Otherwise use standard text
343 fragment.text.c_str(),
345 video::SColor(255, 255, 255, 255),
348 &AbsoluteClippingRect);
354 void GUIChatConsole::drawPrompt()
359 u32 row = m_chat_backend->getConsoleBuffer().getRows();
360 s32 line_height = m_fontsize.Y;
361 s32 y = row * line_height + m_height - m_desired_height;
363 ChatPrompt& prompt = m_chat_backend->getPrompt();
364 std::wstring prompt_text = prompt.getVisiblePortion();
366 // FIXME Draw string at once, not character by character
367 // That will only work with the cursor once we have a monospace font
368 for (u32 i = 0; i < prompt_text.size(); ++i)
370 wchar_t ws[2] = {prompt_text[i], 0};
371 s32 x = (1 + i) * m_fontsize.X;
372 core::rect<s32> destrect(
373 x, y, x + m_fontsize.X, y + m_fontsize.Y);
377 video::SColor(255, 255, 255, 255),
380 &AbsoluteClippingRect);
383 // Draw the cursor during on periods
384 if ((m_cursor_blink & 0x8000) != 0)
386 s32 cursor_pos = prompt.getVisibleCursorPosition();
389 s32 cursor_len = prompt.getCursorLength();
390 video::IVideoDriver* driver = Environment->getVideoDriver();
391 s32 x = (1 + cursor_pos) * m_fontsize.X;
392 core::rect<s32> destrect(
394 y + m_fontsize.Y * (1.0 - m_cursor_height),
395 x + m_fontsize.X * MYMAX(cursor_len, 1),
396 y + m_fontsize.Y * (cursor_len ? m_cursor_height+1 : 1)
398 video::SColor cursor_color(255,255,255,255);
399 driver->draw2DRectangle(
402 &AbsoluteClippingRect);
408 bool GUIChatConsole::OnEvent(const SEvent& event)
411 ChatPrompt &prompt = m_chat_backend->getPrompt();
413 if(event.EventType == EET_KEY_INPUT_EVENT && event.KeyInput.PressedDown)
416 if (KeyPress(event.KeyInput) == getKeySetting("keymap_console")) {
419 // inhibit open so the_game doesn't reopen immediately
420 m_open_inhibited = 50;
421 m_close_on_enter = false;
425 if (event.KeyInput.Key == KEY_ESCAPE) {
426 closeConsoleAtOnce();
427 m_close_on_enter = false;
428 // inhibit open so the_game doesn't reopen immediately
429 m_open_inhibited = 1; // so the ESCAPE button doesn't open the "pause menu"
432 else if(event.KeyInput.Key == KEY_PRIOR)
434 m_chat_backend->scrollPageUp();
437 else if(event.KeyInput.Key == KEY_NEXT)
439 m_chat_backend->scrollPageDown();
442 else if(event.KeyInput.Key == KEY_RETURN)
444 prompt.addToHistory(prompt.getLine());
445 std::wstring text = prompt.replace(L"");
446 m_client->typeChatMessage(text);
447 if (m_close_on_enter) {
448 closeConsoleAtOnce();
449 m_close_on_enter = false;
453 else if(event.KeyInput.Key == KEY_UP)
456 // Move back in history
457 prompt.historyPrev();
460 else if(event.KeyInput.Key == KEY_DOWN)
463 // Move forward in history
464 prompt.historyNext();
467 else if(event.KeyInput.Key == KEY_LEFT || event.KeyInput.Key == KEY_RIGHT)
469 // Left/right pressed
470 // Move/select character/word to the left depending on control and shift keys
471 ChatPrompt::CursorOp op = event.KeyInput.Shift ?
472 ChatPrompt::CURSOROP_SELECT :
473 ChatPrompt::CURSOROP_MOVE;
474 ChatPrompt::CursorOpDir dir = event.KeyInput.Key == KEY_LEFT ?
475 ChatPrompt::CURSOROP_DIR_LEFT :
476 ChatPrompt::CURSOROP_DIR_RIGHT;
477 ChatPrompt::CursorOpScope scope = event.KeyInput.Control ?
478 ChatPrompt::CURSOROP_SCOPE_WORD :
479 ChatPrompt::CURSOROP_SCOPE_CHARACTER;
480 prompt.cursorOperation(op, dir, scope);
483 else if(event.KeyInput.Key == KEY_HOME)
486 // move to beginning of line
487 prompt.cursorOperation(
488 ChatPrompt::CURSOROP_MOVE,
489 ChatPrompt::CURSOROP_DIR_LEFT,
490 ChatPrompt::CURSOROP_SCOPE_LINE);
493 else if(event.KeyInput.Key == KEY_END)
496 // move to end of line
497 prompt.cursorOperation(
498 ChatPrompt::CURSOROP_MOVE,
499 ChatPrompt::CURSOROP_DIR_RIGHT,
500 ChatPrompt::CURSOROP_SCOPE_LINE);
503 else if(event.KeyInput.Key == KEY_BACK)
505 // Backspace or Ctrl-Backspace pressed
506 // delete character / word to the left
507 ChatPrompt::CursorOpScope scope =
508 event.KeyInput.Control ?
509 ChatPrompt::CURSOROP_SCOPE_WORD :
510 ChatPrompt::CURSOROP_SCOPE_CHARACTER;
511 prompt.cursorOperation(
512 ChatPrompt::CURSOROP_DELETE,
513 ChatPrompt::CURSOROP_DIR_LEFT,
517 else if(event.KeyInput.Key == KEY_DELETE)
519 // Delete or Ctrl-Delete pressed
520 // delete character / word to the right
521 ChatPrompt::CursorOpScope scope =
522 event.KeyInput.Control ?
523 ChatPrompt::CURSOROP_SCOPE_WORD :
524 ChatPrompt::CURSOROP_SCOPE_CHARACTER;
525 prompt.cursorOperation(
526 ChatPrompt::CURSOROP_DELETE,
527 ChatPrompt::CURSOROP_DIR_RIGHT,
531 else if(event.KeyInput.Key == KEY_KEY_A && event.KeyInput.Control)
535 prompt.cursorOperation(
536 ChatPrompt::CURSOROP_SELECT,
537 ChatPrompt::CURSOROP_DIR_LEFT, // Ignored
538 ChatPrompt::CURSOROP_SCOPE_LINE);
541 else if(event.KeyInput.Key == KEY_KEY_C && event.KeyInput.Control)
544 // Copy text to clipboard
545 if (prompt.getCursorLength() <= 0)
547 std::wstring wselected = prompt.getSelection();
548 std::string selected(wselected.begin(), wselected.end());
549 Environment->getOSOperator()->copyToClipboard(selected.c_str());
552 else if(event.KeyInput.Key == KEY_KEY_V && event.KeyInput.Control)
555 // paste text from clipboard
556 if (prompt.getCursorLength() > 0) {
557 // Delete selected section of text
558 prompt.cursorOperation(
559 ChatPrompt::CURSOROP_DELETE,
560 ChatPrompt::CURSOROP_DIR_LEFT, // Ignored
561 ChatPrompt::CURSOROP_SCOPE_SELECTION);
563 IOSOperator *os_operator = Environment->getOSOperator();
564 const c8 *text = os_operator->getTextFromClipboard();
567 std::basic_string<unsigned char> str((const unsigned char*)text);
568 prompt.input(std::wstring(str.begin(), str.end()));
571 else if(event.KeyInput.Key == KEY_KEY_X && event.KeyInput.Control)
574 // Cut text to clipboard
575 if (prompt.getCursorLength() <= 0)
577 std::wstring wselected = prompt.getSelection();
578 std::string selected(wselected.begin(), wselected.end());
579 Environment->getOSOperator()->copyToClipboard(selected.c_str());
580 prompt.cursorOperation(
581 ChatPrompt::CURSOROP_DELETE,
582 ChatPrompt::CURSOROP_DIR_LEFT, // Ignored
583 ChatPrompt::CURSOROP_SCOPE_SELECTION);
586 else if(event.KeyInput.Key == KEY_KEY_U && event.KeyInput.Control)
589 // kill line to left end
590 prompt.cursorOperation(
591 ChatPrompt::CURSOROP_DELETE,
592 ChatPrompt::CURSOROP_DIR_LEFT,
593 ChatPrompt::CURSOROP_SCOPE_LINE);
596 else if(event.KeyInput.Key == KEY_KEY_K && event.KeyInput.Control)
599 // kill line to right end
600 prompt.cursorOperation(
601 ChatPrompt::CURSOROP_DELETE,
602 ChatPrompt::CURSOROP_DIR_RIGHT,
603 ChatPrompt::CURSOROP_SCOPE_LINE);
606 else if(event.KeyInput.Key == KEY_TAB)
608 // Tab or Shift-Tab pressed
610 std::list<std::string> names = m_client->getConnectedPlayerNames();
611 bool backwards = event.KeyInput.Shift;
612 prompt.nickCompletion(names, backwards);
614 } else if (!iswcntrl(event.KeyInput.Char) && !event.KeyInput.Control) {
615 #if defined(__linux__) && (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9)
617 mbtowc( &wc, (char *) &event.KeyInput.Char, sizeof(event.KeyInput.Char) );
620 prompt.input(event.KeyInput.Char);
625 else if(event.EventType == EET_MOUSE_INPUT_EVENT)
627 if(event.MouseInput.Event == EMIE_MOUSE_WHEEL)
629 s32 rows = myround(-3.0 * event.MouseInput.Wheel);
630 m_chat_backend->scroll(rows);
634 return Parent ? Parent->OnEvent(event) : false;
637 void GUIChatConsole::setVisible(bool visible)
640 IGUIElement::setVisible(visible);
643 recalculateConsolePosition();