2017-07-13 10:26:03 +02:00
|
|
|
/*
|
|
|
|
* TextControls.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
2014-07-15 10:14:49 +03:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "TextControls.h"
|
|
|
|
|
2023-05-30 16:31:45 +02:00
|
|
|
#include "Slider.h"
|
2014-07-15 10:14:49 +03:00
|
|
|
#include "Images.h"
|
|
|
|
|
2022-12-19 22:53:31 +02:00
|
|
|
#include "../CPlayerInterface.h"
|
2014-07-15 10:14:49 +03:00
|
|
|
#include "../gui/CGuiHandler.h"
|
2023-02-01 20:42:06 +02:00
|
|
|
#include "../windows/CMessage.h"
|
2023-08-26 12:01:19 +02:00
|
|
|
#include "../windows/InfoWindows.h"
|
2023-02-01 20:42:06 +02:00
|
|
|
#include "../adventureMap/CInGameConsole.h"
|
2023-02-02 21:15:13 +02:00
|
|
|
#include "../renderSDL/SDL_Extensions.h"
|
2023-06-02 15:42:18 +02:00
|
|
|
#include "../render/Canvas.h"
|
2023-07-31 18:50:55 +02:00
|
|
|
#include "../render/Graphics.h"
|
|
|
|
#include "../render/IFont.h"
|
2023-02-02 21:15:13 +02:00
|
|
|
|
2023-02-12 23:00:56 +02:00
|
|
|
#include "../../lib/TextOperations.h"
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2017-07-23 19:23:48 +02:00
|
|
|
#ifdef VCMI_ANDROID
|
|
|
|
#include "lib/CAndroidVMHelper.h"
|
|
|
|
#endif
|
|
|
|
|
2014-07-15 10:14:49 +03:00
|
|
|
std::string CLabel::visibleText()
|
|
|
|
{
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2023-06-02 15:42:18 +02:00
|
|
|
void CLabel::showAll(Canvas & to)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
CIntObject::showAll(to);
|
|
|
|
|
|
|
|
if(!visibleText().empty())
|
|
|
|
blitLine(to, pos, visibleText());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-01-08 00:42:48 +02:00
|
|
|
CLabel::CLabel(int x, int y, EFonts Font, ETextAlignment Align, const ColorRGBA & Color, const std::string & Text, int maxWidth)
|
|
|
|
: CTextContainer(Align, Font, Color), text(Text), maxWidth(maxWidth)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2023-07-03 18:24:12 +02:00
|
|
|
setRedrawParent(true);
|
2014-07-15 10:14:49 +03:00
|
|
|
autoRedraw = true;
|
|
|
|
pos.x += x;
|
|
|
|
pos.y += y;
|
|
|
|
pos.w = pos.h = 0;
|
|
|
|
|
2024-01-08 00:42:48 +02:00
|
|
|
trimText();
|
|
|
|
|
2022-11-26 23:12:20 +02:00
|
|
|
if(alignment == ETextAlignment::TOPLEFT) // causes issues for MIDDLE
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2020-10-01 10:38:06 +02:00
|
|
|
pos.w = (int)graphics->fonts[font]->getStringWidth(visibleText().c_str());
|
|
|
|
pos.h = (int)graphics->fonts[font]->getLineHeight();
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Point CLabel::getBorderSize()
|
|
|
|
{
|
|
|
|
return Point(0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CLabel::getText()
|
|
|
|
{
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2018-03-20 10:19:33 +02:00
|
|
|
void CLabel::setAutoRedraw(bool value)
|
|
|
|
{
|
|
|
|
autoRedraw = value;
|
|
|
|
}
|
|
|
|
|
2022-05-07 11:56:34 +02:00
|
|
|
void CLabel::setText(const std::string & Txt)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2024-03-21 18:00:05 +02:00
|
|
|
assert(TextOperations::isValidUnicodeString(Txt));
|
|
|
|
|
2014-07-15 10:14:49 +03:00
|
|
|
text = Txt;
|
2024-01-08 00:42:48 +02:00
|
|
|
|
|
|
|
trimText();
|
|
|
|
|
2014-07-15 10:14:49 +03:00
|
|
|
if(autoRedraw)
|
|
|
|
{
|
2018-04-07 13:34:11 +02:00
|
|
|
if(background || !parent)
|
2014-07-15 10:14:49 +03:00
|
|
|
redraw();
|
|
|
|
else
|
|
|
|
parent->redraw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-27 13:39:50 +02:00
|
|
|
void CLabel::clear()
|
|
|
|
{
|
|
|
|
text.clear();
|
|
|
|
|
|
|
|
if(autoRedraw)
|
|
|
|
{
|
|
|
|
if(background || !parent)
|
|
|
|
redraw();
|
|
|
|
else
|
|
|
|
parent->redraw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-08 01:08:05 +02:00
|
|
|
void CLabel::setMaxWidth(int width)
|
|
|
|
{
|
|
|
|
maxWidth = width;
|
|
|
|
}
|
|
|
|
|
2024-01-08 00:42:48 +02:00
|
|
|
void CLabel::trimText()
|
|
|
|
{
|
|
|
|
if(maxWidth > 0)
|
|
|
|
while ((int)graphics->fonts[font]->getStringWidth(visibleText().c_str()) > maxWidth)
|
|
|
|
TextOperations::trimRightUnicode(text);
|
|
|
|
}
|
|
|
|
|
2023-07-31 18:50:55 +02:00
|
|
|
void CLabel::setColor(const ColorRGBA & Color)
|
2018-03-20 10:19:33 +02:00
|
|
|
{
|
|
|
|
color = Color;
|
|
|
|
if(autoRedraw)
|
|
|
|
{
|
2018-04-07 13:34:11 +02:00
|
|
|
if(background || !parent)
|
2018-03-20 10:19:33 +02:00
|
|
|
redraw();
|
|
|
|
else
|
|
|
|
parent->redraw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t CLabel::getWidth()
|
|
|
|
{
|
2024-01-16 21:02:39 +02:00
|
|
|
return graphics->fonts[font]->getStringWidth(visibleText());
|
2018-03-20 10:19:33 +02:00
|
|
|
}
|
|
|
|
|
2023-07-31 18:50:55 +02:00
|
|
|
CMultiLineLabel::CMultiLineLabel(Rect position, EFonts Font, ETextAlignment Align, const ColorRGBA & Color, const std::string & Text) :
|
2016-03-12 03:41:27 +02:00
|
|
|
CLabel(position.x, position.y, Font, Align, Color, Text),
|
|
|
|
visibleSize(0, 0, position.w, position.h)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
pos.w = position.w;
|
|
|
|
pos.h = position.h;
|
2019-04-20 18:26:54 +02:00
|
|
|
splitText(Text, true);
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2019-04-20 18:26:54 +02:00
|
|
|
void CMultiLineLabel::setVisibleSize(Rect visibleSize, bool redrawElement)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
this->visibleSize = visibleSize;
|
2019-04-20 18:26:54 +02:00
|
|
|
if(redrawElement)
|
|
|
|
redraw();
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMultiLineLabel::scrollTextBy(int distance)
|
|
|
|
{
|
|
|
|
scrollTextTo(visibleSize.y + distance);
|
|
|
|
}
|
|
|
|
|
2019-04-20 18:26:54 +02:00
|
|
|
void CMultiLineLabel::scrollTextTo(int distance, bool redrawAfterScroll)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
Rect size = visibleSize;
|
|
|
|
size.y = distance;
|
2019-04-20 18:26:54 +02:00
|
|
|
setVisibleSize(size, redrawAfterScroll);
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2022-05-07 11:56:34 +02:00
|
|
|
void CMultiLineLabel::setText(const std::string & Txt)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2019-04-20 18:26:54 +02:00
|
|
|
splitText(Txt, false); //setText used below can handle redraw
|
2014-07-15 10:14:49 +03:00
|
|
|
CLabel::setText(Txt);
|
|
|
|
}
|
|
|
|
|
2023-06-02 15:42:18 +02:00
|
|
|
void CTextContainer::blitLine(Canvas & to, Rect destRect, std::string what)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2016-11-07 23:19:53 +02:00
|
|
|
const auto f = graphics->fonts[font];
|
2014-07-15 10:14:49 +03:00
|
|
|
Point where = destRect.topLeft();
|
2023-03-15 13:35:55 +02:00
|
|
|
const std::string delimeters = "{}";
|
|
|
|
auto delimitersCount = std::count_if(what.cbegin(), what.cend(), [&delimeters](char c)
|
|
|
|
{
|
|
|
|
return delimeters.find(c) != std::string::npos;
|
|
|
|
});
|
|
|
|
//We should count delimiters length from string to correct centering later.
|
|
|
|
delimitersCount *= f->getStringWidth(delimeters)/2;
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2023-09-12 15:28:09 +02:00
|
|
|
std::smatch match;
|
2023-09-13 02:04:59 +02:00
|
|
|
std::regex expr("\\{(.*?)\\|");
|
2023-09-12 15:28:09 +02:00
|
|
|
std::string::const_iterator searchStart( what.cbegin() );
|
|
|
|
while(std::regex_search(searchStart, what.cend(), match, expr))
|
|
|
|
{
|
2023-09-13 02:04:59 +02:00
|
|
|
std::string colorText = match[1].str();
|
2023-09-13 16:02:54 +02:00
|
|
|
if(auto c = Colors::parseColor(colorText))
|
2023-09-13 02:04:59 +02:00
|
|
|
delimitersCount += f->getStringWidth(colorText + "|");
|
2023-09-12 15:28:09 +02:00
|
|
|
searchStart = match.suffix().first;
|
|
|
|
}
|
|
|
|
|
2014-07-15 10:14:49 +03:00
|
|
|
// input is rect in which given text should be placed
|
|
|
|
// calculate proper position for top-left corner of the text
|
2024-03-13 23:02:45 +02:00
|
|
|
|
|
|
|
if(alignment == ETextAlignment::TOPLEFT || alignment == ETextAlignment::CENTERLEFT || alignment == ETextAlignment::BOTTOMLEFT)
|
2014-07-15 10:14:49 +03:00
|
|
|
where.x += getBorderSize().x;
|
|
|
|
|
2024-03-13 23:02:45 +02:00
|
|
|
if(alignment == ETextAlignment::CENTER || alignment == ETextAlignment::TOPCENTER || alignment == ETextAlignment::BOTTOMCENTER)
|
|
|
|
where.x += (destRect.w - (static_cast<int>(f->getStringWidth(what)) - delimitersCount)) / 2;
|
2024-03-12 19:52:42 +02:00
|
|
|
|
2024-03-13 23:02:45 +02:00
|
|
|
if(alignment == ETextAlignment::TOPRIGHT || alignment == ETextAlignment::BOTTOMRIGHT || alignment == ETextAlignment::CENTERRIGHT)
|
|
|
|
where.x += getBorderSize().x + destRect.w - (static_cast<int>(f->getStringWidth(what)) - delimitersCount);
|
|
|
|
|
|
|
|
if(alignment == ETextAlignment::TOPLEFT || alignment == ETextAlignment::TOPCENTER || alignment == ETextAlignment::TOPRIGHT)
|
2024-03-12 19:52:42 +02:00
|
|
|
where.y += getBorderSize().y;
|
2023-08-30 00:35:31 +02:00
|
|
|
|
2024-03-13 23:02:45 +02:00
|
|
|
if(alignment == ETextAlignment::CENTERLEFT || alignment == ETextAlignment::CENTER || alignment == ETextAlignment::CENTERRIGHT)
|
|
|
|
where.y += (destRect.h - static_cast<int>(f->getLineHeight())) / 2;
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2024-03-13 23:02:45 +02:00
|
|
|
if(alignment == ETextAlignment::BOTTOMLEFT || alignment == ETextAlignment::BOTTOMCENTER || alignment == ETextAlignment::BOTTOMRIGHT)
|
|
|
|
where.y += getBorderSize().y + destRect.h - static_cast<int>(f->getLineHeight());
|
2014-07-15 10:14:49 +03:00
|
|
|
|
|
|
|
size_t begin = 0;
|
|
|
|
size_t currDelimeter = 0;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
size_t end = what.find_first_of(delimeters[currDelimeter % 2], begin);
|
2022-05-07 11:56:34 +02:00
|
|
|
if(begin != end)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
std::string toPrint = what.substr(begin, end - begin);
|
|
|
|
|
2023-09-12 15:28:09 +02:00
|
|
|
if(currDelimeter % 2) // Enclosed in {} text - set to yellow or defined color
|
|
|
|
{
|
|
|
|
std::smatch match;
|
2023-09-13 02:04:59 +02:00
|
|
|
std::regex expr("^(.*?)\\|");
|
2023-09-12 15:28:09 +02:00
|
|
|
if(std::regex_search(toPrint, match, expr))
|
|
|
|
{
|
2023-09-13 02:04:59 +02:00
|
|
|
std::string colorText = match[1].str();
|
|
|
|
|
2023-09-13 16:02:54 +02:00
|
|
|
if(auto color = Colors::parseColor(colorText))
|
2023-09-13 02:04:59 +02:00
|
|
|
{
|
|
|
|
toPrint = toPrint.substr(colorText.length() + 1, toPrint.length() - colorText.length());
|
2023-09-13 16:02:54 +02:00
|
|
|
to.drawText(where, font, *color, ETextAlignment::TOPLEFT, toPrint);
|
2023-09-13 02:04:59 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
to.drawText(where, font, Colors::YELLOW, ETextAlignment::TOPLEFT, toPrint);
|
2023-09-12 15:28:09 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
to.drawText(where, font, Colors::YELLOW, ETextAlignment::TOPLEFT, toPrint);
|
|
|
|
}
|
2014-07-15 10:14:49 +03:00
|
|
|
else // Non-enclosed text, use default color
|
2023-06-02 15:42:18 +02:00
|
|
|
to.drawText(where, font, color, ETextAlignment::TOPLEFT, toPrint);
|
|
|
|
|
2014-07-15 10:14:49 +03:00
|
|
|
begin = end;
|
|
|
|
|
2020-10-01 10:38:06 +02:00
|
|
|
where.x += (int)f->getStringWidth(toPrint);
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
currDelimeter++;
|
2022-05-07 11:56:34 +02:00
|
|
|
} while(begin++ != std::string::npos);
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2023-07-31 18:50:55 +02:00
|
|
|
CTextContainer::CTextContainer(ETextAlignment alignment, EFonts font, ColorRGBA color) :
|
2014-07-15 10:14:49 +03:00
|
|
|
alignment(alignment),
|
|
|
|
font(font),
|
|
|
|
color(color)
|
2022-05-07 11:56:34 +02:00
|
|
|
{
|
|
|
|
}
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2023-06-02 15:42:18 +02:00
|
|
|
void CMultiLineLabel::showAll(Canvas & to)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
CIntObject::showAll(to);
|
|
|
|
|
2016-11-07 23:19:53 +02:00
|
|
|
const auto f = graphics->fonts[font];
|
2014-07-15 10:14:49 +03:00
|
|
|
|
|
|
|
// calculate which lines should be visible
|
2020-10-01 10:38:06 +02:00
|
|
|
int totalLines = static_cast<int>(lines.size());
|
2022-05-07 11:56:34 +02:00
|
|
|
int beginLine = visibleSize.y;
|
|
|
|
int endLine = getTextLocation().h + visibleSize.y;
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2022-05-07 11:56:34 +02:00
|
|
|
if(beginLine < 0)
|
2014-07-15 10:14:49 +03:00
|
|
|
beginLine = 0;
|
|
|
|
else
|
2020-10-01 10:38:06 +02:00
|
|
|
beginLine /= (int)f->getLineHeight();
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2022-05-07 11:56:34 +02:00
|
|
|
if(endLine < 0)
|
2014-07-15 10:14:49 +03:00
|
|
|
endLine = 0;
|
|
|
|
else
|
2020-10-01 10:38:06 +02:00
|
|
|
endLine /= (int)f->getLineHeight();
|
2014-07-15 10:14:49 +03:00
|
|
|
endLine++;
|
|
|
|
|
|
|
|
// and where they should be displayed
|
2020-10-01 10:38:06 +02:00
|
|
|
Point lineStart = getTextLocation().topLeft() - visibleSize + Point(0, beginLine * (int)f->getLineHeight());
|
2022-05-07 11:56:34 +02:00
|
|
|
Point lineSize = Point(getTextLocation().w, (int)f->getLineHeight());
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2023-06-02 15:42:18 +02:00
|
|
|
CSDL_Ext::CClipRectGuard guard(to.getInternalSurface(), getTextLocation()); // to properly trim text that is too big to fit
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2022-05-07 11:56:34 +02:00
|
|
|
for(int i = beginLine; i < std::min(totalLines, endLine); i++)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2022-05-07 11:56:34 +02:00
|
|
|
if(!lines[i].empty()) //non-empty line
|
2014-07-15 10:14:49 +03:00
|
|
|
blitLine(to, Rect(lineStart, lineSize), lines[i]);
|
|
|
|
|
2020-10-01 10:38:06 +02:00
|
|
|
lineStart.y += (int)f->getLineHeight();
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-07 11:56:34 +02:00
|
|
|
void CMultiLineLabel::splitText(const std::string & Txt, bool redrawAfter)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
lines.clear();
|
|
|
|
|
2016-11-07 23:19:53 +02:00
|
|
|
const auto f = graphics->fonts[font];
|
2022-05-07 11:56:34 +02:00
|
|
|
int lineHeight = static_cast<int>(f->getLineHeight());
|
2014-07-15 10:14:49 +03:00
|
|
|
|
|
|
|
lines = CMessage::breakText(Txt, pos.w, font);
|
|
|
|
|
2022-05-07 11:56:34 +02:00
|
|
|
textSize.y = lineHeight * (int)lines.size();
|
|
|
|
textSize.x = 0;
|
|
|
|
for(const std::string & line : lines)
|
|
|
|
vstd::amax(textSize.x, f->getStringWidth(line.c_str()));
|
2019-04-20 18:26:54 +02:00
|
|
|
if(redrawAfter)
|
|
|
|
redraw();
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Rect CMultiLineLabel::getTextLocation()
|
|
|
|
{
|
|
|
|
// this method is needed for vertical alignment alignment of text
|
|
|
|
// when height of available text is smaller than height of widget
|
|
|
|
// in this case - we should add proper offset to display text at required position
|
2022-05-07 11:56:34 +02:00
|
|
|
if(pos.h <= textSize.y)
|
2014-07-15 10:14:49 +03:00
|
|
|
return pos;
|
|
|
|
|
2020-10-01 10:38:06 +02:00
|
|
|
Point textSize(pos.w, (int)graphics->fonts[font]->getLineHeight() * (int)lines.size());
|
2014-07-15 10:14:49 +03:00
|
|
|
Point textOffset(pos.w - textSize.x, pos.h - textSize.y);
|
|
|
|
|
|
|
|
switch(alignment)
|
|
|
|
{
|
2022-11-26 23:12:20 +02:00
|
|
|
case ETextAlignment::TOPLEFT: return Rect(pos.topLeft(), textSize);
|
2023-08-30 00:35:31 +02:00
|
|
|
case ETextAlignment::TOPCENTER: return Rect(pos.topLeft(), textSize);
|
2022-11-26 23:12:20 +02:00
|
|
|
case ETextAlignment::CENTER: return Rect(pos.topLeft() + textOffset / 2, textSize);
|
|
|
|
case ETextAlignment::BOTTOMRIGHT: return Rect(pos.topLeft() + textOffset, textSize);
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
assert(0);
|
|
|
|
return Rect();
|
|
|
|
}
|
|
|
|
|
2023-07-31 18:50:55 +02:00
|
|
|
CLabelGroup::CLabelGroup(EFonts Font, ETextAlignment Align, const ColorRGBA & Color)
|
2018-04-07 13:34:11 +02:00
|
|
|
: font(Font), align(Align), color(Color)
|
|
|
|
{
|
2022-05-07 11:56:34 +02:00
|
|
|
defActions = 255 - DISPOSE;
|
2018-04-07 13:34:11 +02:00
|
|
|
}
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2022-05-07 11:56:34 +02:00
|
|
|
void CLabelGroup::add(int x, int y, const std::string & text)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2022-05-07 11:56:34 +02:00
|
|
|
OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
|
2018-04-07 13:34:11 +02:00
|
|
|
labels.push_back(std::make_shared<CLabel>(x, y, font, align, color, text));
|
2018-03-22 20:23:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t CLabelGroup::currentSize() const
|
|
|
|
{
|
|
|
|
return labels.size();
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2023-07-31 18:50:55 +02:00
|
|
|
CTextBox::CTextBox(std::string Text, const Rect & rect, int SliderStyle, EFonts Font, ETextAlignment Align, const ColorRGBA & Color) :
|
2016-03-12 03:41:27 +02:00
|
|
|
sliderStyle(SliderStyle),
|
|
|
|
slider(nullptr)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2022-05-07 11:56:34 +02:00
|
|
|
OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
|
2018-04-07 13:34:11 +02:00
|
|
|
label = std::make_shared<CMultiLineLabel>(rect, Font, Align, Color);
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2023-07-03 18:24:12 +02:00
|
|
|
setRedrawParent(true);
|
2014-07-15 10:14:49 +03:00
|
|
|
pos.x += rect.x;
|
|
|
|
pos.y += rect.y;
|
|
|
|
pos.h = rect.h;
|
|
|
|
pos.w = rect.w;
|
|
|
|
|
|
|
|
assert(pos.w >= 40); //we need some space
|
|
|
|
setText(Text);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CTextBox::sliderMoved(int to)
|
|
|
|
{
|
|
|
|
label->scrollTextTo(to);
|
|
|
|
}
|
2024-02-26 18:32:15 +02:00
|
|
|
|
|
|
|
void CTextBox::trimToFit()
|
|
|
|
{
|
|
|
|
if (slider)
|
|
|
|
return;
|
|
|
|
|
|
|
|
pos.w = label->textSize.x;
|
|
|
|
pos.h = label->textSize.y;
|
|
|
|
label->pos.w = label->textSize.x;
|
|
|
|
label->pos.h = label->textSize.y;
|
|
|
|
}
|
2014-07-15 10:14:49 +03:00
|
|
|
|
|
|
|
void CTextBox::resize(Point newSize)
|
|
|
|
{
|
|
|
|
pos.w = newSize.x;
|
|
|
|
pos.h = newSize.y;
|
|
|
|
label->pos.w = pos.w;
|
|
|
|
label->pos.h = pos.h;
|
|
|
|
|
2018-04-07 13:34:11 +02:00
|
|
|
slider.reset();
|
2014-07-15 10:14:49 +03:00
|
|
|
setText(label->getText()); // force refresh
|
|
|
|
}
|
|
|
|
|
2022-05-07 11:56:34 +02:00
|
|
|
void CTextBox::setText(const std::string & text)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2014-11-24 15:18:10 +02:00
|
|
|
label->pos.w = pos.w; // reset to default before textSize.y check
|
2014-07-15 10:14:49 +03:00
|
|
|
label->setText(text);
|
2014-11-24 15:18:10 +02:00
|
|
|
if(label->textSize.y <= label->pos.h && slider)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
// slider is no longer needed
|
2018-04-07 13:34:11 +02:00
|
|
|
slider.reset();
|
2024-05-11 22:53:39 +02:00
|
|
|
label->scrollTextTo(0);
|
2014-11-24 15:18:10 +02:00
|
|
|
}
|
|
|
|
else if(slider)
|
|
|
|
{
|
|
|
|
// decrease width again if slider still used
|
2023-12-23 18:12:55 +02:00
|
|
|
label->pos.w = pos.w - 16;
|
2023-11-26 18:55:50 +02:00
|
|
|
assert(label->pos.w > 0);
|
2014-07-15 10:14:49 +03:00
|
|
|
label->setText(text);
|
2015-03-13 17:45:07 +02:00
|
|
|
slider->setAmount(label->textSize.y);
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
2014-11-24 15:18:10 +02:00
|
|
|
else if(label->textSize.y > label->pos.h)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
// create slider and update widget
|
2023-12-23 18:12:55 +02:00
|
|
|
label->pos.w = pos.w - 16;
|
2023-11-26 18:55:50 +02:00
|
|
|
assert(label->pos.w > 0);
|
2014-07-15 10:14:49 +03:00
|
|
|
label->setText(text);
|
|
|
|
|
2022-05-07 11:56:34 +02:00
|
|
|
OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
|
2023-12-23 18:12:55 +02:00
|
|
|
slider = std::make_shared<CSlider>(Point(pos.w - 16, 0), pos.h, std::bind(&CTextBox::sliderMoved, this, _1),
|
2023-06-22 22:49:38 +02:00
|
|
|
label->pos.h, label->textSize.y, 0, Orientation::VERTICAL, CSlider::EStyle(sliderStyle));
|
2020-10-01 10:38:06 +02:00
|
|
|
slider->setScrollStep((int)graphics->fonts[label->font]->getLineHeight());
|
2023-06-22 22:49:38 +02:00
|
|
|
slider->setPanningStep(1);
|
|
|
|
slider->setScrollBounds(pos - slider->pos.topLeft());
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-12 18:23:41 +02:00
|
|
|
void CGStatusBar::setEnteringMode(bool on)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2022-12-12 18:23:41 +02:00
|
|
|
consoleText.clear();
|
|
|
|
|
|
|
|
if (on)
|
|
|
|
{
|
2023-04-06 20:49:38 +02:00
|
|
|
//assert(enteringText == false);
|
2022-12-12 18:23:41 +02:00
|
|
|
alignment = ETextAlignment::TOPLEFT;
|
2023-02-02 16:15:39 +02:00
|
|
|
GH.startTextInput(pos);
|
2022-12-12 18:23:41 +02:00
|
|
|
setText(consoleText);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-06 20:49:38 +02:00
|
|
|
//assert(enteringText == true);
|
2022-12-12 18:23:41 +02:00
|
|
|
alignment = ETextAlignment::CENTER;
|
2023-02-02 16:15:39 +02:00
|
|
|
GH.stopTextInput();
|
2022-12-12 18:23:41 +02:00
|
|
|
setText(hoverText);
|
|
|
|
}
|
|
|
|
enteringText = on;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGStatusBar::setEnteredText(const std::string & text)
|
|
|
|
{
|
|
|
|
assert(enteringText == true);
|
|
|
|
consoleText = text;
|
|
|
|
setText(text);
|
|
|
|
}
|
|
|
|
|
2022-11-18 17:54:10 +02:00
|
|
|
void CGStatusBar::write(const std::string & Text)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2022-12-12 18:23:41 +02:00
|
|
|
hoverText = Text;
|
|
|
|
|
|
|
|
if (enteringText == false)
|
|
|
|
setText(hoverText);
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2022-12-11 23:43:43 +02:00
|
|
|
void CGStatusBar::clearIfMatching(const std::string & Text)
|
2022-11-18 17:54:10 +02:00
|
|
|
{
|
2022-12-12 18:23:41 +02:00
|
|
|
if (hoverText == Text)
|
2022-11-18 17:54:10 +02:00
|
|
|
clear();
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CGStatusBar::clear()
|
|
|
|
{
|
2022-12-12 18:23:41 +02:00
|
|
|
write({});
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2023-07-31 18:50:55 +02:00
|
|
|
CGStatusBar::CGStatusBar(std::shared_ptr<CIntObject> background_, EFonts Font, ETextAlignment Align, const ColorRGBA & Color)
|
2024-01-08 00:42:48 +02:00
|
|
|
: CLabel(background_->pos.x, background_->pos.y, Font, Align, Color, "", background_->pos.w)
|
2022-12-12 18:23:41 +02:00
|
|
|
, enteringText(false)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2023-01-19 19:00:18 +02:00
|
|
|
addUsedEvents(LCLICK);
|
|
|
|
|
2018-04-07 13:34:11 +02:00
|
|
|
background = background_;
|
|
|
|
addChild(background.get());
|
|
|
|
pos = background->pos;
|
2014-07-15 10:14:49 +03:00
|
|
|
getBorderSize();
|
2019-05-19 16:19:16 +02:00
|
|
|
autoRedraw = false;
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2023-09-26 15:53:44 +02:00
|
|
|
CGStatusBar::CGStatusBar(int x, int y)
|
|
|
|
: CLabel(x, y, FONT_SMALL, ETextAlignment::CENTER)
|
|
|
|
, enteringText(false)
|
|
|
|
{
|
2023-09-26 15:56:39 +02:00
|
|
|
// without background
|
2023-09-26 15:53:44 +02:00
|
|
|
addUsedEvents(LCLICK);
|
|
|
|
}
|
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
CGStatusBar::CGStatusBar(int x, int y, const ImagePath & name, int maxw)
|
2022-11-26 23:12:20 +02:00
|
|
|
: CLabel(x, y, FONT_SMALL, ETextAlignment::CENTER)
|
2022-12-12 18:23:41 +02:00
|
|
|
, enteringText(false)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2023-01-19 19:00:18 +02:00
|
|
|
addUsedEvents(LCLICK);
|
|
|
|
|
2022-05-07 11:56:34 +02:00
|
|
|
OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
|
2023-09-25 01:04:06 +02:00
|
|
|
|
2023-04-26 14:44:10 +02:00
|
|
|
auto backgroundImage = std::make_shared<CPicture>(name);
|
|
|
|
background = backgroundImage;
|
2018-04-07 13:34:11 +02:00
|
|
|
pos = background->pos;
|
2020-10-16 05:08:59 +02:00
|
|
|
|
|
|
|
if((unsigned)maxw < (unsigned)pos.w) //(insigned)-1 > than any correct value of pos.w
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2020-10-16 05:08:59 +02:00
|
|
|
//execution of this block when maxw is incorrect breaks text centralization (issue #3151)
|
2014-07-15 10:14:49 +03:00
|
|
|
vstd::amin(pos.w, maxw);
|
2023-04-26 14:44:10 +02:00
|
|
|
backgroundImage->srcRect = Rect(0, 0, maxw, pos.h);
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
2019-05-19 16:19:16 +02:00
|
|
|
autoRedraw = false;
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2023-05-05 22:36:33 +02:00
|
|
|
CGStatusBar::~CGStatusBar()
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2023-05-16 17:34:23 +02:00
|
|
|
assert(GH.statusbar().get() != this);
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2023-06-02 15:42:18 +02:00
|
|
|
void CGStatusBar::show(Canvas & to)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2023-05-05 22:36:33 +02:00
|
|
|
showAll(to);
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2023-07-09 16:48:25 +02:00
|
|
|
void CGStatusBar::clickPressed(const Point & cursorPosition)
|
2022-11-20 11:53:46 +02:00
|
|
|
{
|
2023-07-08 13:33:04 +02:00
|
|
|
if(LOCPLINT && LOCPLINT->cingconsole->isActive())
|
|
|
|
LOCPLINT->cingconsole->startEnteringText();
|
2022-11-20 11:53:46 +02:00
|
|
|
}
|
|
|
|
|
2023-05-05 22:36:33 +02:00
|
|
|
void CGStatusBar::activate()
|
|
|
|
{
|
2023-05-16 17:34:23 +02:00
|
|
|
GH.setStatusbar(shared_from_this());
|
2023-05-13 16:59:59 +02:00
|
|
|
CIntObject::activate();
|
2023-05-05 22:36:33 +02:00
|
|
|
}
|
|
|
|
|
2022-12-19 22:06:01 +02:00
|
|
|
void CGStatusBar::deactivate()
|
2022-11-20 11:53:46 +02:00
|
|
|
{
|
2023-05-16 17:34:23 +02:00
|
|
|
assert(GH.statusbar().get() == this);
|
|
|
|
GH.setStatusbar(nullptr);
|
2023-05-05 22:36:33 +02:00
|
|
|
|
2022-12-19 22:06:01 +02:00
|
|
|
if (enteringText)
|
2022-12-19 22:53:31 +02:00
|
|
|
LOCPLINT->cingconsole->endEnteringText(false);
|
2022-12-19 22:06:01 +02:00
|
|
|
|
|
|
|
CIntObject::deactivate();
|
2022-11-20 11:53:46 +02:00
|
|
|
}
|
|
|
|
|
2014-07-15 10:14:49 +03:00
|
|
|
Point CGStatusBar::getBorderSize()
|
|
|
|
{
|
|
|
|
//Width of borders where text should not be printed
|
2022-05-07 11:56:34 +02:00
|
|
|
static const Point borderSize(5, 1);
|
2014-07-15 10:14:49 +03:00
|
|
|
|
|
|
|
switch(alignment)
|
|
|
|
{
|
2022-11-26 23:12:20 +02:00
|
|
|
case ETextAlignment::TOPLEFT: return Point(borderSize.x, borderSize.y);
|
2023-08-30 00:35:31 +02:00
|
|
|
case ETextAlignment::TOPCENTER: return Point(pos.w / 2, borderSize.y);
|
2022-11-26 23:12:20 +02:00
|
|
|
case ETextAlignment::CENTER: return Point(pos.w / 2, pos.h / 2);
|
|
|
|
case ETextAlignment::BOTTOMRIGHT: return Point(pos.w - borderSize.x, pos.h - borderSize.y);
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
assert(0);
|
|
|
|
return Point();
|
|
|
|
}
|