1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-10-31 00:07:39 +02:00

fix allied towns & color text input

This commit is contained in:
Laserlicht
2025-10-05 14:24:29 +02:00
parent 3e2a526140
commit 6c1748e0d6
5 changed files with 34 additions and 12 deletions

View File

@@ -85,10 +85,17 @@ void CTextInputWithConfirm::textInputted(const std::string & enteredText)
std::string visibleText = getVisibleText() + enteredText;
const auto & font = ENGINE->renderHandler().loadFont(label->font);
if(!limitToRect || font->getStringWidth(visibleText) < pos.w)
if(!limitToRect || (font->getStringWidth(visibleText) - CLabel::getDelimitersWidth(label->font, visibleText)) < pos.w)
CTextInput::textInputted(enteredText);
}
void CTextInputWithConfirm::deactivate()
{
removeUsedEvents(LCLICK);
CTextInput::deactivate();
}
void CTextInputWithConfirm::confirm()
{
if(getText().empty())
@@ -268,7 +275,7 @@ void CTextInput::updateLabel()
label->alignment = originalAlignment;
const auto & font = ENGINE->renderHandler().loadFont(label->font);
while (font->getStringWidth(visibleText) > pos.w)
while ((font->getStringWidth(visibleText) - CLabel::getDelimitersWidth(label->font, visibleText)) > pos.w)
{
label->alignment = ETextAlignment::CENTERRIGHT;
visibleText = visibleText.substr(TextOperations::getUnicodeCharacterSize(visibleText[0]));

View File

@@ -128,4 +128,5 @@ public:
bool receiveEvent(const Point & position, int eventType) const override;
void onFocusGot() override;
void textInputted(const std::string & enteredText) override;
void deactivate() override;
};

View File

@@ -182,29 +182,39 @@ std::vector<std::string> CMultiLineLabel::getLines()
return lines;
}
void CTextContainer::blitLine(Canvas & to, Rect destRect, std::string what)
const std::string delimiters = "{}";
int CTextContainer::getDelimitersWidth(EFonts font, std::string text)
{
const auto f = ENGINE->renderHandler().loadFont(font);
Point where = destRect.topLeft();
const std::string delimiters = "{}";
auto delimitersCount = std::count_if(what.cbegin(), what.cend(), [&delimiters](char c)
auto delimitersWidth = std::count_if(text.cbegin(), text.cend(), [](char c)
{
return delimiters.find(c) != std::string::npos;
});
//We should count delimiters length from string to correct centering later.
delimitersCount *= f->getStringWidth(delimiters)/2;
delimitersWidth *= f->getStringWidth(delimiters)/2;
std::smatch match;
std::regex expr("\\{(.*?)\\|");
std::string::const_iterator searchStart( what.cbegin() );
while(std::regex_search(searchStart, what.cend(), match, expr))
std::string::const_iterator searchStart( text.cbegin() );
while(std::regex_search(searchStart, text.cend(), match, expr))
{
std::string colorText = match[1].str();
if(auto c = Colors::parseColor(colorText))
delimitersCount += f->getStringWidth(colorText + "|");
delimitersWidth += f->getStringWidth(colorText + "|");
searchStart = match.suffix().first;
}
return delimitersWidth;
}
void CTextContainer::blitLine(Canvas & to, Rect destRect, std::string what)
{
const auto f = ENGINE->renderHandler().loadFont(font);
Point where = destRect.topLeft();
int delimitersWidth = getDelimitersWidth(font, what);
// input is rect in which given text should be placed
// calculate proper position for top-left corner of the text
@@ -212,10 +222,10 @@ void CTextContainer::blitLine(Canvas & to, Rect destRect, std::string what)
where.x += getBorderSize().x;
if(alignment == ETextAlignment::CENTER || alignment == ETextAlignment::TOPCENTER || alignment == ETextAlignment::BOTTOMCENTER)
where.x += (destRect.w - (static_cast<int>(f->getStringWidth(what)) - delimitersCount)) / 2;
where.x += (destRect.w - (static_cast<int>(f->getStringWidth(what)) - delimitersWidth)) / 2;
if(alignment == ETextAlignment::TOPRIGHT || alignment == ETextAlignment::BOTTOMRIGHT || alignment == ETextAlignment::CENTERRIGHT)
where.x += getBorderSize().x + destRect.w - (static_cast<int>(f->getStringWidth(what)) - delimitersCount);
where.x += getBorderSize().x + destRect.w - (static_cast<int>(f->getStringWidth(what)) - delimitersWidth);
if(alignment == ETextAlignment::TOPLEFT || alignment == ETextAlignment::TOPCENTER || alignment == ETextAlignment::TOPRIGHT)
where.y += getBorderSize().y;

View File

@@ -31,6 +31,8 @@ protected:
CTextContainer(ETextAlignment alignment, EFonts font, ColorRGBA color);
public:
static int getDelimitersWidth(EFonts font, std::string text);
ETextAlignment alignment;
EFonts font;
ColorRGBA color; // default font color. Can be overridden by placing "{}" into the string

View File

@@ -1443,6 +1443,8 @@ CCastleInterface::CCastleInterface(const CGTownInstance * Town, const CGTownInst
name = ""; // use textID again
GAME->interface()->cb->setTownName(town, name);
});
if(town->tempOwner != GAME->interface()->playerID) // disable changing for allied towns
title->deactivate();
income = std::make_shared<CLabel>(195, 443, FONT_SMALL, ETextAlignment::CENTER);
icon = std::make_shared<CAnimImage>(AnimationPath::builtin("ITPT"), 0, 0, 15, 387);