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,
51 IGUIElement(gui::EGUIET_ELEMENT, env, parent, id,
52 core::rect<s32>(0,0,100,100)),
53 m_chat_backend(backend),
55 m_screensize(v2u32(0,0)),
56 m_animate_time_old(0),
60 m_desired_height_fraction(0.0),
64 m_cursor_blink_speed(0.0),
67 m_background_color(255, 0, 0, 0),
71 m_animate_time_old = getTimeMs();
73 // load background settings
74 s32 console_alpha = g_settings->getS32("console_alpha");
75 m_background_color.setAlpha(clamp_u8(console_alpha));
77 // load the background texture depending on settings
78 ITextureSource *tsrc = client->getTextureSource();
79 if (tsrc->isKnownSourceImage("background_chat.jpg")) {
80 m_background = tsrc->getTexture("background_chat.jpg");
81 m_background_color.setRed(255);
82 m_background_color.setGreen(255);
83 m_background_color.setBlue(255);
85 v3f console_color = g_settings->getV3F("console_color");
86 m_background_color.setRed(clamp_u8(myround(console_color.X)));
87 m_background_color.setGreen(clamp_u8(myround(console_color.Y)));
88 m_background_color.setBlue(clamp_u8(myround(console_color.Z)));
91 m_font = g_fontengine->getFont(FONT_SIZE_UNSPECIFIED, FM_Mono);
95 errorstream << "GUIChatConsole: Unable to load mono font ";
99 core::dimension2d<u32> dim = m_font->getDimension(L"M");
100 m_fontsize = v2u32(dim.Width, dim.Height);
103 m_fontsize.X = MYMAX(m_fontsize.X, 1);
104 m_fontsize.Y = MYMAX(m_fontsize.Y, 1);
106 // set default cursor options
107 setCursor(true, true, 2.0, 0.1);
110 GUIChatConsole::~GUIChatConsole()
116 void GUIChatConsole::openConsole(f32 height)
119 m_desired_height_fraction = height;
120 m_desired_height = height * m_screensize.Y;
124 bool GUIChatConsole::isOpen() const
129 bool GUIChatConsole::isOpenInhibited() const
131 return m_open_inhibited > 0;
134 void GUIChatConsole::closeConsole()
139 void GUIChatConsole::closeConsoleAtOnce()
143 recalculateConsolePosition();
146 f32 GUIChatConsole::getDesiredHeight() const
148 return m_desired_height_fraction;
151 void GUIChatConsole::setCursor(
152 bool visible, bool blinking, f32 blink_speed, f32 relative_height)
158 // leave m_cursor_blink unchanged
159 m_cursor_blink_speed = blink_speed;
163 m_cursor_blink = 0x8000; // on
164 m_cursor_blink_speed = 0.0;
169 m_cursor_blink = 0; // off
170 m_cursor_blink_speed = 0.0;
172 m_cursor_height = relative_height;
175 void GUIChatConsole::draw()
180 video::IVideoDriver* driver = Environment->getVideoDriver();
183 v2u32 screensize = driver->getScreenSize();
184 if (screensize != m_screensize)
186 // screen size has changed
187 // scale current console height to new window size
188 if (m_screensize.Y != 0)
189 m_height = m_height * screensize.Y / m_screensize.Y;
190 m_desired_height = m_desired_height_fraction * m_screensize.Y;
191 m_screensize = screensize;
196 u32 now = getTimeMs();
197 animate(now - m_animate_time_old);
198 m_animate_time_old = now;
200 // Draw console elements if visible
208 gui::IGUIElement::draw();
211 void GUIChatConsole::reformatConsole()
213 s32 cols = m_screensize.X / m_fontsize.X - 2; // make room for a margin (looks better)
214 s32 rows = m_desired_height / m_fontsize.Y - 1; // make room for the input prompt
215 if (cols <= 0 || rows <= 0)
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;
231 if (m_height != goal)
233 s32 max_change = msec * m_screensize.Y * (m_height_speed / 1000.0);
240 if (m_height + max_change < goal)
241 m_height += max_change;
248 if (m_height > goal + max_change)
249 m_height -= max_change;
254 recalculateConsolePosition();
258 if (m_cursor_blink_speed != 0.0)
260 u32 blink_increase = 0x10000 * msec * (m_cursor_blink_speed / 1000.0);
261 if (blink_increase == 0)
263 m_cursor_blink = ((m_cursor_blink + blink_increase) & 0xffff);
266 // decrease open inhibit counter
267 if (m_open_inhibited > msec)
268 m_open_inhibited -= msec;
270 m_open_inhibited = 0;
273 void GUIChatConsole::drawBackground()
275 video::IVideoDriver* driver = Environment->getVideoDriver();
276 if (m_background != NULL)
278 core::rect<s32> sourcerect(0, -m_height, m_screensize.X, 0);
283 &AbsoluteClippingRect,
289 driver->draw2DRectangle(
291 core::rect<s32>(0, 0, m_screensize.X, m_height),
292 &AbsoluteClippingRect);
296 void GUIChatConsole::drawText()
301 ChatBuffer& buf = m_chat_backend->getConsoleBuffer();
302 for (u32 row = 0; row < buf.getRows(); ++row)
304 const ChatFormattedLine& line = buf.getFormattedLine(row);
305 if (line.fragments.empty())
308 s32 line_height = m_fontsize.Y;
309 s32 y = row * line_height + m_height - m_desired_height;
310 if (y + line_height < 0)
313 for (u32 i = 0; i < line.fragments.size(); ++i)
315 const ChatFormattedFragment& fragment = line.fragments[i];
316 s32 x = (fragment.column + 1) * m_fontsize.X;
317 core::rect<s32> destrect(
318 x, y, x + m_fontsize.X * fragment.text.size(), y + m_fontsize.Y);
320 fragment.text.c_str(),
322 video::SColor(255, 255, 255, 255),
325 &AbsoluteClippingRect);
330 void GUIChatConsole::drawPrompt()
335 u32 row = m_chat_backend->getConsoleBuffer().getRows();
336 s32 line_height = m_fontsize.Y;
337 s32 y = row * line_height + m_height - m_desired_height;
339 ChatPrompt& prompt = m_chat_backend->getPrompt();
340 std::wstring prompt_text = prompt.getVisiblePortion();
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 for (u32 i = 0; i < prompt_text.size(); ++i)
346 wchar_t ws[2] = {prompt_text[i], 0};
347 s32 x = (1 + i) * m_fontsize.X;
348 core::rect<s32> destrect(
349 x, y, x + m_fontsize.X, y + m_fontsize.Y);
353 video::SColor(255, 255, 255, 255),
356 &AbsoluteClippingRect);
359 // Draw the cursor during on periods
360 if ((m_cursor_blink & 0x8000) != 0)
362 s32 cursor_pos = prompt.getVisibleCursorPosition();
365 video::IVideoDriver* driver = Environment->getVideoDriver();
366 s32 x = (1 + cursor_pos) * m_fontsize.X;
367 core::rect<s32> destrect(
369 y + (1.0-m_cursor_height) * m_fontsize.Y,
372 video::SColor cursor_color(255,255,255,255);
373 driver->draw2DRectangle(
376 &AbsoluteClippingRect);
382 bool GUIChatConsole::OnEvent(const SEvent& event)
384 if(event.EventType == EET_KEY_INPUT_EVENT && event.KeyInput.PressedDown)
387 if(KeyPress(event.KeyInput) == getKeySetting("keymap_console"))
390 Environment->removeFocus(this);
392 // inhibit open so the_game doesn't reopen immediately
393 m_open_inhibited = 50;
396 else if(event.KeyInput.Key == KEY_ESCAPE)
398 closeConsoleAtOnce();
399 Environment->removeFocus(this);
400 // the_game will open the pause menu
403 else if(event.KeyInput.Key == KEY_PRIOR)
405 m_chat_backend->scrollPageUp();
408 else if(event.KeyInput.Key == KEY_NEXT)
410 m_chat_backend->scrollPageDown();
413 else if(event.KeyInput.Key == KEY_RETURN)
415 std::wstring text = m_chat_backend->getPrompt().submit();
416 m_client->typeChatMessage(text);
419 else if(event.KeyInput.Key == KEY_UP)
422 // Move back in history
423 m_chat_backend->getPrompt().historyPrev();
426 else if(event.KeyInput.Key == KEY_DOWN)
429 // Move forward in history
430 m_chat_backend->getPrompt().historyNext();
433 else if(event.KeyInput.Key == KEY_LEFT)
435 // Left or Ctrl-Left pressed
436 // move character / word to the left
437 ChatPrompt::CursorOpScope scope =
438 event.KeyInput.Control ?
439 ChatPrompt::CURSOROP_SCOPE_WORD :
440 ChatPrompt::CURSOROP_SCOPE_CHARACTER;
441 m_chat_backend->getPrompt().cursorOperation(
442 ChatPrompt::CURSOROP_MOVE,
443 ChatPrompt::CURSOROP_DIR_LEFT,
447 else if(event.KeyInput.Key == KEY_RIGHT)
449 // Right or Ctrl-Right pressed
450 // move character / word to the right
451 ChatPrompt::CursorOpScope scope =
452 event.KeyInput.Control ?
453 ChatPrompt::CURSOROP_SCOPE_WORD :
454 ChatPrompt::CURSOROP_SCOPE_CHARACTER;
455 m_chat_backend->getPrompt().cursorOperation(
456 ChatPrompt::CURSOROP_MOVE,
457 ChatPrompt::CURSOROP_DIR_RIGHT,
461 else if(event.KeyInput.Key == KEY_HOME)
464 // move to beginning of line
465 m_chat_backend->getPrompt().cursorOperation(
466 ChatPrompt::CURSOROP_MOVE,
467 ChatPrompt::CURSOROP_DIR_LEFT,
468 ChatPrompt::CURSOROP_SCOPE_LINE);
471 else if(event.KeyInput.Key == KEY_END)
474 // move to end of line
475 m_chat_backend->getPrompt().cursorOperation(
476 ChatPrompt::CURSOROP_MOVE,
477 ChatPrompt::CURSOROP_DIR_RIGHT,
478 ChatPrompt::CURSOROP_SCOPE_LINE);
481 else if(event.KeyInput.Key == KEY_BACK)
483 // Backspace or Ctrl-Backspace pressed
484 // delete character / word to the left
485 ChatPrompt::CursorOpScope scope =
486 event.KeyInput.Control ?
487 ChatPrompt::CURSOROP_SCOPE_WORD :
488 ChatPrompt::CURSOROP_SCOPE_CHARACTER;
489 m_chat_backend->getPrompt().cursorOperation(
490 ChatPrompt::CURSOROP_DELETE,
491 ChatPrompt::CURSOROP_DIR_LEFT,
495 else if(event.KeyInput.Key == KEY_DELETE)
497 // Delete or Ctrl-Delete pressed
498 // delete character / word to the right
499 ChatPrompt::CursorOpScope scope =
500 event.KeyInput.Control ?
501 ChatPrompt::CURSOROP_SCOPE_WORD :
502 ChatPrompt::CURSOROP_SCOPE_CHARACTER;
503 m_chat_backend->getPrompt().cursorOperation(
504 ChatPrompt::CURSOROP_DELETE,
505 ChatPrompt::CURSOROP_DIR_RIGHT,
509 else if(event.KeyInput.Key == KEY_KEY_V && event.KeyInput.Control)
512 // paste text from clipboard
513 IOSOperator *os_operator = Environment->getOSOperator();
514 const c8 *text = os_operator->getTextFromClipboard();
517 std::wstring wtext = narrow_to_wide(text);
518 m_chat_backend->getPrompt().input(wtext);
522 else if(event.KeyInput.Key == KEY_KEY_U && event.KeyInput.Control)
525 // kill line to left end
526 m_chat_backend->getPrompt().cursorOperation(
527 ChatPrompt::CURSOROP_DELETE,
528 ChatPrompt::CURSOROP_DIR_LEFT,
529 ChatPrompt::CURSOROP_SCOPE_LINE);
532 else if(event.KeyInput.Key == KEY_KEY_K && event.KeyInput.Control)
535 // kill line to right end
536 m_chat_backend->getPrompt().cursorOperation(
537 ChatPrompt::CURSOROP_DELETE,
538 ChatPrompt::CURSOROP_DIR_RIGHT,
539 ChatPrompt::CURSOROP_SCOPE_LINE);
542 else if(event.KeyInput.Key == KEY_TAB)
544 // Tab or Shift-Tab pressed
546 std::list<std::string> names = m_client->getConnectedPlayerNames();
547 bool backwards = event.KeyInput.Shift;
548 m_chat_backend->getPrompt().nickCompletion(names, backwards);
551 else if(event.KeyInput.Char != 0 && !event.KeyInput.Control)
553 #if (defined(linux) || defined(__linux))
555 mbtowc( &wc, (char *) &event.KeyInput.Char, sizeof(event.KeyInput.Char) );
556 m_chat_backend->getPrompt().input(wc);
558 m_chat_backend->getPrompt().input(event.KeyInput.Char);
563 else if(event.EventType == EET_MOUSE_INPUT_EVENT)
565 if(event.MouseInput.Event == EMIE_MOUSE_WHEEL)
567 s32 rows = myround(-3.0 * event.MouseInput.Wheel);
568 m_chat_backend->scroll(rows);
572 return Parent ? Parent->OnEvent(event) : false;