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-04-27 19:21:06 +02:00
|
|
|
#include "../gui/Shortcut.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
|
|
|
|
|
2023-02-01 20:42:06 +02:00
|
|
|
std::list<CFocusable*> CFocusable::focusables;
|
|
|
|
CFocusable * CFocusable::inputWithFocus;
|
|
|
|
|
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());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-07-31 18:50:55 +02:00
|
|
|
CLabel::CLabel(int x, int y, EFonts Font, ETextAlignment Align, const ColorRGBA & Color, const std::string & Text)
|
2018-04-07 13:34:11 +02:00
|
|
|
: CTextContainer(Align, Font, Color), text(Text)
|
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;
|
|
|
|
|
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
|
|
|
{
|
|
|
|
text = Txt;
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
return graphics->fonts[font]->getStringWidth(visibleText());;
|
|
|
|
}
|
|
|
|
|
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
|
2022-11-26 23:12:20 +02:00
|
|
|
if(alignment == ETextAlignment::TOPLEFT)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
where.x += getBorderSize().x;
|
|
|
|
where.y += getBorderSize().y;
|
|
|
|
}
|
|
|
|
|
2023-08-30 00:35:31 +02:00
|
|
|
if(alignment == ETextAlignment::TOPCENTER)
|
|
|
|
{
|
|
|
|
where.x += (int(destRect.w) - int(f->getStringWidth(what) - delimitersCount)) / 2;
|
|
|
|
where.y += getBorderSize().y;
|
|
|
|
}
|
|
|
|
|
2022-11-26 23:12:20 +02:00
|
|
|
if(alignment == ETextAlignment::CENTER)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2023-03-15 13:35:55 +02:00
|
|
|
where.x += (int(destRect.w) - int(f->getStringWidth(what) - delimitersCount)) / 2;
|
2014-07-15 10:14:49 +03:00
|
|
|
where.y += (int(destRect.h) - int(f->getLineHeight())) / 2;
|
|
|
|
}
|
|
|
|
|
2022-11-26 23:12:20 +02:00
|
|
|
if(alignment == ETextAlignment::BOTTOMRIGHT)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2023-03-15 13:35:55 +02:00
|
|
|
where.x += getBorderSize().x + destRect.w - ((int)f->getStringWidth(what) - delimitersCount);
|
2020-10-01 10:38:06 +02:00
|
|
|
where.y += getBorderSize().y + destRect.h - (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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
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)
|
2018-04-07 13:34:11 +02:00
|
|
|
: CLabel(background_->pos.x, background_->pos.y, Font, Align, Color, "")
|
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();
|
|
|
|
}
|
|
|
|
|
2023-10-13 23:04:35 +02:00
|
|
|
CTextInput::CTextInput(const Rect & Pos, EFonts font, const CFunctionList<void(const std::string &)> & CB, bool giveFocusToInput)
|
2022-11-26 23:12:20 +02:00
|
|
|
: CLabel(Pos.x, Pos.y, font, ETextAlignment::CENTER),
|
2022-05-07 11:56:34 +02:00
|
|
|
cb(CB),
|
|
|
|
CFocusable(std::make_shared<CKeyboardFocusListener>(this))
|
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.h = Pos.h;
|
|
|
|
pos.w = Pos.w;
|
2018-04-07 13:34:11 +02:00
|
|
|
background.reset();
|
2023-08-26 12:01:19 +02:00
|
|
|
addUsedEvents(LCLICK | SHOW_POPUP | KEYBOARD | TEXTINPUT);
|
2022-05-07 11:56:34 +02:00
|
|
|
|
2023-02-27 12:00:13 +02:00
|
|
|
#if !defined(VCMI_MOBILE)
|
2023-10-13 23:04:35 +02:00
|
|
|
if(giveFocusToInput)
|
|
|
|
giveFocus();
|
2022-05-07 11:56:34 +02:00
|
|
|
#endif
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
CTextInput::CTextInput(const Rect & Pos, const Point & bgOffset, const ImagePath & bgName, const CFunctionList<void(const std::string &)> & CB)
|
2022-05-07 11:56:34 +02:00
|
|
|
:cb(CB), CFocusable(std::make_shared<CKeyboardFocusListener>(this))
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2022-12-15 23:24:03 +02:00
|
|
|
pos += Pos.topLeft();
|
2022-05-07 11:56:34 +02:00
|
|
|
pos.h = Pos.h;
|
|
|
|
pos.w = Pos.w;
|
|
|
|
|
2014-07-15 10:14:49 +03:00
|
|
|
OBJ_CONSTRUCTION;
|
2018-04-07 13:34:11 +02:00
|
|
|
background = std::make_shared<CPicture>(bgName, bgOffset.x, bgOffset.y);
|
2023-08-26 12:01:19 +02:00
|
|
|
addUsedEvents(LCLICK | SHOW_POPUP | KEYBOARD | TEXTINPUT);
|
2022-05-07 11:56:34 +02:00
|
|
|
|
2023-02-27 12:00:13 +02:00
|
|
|
#if !defined(VCMI_MOBILE)
|
2014-07-15 10:14:49 +03:00
|
|
|
giveFocus();
|
2022-05-07 11:56:34 +02:00
|
|
|
#endif
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2023-01-30 17:18:59 +02:00
|
|
|
CTextInput::CTextInput(const Rect & Pos, std::shared_ptr<IImage> srf)
|
2022-05-07 11:56:34 +02:00
|
|
|
:CFocusable(std::make_shared<CKeyboardFocusListener>(this))
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2022-12-15 23:24:03 +02:00
|
|
|
pos += Pos.topLeft();
|
2014-07-15 10:14:49 +03:00
|
|
|
OBJ_CONSTRUCTION;
|
2023-01-30 13:58:13 +02:00
|
|
|
background = std::make_shared<CPicture>(srf, Pos);
|
2018-04-07 13:34:11 +02:00
|
|
|
pos.w = background->pos.w;
|
|
|
|
pos.h = background->pos.h;
|
|
|
|
background->pos = pos;
|
2014-07-15 10:14:49 +03:00
|
|
|
addUsedEvents(LCLICK | KEYBOARD | TEXTINPUT);
|
2022-05-07 11:56:34 +02:00
|
|
|
|
2023-02-27 12:00:13 +02:00
|
|
|
#if !defined(VCMI_MOBILE)
|
2014-07-15 10:14:49 +03:00
|
|
|
giveFocus();
|
2022-05-07 11:56:34 +02:00
|
|
|
#endif
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2022-05-07 11:56:34 +02:00
|
|
|
std::atomic<int> CKeyboardFocusListener::usageIndex(0);
|
|
|
|
|
|
|
|
CKeyboardFocusListener::CKeyboardFocusListener(CTextInput * textInput)
|
|
|
|
:textInput(textInput)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void CKeyboardFocusListener::focusGot()
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2023-02-02 16:15:39 +02:00
|
|
|
GH.startTextInput(textInput->pos);
|
2022-05-07 11:56:34 +02:00
|
|
|
usageIndex++;
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2022-05-07 11:56:34 +02:00
|
|
|
void CKeyboardFocusListener::focusLost()
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2022-05-07 11:56:34 +02:00
|
|
|
if(0 == --usageIndex)
|
|
|
|
{
|
2023-02-02 16:15:39 +02:00
|
|
|
GH.stopTextInput();
|
2022-05-07 11:56:34 +02:00
|
|
|
}
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string CTextInput::visibleText()
|
|
|
|
{
|
|
|
|
return focus ? text + newText + "_" : text;
|
|
|
|
}
|
|
|
|
|
2023-07-08 13:33:04 +02:00
|
|
|
void CTextInput::clickPressed(const Point & cursorPosition)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2023-07-08 13:33:04 +02:00
|
|
|
if(!focus)
|
2014-07-15 10:14:49 +03:00
|
|
|
giveFocus();
|
|
|
|
}
|
|
|
|
|
2023-04-27 19:21:06 +02:00
|
|
|
void CTextInput::keyPressed(EShortcut key)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2023-02-02 18:02:25 +02:00
|
|
|
if(!focus)
|
2014-07-15 10:14:49 +03:00
|
|
|
return;
|
|
|
|
|
2023-04-27 19:21:06 +02:00
|
|
|
if(key == EShortcut::GLOBAL_MOVE_FOCUS)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
moveFocus();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool redrawNeeded = false;
|
2016-11-07 23:19:53 +02:00
|
|
|
|
2023-02-02 18:02:25 +02:00
|
|
|
switch(key)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2023-04-27 19:21:06 +02:00
|
|
|
case EShortcut::GLOBAL_BACKSPACE:
|
2014-07-15 10:14:49 +03:00
|
|
|
if(!newText.empty())
|
|
|
|
{
|
2023-02-12 23:52:35 +02:00
|
|
|
TextOperations::trimRightUnicode(newText);
|
2014-07-15 10:14:49 +03:00
|
|
|
redrawNeeded = true;
|
|
|
|
}
|
|
|
|
else if(!text.empty())
|
|
|
|
{
|
2023-02-12 23:52:35 +02:00
|
|
|
TextOperations::trimRightUnicode(text);
|
2014-07-15 10:14:49 +03:00
|
|
|
redrawNeeded = true;
|
2016-11-07 23:19:53 +02:00
|
|
|
}
|
2014-07-15 10:14:49 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2015-06-21 00:13:45 +02:00
|
|
|
|
2022-05-07 11:56:34 +02:00
|
|
|
if(redrawNeeded)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
redraw();
|
|
|
|
cb(text);
|
2016-11-07 23:19:53 +02:00
|
|
|
}
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2023-08-26 12:01:19 +02:00
|
|
|
void CTextInput::showPopupWindow(const Point & cursorPosition)
|
|
|
|
{
|
2023-08-27 19:39:45 +02:00
|
|
|
if(!helpBox.empty()) //there is no point to show window with nothing inside...
|
2023-08-26 12:01:19 +02:00
|
|
|
CRClickPopup::createAndPush(helpBox);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-07 21:50:45 +02:00
|
|
|
void CTextInput::setText(const std::string & nText)
|
|
|
|
{
|
|
|
|
setText(nText, false);
|
|
|
|
}
|
|
|
|
|
2022-05-07 11:56:34 +02:00
|
|
|
void CTextInput::setText(const std::string & nText, bool callCb)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
CLabel::setText(nText);
|
|
|
|
if(callCb)
|
|
|
|
cb(text);
|
|
|
|
}
|
|
|
|
|
2023-08-27 19:39:45 +02:00
|
|
|
void CTextInput::setHelpText(const std::string & text)
|
|
|
|
{
|
|
|
|
helpBox = text;
|
|
|
|
}
|
|
|
|
|
2023-02-02 20:16:41 +02:00
|
|
|
void CTextInput::textInputed(const std::string & enteredText)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
if(!focus)
|
|
|
|
return;
|
|
|
|
std::string oldText = text;
|
2016-11-07 23:19:53 +02:00
|
|
|
|
2023-02-02 20:16:41 +02:00
|
|
|
text += enteredText;
|
2016-11-07 23:19:53 +02:00
|
|
|
|
2022-05-07 11:56:34 +02:00
|
|
|
filters(text, oldText);
|
|
|
|
if(text != oldText)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
redraw();
|
|
|
|
cb(text);
|
2016-11-07 23:19:53 +02:00
|
|
|
}
|
2022-11-15 02:20:55 +02:00
|
|
|
newText.clear();
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2023-02-02 20:16:41 +02:00
|
|
|
void CTextInput::textEdited(const std::string & enteredText)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
if(!focus)
|
|
|
|
return;
|
2016-11-07 23:19:53 +02:00
|
|
|
|
2023-02-02 20:16:41 +02:00
|
|
|
newText = enteredText;
|
2014-07-15 10:14:49 +03:00
|
|
|
redraw();
|
2022-05-07 11:56:34 +02:00
|
|
|
cb(text + newText);
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CTextInput::filenameFilter(std::string & text, const std::string &)
|
|
|
|
{
|
|
|
|
static const std::string forbiddenChars = "<>:\"/\\|?*\r\n"; //if we are entering a filename, some special characters won't be allowed
|
|
|
|
size_t pos;
|
2022-05-07 11:56:34 +02:00
|
|
|
while((pos = text.find_first_of(forbiddenChars)) != std::string::npos)
|
2014-07-15 10:14:49 +03:00
|
|
|
text.erase(pos, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CTextInput::numberFilter(std::string & text, const std::string & oldText, int minValue, int maxValue)
|
|
|
|
{
|
|
|
|
assert(minValue < maxValue);
|
|
|
|
|
2022-05-07 11:56:34 +02:00
|
|
|
if(text.empty())
|
2014-07-15 10:14:49 +03:00
|
|
|
text = "0";
|
|
|
|
|
|
|
|
size_t pos = 0;
|
2022-05-07 11:56:34 +02:00
|
|
|
if(text[0] == '-') //allow '-' sign as first symbol only
|
2014-07-15 10:14:49 +03:00
|
|
|
pos++;
|
|
|
|
|
2022-05-07 11:56:34 +02:00
|
|
|
while(pos < text.size())
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2022-05-07 11:56:34 +02:00
|
|
|
if(text[pos] < '0' || text[pos] > '9')
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
text = oldText;
|
|
|
|
return; //new text is not number.
|
|
|
|
}
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
int value = boost::lexical_cast<int>(text);
|
2022-05-07 11:56:34 +02:00
|
|
|
if(value < minValue)
|
2023-03-09 15:36:46 +02:00
|
|
|
text = std::to_string(minValue);
|
2022-05-07 11:56:34 +02:00
|
|
|
else if(value > maxValue)
|
2023-03-09 15:36:46 +02:00
|
|
|
text = std::to_string(maxValue);
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
catch(boost::bad_lexical_cast &)
|
|
|
|
{
|
|
|
|
//Should never happen. Unless I missed some cases
|
2017-08-11 13:38:10 +02:00
|
|
|
logGlobal->warn("Warning: failed to convert %s to number!", text);
|
2014-07-15 10:14:49 +03:00
|
|
|
text = oldText;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CFocusable::CFocusable()
|
2022-05-07 11:56:34 +02:00
|
|
|
:CFocusable(std::make_shared<IFocusListener>())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CFocusable::CFocusable(std::shared_ptr<IFocusListener> focusListener)
|
|
|
|
: focusListener(focusListener)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2016-11-27 18:00:16 +02:00
|
|
|
focus = false;
|
2014-07-15 10:14:49 +03:00
|
|
|
focusables.push_back(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
CFocusable::~CFocusable()
|
|
|
|
{
|
2022-05-07 11:56:34 +02:00
|
|
|
if(hasFocus())
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
inputWithFocus = nullptr;
|
2022-05-07 11:56:34 +02:00
|
|
|
focusListener->focusLost();
|
2016-11-07 23:19:53 +02:00
|
|
|
}
|
2014-07-15 10:14:49 +03:00
|
|
|
|
|
|
|
focusables -= this;
|
|
|
|
}
|
2021-02-15 20:11:18 +02:00
|
|
|
|
2022-05-07 11:56:34 +02:00
|
|
|
bool CFocusable::hasFocus() const
|
|
|
|
{
|
|
|
|
return inputWithFocus == this;
|
|
|
|
}
|
|
|
|
|
2014-07-15 10:14:49 +03:00
|
|
|
void CFocusable::giveFocus()
|
|
|
|
{
|
2021-02-15 20:11:18 +02:00
|
|
|
focus = true;
|
2022-05-07 11:56:34 +02:00
|
|
|
focusListener->focusGot();
|
2021-02-15 20:11:18 +02:00
|
|
|
redraw();
|
|
|
|
|
2014-07-15 10:14:49 +03:00
|
|
|
if(inputWithFocus)
|
|
|
|
{
|
|
|
|
inputWithFocus->focus = false;
|
2022-05-07 11:56:34 +02:00
|
|
|
inputWithFocus->focusListener->focusLost();
|
2014-07-15 10:14:49 +03:00
|
|
|
inputWithFocus->redraw();
|
|
|
|
}
|
|
|
|
|
|
|
|
inputWithFocus = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CFocusable::moveFocus()
|
|
|
|
{
|
|
|
|
auto i = vstd::find(focusables, this),
|
|
|
|
ourIt = i;
|
|
|
|
for(i++; i != ourIt; i++)
|
|
|
|
{
|
|
|
|
if(i == focusables.end())
|
|
|
|
i = focusables.begin();
|
|
|
|
|
2023-05-17 22:22:45 +02:00
|
|
|
if((*i)->isActive())
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
(*i)->giveFocus();
|
2017-07-12 21:01:10 +02:00
|
|
|
break;
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-14 12:08:38 +02:00
|
|
|
|
|
|
|
void CFocusable::removeFocus()
|
|
|
|
{
|
2023-10-16 01:39:53 +02:00
|
|
|
if(this == inputWithFocus)
|
|
|
|
{
|
|
|
|
focus = false;
|
|
|
|
focusListener->focusLost();
|
|
|
|
redraw();
|
2023-10-14 12:08:38 +02:00
|
|
|
|
2023-10-16 01:39:53 +02:00
|
|
|
inputWithFocus = nullptr;
|
|
|
|
}
|
2023-10-14 12:08:38 +02:00
|
|
|
}
|