Skip to content

Commit

Permalink
Use delegating constructors for cCheckBox/cPushButton.
Browse files Browse the repository at this point in the history
Allow to shorten text for `cLabel` (and use it for `unitNameLabel`).
Disable shorten text for `cCheckBox`/`cPushButton` (especially as "Build xN" loses important "xN" in French :-/ ).
  • Loading branch information
Jarod42 committed Jan 22, 2024
1 parent b656c71 commit 25d98cc
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 63 deletions.
3 changes: 0 additions & 3 deletions src/lib/output/video/unifonts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -888,9 +888,6 @@ std::string cUnicodeFont::shortenStringToSize (const std::string& str, int size,
{
Log.warn ("shorten string : '" + str + "' to '" + res + "'");
}
#if 1 // Use no-shortened string (especially "Build XN" becoming "Build." in French :-/ )
return str;
#endif
}
return res;
}
Expand Down
1 change: 1 addition & 0 deletions src/ui/graphical/game/hud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ cHud::cHud (std::shared_ptr<cAnimationTimer> animationTimer) :

coordsLabel = emplaceChild<cLabel> (cBox<cPosition> (cPosition (265, getEndPosition().y() - 18), cPosition (265 + 64, getEndPosition().y() - 18 + 10)), "", eUnicodeFontType::LatinNormal, eAlignmentType::CenterHorizontal);
unitNameLabel = emplaceChild<cLabel> (cBox<cPosition> (cPosition (343, getEndPosition().y() - 18), cPosition (343 + 212, getEndPosition().y() - 18 + 10)), "", eUnicodeFontType::LatinNormal, eAlignmentType::CenterHorizontal);
unitNameLabel->setShorten (true);
turnLabel = emplaceChild<cLabel> (cBox<cPosition> (cPosition (471, 7), cPosition (471 + 55, 7 + 10)), "", eUnicodeFontType::LatinNormal, eAlignmentType::CenterHorizontal);
turnTimeClockWidget = emplaceChild<cTurnTimeClockWidget> (cBox<cPosition> (cPosition (537, 7), cPosition (537 + 55, 7 + 10)));

Expand Down
19 changes: 4 additions & 15 deletions src/ui/graphical/menu/widgets/checkbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,8 @@

//------------------------------------------------------------------------------
cCheckBox::cCheckBox (const cPosition& position, eCheckBoxType type_, bool centered, cSoundChunk* clickSound_) :
cClickableWidget (position),
type (type_),
fontType (eUnicodeFontType::LatinNormal),
textAnchor (eCheckBoxTextAnchor::Right),
textLimitWidth (-1),
clickSound (clickSound_),
checked (false),
isLocked (false)
cCheckBox (position, "", eUnicodeFontType::LatinNormal, eCheckBoxTextAnchor::Right, type_, centered, clickSound_)
{
renewSurface();
if (centered) move (cPosition (-getSize().x() / 2, 0));
}

cCheckBox::cCheckBox (const cPosition& position, const std::string& text_, eUnicodeFontType fontType_, eCheckBoxTextAnchor textAnchor_, eCheckBoxType type_, bool centered, cSoundChunk* clickSound_) :
Expand All @@ -50,10 +41,7 @@ cCheckBox::cCheckBox (const cPosition& position, const std::string& text_, eUnic
text (text_),
fontType (fontType_),
textAnchor (textAnchor_),
textLimitWidth (-1),
clickSound (clickSound_),
checked (false),
isLocked (false)
clickSound (clickSound_)
{
renewSurface();
if (centered) move (cPosition (-getSize().x() / 2, 0));
Expand Down Expand Up @@ -228,6 +216,7 @@ void cCheckBox::renewSurface()
cPosition size;
SDL_Rect src = {0, 0, 0, 0};
auto font = cUnicodeFont::font.get();
int textLimitWidth = -1;

if (type >= eCheckBoxType::HudIndex_00 && type <= eCheckBoxType::HudIndex_22)
{
Expand Down Expand Up @@ -373,5 +362,5 @@ void cCheckBox::renewSurface()
SDL_BlitSurface (srcSurface, &src, surface.get(), nullptr);
}

if (textLimitWidth != -1) text = font->shortenStringToSize (text, textLimitWidth, fontType);
if (shorten && textLimitWidth != -1) text = font->shortenStringToSize (text, textLimitWidth, fontType);
}
14 changes: 7 additions & 7 deletions src/ui/graphical/menu/widgets/checkbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ class cCheckBox : public cClickableWidget
void setPressed (bool pressed) override;
bool handleClicked (cApplication&, cMouse&, eMouseButtonType) override;

private:
void renewSurface();

private:
UniqueSurface surface;

Expand All @@ -102,15 +105,12 @@ class cCheckBox : public cClickableWidget
std::string text;
eUnicodeFontType fontType;
eCheckBoxTextAnchor textAnchor;
int textLimitWidth;

cSoundChunk* clickSound;
cSoundChunk* clickSound = nullptr;

bool checked;

bool isLocked;

void renewSurface();
bool checked = false;
bool isLocked = false;
bool shorten = false;
};

#endif // ui_graphical_menu_widgets_checkboxH
41 changes: 8 additions & 33 deletions src/ui/graphical/menu/widgets/pushbutton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ cPushButton::cPushButton (const cBox<cPosition>& area) :
buttonType (ePushButtonType::Invisible),
fontType (eUnicodeFontType::LatinBig),
text (""),
clickSound (&SoundData.SNDHudButton),
isLocked (false)

