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"
28 #include "client/tile.h"
29 #include "fontengine.h"
35 #include "xCGUITTFont.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_screensize(v2u32(0,0)),
58 m_animate_time_old(porting::getTimeMs()),
60 m_close_on_enter(false),
63 m_desired_height_fraction(0.0),
67 m_cursor_blink_speed(0.0),
70 m_background_color(255, 0, 0, 0),
74 // load background settings
75 s32 console_alpha = g_settings->getS32("console_alpha");
76 m_background_color.setAlpha(clamp_u8(console_alpha));
78 // load the background texture depending on settings
79 ITextureSource *tsrc = client->getTextureSource();
80 if (tsrc->isKnownSourceImage("background_chat.jpg")) {
81 m_background = tsrc->getTexture("background_chat.jpg");
82 m_background_color.setRed(255);
83 m_background_color.setGreen(255);
84 m_background_color.setBlue(255);
86 v3f console_color = g_settings->getV3F("console_color");
87 m_background_color.setRed(clamp_u8(myround(console_color.X)));
88 m_background_color.setGreen(clamp_u8(myround(console_color.Y)));
89 m_background_color.setBlue(clamp_u8(myround(console_color.Z)));
92 m_font = g_fontengine->getFont(FONT_SIZE_UNSPECIFIED, FM_Mono);
96 errorstream << "GUIChatConsole: Unable to load mono font ";
100 core::dimension2d<u32> dim = m_font->getDimension(L"M");
101 m_fontsize = v2u32(dim.Width, dim.Height);
104 m_fontsize.X = MYMAX(m_fontsize.X, 1);
105 m_fontsize.Y = MYMAX(m_fontsize.Y, 1);
107 // set default cursor options
108 setCursor(true, true, 2.0, 0.1);
111 GUIChatConsole::~GUIChatConsole()
117 void GUIChatConsole::openConsole(f32 scale)
119 assert(scale > 0.0f && scale <= 1.0f);
122 m_desired_height_fraction = scale;
123 m_desired_height = scale * m_screensize.Y;
125 m_animate_time_old = porting::getTimeMs();
126 IGUIElement::setVisible(true);
127 Environment->setFocus(this);
128 m_menumgr->createdMenu(this);
131 bool GUIChatConsole::isOpen() const
136 bool GUIChatConsole::isOpenInhibited() const
138 return m_open_inhibited > 0;
141 void GUIChatConsole::closeConsole()
144 Environment->removeFocus(this);
145 m_menumgr->deletingMenu(this);
148 void GUIChatConsole::closeConsoleAtOnce()
152 recalculateConsolePosition();
155 f32 GUIChatConsole::getDesiredHeight() const
157 return m_desired_height_fraction;
160 void GUIChatConsole::replaceAndAddToHistory(std::wstring line)
162 ChatPrompt& prompt = m_chat_backend->getPrompt();
163 prompt.addToHistory(prompt.getLine());
164 prompt.replace(line);
168 void GUIChatConsole::setCursor(
169 bool visible, bool blinking, f32 blink_speed, f32 relative_height)
175 // leave m_cursor_blink unchanged
176 m_cursor_blink_speed = blink_speed;
180 m_cursor_blink = 0x8000; // on
181 m_cursor_blink_speed = 0.0;
186 m_cursor_blink = 0; // off
187 m_cursor_blink_speed = 0.0;
189 m_cursor_height = relative_height;
192 void GUIChatConsole::draw()
197 video::IVideoDriver* driver = Environment->getVideoDriver();
200 v2u32 screensize = driver->getScreenSize();
201 if (screensize != m_screensize)
203 // screen size has changed
204 // scale current console height to new window size
205 if (m_screensize.Y != 0)
206 m_height = m_height * screensize.Y / m_screensize.Y;
207 m_desired_height = m_desired_height_fraction * m_screensize.Y;
208 m_screensize = screensize;
213 u64 now = porting::getTimeMs();
214 animate(now - m_animate_time_old);
215 m_animate_time_old = now;
217 // Draw console elements if visible
225 gui::IGUIElement::draw();
228 void GUIChatConsole::reformatConsole()
230 s32 cols = m_screensize.X / m_fontsize.X - 2; // make room for a margin (looks better)
231 s32 rows = m_desired_height / m_fontsize.Y - 1; // make room for the input prompt
232 if (cols <= 0 || rows <= 0)
234 m_chat_backend->reformat(cols, rows);
237 void GUIChatConsole::recalculateConsolePosition()
239 core::rect<s32> rect(0, 0, m_screensize.X, m_height);
241 recalculateAbsolutePosition(false);
244 void GUIChatConsole::animate(u32 msec)
246 // animate the console height
247 s32 goal = m_open ? m_desired_height : 0;
249 // Set invisible if close animation finished (reset by openConsole)
250 // This function (animate()) is never called once its visibility becomes false so do not
251 // actually set visible to false before the inhibited period is over
252 if (!m_open && m_height == 0 && m_open_inhibited == 0)
253 IGUIElement::setVisible(false);
255 if (m_height != goal)
257 s32 max_change = msec * m_screensize.Y * (m_height_speed / 1000.0);
264 if (m_height + max_change < goal)
265 m_height += max_change;
272 if (m_height > goal + max_change)
273 m_height -= max_change;
278 recalculateConsolePosition();
282 if (m_cursor_blink_speed != 0.0)
284 u32 blink_increase = 0x10000 * msec * (m_cursor_blink_speed / 1000.0);
285 if (blink_increase == 0)
287 m_cursor_blink = ((m_cursor_blink + blink_increase) & 0xffff);
290 // decrease open inhibit counter
291 if (m_open_inhibited > msec)
292 m_open_inhibited -= msec;
294 m_open_inhibited = 0;
297 void GUIChatConsole::drawBackground()
299 video::IVideoDriver* driver = Environment->getVideoDriver();
300 if (m_background != NULL)
302 core::rect<s32> sourcerect(0, -m_height, m_screensize.X, 0);
307 &AbsoluteClippingRect,
313 driver->draw2DRectangle(
315 core::rect<s32>(0, 0, m_screensize.X, m_height),
316 &AbsoluteClippingRect);
320 void GUIChatConsole::drawText()
325 ChatBuffer& buf = m_chat_backend->getConsoleBuffer();
326 for (u32 row = 0; row < buf.getRows(); ++row)
328 const ChatFormattedLine& line = buf.getFormattedLine(row);
329 if (line.fragments.empty())
332 s32 line_height = m_fontsize.Y;
333 s32 y = row * line_height + m_height - m_desired_height;
334 if (y + line_height < 0)
337 for (u32 i = 0; i < line.fragments.size(); ++i)
339 const ChatFormattedFragment& fragment = line.fragments[i];
340 s32 x = (fragment.column + 1) * m_fontsize.X;
341 core::rect<s32> destrect(
342 x, y, x + m_fontsize.X * fragment.text.size(), y + m_fontsize.Y);
346 // Draw colored text if FreeType is enabled
347 irr::gui::CGUITTFont *tmp = static_cast<irr::gui::CGUITTFont*>(m_font);
351 video::SColor(255, 255, 255, 255),
354 &AbsoluteClippingRect);
356 // Otherwise use standard text
358 fragment.text.c_str(),
360 video::SColor(255, 255, 255, 255),
363 &AbsoluteClippingRect);
369 void GUIChatConsole::drawPrompt()
374 u32 row = m_chat_backend->getConsoleBuffer().getRows();
375 s32 line_height = m_fontsize.Y;
376 s32 y = row * line_height + m_height - m_desired_height;
378 ChatPrompt& prompt = m_chat_backend->getPrompt();
379 std::wstring prompt_text = prompt.getVisiblePortion();
381 // FIXME Draw string at once, not character by character
382 // That will only work with the cursor once we have a monospace font
383 for (u32 i = 0; i < prompt_text.size(); ++i)
385 wchar_t ws[2] = {prompt_text[i], 0};
386 s32 x = (1 + i) * m_fontsize.X;
387 core::rect<s32> destrect(
388 x, y, x + m_fontsize.X, y + m_fontsize.Y);
392 video::SColor(255, 255, 255, 255),
395 &AbsoluteClippingRect);
398 // Draw the cursor during on periods
399 if ((m_cursor_blink & 0x8000) != 0)
401 s32 cursor_pos = prompt.getVisibleCursorPosition();
404 s32 cursor_len = prompt.getCursorLength();
405 video::IVideoDriver* driver = Environment->getVideoDriver();
406 s32 x = (1 + cursor_pos) * m_fontsize.X;
407 core::rect<s32> destrect(
409 y + m_fontsize.Y * (1.0 - m_cursor_height),
410 x + m_fontsize.X * MYMAX(cursor_len, 1),
411 y + m_fontsize.Y * (cursor_len ? m_cursor_height+1 : 1)
413 video::SColor cursor_color(255,255,255,255);
414 driver->draw2DRectangle(
417 &AbsoluteClippingRect);
423 bool GUIChatConsole::OnEvent(const SEvent& event)
426 ChatPrompt &prompt = m_chat_backend->getPrompt();
428 if(event.EventType == EET_KEY_INPUT_EVENT && event.KeyInput.PressedDown)
431 if(KeyPress(event.KeyInput) == getKeySetting("keymap_console"))
435 // inhibit open so the_game doesn't reopen immediately
436 m_open_inhibited = 50;
437 m_close_on_enter = false;
440 else if(event.KeyInput.Key == KEY_ESCAPE)
442 closeConsoleAtOnce();
443 m_close_on_enter = false;
444 // inhibit open so the_game doesn't reopen immediately
445 m_open_inhibited = 1; // so the ESCAPE button doesn't open the "pause menu"
448 else if(event.KeyInput.Key == KEY_PRIOR)
450 m_chat_backend->scrollPageUp();
453 else if(event.KeyInput.Key == KEY_NEXT)
455 m_chat_backend->scrollPageDown();
458 else if(event.KeyInput.Key == KEY_RETURN)
460 prompt.addToHistory(prompt.getLine());
461 std::wstring text = prompt.replace(L"");
462 m_client->typeChatMessage(text);
463 if (m_close_on_enter) {
464 closeConsoleAtOnce();
465 m_close_on_enter = false;
469 else if(event.KeyInput.Key == KEY_UP)
472 // Move back in history
473 prompt.historyPrev();
476 else if(event.KeyInput.Key == KEY_DOWN)
479 // Move forward in history
480 prompt.historyNext();
483 else if(event.KeyInput.Key == KEY_LEFT || event.KeyInput.Key == KEY_RIGHT)
485 // Left/right pressed
486 // Move/select character/word to the left depending on control and shift keys
487 ChatPrompt::CursorOp op = event.KeyInput.Shift ?
488 ChatPrompt::CURSOROP_SELECT :
489 ChatPrompt::CURSOROP_MOVE;
490 ChatPrompt::CursorOpDir dir = event.KeyInput.Key == KEY_LEFT ?
491 ChatPrompt::CURSOROP_DIR_LEFT :
492 ChatPrompt::CURSOROP_DIR_RIGHT;
493 ChatPrompt::CursorOpScope scope = event.KeyInput.Control ?
494 ChatPrompt::CURSOROP_SCOPE_WORD :
495 ChatPrompt::CURSOROP_SCOPE_CHARACTER;
496 prompt.cursorOperation(op, dir, scope);
499 else if(event.KeyInput.Key == KEY_HOME)
502 // move to beginning of line
503 prompt.cursorOperation(
504 ChatPrompt::CURSOROP_MOVE,
505 ChatPrompt::CURSOROP_DIR_LEFT,
506 ChatPrompt::CURSOROP_SCOPE_LINE);
509 else if(event.KeyInput.Key == KEY_END)
512 // move to end of line
513 prompt.cursorOperation(
514 ChatPrompt::CURSOROP_MOVE,
515 ChatPrompt::CURSOROP_DIR_RIGHT,
516 ChatPrompt::CURSOROP_SCOPE_LINE);
519 else if(event.KeyInput.Key == KEY_BACK)
521 // Backspace or Ctrl-Backspace pressed
522 // delete character / word to the left
523 ChatPrompt::CursorOpScope scope =
524 event.KeyInput.Control ?
525 ChatPrompt::CURSOROP_SCOPE_WORD :
526 ChatPrompt::CURSOROP_SCOPE_CHARACTER;
527 prompt.cursorOperation(
528 ChatPrompt::CURSOROP_DELETE,
529 ChatPrompt::CURSOROP_DIR_LEFT,
533 else if(event.KeyInput.Key == KEY_DELETE)
535 // Delete or Ctrl-Delete pressed
536 // delete character / word to the right
537 ChatPrompt::CursorOpScope scope =
538 event.KeyInput.Control ?
539 ChatPrompt::CURSOROP_SCOPE_WORD :
540 ChatPrompt::CURSOROP_SCOPE_CHARACTER;
541 prompt.cursorOperation(
542 ChatPrompt::CURSOROP_DELETE,
543 ChatPrompt::CURSOROP_DIR_RIGHT,
547 else if(event.KeyInput.Key == KEY_KEY_A && event.KeyInput.Control)
551 prompt.cursorOperation(
552 ChatPrompt::CURSOROP_SELECT,
553 ChatPrompt::CURSOROP_DIR_LEFT, // Ignored
554 ChatPrompt::CURSOROP_SCOPE_LINE);
557 else if(event.KeyInput.Key == KEY_KEY_C && event.KeyInput.Control)
560 // Copy text to clipboard
561 if (prompt.getCursorLength() <= 0)
563 std::wstring wselected = prompt.getSelection();
564 std::string selected(wselected.begin(), wselected.end());
565 Environment->getOSOperator()->copyToClipboard(selected.c_str());
568 else if(event.KeyInput.Key == KEY_KEY_V && event.KeyInput.Control)
571 // paste text from clipboard
572 if (prompt.getCursorLength() > 0) {
573 // Delete selected section of text
574 prompt.cursorOperation(
575 ChatPrompt::CURSOROP_DELETE,
576 ChatPrompt::CURSOROP_DIR_LEFT, // Ignored
577 ChatPrompt::CURSOROP_SCOPE_SELECTION);
579 IOSOperator *os_operator = Environment->getOSOperator();
580 const c8 *text = os_operator->getTextFromClipboard();
583 std::basic_string<unsigned char> str((const unsigned char*)text);
584 prompt.input(std::wstring(str.begin(), str.end()));
587 else if(event.KeyInput.Key == KEY_KEY_X && event.KeyInput.Control)
590 // Cut text to clipboard
591 if (prompt.getCursorLength() <= 0)
593 std::wstring wselected = prompt.getSelection();
594 std::string selected(wselected.begin(), wselected.end());
595 Environment->getOSOperator()->copyToClipboard(selected.c_str());
596 prompt.cursorOperation(
597 ChatPrompt::CURSOROP_DELETE,
598 ChatPrompt::CURSOROP_DIR_LEFT, // Ignored
599 ChatPrompt::CURSOROP_SCOPE_SELECTION);
602 else if(event.KeyInput.Key == KEY_KEY_U && event.KeyInput.Control)
605 // kill line to left end
606 prompt.cursorOperation(
607 ChatPrompt::CURSOROP_DELETE,
608 ChatPrompt::CURSOROP_DIR_LEFT,
609 ChatPrompt::CURSOROP_SCOPE_LINE);
612 else if(event.KeyInput.Key == KEY_KEY_K && event.KeyInput.Control)
615 // kill line to right end
616 prompt.cursorOperation(
617 ChatPrompt::CURSOROP_DELETE,
618 ChatPrompt::CURSOROP_DIR_RIGHT,
619 ChatPrompt::CURSOROP_SCOPE_LINE);
622 else if(event.KeyInput.Key == KEY_TAB)
624 // Tab or Shift-Tab pressed
626 std::list<std::string> names = m_client->getConnectedPlayerNames();
627 bool backwards = event.KeyInput.Shift;
628 prompt.nickCompletion(names, backwards);
631 else if(event.KeyInput.Char != 0 && !event.KeyInput.Control)
633 #if defined(__linux__) && (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9)
635 mbtowc( &wc, (char *) &event.KeyInput.Char, sizeof(event.KeyInput.Char) );
638 prompt.input(event.KeyInput.Char);
643 else if(event.EventType == EET_MOUSE_INPUT_EVENT)
645 if(event.MouseInput.Event == EMIE_MOUSE_WHEEL)
647 s32 rows = myround(-3.0 * event.MouseInput.Wheel);
648 m_chat_backend->scroll(rows);
652 return Parent ? Parent->OnEvent(event) : false;
655 void GUIChatConsole::setVisible(bool visible)
658 IGUIElement::setVisible(visible);
661 recalculateConsolePosition();