@@ -734,7 +734,7 @@ namespace fheroes2
734
734
void makeTransparentBackground ( const Sprite & released, Sprite & pressed, const int backgroundIcnID )
735
735
{
736
736
// We need to copy the background image to pressed button only where it does not overlay the image of released button.
737
- const fheroes2:: Sprite & background = fheroes2:: AGG::GetICN ( backgroundIcnID, 0 );
737
+ const Sprite & background = AGG::GetICN ( backgroundIcnID, 0 );
738
738
// you are trying to apply transform on an image that is single-layered
739
739
assert ( !pressed.singleLayer () && !released.singleLayer () );
740
740
const uint8_t * releasedTransform = released.transform ();
@@ -778,7 +778,7 @@ namespace fheroes2
778
778
ButtonSprite makeButtonWithShadow ( int32_t offsetX, int32_t offsetY, const Sprite & released, const Sprite & pressed, const Image & background,
779
779
const Point & shadowOffset )
780
780
{
781
- const Sprite & shadow = fheroes2:: makeShadow ( released, shadowOffset, 3 );
781
+ const Sprite & shadow = makeShadow ( released, shadowOffset, 3 );
782
782
783
783
Sprite croppedBackground = Crop ( background, offsetX + shadow.x (), offsetY + shadow.y (), shadow.width (), shadow.height () );
784
784
Blit ( shadow, croppedBackground );
@@ -837,28 +837,28 @@ namespace fheroes2
837
837
}
838
838
}
839
839
840
- void getTextAdaptedSprite ( Sprite & released, Sprite & pressed, const char * text , const int emptyButtonIcnID, const int buttonBackgroundIcnID )
840
+ void getTextAdaptedSprite ( Sprite & released, Sprite & pressed, const char * untranslatedText , const int emptyButtonIcnID, const int buttonBackgroundIcnID )
841
841
{
842
- fheroes2:: FontColor buttonFont = fheroes2:: FontColor::WHITE;
843
- fheroes2:: Point textAreaMargins = { 0 , 3 };
842
+ FontColor buttonFont = FontColor::WHITE;
843
+ Point textAreaMargins = { 0 , 3 };
844
844
845
- fheroes2:: Size minimumTextArea = { 0 , 15 };
846
- fheroes2:: Size maximumTextArea = { 200 , 200 }; // Why is such a wide button needed?
845
+ Size minimumTextArea = { 0 , 15 };
846
+ Size maximumTextArea = { 200 , 200 }; // Why is such a wide button needed?
847
847
848
- fheroes2:: Size backgroundBorders = { 0 , 7 };
848
+ Size backgroundBorders = { 0 , 7 };
849
849
850
- fheroes2:: Point releasedOffset;
851
- fheroes2:: Point pressedOffset;
850
+ Point releasedOffset;
851
+ Point pressedOffset;
852
852
853
853
getButtonSpecificValues ( emptyButtonIcnID, buttonFont, textAreaMargins, minimumTextArea, maximumTextArea, backgroundBorders, releasedOffset, pressedOffset );
854
854
855
- const fheroes2:: FontType releasedButtonFont{ fheroes2:: FontSize::BUTTON_RELEASED, buttonFont };
855
+ const FontType releasedButtonFont{ FontSize::BUTTON_RELEASED, buttonFont };
856
856
857
- const char * translatedText = _ ( text );
858
- const char * supportedText = fheroes2::isFontAvailable ( translatedText , releasedButtonFont ) ? translatedText : text ;
857
+ // TODO: do not do translations for button generation. We shouldn't assume that we receive a non-translated string.
858
+ const char * supportedText = getSupportedText ( untranslatedText , releasedButtonFont );
859
859
860
- const fheroes2:: Text releasedText ( supportedText, releasedButtonFont );
861
- const fheroes2:: Text pressedText ( supportedText, { fheroes2:: FontSize::BUTTON_PRESSED, buttonFont } );
860
+ const Text releasedText ( supportedText, releasedButtonFont );
861
+ const Text pressedText ( supportedText, { FontSize::BUTTON_PRESSED, buttonFont } );
862
862
863
863
// We need to pass an argument to width() so that it correctly accounts for multi-lined texts.
864
864
const int32_t textWidth = releasedText.width ( maximumTextArea.width );
@@ -879,10 +879,12 @@ namespace fheroes2
879
879
const int32_t borderedTextHeight = textHeight + textAreaMargins.y ;
880
880
const int32_t textAreaHeight = std::clamp ( borderedTextHeight, minimumTextArea.height , maximumTextArea.height );
881
881
882
- assert ( textAreaHeight + backgroundBorders.height > 0 );
882
+ const Size buttonSize ( textAreaWidth + backgroundBorders.width , textAreaHeight + backgroundBorders. height );
883
883
884
- released = resizeButton ( AGG::GetICN ( emptyButtonIcnID, 0 ), { textAreaWidth + backgroundBorders.width , textAreaHeight + backgroundBorders.height } );
885
- pressed = resizeButton ( AGG::GetICN ( emptyButtonIcnID, 1 ), { textAreaWidth + backgroundBorders.width , textAreaHeight + backgroundBorders.height } );
884
+ assert ( buttonSize.height > 0 );
885
+
886
+ released = resizeButton ( AGG::GetICN ( emptyButtonIcnID, 0 ), buttonSize );
887
+ pressed = resizeButton ( AGG::GetICN ( emptyButtonIcnID, 1 ), buttonSize );
886
888
887
889
if ( buttonBackgroundIcnID != ICN::UNKNOWN ) {
888
890
makeTransparentBackground ( released, pressed, buttonBackgroundIcnID );
@@ -891,27 +893,38 @@ namespace fheroes2
891
893
addButtonShine ( released, emptyButtonIcnID );
892
894
}
893
895
894
- const fheroes2::Size releasedTextSize ( releasedText.width ( textAreaWidth ), releasedText.height ( textAreaWidth ) );
895
- const fheroes2::Size pressedTextSize ( pressedText.width ( textAreaWidth ), pressedText.height ( textAreaWidth ) );
896
-
897
896
// The button font letters are all shifted 1 pixel to the left due to shadows, so we have to add 1 to the x position when drawing
898
897
// to properly center-align.
899
- releasedText.draw ( releasedOffset.x + 1 , releasedOffset.y + ( textAreaHeight - releasedTextSize. height ) / 2 , textAreaWidth, released );
900
- pressedText.draw ( pressedOffset.x + 1 , pressedOffset.y + ( textAreaHeight - pressedTextSize. height ) / 2 , textAreaWidth, pressed );
898
+ releasedText.draw ( releasedOffset.x + 1 , releasedOffset.y + ( textAreaHeight - textHeight ) / 2 , textAreaWidth, released );
899
+ pressedText.draw ( pressedOffset.x + 1 , pressedOffset.y + ( textAreaHeight - textHeight ) / 2 , textAreaWidth, pressed );
901
900
}
902
901
903
902
void makeButtonSprites ( Sprite & released, Sprite & pressed, const std::string & text, const Size buttonSize, const bool isEvilInterface, const int backgroundIcnId )
904
903
{
905
- fheroes2::Point releasedOffset;
906
- fheroes2::Point pressedOffset;
907
- fheroes2::getCustomNormalButton ( released, pressed, isEvilInterface, buttonSize, releasedOffset, pressedOffset, backgroundIcnId );
904
+ Point releasedOffset;
905
+ Point pressedOffset;
906
+ getCustomNormalButton ( released, pressed, isEvilInterface, buttonSize, releasedOffset, pressedOffset, backgroundIcnId );
907
+
908
+ const fheroes2::FontColor buttonFontColor = isEvilInterface ? fheroes2::FontColor::GRAY : fheroes2::FontColor::WHITE;
909
+ renderTextOnButton ( released, pressed, text, releasedOffset, pressedOffset, buttonSize, buttonFontColor );
910
+ }
911
+
912
+ const char * getSupportedText ( const char * untranslatedText, const FontType font )
913
+ {
914
+ const char * translatedText = _ ( untranslatedText );
915
+ return isFontAvailable ( translatedText, font ) ? translatedText : untranslatedText;
916
+ }
908
917
909
- const fheroes2::FontColor fontColor = isEvilInterface ? fheroes2::FontColor::GRAY : fheroes2::FontColor::WHITE;
918
+ void renderTextOnButton ( Image & releasedState, Image & pressedState, const std::string & text, const Point & releasedTextOffset, const Point & pressedTextOffset,
919
+ const Size & buttonSize, const FontColor fontColor )
920
+ {
921
+ const FontType releasedFont{ FontSize::BUTTON_RELEASED, fontColor };
922
+ const FontType pressedFont{ FontSize::BUTTON_PRESSED, fontColor };
910
923
911
- const fheroes2:: Text releasedText ( text, { fheroes2::FontSize::BUTTON_RELEASED, fontColor } );
912
- const fheroes2:: Text pressedText ( text, { fheroes2::FontSize::BUTTON_PRESSED, fontColor } );
924
+ const Text releasedText ( text, releasedFont );
925
+ const Text pressedText ( text, pressedFont );
913
926
914
- releasedText.draw ( releasedOffset .x , ( buttonSize.height - releasedText.height ( buttonSize.width ) ) / 2 , buttonSize.width , released );
915
- pressedText.draw ( pressedOffset .x , ( buttonSize.height - pressedText.height ( buttonSize.width ) ) / 2 + 1 , buttonSize.width , pressed );
927
+ releasedText.draw ( releasedTextOffset .x , ( buttonSize.height - releasedText.height ( buttonSize.width ) ) / 2 , buttonSize.width , releasedState );
928
+ pressedText.draw ( pressedTextOffset .x , ( buttonSize.height - pressedText.height ( buttonSize.width ) ) / 2 + 1 , buttonSize.width , pressedState );
916
929
}
917
930
}
0 commit comments