This commit is contained in:
chmodsayshello 2024-12-31 20:14:50 +01:00 committed by GitHub
commit 3343e0ab4d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 63 additions and 5 deletions

View File

@ -112,7 +112,8 @@ public:
void resize(u32 scrollback);
protected:
// Get the current scroll position
s32 getScrollPosition() const { return m_scroll; }
s32 getTopScrollPos() const;
s32 getBottomScrollPos() const;

View File

@ -1800,7 +1800,9 @@ void Game::processUserInput(f32 dtime)
m_game_focused = true;
}
if (!guienv->hasFocus(gui_chat_console.get()) && gui_chat_console->isOpen()) {
if (!guienv->hasFocus(gui_chat_console.get()) && gui_chat_console->isOpen()
&& !gui_chat_console->isMyChild(guienv->getFocus()))
{
gui_chat_console->closeConsoleAtOnce();
}

View File

@ -16,6 +16,7 @@
#include "gettext.h"
#include "irrlicht_changes/CGUITTFont.h"
#include "util/string.h"
#include "guiScrollBar.h"
#include <string>
inline u32 clamp_u8(s32 value)
@ -28,6 +29,11 @@ inline bool isInCtrlKeys(const irr::EKEY_CODE& kc)
return kc == KEY_LCONTROL || kc == KEY_RCONTROL || kc == KEY_CONTROL;
}
inline u32 getScrollbarSize(IGUIEnvironment* env)
{
return env->getSkin()->getSize(gui::EGDS_SCROLLBAR_SIZE);
}
GUIChatConsole::GUIChatConsole(
gui::IGUIEnvironment* env,
gui::IGUIElement* parent,
@ -81,12 +87,20 @@ GUIChatConsole::GUIChatConsole(
// track ctrl keys for mouse event
m_is_ctrl_down = false;
m_cache_clickable_chat_weblinks = g_settings->getBool("clickable_chat_weblinks");
m_scrollbar = new GUIScrollBar(env, this, -1, core::rect<s32>(0, 0, 30, m_height), false, true, tsrc);
m_scrollbar->setSubElement(true);
m_scrollbar->setLargeStep(1);
m_scrollbar->setSmallStep(1);
}
GUIChatConsole::~GUIChatConsole()
{
if (m_font)
m_font->drop();
if (m_scrollbar)
m_scrollbar->drop();
}
void GUIChatConsole::openConsole(f32 scale)
@ -121,6 +135,7 @@ void GUIChatConsole::closeConsole()
m_open = false;
Environment->removeFocus(this);
m_menumgr->deletingMenu(this);
m_scrollbar->setVisible(false);
}
void GUIChatConsole::closeConsoleAtOnce()
@ -180,7 +195,10 @@ void GUIChatConsole::draw()
m_screensize = screensize;
m_desired_height = m_desired_height_fraction * m_screensize.Y;
reformatConsole();
}
} else if (!m_scrollbar->getAbsolutePosition().isPointInside(core::vector2di(screensize.X, m_height)))
// the height of the chat window is no longer the height of the scrollbar
// happens while opening/closing the window
updateScrollbar(true);
// Animation
u64 now = porting::getTimeMs();
@ -204,6 +222,9 @@ void GUIChatConsole::reformatConsole()
s32 rows = m_desired_height / m_fontsize.Y - 1; // make room for the input prompt
if (cols <= 0 || rows <= 0)
cols = rows = 0;
updateScrollbar(true);
recalculateConsolePosition();
m_chat_backend->reformat(cols, rows);
}
@ -313,6 +334,14 @@ void GUIChatConsole::drawText()
core::rect<s32> destrect(
x, y, x + m_fontsize.X * fragment.text.size(), y + m_fontsize.Y);
core::recti rect;
if (m_scrollbar->isVisible())
rect = core::rect<s32> (0, 0, m_screensize.X - getScrollbarSize(Environment), m_height);
else
rect = AbsoluteClippingRect;
if (m_font->getType() == irr::gui::EGFT_CUSTOM) {
// Draw colored text if possible
gui::CGUITTFont *tmp = static_cast<gui::CGUITTFont*>(m_font);
@ -321,7 +350,7 @@ void GUIChatConsole::drawText()
destrect,
false,
false,
&AbsoluteClippingRect);
&rect);
} else {
// Otherwise use standard text
m_font->draw(
@ -330,10 +359,12 @@ void GUIChatConsole::drawText()
video::SColor(255, 255, 255, 255),
false,
false,
&AbsoluteClippingRect);
&rect);
}
}
}
updateScrollbar();
}
void GUIChatConsole::drawPrompt()
@ -680,6 +711,11 @@ bool GUIChatConsole::OnEvent(const SEvent& event)
prompt.input(std::wstring(event.StringInput.Str->c_str()));
return true;
}
else if (event.EventType == EET_GUI_EVENT && event.GUIEvent.EventType == EGET_SCROLL_BAR_CHANGED &&
(void*) event.GUIEvent.Caller == (void*) m_scrollbar)
{
m_chat_backend->getConsoleBuffer().scrollAbsolute(m_scrollbar->getPos());
}
return Parent ? Parent->OnEvent(event) : false;
}
@ -692,6 +728,7 @@ void GUIChatConsole::setVisible(bool visible)
m_height = 0;
recalculateConsolePosition();
}
m_scrollbar->setVisible(visible);
}
bool GUIChatConsole::weblinkClick(s32 col, s32 row)
@ -763,3 +800,17 @@ void GUIChatConsole::updatePrimarySelection()
std::string selected = wide_to_utf8(wselected);
Environment->getOSOperator()->copyToPrimarySelection(selected.c_str());
}
void GUIChatConsole::updateScrollbar(bool update_size)
{
m_scrollbar->setMin(m_chat_backend->getConsoleBuffer().getTopScrollPos());
m_scrollbar->setMax(m_chat_backend->getConsoleBuffer().getBottomScrollPos());
m_scrollbar->setPos(m_chat_backend->getConsoleBuffer().getScrollPosition());
m_scrollbar->setVisible(m_scrollbar->getMin() != m_scrollbar->getMax());
m_scrollbar->setPageSize(m_fontsize.Y * m_chat_backend->getConsoleBuffer().getLineCount());
if (update_size) {
const core::rect<s32> rect (m_screensize.X - getScrollbarSize(Environment), 0, m_screensize.X, m_height);
m_scrollbar->setRelativePosition(rect);
}
}

View File

@ -10,6 +10,7 @@
#include "config.h"
class Client;
class GUIScrollBar;
class GUIChatConsole : public gui::IGUIElement
{
@ -76,10 +77,13 @@ private:
// If the selected text changed, we need to update the (X11) primary selection.
void updatePrimarySelection();
void updateScrollbar(bool update_size = false);
private:
ChatBackend* m_chat_backend;
Client* m_client;
IMenuManager* m_menumgr;
GUIScrollBar* m_scrollbar = nullptr;
// current screen size
v2u32 m_screensize;