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 "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 m_font = g_fontengine->getFont(FONT_SIZE_UNSPECIFIED, FM_Mono);
80 errorstream << "GUIChatConsole: Unable to load mono font ";
82 core::dimension2d<u32> dim = m_font->getDimension(L"M");
83 m_fontsize = v2u32(dim.Width, dim.Height);
86 m_fontsize.X = MYMAX(m_fontsize.X, 1);
87 m_fontsize.Y = MYMAX(m_fontsize.Y, 1);
89 // set default cursor options
90 setCursor(true, true, 2.0, 0.1);
93 GUIChatConsole::~GUIChatConsole()
99 void GUIChatConsole::openConsole(f32 scale)
101 assert(scale > 0.0f && scale <= 1.0f);
104 m_desired_height_fraction = scale;
105 m_desired_height = scale * m_screensize.Y;
107 m_animate_time_old = porting::getTimeMs();
108 IGUIElement::setVisible(true);
109 Environment->setFocus(this);
110 m_menumgr->createdMenu(this);
113 bool GUIChatConsole::isOpen() const
118 bool GUIChatConsole::isOpenInhibited() const
120 return m_open_inhibited > 0;
123 void GUIChatConsole::closeConsole()
126 Environment->removeFocus(this);
127 m_menumgr->deletingMenu(this);
130 void GUIChatConsole::closeConsoleAtOnce()
134 recalculateConsolePosition();
137 f32 GUIChatConsole::getDesiredHeight() const
139 return m_desired_height_fraction;
142 void GUIChatConsole::replaceAndAddToHistory(std::wstring line)
144 ChatPrompt& prompt = m_chat_backend->getPrompt();
145 prompt.addToHistory(prompt.getLine());
146 prompt.replace(line);
150 void GUIChatConsole::setCursor(
151 bool visible, bool blinking, f32 blink_speed, f32 relative_height)
157 // leave m_cursor_blink unchanged
158 m_cursor_blink_speed = blink_speed;
162 m_cursor_blink = 0x8000; // on
163 m_cursor_blink_speed = 0.0;
168 m_cursor_blink = 0; // off
169 m_cursor_blink_speed = 0.0;
171 m_cursor_height = relative_height;
174 void GUIChatConsole::draw()
179 video::IVideoDriver* driver = Environment->getVideoDriver();
182 v2u32 screensize = driver->getScreenSize();
183 if (screensize != m_screensize)
185 // screen size has changed
186 // scale current console height to new window size
187 if (m_screensize.Y != 0)
188 m_height = m_height * screensize.Y / m_screensize.Y;
189 m_screensize = screensize;
190 m_desired_height = m_desired_height_fraction * m_screensize.Y;
195 u64 now = porting::getTimeMs();
196 animate(now - m_animate_time_old);
197 m_animate_time_old = now;
199 // Draw console elements if visible
207 gui::IGUIElement::draw();
210 void GUIChatConsole::reformatConsole()
212 s32 cols = m_screensize.X / m_fontsize.X - 2; // make room for a margin (looks better)
213 s32 rows = m_desired_height / m_fontsize.Y - 1; // make room for the input prompt
214 if (cols <= 0 || rows <= 0)
216 recalculateConsolePosition();
217 m_chat_backend->reformat(cols, rows);
220 void GUIChatConsole::recalculateConsolePosition()
222 core::rect<s32> rect(0, 0, m_screensize.X, m_height);
224 recalculateAbsolutePosition(false);
227 void GUIChatConsole::animate(u32 msec)
229 // animate the console height
230 s32 goal = m_open ? m_desired_height : 0;
232 // Set invisible if close animation finished (reset by openConsole)
233 // This function (animate()) is never called once its visibility becomes false so do not
234 // actually set visible to false before the inhibited period is over
235 if (!m_open && m_height == 0 && m_open_inhibited == 0)
236 IGUIElement::setVisible(false);
238 if (m_height != goal)
240 s32 max_change = msec * m_screensize.Y * (m_height_speed / 1000.0);
247 if (m_height + max_change < goal)
248 m_height += max_change;
255 if (m_height > goal + max_change)
256 m_height -= max_change;
261 recalculateConsolePosition();
265 if (m_cursor_blink_speed != 0.0)
267 u32 blink_increase = 0x10000 * msec * (m_cursor_blink_speed / 1000.0);
268 if (blink_increase == 0)
270 m_cursor_blink = ((m_cursor_blink + blink_increase) & 0xffff);
273 // decrease open inhibit counter
274 if (m_open_inhibited > msec)
275 m_open_inhibited -= msec;
277 m_open_inhibited = 0;
280 void GUIChatConsole::drawBackground()
282 video::IVideoDriver* driver = Environment->getVideoDriver();
283 if (m_background != NULL)
285 core::rect<s32> sourcerect(0, -m_height, m_screensize.X, 0);
290 &AbsoluteClippingRect,
296 driver->draw2DRectangle(
298 core::rect<s32>(0, 0, m_screensize.X, m_height),
299 &AbsoluteClippingRect);
303 void GUIChatConsole::drawText()
308 ChatBuffer& buf = m_chat_backend->getConsoleBuffer();
309 for (u32 row = 0; row < buf.getRows(); ++row)
311 const ChatFormattedLine& line = buf.getFormattedLine(row);
312 if (line.fragments.empty())
315 s32 line_height = m_fontsize.Y;
316 s32 y = row * line_height + m_height - m_desired_height;
317 if (y + line_height < 0)
320 for (u32 i = 0; i < line.fragments.size(); ++i)
322 const ChatFormattedFragment& fragment = line.fragments[i];
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);
329 // Draw colored text if FreeType is enabled
330 irr::gui::CGUITTFont *tmp = static_cast<irr::gui::CGUITTFont*>(m_font);
334 video::SColor(255, 255, 255, 255),
337 &AbsoluteClippingRect);
339 // Otherwise use standard text
341 fragment.text.c_str(),
343 video::SColor(255, 255, 255, 255),
346 &AbsoluteClippingRect);
352 void GUIChatConsole::drawPrompt()
357 u32 row = m_chat_backend->getConsoleBuffer().getRows();
358 s32 line_height = m_fontsize.Y;
359 s32 y = row * line_height + m_height - m_desired_height;
361 ChatPrompt& prompt = m_chat_backend->getPrompt();
362 std::wstring prompt_text = prompt.getVisiblePortion();
364 // FIXME Draw string at once, not character by character
365 // That will only work with the cursor once we have a monospace font
366 for (u32 i = 0; i < prompt_text.size(); ++i)
368 wchar_t ws[2] = {prompt_text[i], 0};
369 s32 x = (1 + i) * m_fontsize.X;
370 core::rect<s32> destrect(
371 x, y, x + m_fontsize.X, y + m_fontsize.Y);
375 video::SColor(255, 255, 255, 255),
378 &AbsoluteClippingRect);
381 // Draw the cursor during on periods
382 if ((m_cursor_blink & 0x8000) != 0)
384 s32 cursor_pos = prompt.getVisibleCursorPosition();
387 s32 cursor_len = prompt.getCursorLength();
388 video::IVideoDriver* driver = Environment->getVideoDriver();
389 s32 x = (1 + cursor_pos) * m_fontsize.X;
390 core::rect<s32> destrect(
392 y + m_fontsize.Y * (1.0 - m_cursor_height),
393 x + m_fontsize.X * MYMAX(cursor_len, 1),
394 y + m_fontsize.Y * (cursor_len ? m_cursor_height+1 : 1)
396 video::SColor cursor_color(255,255,255,255);
397 driver->draw2DRectangle(
400 &AbsoluteClippingRect);
406 bool GUIChatConsole::OnEvent(const SEvent& event)
409 ChatPrompt &prompt = m_chat_backend->getPrompt();
411 if(event.EventType == EET_KEY_INPUT_EVENT && event.KeyInput.PressedDown)
414 if(KeyPress(event.KeyInput) == getKeySetting("keymap_console"))
418 // inhibit open so the_game doesn't reopen immediately
419 m_open_inhibited = 50;
420 m_close_on_enter = false;
423 else if(event.KeyInput.Key == KEY_ESCAPE)
425 closeConsoleAtOnce();
426 m_close_on_enter = false;
427 // inhibit open so the_game doesn't reopen immediately
428 m_open_inhibited = 1; // so the ESCAPE button doesn't open the "pause menu"
431 else if(event.KeyInput.Key == KEY_PRIOR)
433 m_chat_backend->scrollPageUp();
436 else if(event.KeyInput.Key == KEY_NEXT)
438 m_chat_backend->scrollPageDown();
441 else if(event.KeyInput.Key == KEY_RETURN)
443 prompt.addToHistory(prompt.getLine());
444 std::wstring text = prompt.replace(L"");
445 m_client->typeChatMessage(text);
446 if (m_close_on_enter) {
447 closeConsoleAtOnce();
448 m_close_on_enter = false;
452 else if(event.KeyInput.Key == KEY_UP)
455 // Move back in history
456 prompt.historyPrev();
459 else if(event.KeyInput.Key == KEY_DOWN)
462 // Move forward in history
463 prompt.historyNext();
466 else if(event.KeyInput.Key == KEY_LEFT || event.KeyInput.Key == KEY_RIGHT)
468 // Left/right pressed
469 // Move/select character/word to the left depending on control and shift keys
470 ChatPrompt::CursorOp op = event.KeyInput.Shift ?
471 ChatPrompt::CURSOROP_SELECT :
472 ChatPrompt::CURSOROP_MOVE;
473 ChatPrompt::CursorOpDir dir = event.KeyInput.Key == KEY_LEFT ?
474 ChatPrompt::CURSOROP_DIR_LEFT :
475 ChatPrompt::CURSOROP_DIR_RIGHT;
476 ChatPrompt::CursorOpScope scope = event.KeyInput.Control ?
477 ChatPrompt::CURSOROP_SCOPE_WORD :
478 ChatPrompt::CURSOROP_SCOPE_CHARACTER;
479 prompt.cursorOperation(op, dir, scope);
482 else if(event.KeyInput.Key == KEY_HOME)
485 // move to beginning of line
486 prompt.cursorOperation(
487 ChatPrompt::CURSOROP_MOVE,
488 ChatPrompt::CURSOROP_DIR_LEFT,
489 ChatPrompt::CURSOROP_SCOPE_LINE);
492 else if(event.KeyInput.Key == KEY_END)
495 // move to end of line
496 prompt.cursorOperation(
497 ChatPrompt::CURSOROP_MOVE,
498 ChatPrompt::CURSOROP_DIR_RIGHT,
499 ChatPrompt::CURSOROP_SCOPE_LINE);
502 else if(event.KeyInput.Key == KEY_BACK)
504 // Backspace or Ctrl-Backspace pressed
505 // delete character / word to the left
506 ChatPrompt::CursorOpScope scope =
507 event.KeyInput.Control ?
508 ChatPrompt::CURSOROP_SCOPE_WORD :
509 ChatPrompt::CURSOROP_SCOPE_CHARACTER;
510 prompt.cursorOperation(
511 ChatPrompt::CURSOROP_DELETE,
512 ChatPrompt::CURSOROP_DIR_LEFT,
516 else if(event.KeyInput.Key == KEY_DELETE)
518 // Delete or Ctrl-Delete pressed
519 // delete character / word to the right
520 ChatPrompt::CursorOpScope scope =
521 event.KeyInput.Control ?
522 ChatPrompt::CURSOROP_SCOPE_WORD :
523 ChatPrompt::CURSOROP_SCOPE_CHARACTER;
524 prompt.cursorOperation(
525 ChatPrompt::CURSOROP_DELETE,
526 ChatPrompt::CURSOROP_DIR_RIGHT,
530 else if(event.KeyInput.Key == KEY_KEY_A && event.KeyInput.Control)
534 prompt.cursorOperation(
535 ChatPrompt::CURSOROP_SELECT,
536 ChatPrompt::CURSOROP_DIR_LEFT, // Ignored
537 ChatPrompt::CURSOROP_SCOPE_LINE);
540 else if(event.KeyInput.Key == KEY_KEY_C && event.KeyInput.Control)
543 // Copy text to clipboard
544 if (prompt.getCursorLength() <= 0)
546 std::wstring wselected = prompt.getSelection();
547 std::string selected(wselected.begin(), wselected.end());
548 Environment->getOSOperator()->copyToClipboard(selected.c_str());
551 else if(event.KeyInput.Key == KEY_KEY_V && event.KeyInput.Control)
554 // paste text from clipboard
555 if (prompt.getCursorLength() > 0) {
556 // Delete selected section of text
557 prompt.cursorOperation(
558 ChatPrompt::CURSOROP_DELETE,
559 ChatPrompt::CURSOROP_DIR_LEFT, // Ignored
560 ChatPrompt::CURSOROP_SCOPE_SELECTION);
562 IOSOperator *os_operator = Environment->getOSOperator();
563 const c8 *text = os_operator->getTextFromClipboard();
566 std::basic_string<unsigned char> str((const unsigned char*)text);
567 prompt.input(std::wstring(str.begin(), str.end()));
570 else if(event.KeyInput.Key == KEY_KEY_X && event.KeyInput.Control)
573 // Cut text to clipboard
574 if (prompt.getCursorLength() <= 0)
576 std::wstring wselected = prompt.getSelection();
577 std::string selected(wselected.begin(), wselected.end());
578 Environment->getOSOperator()->copyToClipboard(selected.c_str());
579 prompt.cursorOperation(
580 ChatPrompt::CURSOROP_DELETE,
581 ChatPrompt::CURSOROP_DIR_LEFT, // Ignored
582 ChatPrompt::CURSOROP_SCOPE_SELECTION);
585 else if(event.KeyInput.Key == KEY_KEY_U && event.KeyInput.Control)
588 // kill line to left end
589 prompt.cursorOperation(
590 ChatPrompt::CURSOROP_DELETE,
591 ChatPrompt::CURSOROP_DIR_LEFT,
592 ChatPrompt::CURSOROP_SCOPE_LINE);
595 else if(event.KeyInput.Key == KEY_KEY_K && event.KeyInput.Control)
598 // kill line to right end
599 prompt.cursorOperation(
600 ChatPrompt::CURSOROP_DELETE,
601 ChatPrompt::CURSOROP_DIR_RIGHT,
602 ChatPrompt::CURSOROP_SCOPE_LINE);
605 else if(event.KeyInput.Key == KEY_TAB)
607 // Tab or Shift-Tab pressed
609 std::list<std::string> names = m_client->getConnectedPlayerNames();
610 bool backwards = event.KeyInput.Shift;
611 prompt.nickCompletion(names, backwards);
613 } else if (!iswcntrl(event.KeyInput.Char) && !event.KeyInput.Control) {
614 #if defined(__linux__) && (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9)
616 mbtowc( &wc, (char *) &event.KeyInput.Char, sizeof(event.KeyInput.Char) );
619 prompt.input(event.KeyInput.Char);
624 else if(event.EventType == EET_MOUSE_INPUT_EVENT)
626 if(event.MouseInput.Event == EMIE_MOUSE_WHEEL)
628 s32 rows = myround(-3.0 * event.MouseInput.Wheel);
629 m_chat_backend->scroll(rows);
633 return Parent ? Parent->OnEvent(event) : false;
636 void GUIChatConsole::setVisible(bool visible)
639 IGUIElement::setVisible(visible);
642 recalculateConsolePosition();