1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-02-03 13:01:33 +02:00

Merge pull request #3462 from Laserlicht/trim_text

limit textboxes
This commit is contained in:
Ivan Savenko 2024-01-12 21:53:30 +02:00 committed by GitHub
commit 7cf738b20e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 44 additions and 18 deletions

View File

@ -102,7 +102,7 @@
"vcmi.systemOptions.resolutionMenu.help" : "Ändere die Spielauflösung.", "vcmi.systemOptions.resolutionMenu.help" : "Ändere die Spielauflösung.",
"vcmi.systemOptions.scalingButton.hover" : "Interface-Skalierung: %p%", "vcmi.systemOptions.scalingButton.hover" : "Interface-Skalierung: %p%",
"vcmi.systemOptions.scalingButton.help" : "{Interface-Skalierung}\n\nÄndern der Skalierung des Interfaces im Spiel", "vcmi.systemOptions.scalingButton.help" : "{Interface-Skalierung}\n\nÄndern der Skalierung des Interfaces im Spiel",
"vcmi.systemOptions.scalingMenu.hover" : "Skalierung des Interfaces auswählen", "vcmi.systemOptions.scalingMenu.hover" : "Skalierung auswählen",
"vcmi.systemOptions.scalingMenu.help" : "Ändern der Skalierung des Interfaces im Spiel.", "vcmi.systemOptions.scalingMenu.help" : "Ändern der Skalierung des Interfaces im Spiel.",
"vcmi.systemOptions.longTouchButton.hover" : "Berührungsdauer für langer Touch: %d ms", // Translation note: "ms" = "milliseconds" "vcmi.systemOptions.longTouchButton.hover" : "Berührungsdauer für langer Touch: %d ms", // Translation note: "ms" = "milliseconds"
"vcmi.systemOptions.longTouchButton.help" : "{Berührungsdauer für langer Touch}\n\nBei Verwendung des Touchscreens erscheinen Popup-Fenster nach Berührung des Bildschirms für die angegebene Dauer (in Millisekunden)", "vcmi.systemOptions.longTouchButton.help" : "{Berührungsdauer für langer Touch}\n\nBei Verwendung des Touchscreens erscheinen Popup-Fenster nach Berührung des Bildschirms für die angegebene Dauer (in Millisekunden)",

View File