clickSound (&SoundData.SNDHudButton)
{
if (buttonType >= ePushButtonType::HudNext && buttonType <= ePushButtonType::HudFiles)
fontType = eUnicodeFontType::LatinSmallWhite;
Expand All @@ -89,44 +87,20 @@ cPushButton::cPushButton (const cBox<cPosition>& area) :

//------------------------------------------------------------------------------
cPushButton::cPushButton (const cPosition& position, ePushButtonType buttonType_) :
cClickableWidget (position),
buttonType (buttonType_),
fontType (eUnicodeFontType::LatinBig),
text (""),
clickSound (&SoundData.SNDHudButton),
isLocked (false)
cPushButton (position, buttonType_, &SoundData.SNDHudButton)
{
if (buttonType >= ePushButtonType::HudNext && buttonType <= ePushButtonType::HudFiles)
fontType = eUnicodeFontType::LatinSmallWhite;
renewSurface();
}

//------------------------------------------------------------------------------
cPushButton::cPushButton (const cPosition& position, ePushButtonType buttonType_, cSoundChunk* clickSound_) :
cClickableWidget (position),
buttonType (buttonType_),
fontType (eUnicodeFontType::LatinBig),
text (""),
clickSound (clickSound_),
isLocked (false)
cPushButton (position, buttonType_, clickSound_, "", eUnicodeFontType::LatinBig)
{
if (buttonType >= ePushButtonType::HudNext && buttonType <= ePushButtonType::HudFiles)
fontType = eUnicodeFontType::LatinSmallWhite;
renewSurface();
}

//------------------------------------------------------------------------------
cPushButton::cPushButton (const cPosition& position, ePushButtonType buttonType_, const std::string& text_, eUnicodeFontType fontType_) :
cClickableWidget (position),
buttonType (buttonType_),
fontType (fontType_),
text (text_),
clickSound (&SoundData.SNDHudButton),
isLocked (false)
cPushButton (position, buttonType_, &SoundData.SNDHudButton, text_, fontType_)
{
if (buttonType >= ePushButtonType::HudNext && buttonType <= ePushButtonType::HudFiles)
fontType = eUnicodeFontType::LatinSmallWhite;
renewSurface();
}

//------------------------------------------------------------------------------
Expand All @@ -135,8 +109,7 @@ cPushButton::cPushButton (const cPosition& position, ePushButtonType buttonType_
buttonType (buttonType_),
fontType (fontType_),
text (text_),
clickSound (clickSound_),
isLocked (false)
clickSound (clickSound_)
{
if (buttonType >= ePushButtonType::HudNext && buttonType <= ePushButtonType::HudFiles)
fontType = eUnicodeFontType::LatinSmallWhite;
Expand Down Expand Up @@ -379,7 +352,9 @@ void cPushButton::renewSurface()

SDL_BlitSurface (srcSurface, &src, surface.get(), nullptr);

text = cUnicodeFont::font->shortenStringToSize (text, size.x() - getBordersSize(), fontType);
if (shorten) {
text = cUnicodeFont::font->shortenStringToSize (text, size.x() - getBordersSize(), fontType);
}
}

//------------------------------------------------------------------------------
Expand Down
5 changes: 3 additions & 2 deletions src/ui/graphical/menu/widgets/pushbutton.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,10 @@ class cPushButton : public cClickableWidget
eUnicodeFontType fontType;
std::string text;

cSoundChunk* clickSound;
cSoundChunk* clickSound = nullptr;

bool isLocked;
bool isLocked = false;
bool shorten = false;

void renewSurface();

Expand Down
16 changes: 13 additions & 3 deletions src/ui/widgets/label.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,29 @@ const std::string& cLabel::getText() const
//------------------------------------------------------------------------------
void cLabel::setFont (eUnicodeFontType fontType_)
{
std::swap (fontType, fontType_);
if (fontType != fontType_) { dirty = true; }
fontType = fontType_;
}

//------------------------------------------------------------------------------
void cLabel::setAlignment (AlignmentFlags alignment_)
{
std::swap (alignment, alignment_);
if (alignment != alignment_) { dirty = true; }
alignment = alignment_;
}

//------------------------------------------------------------------------------
void cLabel::setWordWrap (bool wordWrap_)
{
std::swap (wordWrap, wordWrap_);
if (wordWrap != wordWrap_) { dirty = true; }
wordWrap = wordWrap_;
}

//------------------------------------------------------------------------------
void cLabel::setShorten (bool value)
{
if (shorten != value) { dirty = true; }
shorten = value;
}

//------------------------------------------------------------------------------
Expand All @@ -109,6 +116,9 @@ void cLabel::updateDisplayInformation()

auto font = cUnicodeFont::font.get();
drawLines = wordWrap ? font->breakText (text, getSize().x(), fontType): std::vector{text};
if (shorten) {
drawLines = ranges::Transform (drawLines, [&] (const auto& s) { return font->shortenStringToSize (s, surface->w, fontType); });
}

SDL_FillRect (surface.get(), nullptr, 0xFF00FF);

Expand Down
2 changes: 2 additions & 0 deletions src/ui/widgets/label.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class cLabel : public cClickableWidget
void setFont (eUnicodeFontType);
void setAlignment (AlignmentFlags);
void setWordWrap (bool wordWrap);
void setShorten (bool);

void resizeToTextHeight();

Expand All @@ -57,6 +58,7 @@ class cLabel : public cClickableWidget
eUnicodeFontType fontType;
AlignmentFlags alignment;
bool wordWrap = false;
bool shorten = false;
bool dirty = true;

std::vector<std::string> drawLines;
Expand Down

0 comments on commit 25d98cc

Please sign in to comment.