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"
27 #include "main.h" // for g_settings
36 #include "xCGUITTFont.h"
39 inline u32 clamp_u8(s32 value)
41 return (u32) MYMIN(MYMAX(value, 0), 255);
45 GUIChatConsole::GUIChatConsole(
46 gui::IGUIEnvironment* env,
47 gui::IGUIElement* parent,
52 IGUIElement(gui::EGUIET_ELEMENT, env, parent, id,
53 core::rect<s32>(0,0,100,100)),
54 m_chat_backend(backend),
56 m_screensize(v2u32(0,0)),
57 m_animate_time_old(0),
61 m_desired_height_fraction(0.0),
65 m_cursor_blink_speed(0.0),
68 m_background_color(255, 0, 0, 0),
72 m_animate_time_old = getTimeMs();
74 // load background settings
75 bool console_color_set = !g_settings->get("console_color").empty();
76 s32 console_alpha = g_settings->getS32("console_alpha");
78 // load the background texture depending on settings
79 m_background_color.setAlpha(clamp_u8(console_alpha));
80 if (console_color_set)
82 v3f console_color = g_settings->getV3F("console_color");
83 m_background_color.setRed(clamp_u8(myround(console_color.X)));
84 m_background_color.setGreen(clamp_u8(myround(console_color.Y)));
85 m_background_color.setBlue(clamp_u8(myround(console_color.Z)));
89 m_background = env->getVideoDriver()->getTexture(getTexturePath("background_chat.jpg").c_str());
90 m_background_color.setRed(255);
91 m_background_color.setGreen(255);
92 m_background_color.setBlue(255);
96 // FIXME should a custom texture_path be searched too?
98 std::string font_name = g_settings->get("mono_font_path");
99 u16 font_size = g_settings->getU16("mono_font_size");
100 m_font = gui::CGUITTFont::createTTFont(env, font_name.c_str(), font_size);
102 std::string font_name = "fontdejavusansmono.png";
103 m_font = env->getFont(getTexturePath(font_name).c_str());
107 dstream << "Unable to load font: " << font_name << std::endl;
111 core::dimension2d<u32> dim = m_font->getDimension(L"M");
112 m_fontsize = v2u32(dim.Width, dim.Height);
113 dstream << "Font size: " << m_fontsize.X << " " << m_fontsize.Y << std::endl;
115 m_fontsize.X = MYMAX(m_fontsize.X, 1);
116 m_fontsize.Y = MYMAX(m_fontsize.Y, 1);
118 // set default cursor options
119 setCursor(true, true, 2.0, 0.1);
122 GUIChatConsole::~GUIChatConsole()
129 void GUIChatConsole::openConsole(f32 height)
132 m_desired_height_fraction = height;
133 m_desired_height = height * m_screensize.Y;
137 bool GUIChatConsole::isOpenInhibited() const
139 return m_open_inhibited > 0;
142 void GUIChatConsole::closeConsole()
147 void GUIChatConsole::closeConsoleAtOnce()
151 recalculateConsolePosition();
154 f32 GUIChatConsole::getDesiredHeight() const
156 return m_desired_height_fraction;
159 void GUIChatConsole::setCursor(
160 bool visible, bool blinking, f32 blink_speed, f32 relative_height)
166 // leave m_cursor_blink unchanged
167 m_cursor_blink_speed = blink_speed;
171 m_cursor_blink = 0x8000; // on
172 m_cursor_blink_speed = 0.0;
177 m_cursor_blink = 0; // off
178 m_cursor_blink_speed = 0.0;
180 m_cursor_height = relative_height;
183 void GUIChatConsole::draw()
188 video::IVideoDriver* driver = Environment->getVideoDriver();
191 v2u32 screensize = driver->getScreenSize();
192 if (screensize != m_screensize)
194 // screen size has changed
195 // scale current console height to new window size
196 if (m_screensize.Y != 0)
197 m_height = m_height * screensize.Y / m_screensize.Y;
198 m_desired_height = m_desired_height_fraction * m_screensize.Y;
199 m_screensize = screensize;
204 u32 now = getTimeMs();
205 animate(now - m_animate_time_old);
206 m_animate_time_old = now;
208 // Draw console elements if visible
216 gui::IGUIElement::draw();
219 void GUIChatConsole::reformatConsole()
221 s32 cols = m_screensize.X / m_fontsize.X - 2; // make room for a margin (looks better)
222 s32 rows = m_desired_height / m_fontsize.Y - 1; // make room for the input prompt
223 if (cols <= 0 || rows <= 0)
225 m_chat_backend->reformat(cols, rows);
228 void GUIChatConsole::recalculateConsolePosition()
230 core::rect<s32> rect(0, 0, m_screensize.X, m_height);
232 recalculateAbsolutePosition(false);
235 void GUIChatConsole::animate(u32 msec)
237 // animate the console height
238 s32 goal = m_open ? m_desired_height : 0;
239 if (m_height != goal)
241 s32 max_change = msec * m_screensize.Y * (m_height_speed / 1000.0);
248 if (m_height + max_change < goal)
249 m_height += max_change;
256 if (m_height > goal + max_change)
257 m_height -= max_change;
262 recalculateConsolePosition();
266 if (m_cursor_blink_speed != 0.0)
268 u32 blink_increase = 0x10000 * msec * (m_cursor_blink_speed / 1000.0);
269 if (blink_increase == 0)
271 m_cursor_blink = ((m_cursor_blink + blink_increase) & 0xffff);
274 // decrease open inhibit counter
275 if (m_open_inhibited > msec)
276 m_open_inhibited -= msec;
278 m_open_inhibited = 0;
281 void GUIChatConsole::drawBackground()
283 video::IVideoDriver* driver = Environment->getVideoDriver();
284 if (m_background != NULL)
286 core::rect<s32> sourcerect(0, -m_height, m_screensize.X, 0);
291 &AbsoluteClippingRect,
297 driver->draw2DRectangle(
299 core::rect<s32>(0, 0, m_screensize.X, m_height),
300 &AbsoluteClippingRect);
304 void GUIChatConsole::drawText()
309 ChatBuffer& buf = m_chat_backend->getConsoleBuffer();
310 for (u32 row = 0; row < buf.getRows(); ++row)
312 const ChatFormattedLine& line = buf.getFormattedLine(row);
313 if (line.fragments.empty())
316 s32 line_height = m_fontsize.Y;
317 s32 y = row * line_height + m_height - m_desired_height;
318 if (y + line_height < 0)
321 for (u32 i = 0; i < line.fragments.size(); ++i)
323 const ChatFormattedFragment& fragment = line.fragments[i];
324 s32 x = (fragment.column + 1) * m_fontsize.X;
325 core::rect<s32> destrect(
326 x, y, x + m_fontsize.X * fragment.text.size(), y + m_fontsize.Y);
328 fragment.text.c_str(),
330 video::SColor(255, 255, 255, 255),
333 &AbsoluteClippingRect);
338 void GUIChatConsole::drawPrompt()
343 u32 row = m_chat_backend->getConsoleBuffer().getRows();
344 s32 line_height = m_fontsize.Y;
345 s32 y = row * line_height + m_height - m_desired_height;
347 ChatPrompt& prompt = m_chat_backend->getPrompt();
348 std::wstring prompt_text = prompt.getVisiblePortion();
350 // FIXME Draw string at once, not character by character
351 // That will only work with the cursor once we have a monospace font
352 for (u32 i = 0; i < prompt_text.size(); ++i)
354 wchar_t ws[2] = {prompt_text[i], 0};
355 s32 x = (1 + i) * m_fontsize.X;
356 core::rect<s32> destrect(
357 x, y, x + m_fontsize.X, y + m_fontsize.Y);
361 video::SColor(255, 255, 255, 255),
364 &AbsoluteClippingRect);
367 // Draw the cursor during on periods
368 if ((m_cursor_blink & 0x8000) != 0)
370 s32 cursor_pos = prompt.getVisibleCursorPosition();
373 video::IVideoDriver* driver = Environment->getVideoDriver();
374 s32 x = (1 + cursor_pos) * m_fontsize.X;
375 core::rect<s32> destrect(
377 y + (1.0-m_cursor_height) * m_fontsize.Y,
380 video::SColor cursor_color(255,255,255,255);
381 driver->draw2DRectangle(
384 &AbsoluteClippingRect);
390 bool GUIChatConsole::OnEvent(const SEvent& event)
392 if(event.EventType == EET_KEY_INPUT_EVENT && event.KeyInput.PressedDown)
395 if(KeyPress(event.KeyInput) == getKeySetting("keymap_console"))
398 Environment->removeFocus(this);
400 // inhibit open so the_game doesn't reopen immediately
401 m_open_inhibited = 50;
404 else if(event.KeyInput.Key == KEY_ESCAPE)
406 closeConsoleAtOnce();
407 Environment->removeFocus(this);
408 // the_game will open the pause menu
411 else if(event.KeyInput.Key == KEY_PRIOR)
413 m_chat_backend->scrollPageUp();
416 else if(event.KeyInput.Key == KEY_NEXT)
418 m_chat_backend->scrollPageDown();
421 else if(event.KeyInput.Key == KEY_RETURN)
423 std::wstring text = m_chat_backend->getPrompt().submit();
424 m_client->typeChatMessage(text);
427 else if(event.KeyInput.Key == KEY_UP)
430 // Move back in history
431 m_chat_backend->getPrompt().historyPrev();
434 else if(event.KeyInput.Key == KEY_DOWN)
437 // Move forward in history
438 m_chat_backend->getPrompt().historyNext();
441 else if(event.KeyInput.Key == KEY_LEFT)
443 // Left or Ctrl-Left pressed
444 // move character / word to the left
445 ChatPrompt::CursorOpScope scope =
446 event.KeyInput.Control ?
447 ChatPrompt::CURSOROP_SCOPE_WORD :
448 ChatPrompt::CURSOROP_SCOPE_CHARACTER;
449 m_chat_backend->getPrompt().cursorOperation(
450 ChatPrompt::CURSOROP_MOVE,
451 ChatPrompt::CURSOROP_DIR_LEFT,
455 else if(event.KeyInput.Key == KEY_RIGHT)
457 // Right or Ctrl-Right pressed
458 // move character / word to the right
459 ChatPrompt::CursorOpScope scope =
460 event.KeyInput.Control ?
461 ChatPrompt::CURSOROP_SCOPE_WORD :
462 ChatPrompt::CURSOROP_SCOPE_CHARACTER;
463 m_chat_backend->getPrompt().cursorOperation(
464 ChatPrompt::CURSOROP_MOVE,
465 ChatPrompt::CURSOROP_DIR_RIGHT,
469 else if(event.KeyInput.Key == KEY_HOME)
472 // move to beginning of line
473 m_chat_backend->getPrompt().cursorOperation(
474 ChatPrompt::CURSOROP_MOVE,
475 ChatPrompt::CURSOROP_DIR_LEFT,
476 ChatPrompt::CURSOROP_SCOPE_LINE);
479 else if(event.KeyInput.Key == KEY_END)
482 // move to end of line
483 m_chat_backend->getPrompt().cursorOperation(
484 ChatPrompt::CURSOROP_MOVE,
485 ChatPrompt::CURSOROP_DIR_RIGHT,
486 ChatPrompt::CURSOROP_SCOPE_LINE);
489 else if(event.KeyInput.Key == KEY_BACK)
491 // Backspace or Ctrl-Backspace pressed
492 // delete character / word to the left
493 ChatPrompt::CursorOpScope scope =
494 event.KeyInput.Control ?
495 ChatPrompt::CURSOROP_SCOPE_WORD :
496 ChatPrompt::CURSOROP_SCOPE_CHARACTER;
497 m_chat_backend->getPrompt().cursorOperation(
498 ChatPrompt::CURSOROP_DELETE,
499 ChatPrompt::CURSOROP_DIR_LEFT,
503 else if(event.KeyInput.Key == KEY_DELETE)
505 // Delete or Ctrl-Delete pressed
506 // delete character / word to the right
507 ChatPrompt::CursorOpScope scope =
508 event.KeyInput.Control ?
509 ChatPrompt::CURSOROP_SCOPE_WORD :
510 ChatPrompt::CURSOROP_SCOPE_CHARACTER;
511 m_chat_backend->getPrompt().cursorOperation(
512 ChatPrompt::CURSOROP_DELETE,
513 ChatPrompt::CURSOROP_DIR_RIGHT,
517 else if(event.KeyInput.Key == KEY_KEY_U && event.KeyInput.Control)
520 // kill line to left end
521 m_chat_backend->getPrompt().cursorOperation(
522 ChatPrompt::CURSOROP_DELETE,
523 ChatPrompt::CURSOROP_DIR_LEFT,
524 ChatPrompt::CURSOROP_SCOPE_LINE);
527 else if(event.KeyInput.Key == KEY_KEY_K && event.KeyInput.Control)
530 // kill line to right end
531 m_chat_backend->getPrompt().cursorOperation(
532 ChatPrompt::CURSOROP_DELETE,
533 ChatPrompt::CURSOROP_DIR_RIGHT,
534 ChatPrompt::CURSOROP_SCOPE_LINE);
537 else if(event.KeyInput.Key == KEY_TAB)
539 // Tab or Shift-Tab pressed
541 std::list<std::string> names = m_client->getConnectedPlayerNames();
542 bool backwards = event.KeyInput.Shift;
543 m_chat_backend->getPrompt().nickCompletion(names, backwards);
546 else if(event.KeyInput.Char != 0 && !event.KeyInput.Control)
548 m_chat_backend->getPrompt().input(event.KeyInput.Char);
552 else if(event.EventType == EET_MOUSE_INPUT_EVENT)
554 if(event.MouseInput.Event == EMIE_MOUSE_WHEEL)
556 s32 rows = myround(-3.0 * event.MouseInput.Wheel);
557 m_chat_backend->scroll(rows);
561 return Parent ? Parent->OnEvent(event) : false;