guiChatConsole.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "guiChatConsole.h"
  17. #include "chat.h"
  18. #include "client/client.h"
  19. #include "debug.h"
  20. #include "gettime.h"
  21. #include "client/keycode.h"
  22. #include "settings.h"
  23. #include "porting.h"
  24. #include "client/texturesource.h"
  25. #include "client/fontengine.h"
  26. #include "log.h"
  27. #include "gettext.h"
  28. #include "irrlicht_changes/CGUITTFont.h"
  29. #include "util/string.h"
  30. #include <string>
  31. inline u32 clamp_u8(s32 value)
  32. {
  33. return (u32) MYMIN(MYMAX(value, 0), 255);
  34. }
  35. inline bool isInCtrlKeys(const irr::EKEY_CODE& kc)
  36. {
  37. return kc == KEY_LCONTROL || kc == KEY_RCONTROL || kc == KEY_CONTROL;
  38. }
  39. GUIChatConsole::GUIChatConsole(
  40. gui::IGUIEnvironment* env,
  41. gui::IGUIElement* parent,
  42. s32 id,
  43. ChatBackend* backend,
  44. Client* client,
  45. IMenuManager* menumgr
  46. ):
  47. IGUIElement(gui::EGUIET_ELEMENT, env, parent, id,
  48. core::rect<s32>(0,0,100,100)),
  49. m_chat_backend(backend),
  50. m_client(client),
  51. m_menumgr(menumgr),
  52. m_animate_time_old(porting::getTimeMs())
  53. {
  54. // load background settings
  55. s32 console_alpha = g_settings->getS32("console_alpha");
  56. m_background_color.setAlpha(clamp_u8(console_alpha));
  57. // load the background texture depending on settings
  58. ITextureSource *tsrc = client->getTextureSource();
  59. if (tsrc->isKnownSourceImage("background_chat.jpg")) {
  60. m_background = tsrc->getTexture("background_chat.jpg");
  61. m_background_color.setRed(255);
  62. m_background_color.setGreen(255);
  63. m_background_color.setBlue(255);
  64. } else {
  65. v3f console_color = g_settings->getV3F("console_color");
  66. m_background_color.setRed(clamp_u8(myround(console_color.X)));
  67. m_background_color.setGreen(clamp_u8(myround(console_color.Y)));
  68. m_background_color.setBlue(clamp_u8(myround(console_color.Z)));
  69. }
  70. const u16 chat_font_size = g_settings->getU16("chat_font_size");
  71. m_font = g_fontengine->getFont(chat_font_size != 0 ?
  72. rangelim(chat_font_size, 5, 72) : FONT_SIZE_UNSPECIFIED, FM_Mono);
  73. if (!m_font) {
  74. errorstream << "GUIChatConsole: Unable to load mono font" << std::endl;
  75. } else {
  76. core::dimension2d<u32> dim = m_font->getDimension(L"M");
  77. m_fontsize = v2u32(dim.Width, dim.Height);
  78. m_font->grab();
  79. }
  80. m_fontsize.X = MYMAX(m_fontsize.X, 1);
  81. m_fontsize.Y = MYMAX(m_fontsize.Y, 1);
  82. // set default cursor options
  83. setCursor(true, true, 2.0, 0.1);
  84. // track ctrl keys for mouse event
  85. m_is_ctrl_down = false;
  86. m_cache_clickable_chat_weblinks = g_settings->getBool("clickable_chat_weblinks");
  87. }
  88. GUIChatConsole::~GUIChatConsole()
  89. {
  90. if (m_font)
  91. m_font->drop();
  92. }
  93. void GUIChatConsole::openConsole(f32 scale)
  94. {
  95. assert(scale > 0.0f && scale <= 1.0f);
  96. m_open = true;
  97. m_desired_height_fraction = scale;
  98. m_desired_height = scale * m_screensize.Y;
  99. reformatConsole();
  100. m_animate_time_old = porting::getTimeMs();
  101. IGUIElement::setVisible(true);
  102. m_menumgr->createdMenu(this);
  103. }
  104. bool GUIChatConsole::isOpen() const
  105. {
  106. return m_open;
  107. }
  108. bool GUIChatConsole::isOpenInhibited() const
  109. {
  110. return m_open_inhibited > 0;
  111. }
  112. void GUIChatConsole::closeConsole()
  113. {
  114. m_open = false;
  115. Environment->removeFocus(this);
  116. m_menumgr->deletingMenu(this);
  117. }
  118. void GUIChatConsole::closeConsoleAtOnce()
  119. {
  120. closeConsole();
  121. m_height = 0;
  122. recalculateConsolePosition();
  123. }
  124. void GUIChatConsole::replaceAndAddToHistory(const std::wstring &line)
  125. {
  126. ChatPrompt& prompt = m_chat_backend->getPrompt();
  127. prompt.addToHistory(prompt.getLine());
  128. prompt.replace(line);
  129. }
  130. void GUIChatConsole::setCursor(
  131. bool visible, bool blinking, f32 blink_speed, f32 relative_height)
  132. {
  133. if (visible)
  134. {
  135. if (blinking)
  136. {
  137. // leave m_cursor_blink unchanged
  138. m_cursor_blink_speed = blink_speed;
  139. }
  140. else
  141. {
  142. m_cursor_blink = 0x8000; // on
  143. m_cursor_blink_speed = 0.0;
  144. }
  145. }
  146. else
  147. {
  148. m_cursor_blink = 0; // off
  149. m_cursor_blink_speed = 0.0;
  150. }
  151. m_cursor_height = relative_height;
  152. }
  153. void GUIChatConsole::draw()
  154. {
  155. if(!IsVisible)
  156. return;
  157. video::IVideoDriver* driver = Environment->getVideoDriver();
  158. // Check screen size
  159. v2u32 screensize = driver->getScreenSize();
  160. if (screensize != m_screensize)
  161. {
  162. // screen size has changed
  163. // scale current console height to new window size
  164. if (m_screensize.Y != 0)
  165. m_height = m_height * screensize.Y / m_screensize.Y;
  166. m_screensize = screensize;
  167. m_desired_height = m_desired_height_fraction * m_screensize.Y;
  168. reformatConsole();
  169. }
  170. // Animation
  171. u64 now = porting::getTimeMs();
  172. animate(now - m_animate_time_old);
  173. m_animate_time_old = now;
  174. // Draw console elements if visible
  175. if (m_height > 0)
  176. {
  177. drawBackground();
  178. drawText();
  179. drawPrompt();
  180. }
  181. gui::IGUIElement::draw();
  182. }
  183. void GUIChatConsole::reformatConsole()
  184. {
  185. s32 cols = m_screensize.X / m_fontsize.X - 2; // make room for a margin (looks better)
  186. s32 rows = m_desired_height / m_fontsize.Y - 1; // make room for the input prompt
  187. if (cols <= 0 || rows <= 0)
  188. cols = rows = 0;
  189. recalculateConsolePosition();
  190. m_chat_backend->reformat(cols, rows);
  191. }
  192. void GUIChatConsole::recalculateConsolePosition()
  193. {
  194. core::rect<s32> rect(0, 0, m_screensize.X, m_height);
  195. DesiredRect = rect;
  196. recalculateAbsolutePosition(false);
  197. }
  198. void GUIChatConsole::animate(u32 msec)
  199. {
  200. // animate the console height
  201. s32 goal = m_open ? m_desired_height : 0;
  202. // Set invisible if close animation finished (reset by openConsole)
  203. // This function (animate()) is never called once its visibility becomes false so do not
  204. // actually set visible to false before the inhibited period is over
  205. if (!m_open && m_height == 0 && m_open_inhibited == 0)
  206. IGUIElement::setVisible(false);
  207. if (m_height != goal)
  208. {
  209. s32 max_change = msec * m_screensize.Y * (m_height_speed / 1000.0);
  210. if (max_change == 0)
  211. max_change = 1;
  212. if (m_height < goal)
  213. {
  214. // increase height
  215. if (m_height + max_change < goal)
  216. m_height += max_change;
  217. else
  218. m_height = goal;
  219. }
  220. else
  221. {
  222. // decrease height
  223. if (m_height > goal + max_change)
  224. m_height -= max_change;
  225. else
  226. m_height = goal;
  227. }
  228. recalculateConsolePosition();
  229. }
  230. // blink the cursor
  231. if (m_cursor_blink_speed != 0.0)
  232. {
  233. u32 blink_increase = 0x10000 * msec * (m_cursor_blink_speed / 1000.0);
  234. if (blink_increase == 0)
  235. blink_increase = 1;
  236. m_cursor_blink = ((m_cursor_blink + blink_increase) & 0xffff);
  237. }
  238. // decrease open inhibit counter
  239. if (m_open_inhibited > msec)
  240. m_open_inhibited -= msec;
  241. else
  242. m_open_inhibited = 0;
  243. }
  244. void GUIChatConsole::drawBackground()
  245. {
  246. video::IVideoDriver* driver = Environment->getVideoDriver();
  247. if (m_background != NULL)
  248. {
  249. core::rect<s32> sourcerect(0, -m_height, m_screensize.X, 0);
  250. driver->draw2DImage(
  251. m_background,
  252. v2s32(0, 0),
  253. sourcerect,
  254. &AbsoluteClippingRect,
  255. m_background_color,
  256. false);
  257. }
  258. else
  259. {
  260. driver->draw2DRectangle(
  261. m_background_color,
  262. core::rect<s32>(0, 0, m_screensize.X, m_height),
  263. &AbsoluteClippingRect);
  264. }
  265. }
  266. void GUIChatConsole::drawText()
  267. {
  268. if (m_font == NULL)
  269. return;
  270. ChatBuffer& buf = m_chat_backend->getConsoleBuffer();
  271. for (u32 row = 0; row < buf.getRows(); ++row)
  272. {
  273. const ChatFormattedLine& line = buf.getFormattedLine(row);
  274. if (line.fragments.empty())
  275. continue;
  276. s32 line_height = m_fontsize.Y;
  277. s32 y = row * line_height + m_height - m_desired_height;
  278. if (y + line_height < 0)
  279. continue;
  280. for (const ChatFormattedFragment &fragment : line.fragments) {
  281. s32 x = (fragment.column + 1) * m_fontsize.X;
  282. core::rect<s32> destrect(
  283. x, y, x + m_fontsize.X * fragment.text.size(), y + m_fontsize.Y);
  284. if (m_font->getType() == irr::gui::EGFT_CUSTOM) {
  285. // Draw colored text if possible
  286. gui::CGUITTFont *tmp = static_cast<gui::CGUITTFont*>(m_font);
  287. tmp->draw(
  288. fragment.text,
  289. destrect,
  290. false,
  291. false,
  292. &AbsoluteClippingRect);
  293. } else {
  294. // Otherwise use standard text
  295. m_font->draw(
  296. fragment.text.c_str(),
  297. destrect,
  298. video::SColor(255, 255, 255, 255),
  299. false,
  300. false,
  301. &AbsoluteClippingRect);
  302. }
  303. }
  304. }
  305. }
  306. void GUIChatConsole::drawPrompt()
  307. {
  308. if (!m_font)
  309. return;
  310. ChatPrompt& prompt = m_chat_backend->getPrompt();
  311. std::wstring prompt_text = prompt.getVisiblePortion();
  312. u32 font_width = m_fontsize.X;
  313. u32 font_height = m_fontsize.Y;
  314. core::dimension2d<u32> size = m_font->getDimension(prompt_text.c_str());
  315. u32 text_width = size.Width;
  316. if (size.Height > font_height)
  317. font_height = size.Height;
  318. u32 row = m_chat_backend->getConsoleBuffer().getRows();
  319. s32 y = row * font_height + m_height - m_desired_height;
  320. core::rect<s32> destrect(
  321. font_width, y, font_width + text_width, y + font_height);
  322. m_font->draw(
  323. prompt_text.c_str(),
  324. destrect,
  325. video::SColor(255, 255, 255, 255),
  326. false,
  327. false,
  328. &AbsoluteClippingRect);
  329. // Draw the cursor during on periods
  330. if ((m_cursor_blink & 0x8000) != 0)
  331. {
  332. s32 cursor_pos = prompt.getVisibleCursorPosition();
  333. if (cursor_pos >= 0)
  334. {
  335. u32 text_to_cursor_pos_width = m_font->getDimension(prompt_text.substr(0, cursor_pos).c_str()).Width;
  336. s32 cursor_len = prompt.getCursorLength();
  337. video::IVideoDriver* driver = Environment->getVideoDriver();
  338. s32 x = font_width + text_to_cursor_pos_width;
  339. core::rect<s32> destrect(
  340. x,
  341. y + font_height * (1.0 - m_cursor_height),
  342. x + font_width * MYMAX(cursor_len, 1),
  343. y + font_height * (cursor_len ? m_cursor_height+1 : 1)
  344. );
  345. video::SColor cursor_color(255,255,255,255);
  346. driver->draw2DRectangle(
  347. cursor_color,
  348. destrect,
  349. &AbsoluteClippingRect);
  350. }
  351. }
  352. }
  353. bool GUIChatConsole::OnEvent(const SEvent& event)
  354. {
  355. ChatPrompt &prompt = m_chat_backend->getPrompt();
  356. if (event.EventType == EET_KEY_INPUT_EVENT && !event.KeyInput.PressedDown)
  357. {
  358. // CTRL up
  359. if (isInCtrlKeys(event.KeyInput.Key))
  360. {
  361. m_is_ctrl_down = false;
  362. }
  363. }
  364. else if(event.EventType == EET_KEY_INPUT_EVENT && event.KeyInput.PressedDown)
  365. {
  366. // CTRL down
  367. if (isInCtrlKeys(event.KeyInput.Key)) {
  368. m_is_ctrl_down = true;
  369. }
  370. // Key input
  371. if (KeyPress(event.KeyInput) == getKeySetting("keymap_console")) {
  372. closeConsole();
  373. // inhibit open so the_game doesn't reopen immediately
  374. m_open_inhibited = 50;
  375. m_close_on_enter = false;
  376. return true;
  377. }
  378. // Mac OS sends private use characters along with some keys.
  379. bool has_char = event.KeyInput.Char && !event.KeyInput.Control &&
  380. !iswcntrl(event.KeyInput.Char) && !IS_PRIVATE_USE_CHAR(event.KeyInput.Char);
  381. if (event.KeyInput.Key == KEY_ESCAPE) {
  382. closeConsoleAtOnce();
  383. m_close_on_enter = false;
  384. // inhibit open so the_game doesn't reopen immediately
  385. m_open_inhibited = 1; // so the ESCAPE button doesn't open the "pause menu"
  386. return true;
  387. }
  388. else if(event.KeyInput.Key == KEY_PRIOR)
  389. {
  390. if (!has_char) { // no num lock
  391. m_chat_backend->scrollPageUp();
  392. return true;
  393. }
  394. }
  395. else if(event.KeyInput.Key == KEY_NEXT)
  396. {
  397. if (!has_char) { // no num lock
  398. m_chat_backend->scrollPageDown();
  399. return true;
  400. }
  401. }
  402. else if(event.KeyInput.Key == KEY_RETURN)
  403. {
  404. prompt.addToHistory(prompt.getLine());
  405. std::wstring text = prompt.replace(L"");
  406. m_client->typeChatMessage(text);
  407. if (m_close_on_enter) {
  408. closeConsoleAtOnce();
  409. m_close_on_enter = false;
  410. }
  411. return true;
  412. }
  413. else if(event.KeyInput.Key == KEY_UP)
  414. {
  415. if (!has_char) { // no num lock
  416. // Up pressed
  417. // Move back in history
  418. prompt.historyPrev();
  419. return true;
  420. }
  421. }
  422. else if(event.KeyInput.Key == KEY_DOWN)
  423. {
  424. if (!has_char) { // no num lock
  425. // Down pressed
  426. // Move forward in history
  427. prompt.historyNext();
  428. return true;
  429. }
  430. }
  431. else if(event.KeyInput.Key == KEY_LEFT || event.KeyInput.Key == KEY_RIGHT)
  432. {
  433. if (!has_char) { // no num lock
  434. // Left/right pressed
  435. // Move/select character/word to the left depending on control and shift keys
  436. ChatPrompt::CursorOp op = event.KeyInput.Shift ?
  437. ChatPrompt::CURSOROP_SELECT :
  438. ChatPrompt::CURSOROP_MOVE;
  439. ChatPrompt::CursorOpDir dir = event.KeyInput.Key == KEY_LEFT ?
  440. ChatPrompt::CURSOROP_DIR_LEFT :
  441. ChatPrompt::CURSOROP_DIR_RIGHT;
  442. ChatPrompt::CursorOpScope scope = event.KeyInput.Control ?
  443. ChatPrompt::CURSOROP_SCOPE_WORD :
  444. ChatPrompt::CURSOROP_SCOPE_CHARACTER;
  445. prompt.cursorOperation(op, dir, scope);
  446. if (op == ChatPrompt::CURSOROP_SELECT)
  447. updatePrimarySelection();
  448. return true;
  449. }
  450. }
  451. else if(event.KeyInput.Key == KEY_HOME)
  452. {
  453. if (!has_char) { // no num lock
  454. // Home pressed
  455. // move to beginning of line
  456. prompt.cursorOperation(
  457. ChatPrompt::CURSOROP_MOVE,
  458. ChatPrompt::CURSOROP_DIR_LEFT,
  459. ChatPrompt::CURSOROP_SCOPE_LINE);
  460. return true;
  461. }
  462. }
  463. else if(event.KeyInput.Key == KEY_END)
  464. {
  465. if (!has_char) { // no num lock
  466. // End pressed
  467. // move to end of line
  468. prompt.cursorOperation(
  469. ChatPrompt::CURSOROP_MOVE,
  470. ChatPrompt::CURSOROP_DIR_RIGHT,
  471. ChatPrompt::CURSOROP_SCOPE_LINE);
  472. return true;
  473. }
  474. }
  475. else if(event.KeyInput.Key == KEY_BACK)
  476. {
  477. // Backspace or Ctrl-Backspace pressed
  478. // delete character / word to the left
  479. ChatPrompt::CursorOpScope scope =
  480. event.KeyInput.Control ?
  481. ChatPrompt::CURSOROP_SCOPE_WORD :
  482. ChatPrompt::CURSOROP_SCOPE_CHARACTER;
  483. prompt.cursorOperation(
  484. ChatPrompt::CURSOROP_DELETE,
  485. ChatPrompt::CURSOROP_DIR_LEFT,
  486. scope);
  487. return true;
  488. }
  489. else if(event.KeyInput.Key == KEY_DELETE)
  490. {
  491. if (!has_char) { // no num lock
  492. // Delete or Ctrl-Delete pressed
  493. // delete character / word to the right
  494. ChatPrompt::CursorOpScope scope =
  495. event.KeyInput.Control ?
  496. ChatPrompt::CURSOROP_SCOPE_WORD :
  497. ChatPrompt::CURSOROP_SCOPE_CHARACTER;
  498. prompt.cursorOperation(
  499. ChatPrompt::CURSOROP_DELETE,
  500. ChatPrompt::CURSOROP_DIR_RIGHT,
  501. scope);
  502. return true;
  503. }
  504. }
  505. else if(event.KeyInput.Key == KEY_KEY_A && event.KeyInput.Control)
  506. {
  507. // Ctrl-A pressed
  508. // Select all text
  509. prompt.cursorOperation(
  510. ChatPrompt::CURSOROP_SELECT,
  511. ChatPrompt::CURSOROP_DIR_LEFT, // Ignored
  512. ChatPrompt::CURSOROP_SCOPE_LINE);
  513. updatePrimarySelection();
  514. return true;
  515. }
  516. else if(event.KeyInput.Key == KEY_KEY_C && event.KeyInput.Control)
  517. {
  518. // Ctrl-C pressed
  519. // Copy text to clipboard
  520. if (prompt.getCursorLength() <= 0)
  521. return true;
  522. std::wstring wselected = prompt.getSelection();
  523. std::string selected = wide_to_utf8(wselected);
  524. Environment->getOSOperator()->copyToClipboard(selected.c_str());
  525. return true;
  526. }
  527. else if(event.KeyInput.Key == KEY_KEY_V && event.KeyInput.Control)
  528. {
  529. // Ctrl-V pressed
  530. // paste text from clipboard
  531. if (prompt.getCursorLength() > 0) {
  532. // Delete selected section of text
  533. prompt.cursorOperation(
  534. ChatPrompt::CURSOROP_DELETE,
  535. ChatPrompt::CURSOROP_DIR_LEFT, // Ignored
  536. ChatPrompt::CURSOROP_SCOPE_SELECTION);
  537. }
  538. IOSOperator *os_operator = Environment->getOSOperator();
  539. const c8 *text = os_operator->getTextFromClipboard();
  540. if (!text)
  541. return true;
  542. prompt.input(utf8_to_wide(text));
  543. return true;
  544. }
  545. else if(event.KeyInput.Key == KEY_KEY_X && event.KeyInput.Control)
  546. {
  547. // Ctrl-X pressed
  548. // Cut text to clipboard
  549. if (prompt.getCursorLength() <= 0)
  550. return true;
  551. std::wstring wselected = prompt.getSelection();
  552. std::string selected = wide_to_utf8(wselected);
  553. Environment->getOSOperator()->copyToClipboard(selected.c_str());
  554. prompt.cursorOperation(
  555. ChatPrompt::CURSOROP_DELETE,
  556. ChatPrompt::CURSOROP_DIR_LEFT, // Ignored
  557. ChatPrompt::CURSOROP_SCOPE_SELECTION);
  558. return true;
  559. }
  560. else if(event.KeyInput.Key == KEY_KEY_U && event.KeyInput.Control)
  561. {
  562. // Ctrl-U pressed
  563. // kill line to left end
  564. prompt.cursorOperation(
  565. ChatPrompt::CURSOROP_DELETE,
  566. ChatPrompt::CURSOROP_DIR_LEFT,
  567. ChatPrompt::CURSOROP_SCOPE_LINE);
  568. return true;
  569. }
  570. else if(event.KeyInput.Key == KEY_KEY_K && event.KeyInput.Control)
  571. {
  572. // Ctrl-K pressed
  573. // kill line to right end
  574. prompt.cursorOperation(
  575. ChatPrompt::CURSOROP_DELETE,
  576. ChatPrompt::CURSOROP_DIR_RIGHT,
  577. ChatPrompt::CURSOROP_SCOPE_LINE);
  578. return true;
  579. }
  580. else if(event.KeyInput.Key == KEY_TAB)
  581. {
  582. // Tab or Shift-Tab pressed
  583. // Nick completion
  584. auto names = m_client->getConnectedPlayerNames();
  585. bool backwards = event.KeyInput.Shift;
  586. prompt.nickCompletion(names, backwards);
  587. return true;
  588. }
  589. if (has_char) {
  590. prompt.input(event.KeyInput.Char);
  591. return true;
  592. }
  593. }
  594. else if(event.EventType == EET_MOUSE_INPUT_EVENT)
  595. {
  596. if (event.MouseInput.Event == EMIE_MOUSE_WHEEL)
  597. {
  598. s32 rows = myround(-3.0 * event.MouseInput.Wheel);
  599. m_chat_backend->scroll(rows);
  600. }
  601. // Middle click or ctrl-click opens weblink, if enabled in config
  602. // Otherwise, middle click pastes primary selection
  603. else if (event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN ||
  604. (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN && m_is_ctrl_down))
  605. {
  606. // If clicked within console output region
  607. if (event.MouseInput.Y / m_fontsize.Y < (m_height / m_fontsize.Y) - 1 )
  608. {
  609. // Translate pixel position to font position
  610. bool was_url_pressed = m_cache_clickable_chat_weblinks &&
  611. weblinkClick(event.MouseInput.X / m_fontsize.X,
  612. event.MouseInput.Y / m_fontsize.Y);
  613. if (!was_url_pressed
  614. && event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN) {
  615. // Paste primary selection at cursor pos
  616. const c8 *text = Environment->getOSOperator()
  617. ->getTextFromPrimarySelection();
  618. if (text)
  619. prompt.input(utf8_to_wide(text));
  620. }
  621. }
  622. }
  623. }
  624. else if(event.EventType == EET_STRING_INPUT_EVENT)
  625. {
  626. prompt.input(std::wstring(event.StringInput.Str->c_str()));
  627. return true;
  628. }
  629. return Parent ? Parent->OnEvent(event) : false;
  630. }
  631. void GUIChatConsole::setVisible(bool visible)
  632. {
  633. m_open = visible;
  634. IGUIElement::setVisible(visible);
  635. if (!visible) {
  636. m_height = 0;
  637. recalculateConsolePosition();
  638. }
  639. }
  640. bool GUIChatConsole::weblinkClick(s32 col, s32 row)
  641. {
  642. // Prevent accidental rapid clicking
  643. static u64 s_oldtime = 0;
  644. u64 newtime = porting::getTimeMs();
  645. // 0.6 seconds should suffice
  646. if (newtime - s_oldtime < 600)
  647. return false;
  648. s_oldtime = newtime;
  649. const std::vector<ChatFormattedFragment> &
  650. frags = m_chat_backend->getConsoleBuffer().getFormattedLine(row).fragments;
  651. std::string weblink = ""; // from frag meta
  652. // Identify targetted fragment, if exists
  653. int indx = frags.size() - 1;
  654. if (indx < 0) {
  655. // Invalid row, frags is empty
  656. return false;
  657. }
  658. // Scan from right to left, offset by 1 font space because left margin
  659. while (indx > -1 && (u32)col < frags[indx].column + 1) {
  660. --indx;
  661. }
  662. if (indx > -1) {
  663. weblink = frags[indx].weblink;
  664. // Note if(indx < 0) then a frag somehow had a corrupt column field
  665. }
  666. /*
  667. // Debug help. Please keep this in case adjustments are made later.
  668. std::string ws;
  669. ws = "Middleclick: (" + std::to_string(col) + ',' + std::to_string(row) + ')' + " frags:";
  670. // show all frags <position>(<length>) for the clicked row
  671. for (u32 i=0;i<frags.size();++i) {
  672. if (indx == int(i))
  673. // tag the actual clicked frag
  674. ws += '*';
  675. ws += std::to_string(frags.at(i).column) + '('
  676. + std::to_string(frags.at(i).text.size()) + "),";
  677. }
  678. actionstream << ws << std::endl;
  679. */
  680. // User notification
  681. if (weblink.size() != 0) {
  682. std::ostringstream msg;
  683. msg << " * ";
  684. if (porting::open_url(weblink)) {
  685. msg << gettext("Opening webpage");
  686. }
  687. else {
  688. msg << gettext("Failed to open webpage");
  689. }
  690. msg << " '" << weblink << "'";
  691. m_chat_backend->addUnparsedMessage(utf8_to_wide(msg.str()));
  692. return true;
  693. }
  694. return false;
  695. }
  696. void GUIChatConsole::updatePrimarySelection()
  697. {
  698. std::wstring wselected = m_chat_backend->getPrompt().getSelection();
  699. std::string selected = wide_to_utf8(wselected);
  700. Environment->getOSOperator()->copyToPrimarySelection(selected.c_str());
  701. }