2011-12-03 09:01:14 +01:00
|
|
|
/*
|
2013-02-24 18:40:43 +01:00
|
|
|
Minetest
|
2013-02-24 19:38:45 +01:00
|
|
|
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
2011-12-03 09:01:14 +01:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
2012-06-05 16:56:56 +02:00
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
2011-12-03 09:01:14 +01:00
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2012-06-05 16:56:56 +02:00
|
|
|
GNU Lesser General Public License for more details.
|
2011-12-03 09:01:14 +01:00
|
|
|
|
2012-06-05 16:56:56 +02:00
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
2011-12-03 09:01:14 +01:00
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "guiChatConsole.h"
|
|
|
|
#include "chat.h"
|
2018-11-28 20:01:49 +01:00
|
|
|
#include "client/client.h"
|
2011-12-03 09:01:14 +01:00
|
|
|
#include "debug.h"
|
|
|
|
#include "gettime.h"
|
2018-11-28 20:01:49 +01:00
|
|
|
#include "client/keycode.h"
|
2011-12-03 09:01:14 +01:00
|
|
|
#include "settings.h"
|
|
|
|
#include "porting.h"
|
2015-03-05 11:52:57 +01:00
|
|
|
#include "client/tile.h"
|
2018-11-28 20:01:49 +01:00
|
|
|
#include "client/fontengine.h"
|
2015-02-01 23:59:23 +01:00
|
|
|
#include "log.h"
|
2011-12-03 09:01:14 +01:00
|
|
|
#include "gettext.h"
|
2015-02-01 23:59:23 +01:00
|
|
|
#include <string>
|
2011-12-03 09:01:14 +01:00
|
|
|
|
2013-02-03 13:19:09 +01:00
|
|
|
#if USE_FREETYPE
|
2017-06-20 17:18:34 +02:00
|
|
|
#include "irrlicht_changes/CGUITTFont.h"
|
2013-02-03 13:19:09 +01:00
|
|
|
#endif
|
|
|
|
|
2011-12-03 09:01:14 +01:00
|
|
|
inline u32 clamp_u8(s32 value)
|
|
|
|
{
|
|
|
|
return (u32) MYMIN(MYMAX(value, 0), 255);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
GUIChatConsole::GUIChatConsole(
|
|
|
|
gui::IGUIEnvironment* env,
|
|
|
|
gui::IGUIElement* parent,
|
|
|
|
s32 id,
|
|
|
|
ChatBackend* backend,
|
2016-02-27 21:51:09 +01:00
|
|
|
Client* client,
|
|
|
|
IMenuManager* menumgr
|
2011-12-03 09:01:14 +01:00
|
|
|
):
|
|
|
|
IGUIElement(gui::EGUIET_ELEMENT, env, parent, id,
|
|
|
|
core::rect<s32>(0,0,100,100)),
|
|
|
|
m_chat_backend(backend),
|
|
|
|
m_client(client),
|
2016-02-27 21:51:09 +01:00
|
|
|
m_menumgr(menumgr),
|
2017-06-16 11:25:52 +02:00
|
|
|
m_animate_time_old(porting::getTimeMs())
|
2011-12-03 09:01:14 +01:00
|
|
|
{
|
|
|
|
// load background settings
|
|
|
|
s32 console_alpha = g_settings->getS32("console_alpha");
|
2015-01-17 20:05:45 +01:00
|
|
|
m_background_color.setAlpha(clamp_u8(console_alpha));
|
2011-12-03 09:01:14 +01:00
|
|
|
|
|
|
|
// load the background texture depending on settings
|
2015-01-17 20:05:45 +01:00
|
|
|
ITextureSource *tsrc = client->getTextureSource();
|
|
|
|
if (tsrc->isKnownSourceImage("background_chat.jpg")) {
|
|
|
|
m_background = tsrc->getTexture("background_chat.jpg");
|
|
|
|
m_background_color.setRed(255);
|
|
|
|
m_background_color.setGreen(255);
|
|
|
|
m_background_color.setBlue(255);
|
|
|
|
} else {
|
2011-12-03 09:01:14 +01:00
|
|
|
v3f console_color = g_settings->getV3F("console_color");
|
|
|
|
m_background_color.setRed(clamp_u8(myround(console_color.X)));
|
|
|
|
m_background_color.setGreen(clamp_u8(myround(console_color.Y)));
|
|
|
|
m_background_color.setBlue(clamp_u8(myround(console_color.Z)));
|
|
|
|
}
|
|
|
|
|
2020-05-24 14:24:13 +02:00
|
|
|
u16 chat_font_size = g_settings->getU16("chat_font_size");
|
|
|
|
m_font = g_fontengine->getFont(chat_font_size != 0 ?
|
|
|
|
chat_font_size : FONT_SIZE_UNSPECIFIED, FM_Mono);
|
2014-11-23 13:40:43 +01:00
|
|
|
|
2017-06-17 19:11:28 +02:00
|
|
|
if (!m_font) {
|
2019-08-06 21:33:13 +02:00
|
|
|
errorstream << "GUIChatConsole: Unable to load mono font" << std::endl;
|
2017-06-17 19:11:28 +02:00
|
|
|
} else {
|
2011-12-03 09:01:14 +01:00
|
|
|
core::dimension2d<u32> dim = m_font->getDimension(L"M");
|
|
|
|
m_fontsize = v2u32(dim.Width, dim.Height);
|
2015-02-17 10:29:44 +01:00
|
|
|
m_font->grab();
|
2011-12-03 09:01:14 +01:00
|
|
|
}
|
|
|
|
m_fontsize.X = MYMAX(m_fontsize.X, 1);
|
|
|
|
m_fontsize.Y = MYMAX(m_fontsize.Y, 1);
|
|
|
|
|
|
|
|
// set default cursor options
|
|
|
|
setCursor(true, true, 2.0, 0.1);
|
|
|
|
}
|
|
|
|
|
|
|
|
GUIChatConsole::~GUIChatConsole()
|
2015-02-17 10:29:44 +01:00
|
|
|
{
|
|
|
|
if (m_font)
|
|
|
|
m_font->drop();
|
|
|
|
}
|
2011-12-03 09:01:14 +01:00
|
|
|
|
2017-01-29 10:26:00 +01:00
|
|
|
void GUIChatConsole::openConsole(f32 scale)
|
2011-12-03 09:01:14 +01:00
|
|
|
{
|
2017-01-29 10:26:00 +01:00
|
|
|
assert(scale > 0.0f && scale <= 1.0f);
|
|
|
|
|
2011-12-03 09:01:14 +01:00
|
|
|
m_open = true;
|
2017-01-29 10:26:00 +01:00
|
|
|
m_desired_height_fraction = scale;
|
|
|
|
m_desired_height = scale * m_screensize.Y;
|
2011-12-03 09:01:14 +01:00
|
|
|
reformatConsole();
|
2016-03-06 20:31:16 +01:00
|
|
|
m_animate_time_old = porting::getTimeMs();
|
2016-03-12 17:58:02 +01:00
|
|
|
IGUIElement::setVisible(true);
|
2016-02-27 21:51:09 +01:00
|
|
|
Environment->setFocus(this);
|
|
|
|
m_menumgr->createdMenu(this);
|
2011-12-03 09:01:14 +01:00
|
|
|
}
|
|
|
|
|
2013-06-05 04:26:52 +02:00
|
|
|
bool GUIChatConsole::isOpen() const
|
|
|
|
{
|
|
|
|
return m_open;
|
|
|
|
}
|
|
|
|
|
2011-12-03 09:01:14 +01:00
|
|
|
bool GUIChatConsole::isOpenInhibited() const
|
|
|
|
{
|
|
|
|
return m_open_inhibited > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GUIChatConsole::closeConsole()
|
|
|
|
{
|
|
|
|
m_open = false;
|
2016-02-27 21:51:09 +01:00
|
|
|
Environment->removeFocus(this);
|
|
|
|
m_menumgr->deletingMenu(this);
|
2011-12-03 09:01:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GUIChatConsole::closeConsoleAtOnce()
|
|
|
|
{
|
2016-02-27 21:51:09 +01:00
|
|
|
closeConsole();
|
2011-12-03 09:01:14 +01:00
|
|
|
m_height = 0;
|
|
|
|
recalculateConsolePosition();
|
|
|
|
}
|
|
|
|
|
Optimize string (mis)handling (#8128)
* Optimize statbar drawing
The texture name of the statbar is a string passed by value.
That slows down the client and creates litter in the heap
as the content of the string is allocated there. Convert the
offending parameter to a const reference to avoid the
performance hit.
* Optimize texture cache
There is an unnecessary temporary created when the texture
path is being generated. This slows down the cache each time
a new texture is encountered and it needs to be loaded into
the cache. Additionally, the heap litter created by this
unnecessary temporary is particularly troublesome here as
the following code then piles another string (the resulting
full path of the texture) on top of it, followed by the
texture itself, which both are quite long term objects as
they are subsequently inserted into the cache where they can
remain for quite a while (especially if the texture turns
out to be a common one like dirt, grass or stone).
Use std::string.append to get rid of the temporary which
solves both issues (speed and heap fragmentation).
* Optimize animations in client
Each time an animated node is updated, an unnecessary copy of
the texture name is created, littering the heap with lots of
fragments. This can be specifically troublesome when looking
at oceans or large lava lakes as both of these nodes are
usually animated (the lava animation is pretty visible).
Convert the parameter of GenericCAO::updateTextures to a
const reference to get rid of the unnecessary copy.
There is a comment stating "std::string copy is mandatory as
mod can be a class member and there is a swap on those class
members ... do NOT pass by reference", reinforcing the
belief that the unnecessary copy is in fact necessary.
However one of the first things the code of the method does
is to assign the parameter to its class member, creating
another copy. By rearranging the code a little bit this
"another copy" can then be used by the subsequent code,
getting rid of the need to pass the parameter by value and
thus saving that copying effort.
* Optimize chat console history handling
The GUIChatConsole::replaceAndAddToHistory was getting the
line to work on by value which turns out to be unnecessary.
Get rid of that unnecessary copy by converting the parameter
to a const reference.
* Optimize gui texture setting
The code used to set the texture for GUI components was
getting the name of the texture by value, creating
unnecessary performance bottleneck for mods/games with
heavily textured GUIs. Get rid of the bottleneck by passing
the texture name as a const reference.
* Optimize sound playing code in GUIEngine
The GUIEngine's code receives the specification of the sound
to be played by value, which turns out to be most likely a
mistake as the underlying sound manager interface receives
the same thing by reference. Convert the offending parameter
to a const reference to get rid of the rather bulky copying
effort and the associated performance hit.
* Silence CLANG TIDY warnings for unit tests
Change "std::string" to "const std::string &" to avoid an
unnecessary local value copy, silencing the CLANG TIDY
process.
* Optimize formspec handling
The "formspec prepend" parameter was passed to the formspec
handling code by value, creating unnecessary copy of
std::string and slowing down the game if mods add things like
textured backgrounds for the player inventory and/or other
forms. Get rid of that performance bottleneck by converting
the parameter to a const reference.
* Optimize hotbar image handling
The code that sets the background images for the hotbar is
getting the name of the image by value, creating an
unnecessary std::string copying effort. Fix that by
converting the relevant parameters to const references.
* Optimize inventory deserialization
The inventory manager deserialization code gets the
serialized version of the inventory by value, slowing the
server and the client down when there are inventory updates.
This can get particularly troublesome with pipeworks which
adds nodes that can mess around with inventories
automatically or with mods that have mobs with inventories
that actively use them.
* Optimize texture scaling cache
There is an io::path parameter passed by value in the
procedure used to add images converted from textures,
leading to slowdown when the image is not yet created and
the conversion is thus needed. The performance hit is
quite significant as io::path is similar to std::string
so convert the parameter to a const reference to get rid of
it.
* Optimize translation file loader
Use "std::string::append" when calculating the final index
for the translation table to avoid unnecessary temporary
strings. This speeds the translation file loader up
significantly as std::string uses heap allocation which
tends to be rather slow. Additionally, the heap is no
longer being littered by these unnecessary string
temporaries, increasing performance of code that gets
executed after the translation file loader finishes.
* Optimize server map saving
When the directory structure for the world data is created
during server map saving, an unnecessary value passing of
the directory name slows things down. Remove that overhead
by converting the offending parameter to a const reference.
2019-05-18 17:19:13 +02:00
|
|
|
void GUIChatConsole::replaceAndAddToHistory(const std::wstring &line)
|
2013-06-03 02:16:32 +02:00
|
|
|
{
|
|
|
|
ChatPrompt& prompt = m_chat_backend->getPrompt();
|
|
|
|
prompt.addToHistory(prompt.getLine());
|
|
|
|
prompt.replace(line);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-03 09:01:14 +01:00
|
|
|
void GUIChatConsole::setCursor(
|
|
|
|
bool visible, bool blinking, f32 blink_speed, f32 relative_height)
|
|
|
|
{
|
|
|
|
if (visible)
|
|
|
|
{
|
|
|
|
if (blinking)
|
|
|
|
{
|
|
|
|
// leave m_cursor_blink unchanged
|
|
|
|
m_cursor_blink_speed = blink_speed;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_cursor_blink = 0x8000; // on
|
|
|
|
m_cursor_blink_speed = 0.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_cursor_blink = 0; // off
|
|
|
|
m_cursor_blink_speed = 0.0;
|
|
|
|
}
|
|
|
|
m_cursor_height = relative_height;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GUIChatConsole::draw()
|
|
|
|
{
|
|
|
|
if(!IsVisible)
|
|
|
|
return;
|
|
|
|
|
|
|
|
video::IVideoDriver* driver = Environment->getVideoDriver();
|
|
|
|
|
|
|
|
// Check screen size
|
|
|
|
v2u32 screensize = driver->getScreenSize();
|
|
|
|
if (screensize != m_screensize)
|
|
|
|
{
|
|
|
|
// screen size has changed
|
|
|
|
// scale current console height to new window size
|
|
|
|
if (m_screensize.Y != 0)
|
|
|
|
m_height = m_height * screensize.Y / m_screensize.Y;
|
|
|
|
m_screensize = screensize;
|
2017-06-21 07:50:57 +02:00
|
|
|
m_desired_height = m_desired_height_fraction * m_screensize.Y;
|
2011-12-03 09:01:14 +01:00
|
|
|
reformatConsole();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Animation
|
2016-03-06 20:31:16 +01:00
|
|
|
u64 now = porting::getTimeMs();
|
2011-12-03 09:01:14 +01:00
|
|
|
animate(now - m_animate_time_old);
|
|
|
|
m_animate_time_old = now;
|
|
|
|
|
|
|
|
// Draw console elements if visible
|
|
|
|
if (m_height > 0)
|
|
|
|
{
|
|
|
|
drawBackground();
|
|
|
|
drawText();
|
|
|
|
drawPrompt();
|
|
|
|
}
|
|
|
|
|
|
|
|
gui::IGUIElement::draw();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GUIChatConsole::reformatConsole()
|
|
|
|
{
|
|
|
|
s32 cols = m_screensize.X / m_fontsize.X - 2; // make room for a margin (looks better)
|
|
|
|
s32 rows = m_desired_height / m_fontsize.Y - 1; // make room for the input prompt
|
|
|
|
if (cols <= 0 || rows <= 0)
|
|
|
|
cols = rows = 0;
|
2017-06-20 12:36:58 +02:00
|
|
|
recalculateConsolePosition();
|
2011-12-03 09:01:14 +01:00
|
|
|
m_chat_backend->reformat(cols, rows);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GUIChatConsole::recalculateConsolePosition()
|
|
|
|
{
|
|
|
|
core::rect<s32> rect(0, 0, m_screensize.X, m_height);
|
|
|
|
DesiredRect = rect;
|
|
|
|
recalculateAbsolutePosition(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GUIChatConsole::animate(u32 msec)
|
|
|
|
{
|
|
|
|
// animate the console height
|
|
|
|
s32 goal = m_open ? m_desired_height : 0;
|
2016-03-12 17:58:02 +01:00
|
|
|
|
|
|
|
// Set invisible if close animation finished (reset by openConsole)
|
2016-05-04 13:21:13 +02:00
|
|
|
// This function (animate()) is never called once its visibility becomes false so do not
|
|
|
|
// actually set visible to false before the inhibited period is over
|
|
|
|
if (!m_open && m_height == 0 && m_open_inhibited == 0)
|
2016-03-12 17:58:02 +01:00
|
|
|
IGUIElement::setVisible(false);
|
|
|
|
|
2011-12-03 09:01:14 +01:00
|
|
|
if (m_height != goal)
|
|
|
|
{
|
|
|
|
s32 max_change = msec * m_screensize.Y * (m_height_speed / 1000.0);
|
|
|
|
if (max_change == 0)
|
|
|
|
max_change = 1;
|
|
|
|
|
|
|
|
if (m_height < goal)
|
|
|
|
{
|
|
|
|
// increase height
|
|
|
|
if (m_height + max_change < goal)
|
|
|
|
m_height += max_change;
|
|
|
|
else
|
|
|
|
m_height = goal;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// decrease height
|
|
|
|
if (m_height > goal + max_change)
|
|
|
|
m_height -= max_change;
|
|
|
|
else
|
|
|
|
m_height = goal;
|
|
|
|
}
|
|
|
|
|
|
|
|
recalculateConsolePosition();
|
|
|
|
}
|
|
|
|
|
|
|
|
// blink the cursor
|
|
|
|
if (m_cursor_blink_speed != 0.0)
|
|
|
|
{
|
|
|
|
u32 blink_increase = 0x10000 * msec * (m_cursor_blink_speed / 1000.0);
|
|
|
|
if (blink_increase == 0)
|
|
|
|
blink_increase = 1;
|
|
|
|
m_cursor_blink = ((m_cursor_blink + blink_increase) & 0xffff);
|
|
|
|
}
|
|
|
|
|
|
|
|
// decrease open inhibit counter
|
|
|
|
if (m_open_inhibited > msec)
|
|
|
|
m_open_inhibited -= msec;
|
|
|
|
else
|
|
|
|
m_open_inhibited = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GUIChatConsole::drawBackground()
|
|
|
|
{
|
|
|
|
video::IVideoDriver* driver = Environment->getVideoDriver();
|
|
|
|
if (m_background != NULL)
|
|
|
|
{
|
|
|
|
core::rect<s32> sourcerect(0, -m_height, m_screensize.X, 0);
|
|
|
|
driver->draw2DImage(
|
|
|
|
m_background,
|
|
|
|
v2s32(0, 0),
|
|
|
|
sourcerect,
|
|
|
|
&AbsoluteClippingRect,
|
|
|
|
m_background_color,
|
|
|
|
false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
driver->draw2DRectangle(
|
|
|
|
m_background_color,
|
|
|
|
core::rect<s32>(0, 0, m_screensize.X, m_height),
|
|
|
|
&AbsoluteClippingRect);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GUIChatConsole::drawText()
|
|
|
|
{
|
|
|
|
if (m_font == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ChatBuffer& buf = m_chat_backend->getConsoleBuffer();
|
|
|
|
for (u32 row = 0; row < buf.getRows(); ++row)
|
|
|
|
{
|
|
|
|
const ChatFormattedLine& line = buf.getFormattedLine(row);
|
|
|
|
if (line.fragments.empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
s32 line_height = m_fontsize.Y;
|
|
|
|
s32 y = row * line_height + m_height - m_desired_height;
|
|
|
|
if (y + line_height < 0)
|
|
|
|
continue;
|
|
|
|
|
2017-08-20 19:37:29 +02:00
|
|
|
for (const ChatFormattedFragment &fragment : line.fragments) {
|
2011-12-03 09:01:14 +01:00
|
|
|
s32 x = (fragment.column + 1) * m_fontsize.X;
|
|
|
|
core::rect<s32> destrect(
|
|
|
|
x, y, x + m_fontsize.X * fragment.text.size(), y + m_fontsize.Y);
|
2015-01-16 07:54:26 +01:00
|
|
|
|
2019-08-06 21:33:13 +02:00
|
|
|
#if USE_FREETYPE
|
|
|
|
if (m_font->getType() == irr::gui::EGFT_CUSTOM) {
|
|
|
|
// Draw colored text if FreeType is enabled
|
2017-08-20 19:37:29 +02:00
|
|
|
irr::gui::CGUITTFont *tmp = dynamic_cast<irr::gui::CGUITTFont *>(m_font);
|
2015-01-16 07:54:26 +01:00
|
|
|
tmp->draw(
|
2016-05-31 17:30:11 +02:00
|
|
|
fragment.text,
|
2015-01-16 07:54:26 +01:00
|
|
|
destrect,
|
2016-05-31 17:30:11 +02:00
|
|
|
video::SColor(255, 255, 255, 255),
|
2015-01-16 07:54:26 +01:00
|
|
|
false,
|
|
|
|
false,
|
|
|
|
&AbsoluteClippingRect);
|
2019-08-06 21:33:13 +02:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
// Otherwise use standard text
|
2015-01-16 07:54:26 +01:00
|
|
|
m_font->draw(
|
|
|
|
fragment.text.c_str(),
|
|
|
|
destrect,
|
|
|
|
video::SColor(255, 255, 255, 255),
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
&AbsoluteClippingRect);
|
2019-08-06 21:33:13 +02:00
|
|
|
}
|
2011-12-03 09:01:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GUIChatConsole::drawPrompt()
|
|
|
|
{
|
2017-06-17 19:11:28 +02:00
|
|
|
if (!m_font)
|
2011-12-03 09:01:14 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
u32 row = m_chat_backend->getConsoleBuffer().getRows();
|
|
|
|
s32 line_height = m_fontsize.Y;
|
|
|
|
s32 y = row * line_height + m_height - m_desired_height;
|
|
|
|
|
|
|
|
ChatPrompt& prompt = m_chat_backend->getPrompt();
|
|
|
|
std::wstring prompt_text = prompt.getVisiblePortion();
|
|
|
|
|
|
|
|
// FIXME Draw string at once, not character by character
|
|
|
|
// That will only work with the cursor once we have a monospace font
|
|
|
|
for (u32 i = 0; i < prompt_text.size(); ++i)
|
|
|
|
{
|
|
|
|
wchar_t ws[2] = {prompt_text[i], 0};
|
|
|
|
s32 x = (1 + i) * m_fontsize.X;
|
|
|
|
core::rect<s32> destrect(
|
|
|
|
x, y, x + m_fontsize.X, y + m_fontsize.Y);
|
|
|
|
m_font->draw(
|
|
|
|
ws,
|
|
|
|
destrect,
|
|
|
|
video::SColor(255, 255, 255, 255),
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
&AbsoluteClippingRect);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw the cursor during on periods
|
|
|
|
if ((m_cursor_blink & 0x8000) != 0)
|
|
|
|
{
|
|
|
|
s32 cursor_pos = prompt.getVisibleCursorPosition();
|
|
|
|
if (cursor_pos >= 0)
|
|
|
|
{
|
2016-02-27 22:04:44 +01:00
|
|
|
s32 cursor_len = prompt.getCursorLength();
|
2011-12-03 09:01:14 +01:00
|
|
|
video::IVideoDriver* driver = Environment->getVideoDriver();
|
|
|
|
s32 x = (1 + cursor_pos) * m_fontsize.X;
|
|
|
|
core::rect<s32> destrect(
|
|
|
|
x,
|
2016-02-27 22:04:44 +01:00
|
|
|
y + m_fontsize.Y * (1.0 - m_cursor_height),
|
|
|
|
x + m_fontsize.X * MYMAX(cursor_len, 1),
|
|
|
|
y + m_fontsize.Y * (cursor_len ? m_cursor_height+1 : 1)
|
|
|
|
);
|
2011-12-03 09:01:14 +01:00
|
|
|
video::SColor cursor_color(255,255,255,255);
|
|
|
|
driver->draw2DRectangle(
|
|
|
|
cursor_color,
|
|
|
|
destrect,
|
|
|
|
&AbsoluteClippingRect);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GUIChatConsole::OnEvent(const SEvent& event)
|
|
|
|
{
|
2013-06-03 02:16:32 +02:00
|
|
|
|
|
|
|
ChatPrompt &prompt = m_chat_backend->getPrompt();
|
|
|
|
|
2011-12-03 09:01:14 +01:00
|
|
|
if(event.EventType == EET_KEY_INPUT_EVENT && event.KeyInput.PressedDown)
|
|
|
|
{
|
|
|
|
// Key input
|
2017-08-20 19:37:29 +02:00
|
|
|
if (KeyPress(event.KeyInput) == getKeySetting("keymap_console")) {
|
2011-12-03 09:01:14 +01:00
|
|
|
closeConsole();
|
|
|
|
|
|
|
|
// inhibit open so the_game doesn't reopen immediately
|
|
|
|
m_open_inhibited = 50;
|
2013-06-03 02:16:32 +02:00
|
|
|
m_close_on_enter = false;
|
2011-12-03 09:01:14 +01:00
|
|
|
return true;
|
|
|
|
}
|
2017-08-20 19:37:29 +02:00
|
|
|
|
|
|
|
if (event.KeyInput.Key == KEY_ESCAPE) {
|
2011-12-03 09:01:14 +01:00
|
|
|
closeConsoleAtOnce();
|
2013-06-03 02:16:32 +02:00
|
|
|
m_close_on_enter = false;
|
|
|
|
// inhibit open so the_game doesn't reopen immediately
|
|
|
|
m_open_inhibited = 1; // so the ESCAPE button doesn't open the "pause menu"
|
2011-12-03 09:01:14 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if(event.KeyInput.Key == KEY_PRIOR)
|
|
|
|
{
|
|
|
|
m_chat_backend->scrollPageUp();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if(event.KeyInput.Key == KEY_NEXT)
|
|
|
|
{
|
|
|
|
m_chat_backend->scrollPageDown();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if(event.KeyInput.Key == KEY_RETURN)
|
|
|
|
{
|
2013-06-03 02:16:32 +02:00
|
|
|
prompt.addToHistory(prompt.getLine());
|
|
|
|
std::wstring text = prompt.replace(L"");
|
2011-12-03 09:01:14 +01:00
|
|
|
m_client->typeChatMessage(text);
|
2013-06-03 02:16:32 +02:00
|
|
|
if (m_close_on_enter) {
|
|
|
|
closeConsoleAtOnce();
|
|
|
|
m_close_on_enter = false;
|
|
|
|
}
|
2011-12-03 09:01:14 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if(event.KeyInput.Key == KEY_UP)
|
|
|
|
{
|
|
|
|
// Up pressed
|
|
|
|
// Move back in history
|
2013-06-03 02:16:32 +02:00
|
|
|
prompt.historyPrev();
|
2011-12-03 09:01:14 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if(event.KeyInput.Key == KEY_DOWN)
|
|
|
|
{
|
|
|
|
// Down pressed
|
|
|
|
// Move forward in history
|
2013-06-03 02:16:32 +02:00
|
|
|
prompt.historyNext();
|
2011-12-03 09:01:14 +01:00
|
|
|
return true;
|
|
|
|
}
|
2016-02-27 22:04:44 +01:00
|
|
|
else if(event.KeyInput.Key == KEY_LEFT || event.KeyInput.Key == KEY_RIGHT)
|
2011-12-03 09:01:14 +01:00
|
|
|
{
|
2016-02-27 22:04:44 +01:00
|
|
|
// Left/right pressed
|
|
|
|
// Move/select character/word to the left depending on control and shift keys
|
|
|
|
ChatPrompt::CursorOp op = event.KeyInput.Shift ?
|
|
|
|
ChatPrompt::CURSOROP_SELECT :
|
|
|
|
ChatPrompt::CURSOROP_MOVE;
|
|
|
|
ChatPrompt::CursorOpDir dir = event.KeyInput.Key == KEY_LEFT ?
|
|
|
|
ChatPrompt::CURSOROP_DIR_LEFT :
|
|
|
|
ChatPrompt::CURSOROP_DIR_RIGHT;
|
|
|
|
ChatPrompt::CursorOpScope scope = event.KeyInput.Control ?
|
2011-12-03 09:01:14 +01:00
|
|
|
ChatPrompt::CURSOROP_SCOPE_WORD :
|
|
|
|
ChatPrompt::CURSOROP_SCOPE_CHARACTER;
|
2016-02-27 22:04:44 +01:00
|
|
|
prompt.cursorOperation(op, dir, scope);
|
2011-12-03 09:01:14 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if(event.KeyInput.Key == KEY_HOME)
|
|
|
|
{
|
|
|
|
// Home pressed
|
|
|
|
// move to beginning of line
|
2013-06-03 02:16:32 +02:00
|
|
|
prompt.cursorOperation(
|
2011-12-03 09:01:14 +01:00
|
|
|
ChatPrompt::CURSOROP_MOVE,
|
|
|
|
ChatPrompt::CURSOROP_DIR_LEFT,
|
|
|
|
ChatPrompt::CURSOROP_SCOPE_LINE);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if(event.KeyInput.Key == KEY_END)
|
|
|
|
{
|
|
|
|
// End pressed
|
|
|
|
// move to end of line
|
2013-06-03 02:16:32 +02:00
|
|
|
prompt.cursorOperation(
|
2011-12-03 09:01:14 +01:00
|
|
|
ChatPrompt::CURSOROP_MOVE,
|
|
|
|
ChatPrompt::CURSOROP_DIR_RIGHT,
|
|
|
|
ChatPrompt::CURSOROP_SCOPE_LINE);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if(event.KeyInput.Key == KEY_BACK)
|
|
|
|
{
|
|
|
|
// Backspace or Ctrl-Backspace pressed
|
|
|
|
// delete character / word to the left
|
|
|
|
ChatPrompt::CursorOpScope scope =
|
|
|
|
event.KeyInput.Control ?
|
|
|
|
ChatPrompt::CURSOROP_SCOPE_WORD :
|
|
|
|
ChatPrompt::CURSOROP_SCOPE_CHARACTER;
|
2013-06-03 02:16:32 +02:00
|
|
|
prompt.cursorOperation(
|
2011-12-03 09:01:14 +01:00
|
|
|
ChatPrompt::CURSOROP_DELETE,
|
|
|
|
ChatPrompt::CURSOROP_DIR_LEFT,
|
|
|
|
scope);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if(event.KeyInput.Key == KEY_DELETE)
|
|
|
|
{
|
|
|
|
// Delete or Ctrl-Delete pressed
|
|
|
|
// delete character / word to the right
|
|
|
|
ChatPrompt::CursorOpScope scope =
|
|
|
|
event.KeyInput.Control ?
|
|
|
|
ChatPrompt::CURSOROP_SCOPE_WORD :
|
|
|
|
ChatPrompt::CURSOROP_SCOPE_CHARACTER;
|
2013-06-03 02:16:32 +02:00
|
|
|
prompt.cursorOperation(
|
2011-12-03 09:01:14 +01:00
|
|
|
ChatPrompt::CURSOROP_DELETE,
|
|
|
|
ChatPrompt::CURSOROP_DIR_RIGHT,
|
|
|
|
scope);
|
|
|
|
return true;
|
|
|
|
}
|
2016-02-27 22:04:44 +01:00
|
|
|
else if(event.KeyInput.Key == KEY_KEY_A && event.KeyInput.Control)
|
|
|
|
{
|
|
|
|
// Ctrl-A pressed
|
|
|
|
// Select all text
|
|
|
|
prompt.cursorOperation(
|
|
|
|
ChatPrompt::CURSOROP_SELECT,
|
|
|
|
ChatPrompt::CURSOROP_DIR_LEFT, // Ignored
|
|
|
|
ChatPrompt::CURSOROP_SCOPE_LINE);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if(event.KeyInput.Key == KEY_KEY_C && event.KeyInput.Control)
|
|
|
|
{
|
|
|
|
// Ctrl-C pressed
|
|
|
|
// Copy text to clipboard
|
|
|
|
if (prompt.getCursorLength() <= 0)
|
|
|
|
return true;
|
2016-02-28 19:27:54 +01:00
|
|
|
std::wstring wselected = prompt.getSelection();
|
2020-08-23 15:41:04 +02:00
|
|
|
std::string selected = wide_to_utf8(wselected);
|
2016-02-27 22:04:44 +01:00
|
|
|
Environment->getOSOperator()->copyToClipboard(selected.c_str());
|
|
|
|
return true;
|
|
|
|
}
|
2014-09-19 22:26:38 +02:00
|
|
|
else if(event.KeyInput.Key == KEY_KEY_V && event.KeyInput.Control)
|
|
|
|
{
|
|
|
|
// Ctrl-V pressed
|
|
|
|
// paste text from clipboard
|
2016-02-27 22:04:44 +01:00
|
|
|
if (prompt.getCursorLength() > 0) {
|
|
|
|
// Delete selected section of text
|
|
|
|
prompt.cursorOperation(
|
|
|
|
ChatPrompt::CURSOROP_DELETE,
|
|
|
|
ChatPrompt::CURSOROP_DIR_LEFT, // Ignored
|
|
|
|
ChatPrompt::CURSOROP_SCOPE_SELECTION);
|
|
|
|
}
|
2014-09-19 22:26:38 +02:00
|
|
|
IOSOperator *os_operator = Environment->getOSOperator();
|
|
|
|
const c8 *text = os_operator->getTextFromClipboard();
|
2016-02-28 19:27:54 +01:00
|
|
|
if (!text)
|
|
|
|
return true;
|
|
|
|
std::basic_string<unsigned char> str((const unsigned char*)text);
|
|
|
|
prompt.input(std::wstring(str.begin(), str.end()));
|
2014-09-19 22:26:38 +02:00
|
|
|
return true;
|
|
|
|
}
|
2016-02-27 22:04:44 +01:00
|
|
|
else if(event.KeyInput.Key == KEY_KEY_X && event.KeyInput.Control)
|
|
|
|
{
|
|
|
|
// Ctrl-X pressed
|
|
|
|
// Cut text to clipboard
|
|
|
|
if (prompt.getCursorLength() <= 0)
|
|
|
|
return true;
|
|
|
|
std::wstring wselected = prompt.getSelection();
|
2020-08-23 15:41:04 +02:00
|
|
|
std::string selected = wide_to_utf8(wselected);
|
2016-02-27 22:04:44 +01:00
|
|
|
Environment->getOSOperator()->copyToClipboard(selected.c_str());
|
|
|
|
prompt.cursorOperation(
|
|
|
|
ChatPrompt::CURSOROP_DELETE,
|
|
|
|
ChatPrompt::CURSOROP_DIR_LEFT, // Ignored
|
|
|
|
ChatPrompt::CURSOROP_SCOPE_SELECTION);
|
|
|
|
return true;
|
|
|
|
}
|
2011-12-03 09:01:14 +01:00
|
|
|
else if(event.KeyInput.Key == KEY_KEY_U && event.KeyInput.Control)
|
|
|
|
{
|
|
|
|
// Ctrl-U pressed
|
|
|
|
// kill line to left end
|
2013-06-03 02:16:32 +02:00
|
|
|
prompt.cursorOperation(
|
2011-12-03 09:01:14 +01:00
|
|
|
ChatPrompt::CURSOROP_DELETE,
|
|
|
|
ChatPrompt::CURSOROP_DIR_LEFT,
|
|
|
|
ChatPrompt::CURSOROP_SCOPE_LINE);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if(event.KeyInput.Key == KEY_KEY_K && event.KeyInput.Control)
|
|
|
|
{
|
|
|
|
// Ctrl-K pressed
|
|
|
|
// kill line to right end
|
2013-06-03 02:16:32 +02:00
|
|
|
prompt.cursorOperation(
|
2011-12-03 09:01:14 +01:00
|
|
|
ChatPrompt::CURSOROP_DELETE,
|
|
|
|
ChatPrompt::CURSOROP_DIR_RIGHT,
|
|
|
|
ChatPrompt::CURSOROP_SCOPE_LINE);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if(event.KeyInput.Key == KEY_TAB)
|
|
|
|
{
|
|
|
|
// Tab or Shift-Tab pressed
|
|
|
|
// Nick completion
|
2013-03-25 19:13:25 +01:00
|
|
|
std::list<std::string> names = m_client->getConnectedPlayerNames();
|
2011-12-03 09:01:14 +01:00
|
|
|
bool backwards = event.KeyInput.Shift;
|
2013-06-03 02:16:32 +02:00
|
|
|
prompt.nickCompletion(names, backwards);
|
2011-12-03 09:01:14 +01:00
|
|
|
return true;
|
2017-05-20 22:29:15 +02:00
|
|
|
} else if (!iswcntrl(event.KeyInput.Char) && !event.KeyInput.Control) {
|
2016-12-24 14:26:03 +01:00
|
|
|
#if defined(__linux__) && (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9)
|
2013-06-05 03:14:31 +02:00
|
|
|
wchar_t wc = L'_';
|
|
|
|
mbtowc( &wc, (char *) &event.KeyInput.Char, sizeof(event.KeyInput.Char) );
|
2013-06-03 02:16:32 +02:00
|
|
|
prompt.input(wc);
|
2013-06-05 03:14:31 +02:00
|
|
|
#else
|
2013-06-03 02:16:32 +02:00
|
|
|
prompt.input(event.KeyInput.Char);
|
2013-06-05 03:14:31 +02:00
|
|
|
#endif
|
2011-12-03 09:01:14 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(event.EventType == EET_MOUSE_INPUT_EVENT)
|
|
|
|
{
|
|
|
|
if(event.MouseInput.Event == EMIE_MOUSE_WHEEL)
|
|
|
|
{
|
|
|
|
s32 rows = myround(-3.0 * event.MouseInput.Wheel);
|
|
|
|
m_chat_backend->scroll(rows);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Parent ? Parent->OnEvent(event) : false;
|
|
|
|
}
|
|
|
|
|
2016-03-12 17:58:02 +01:00
|
|
|
void GUIChatConsole::setVisible(bool visible)
|
|
|
|
{
|
|
|
|
m_open = visible;
|
|
|
|
IGUIElement::setVisible(visible);
|
|
|
|
if (!visible) {
|
|
|
|
m_height = 0;
|
|
|
|
recalculateConsolePosition();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|