@ -537,11 +537,11 @@ void OptionsTab::SelectionWindow::recreate()
void OptionsTab::SelectionWindow::drawOutlinedText(int x, int y, ColorRGBA color, std::string text) void OptionsTab::SelectionWindow::drawOutlinedText(int x, int y, ColorRGBA color, std::string text)
{ {
components.push_back(std::make_shared<CLabel>(x-1, y, FONT_TINY, ETextAlignment::CENTER, Colors::BLACK, text)); components.push_back(std::make_shared<CLabel>(x-1, y, FONT_TINY, ETextAlignment::CENTER, Colors::BLACK, text, 56));
components.push_back(std::make_shared<CLabel>(x+1, y, FONT_TINY, ETextAlignment::CENTER, Colors::BLACK, text)); components.push_back(std::make_shared<CLabel>(x+1, y, FONT_TINY, ETextAlignment::CENTER, Colors::BLACK, text, 56));
components.push_back(std::make_shared<CLabel>(x, y-1, FONT_TINY, ETextAlignment::CENTER, Colors::BLACK, text)); components.push_back(std::make_shared<CLabel>(x, y-1, FONT_TINY, ETextAlignment::CENTER, Colors::BLACK, text, 56));
components.push_back(std::make_shared<CLabel>(x, y+1, FONT_TINY, ETextAlignment::CENTER, Colors::BLACK, text)); components.push_back(std::make_shared<CLabel>(x, y+1, FONT_TINY, ETextAlignment::CENTER, Colors::BLACK, text, 56));
components.push_back(std::make_shared<CLabel>(x, y, FONT_TINY, ETextAlignment::CENTER, color, text)); components.push_back(std::make_shared<CLabel>(x, y, FONT_TINY, ETextAlignment::CENTER, color, text, 56));
} }
void OptionsTab::SelectionWindow::genContentGrid(int lines) void OptionsTab::SelectionWindow::genContentGrid(int lines)
@ -774,7 +774,7 @@ OptionsTab::SelectedBox::SelectedBox(Point position, PlayerSettings & playerSett
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE; OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
image = std::make_shared<CAnimImage>(getImageName(), getImageIndex()); image = std::make_shared<CAnimImage>(getImageName(), getImageIndex());
subtitle = std::make_shared<CLabel>(23, 39, FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, getName()); subtitle = std::make_shared<CLabel>(24, 39, FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, getName(), 71);
pos = image->pos; pos = image->pos;
@ -889,7 +889,7 @@ OptionsTab::PlayerOptionsEntry::PlayerOptionsEntry(const PlayerSettings & S, con
background = std::make_shared<CPicture>(ImagePath::builtin(bgs[s->color]), 0, 0); background = std::make_shared<CPicture>(ImagePath::builtin(bgs[s->color]), 0, 0);
if(s->isControlledByAI() || CSH->isGuest()) if(s->isControlledByAI() || CSH->isGuest())
labelPlayerName = std::make_shared<CLabel>(55, 10, EFonts::FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, name); labelPlayerName = std::make_shared<CLabel>(55, 10, EFonts::FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, name, 95);
else else
{ {
labelPlayerNameEdit = std::make_shared<CTextInput>(Rect(6, 3, 95, 15), EFonts::FONT_SMALL, nullptr, false); labelPlayerNameEdit = std::make_shared<CTextInput>(Rect(6, 3, 95, 15), EFonts::FONT_SMALL, nullptr, false);

View File

@ -833,7 +833,7 @@ SelectionTab::ListItem::ListItem(Point position, std::shared_ptr<CAnimation> ico
{ {
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE; OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
pictureEmptyLine = std::make_shared<CPicture>(GH.renderHandler().loadImage(ImagePath::builtin("camcust")), Rect(25, 121, 349, 26), -8, -14); pictureEmptyLine = std::make_shared<CPicture>(GH.renderHandler().loadImage(ImagePath::builtin("camcust")), Rect(25, 121, 349, 26), -8, -14);
labelName = std::make_shared<CLabel>(184, 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE); labelName = std::make_shared<CLabel>(184, 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, "", 185);
labelName->setAutoRedraw(false); labelName->setAutoRedraw(false);
labelAmountOfPlayers = std::make_shared<CLabel>(8, 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE); labelAmountOfPlayers = std::make_shared<CLabel>(8, 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
labelAmountOfPlayers->setAutoRedraw(false); labelAmountOfPlayers->setAutoRedraw(false);
@ -876,11 +876,13 @@ void SelectionTab::ListItem::updateItem(std::shared_ptr<ElementInfo> info, bool
iconLossCondition->disable(); iconLossCondition->disable();
labelNumberOfCampaignMaps->disable(); labelNumberOfCampaignMaps->disable();
labelName->enable(); labelName->enable();
labelName->setMaxWidth(316);
labelName->setText(info->folderName); labelName->setText(info->folderName);
labelName->setColor(color); labelName->setColor(color);
return; return;
} }
labelName->enable();
if(info->campaign) if(info->campaign)
{ {
labelAmountOfPlayers->disable(); labelAmountOfPlayers->disable();
@ -895,6 +897,7 @@ void SelectionTab::ListItem::updateItem(std::shared_ptr<ElementInfo> info, bool
ostr << info->campaign->scenariosCount(); ostr << info->campaign->scenariosCount();
labelNumberOfCampaignMaps->setText(ostr.str()); labelNumberOfCampaignMaps->setText(ostr.str());
labelNumberOfCampaignMaps->setColor(color); labelNumberOfCampaignMaps->setColor(color);
labelName->setMaxWidth(316);
} }
else else
{ {
@ -915,8 +918,8 @@ void SelectionTab::ListItem::updateItem(std::shared_ptr<ElementInfo> info, bool
iconVictoryCondition->setFrame(info->mapHeader->victoryIconIndex, 0); iconVictoryCondition->setFrame(info->mapHeader->victoryIconIndex, 0);
iconLossCondition->enable(); iconLossCondition->enable();
iconLossCondition->setFrame(info->mapHeader->defeatIconIndex, 0); iconLossCondition->setFrame(info->mapHeader->defeatIconIndex, 0);
labelName->setMaxWidth(185);
} }
labelName->enable();
labelName->setText(info->getNameForList()); labelName->setText(info->getNameForList());
labelName->setColor(color); labelName->setColor(color);
} }

View File

@ -47,8 +47,8 @@ void CLabel::showAll(Canvas & to)
} }
CLabel::CLabel(int x, int y, EFonts Font, ETextAlignment Align, const ColorRGBA & Color, const std::string & Text) 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) : CTextContainer(Align, Font, Color), text(Text), maxWidth(maxWidth)
{ {
setRedrawParent(true); setRedrawParent(true);
autoRedraw = true; autoRedraw = true;
@ -56,6 +56,8 @@ CLabel::CLabel(int x, int y, EFonts Font, ETextAlignment Align, const ColorRGBA
pos.y += y; pos.y += y;
pos.w = pos.h = 0; pos.w = pos.h = 0;
trimText();
if(alignment == ETextAlignment::TOPLEFT) // causes issues for MIDDLE if(alignment == ETextAlignment::TOPLEFT) // causes issues for MIDDLE
{ {
pos.w = (int)graphics->fonts[font]->getStringWidth(visibleText().c_str()); pos.w = (int)graphics->fonts[font]->getStringWidth(visibleText().c_str());
@ -81,6 +83,9 @@ void CLabel::setAutoRedraw(bool value)
void CLabel::setText(const std::string & Txt) void CLabel::setText(const std::string & Txt)
{ {
text = Txt; text = Txt;
trimText();
if(autoRedraw) if(autoRedraw)
{ {
if(background || !parent) if(background || !parent)
@ -90,6 +95,18 @@ void CLabel::setText(const std::string & Txt)
} }
} }
void CLabel::setMaxWidth(int width)
{
maxWidth = width;
}
void CLabel::trimText()
{
if(maxWidth > 0)
while ((int)graphics->fonts[font]->getStringWidth(visibleText().c_str()) > maxWidth)
TextOperations::trimRightUnicode(text);
}
void CLabel::setColor(const ColorRGBA & Color) void CLabel::setColor(const ColorRGBA & Color)
{ {
color = Color; color = Color;
@ -444,7 +461,7 @@ void CGStatusBar::clear()
} }
CGStatusBar::CGStatusBar(std::shared_ptr<CIntObject> background_, EFonts Font, ETextAlignment Align, const ColorRGBA & Color) CGStatusBar::CGStatusBar(std::shared_ptr<CIntObject> background_, EFonts Font, ETextAlignment Align, const ColorRGBA & Color)
: CLabel(background_->pos.x, background_->pos.y, Font, Align, Color, "") : CLabel(background_->pos.x, background_->pos.y, Font, Align, Color, "", background_->pos.w)
, enteringText(false) , enteringText(false)
{ {
addUsedEvents(LCLICK); addUsedEvents(LCLICK);
@ -542,6 +559,7 @@ CTextInput::CTextInput(const Rect & Pos, EFonts font, const CFunctionList<void(c
setRedrawParent(true); setRedrawParent(true);
pos.h = Pos.h; pos.h = Pos.h;
pos.w = Pos.w; pos.w = Pos.w;
maxWidth = Pos.w;
background.reset(); background.reset();
addUsedEvents(LCLICK | SHOW_POPUP | KEYBOARD | TEXTINPUT); addUsedEvents(LCLICK | SHOW_POPUP | KEYBOARD | TEXTINPUT);
@ -557,6 +575,7 @@ CTextInput::CTextInput(const Rect & Pos, const Point & bgOffset, const ImagePath
pos += Pos.topLeft(); pos += Pos.topLeft();
pos.h = Pos.h; pos.h = Pos.h;
pos.w = Pos.w; pos.w = Pos.w;
maxWidth = Pos.w;
OBJ_CONSTRUCTION; OBJ_CONSTRUCTION;
background = std::make_shared<CPicture>(bgName, bgOffset.x, bgOffset.y); background = std::make_shared<CPicture>(bgName, bgOffset.x, bgOffset.y);
@ -575,6 +594,7 @@ CTextInput::CTextInput(const Rect & Pos, std::shared_ptr<IImage> srf)
background = std::make_shared<CPicture>(srf, Pos); background = std::make_shared<CPicture>(srf, Pos);
pos.w = background->pos.w; pos.w = background->pos.w;
pos.h = background->pos.h; pos.h = background->pos.h;
maxWidth = Pos.w;
background->pos = pos; background->pos = pos;
addUsedEvents(LCLICK | KEYBOARD | TEXTINPUT); addUsedEvents(LCLICK | KEYBOARD | TEXTINPUT);
@ -683,7 +703,7 @@ void CTextInput::textInputed(const std::string & enteredText)
return; return;
std::string oldText = text; std::string oldText = text;
text += enteredText; setText(getText() + enteredText);
filters(text, oldText); filters(text, oldText);
if(text != oldText) if(text != oldText)

View File

@ -43,9 +43,11 @@ class CLabel : public CTextContainer
protected: protected:
Point getBorderSize() override; Point getBorderSize() override;
virtual std::string visibleText(); virtual std::string visibleText();
virtual void trimText();
std::shared_ptr<CIntObject> background; std::shared_ptr<CIntObject> background;
std::string text; std::string text;
int maxWidth;
bool autoRedraw; //whether control will redraw itself on setTxt bool autoRedraw; //whether control will redraw itself on setTxt
public: public:
@ -53,11 +55,12 @@ public:
std::string getText(); std::string getText();
virtual void setAutoRedraw(bool option); virtual void setAutoRedraw(bool option);
virtual void setText(const std::string & Txt); virtual void setText(const std::string & Txt);
virtual void setMaxWidth(int width);
virtual void setColor(const ColorRGBA & Color); virtual void setColor(const ColorRGBA & Color);
size_t getWidth(); size_t getWidth();
CLabel(int x = 0, int y = 0, EFonts Font = FONT_SMALL, ETextAlignment Align = ETextAlignment::TOPLEFT, CLabel(int x = 0, int y = 0, EFonts Font = FONT_SMALL, ETextAlignment Align = ETextAlignment::TOPLEFT,
const ColorRGBA & Color = Colors::WHITE, const std::string & Text = ""); const ColorRGBA & Color = Colors::WHITE, const std::string & Text = "", int maxWidth = 0);
void showAll(Canvas & to) override; //shows statusbar (with current text) void showAll(Canvas & to) override; //shows statusbar (with current text)
}; };

View File

@ -1430,7 +1430,7 @@ CHallInterface::CBuildingBox::CBuildingBox(int x, int y, const CGTownInstance *
header = std::make_shared<CAnimImage>(AnimationPath::builtin("TPTHBAR"), panelIndex[static_cast<int>(state)], 0, 1, 73); header = std::make_shared<CAnimImage>(AnimationPath::builtin("TPTHBAR"), panelIndex[static_cast<int>(state)], 0, 1, 73);
if(iconIndex[static_cast<int>(state)] >=0) if(iconIndex[static_cast<int>(state)] >=0)
mark = std::make_shared<CAnimImage>(AnimationPath::builtin("TPTHCHK"), iconIndex[static_cast<int>(state)], 0, 136, 56); mark = std::make_shared<CAnimImage>(AnimationPath::builtin("TPTHCHK"), iconIndex[static_cast<int>(state)], 0, 136, 56);
name = std::make_shared<CLabel>(75, 81, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, building->getNameTranslated()); name = std::make_shared<CLabel>(78, 81, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, building->getNameTranslated(), 150);
//todo: add support for all possible states //todo: add support for all possible states
if(state >= EBuildingState::BUILDING_ERROR) if(state >= EBuildingState::BUILDING_ERROR)
@ -1769,7 +1769,7 @@ CFortScreen::RecruitArea::RecruitArea(int posX, int posY, const CGTownInstance *
if(getMyBuilding() != nullptr) if(getMyBuilding() != nullptr)
{ {
buildingIcon = std::make_shared<CAnimImage>(town->town->clientInfo.buildingsIcons, getMyBuilding()->bid, 0, 4, 21); buildingIcon = std::make_shared<CAnimImage>(town->town->clientInfo.buildingsIcons, getMyBuilding()->bid, 0, 4, 21);
buildingName = std::make_shared<CLabel>(78, 101, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, getMyBuilding()->getNameTranslated()); buildingName = std::make_shared<CLabel>(78, 101, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, getMyBuilding()->getNameTranslated(), 152);
if(vstd::contains(town->builtBuildings, getMyBuilding()->bid)) if(vstd::contains(town->builtBuildings, getMyBuilding()->bid))
{ {
@ -1783,7 +1783,7 @@ CFortScreen::RecruitArea::RecruitArea(int posX, int posY, const CGTownInstance *
{ {
hoverText = boost::str(boost::format(CGI->generaltexth->tcommands[21]) % getMyCreature()->getNamePluralTranslated()); hoverText = boost::str(boost::format(CGI->generaltexth->tcommands[21]) % getMyCreature()->getNamePluralTranslated());
new CCreaturePic(159, 4, getMyCreature(), false); new CCreaturePic(159, 4, getMyCreature(), false);
new CLabel(78, 11, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, getMyCreature()->getNamePluralTranslated()); new CLabel(78, 11, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, getMyCreature()->getNamePluralTranslated(), 152);
Rect sizes(287, 4, 96, 18); Rect sizes(287, 4, 96, 18);
values.push_back(std::make_shared<LabeledValue>(sizes, CGI->generaltexth->allTexts[190], CGI->generaltexth->fcommands[0], getMyCreature()->getAttack(false))); values.push_back(std::make_shared<LabeledValue>(sizes, CGI->generaltexth->allTexts[190], CGI->generaltexth->fcommands[0], getMyCreature()->getAttack(false)));