Skip to content

Commit

Permalink
Shorten map title when too long in several places (#9207)
Browse files Browse the repository at this point in the history
  • Loading branch information
zenseii authored Oct 18, 2024
1 parent 8a79173 commit dddf722
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
10 changes: 6 additions & 4 deletions src/fheroes2/dialog/dialog_selectscenario.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ void ScenarioListBox::_renderSelectedScenarioInfo( fheroes2::Display & display,
_renderMapIcon( info.width, display, dst.x + SELECTED_SCENARIO_MAP_SIZE_OFFSET_X, dst.y + SELECTED_SCENARIO_GENERAL_OFFSET_Y );
fheroes2::Blit( _getMapTypeIcon( info.version ), display, dst.x + SELECTED_SCENARIO_MAP_TYPE_OFFSET_X, dst.y + SELECTED_SCENARIO_GENERAL_OFFSET_Y );

const fheroes2::Text mapNameText{ info.name, fheroes2::FontType::normalWhite(), info.getSupportedLanguage() };
fheroes2::Text mapNameText{ info.name, fheroes2::FontType::normalWhite(), info.getSupportedLanguage() };
mapNameText.fitToOneRow( SCENARIO_LIST_MAP_NAME_WIDTH );
mapNameText.draw( GetCenteredTextXCoordinate( dst.x + SELECTED_SCENARIO_MAP_NAME_OFFSET_X, SELECTED_SCENARIO_MAP_NAME_WIDTH, mapNameText.width() ),
dst.y + SELECTED_SCENARIO_GENERAL_OFFSET_Y + 2, display );

Expand All @@ -310,9 +311,10 @@ void ScenarioListBox::_renderSelectedScenarioInfo( fheroes2::Display & display,

void ScenarioListBox::_renderMapName( const Maps::FileInfo & info, bool selected, const int32_t & baseYOffset, fheroes2::Display & display ) const
{
const fheroes2::Text mapName{ info.name,
{ fheroes2::FontSize::NORMAL, ( selected ? fheroes2::FontColor::YELLOW : fheroes2::FontColor::WHITE ) },
info.getSupportedLanguage() };
fheroes2::Text mapName{ info.name,
{ fheroes2::FontSize::NORMAL, ( selected ? fheroes2::FontColor::YELLOW : fheroes2::FontColor::WHITE ) },
info.getSupportedLanguage() };
mapName.fitToOneRow( SCENARIO_LIST_MAP_NAME_WIDTH );
const int32_t xCoordinate = GetCenteredTextXCoordinate( _offsetX + SCENARIO_LIST_MAP_NAME_OFFSET_X, SCENARIO_LIST_MAP_NAME_WIDTH, mapName.width() );
const int32_t yCoordinate = baseYOffset + MAP_LIST_ROW_SPACING_Y - 1;
mapName.draw( xCoordinate, yCoordinate, display );
Expand Down
18 changes: 13 additions & 5 deletions src/fheroes2/game/game_scenarioinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@ namespace
void RedrawMapTitle( const fheroes2::Rect & roi )
{
const auto & info = Settings::Get().getCurrentMapInfo();
const fheroes2::Text text{ info.name, fheroes2::FontType::normalWhite(), info.getSupportedLanguage() };
text.draw( roi.x, roi.y + 8, roi.width, fheroes2::Display::instance() );
fheroes2::Text text{ info.name, fheroes2::FontType::normalWhite(), info.getSupportedLanguage() };
text.fitToOneRow( roi.width );
text.draw( roi.x, roi.y + 3, roi.width, fheroes2::Display::instance() );
}

void RedrawDifficultyInfo( const fheroes2::Point & dst )
Expand Down Expand Up @@ -245,16 +246,23 @@ namespace
fheroes2::addGradientShadow( icon, display, { coordDifficulty[i].x, coordDifficulty[i].y }, { -5, 5 } );
}

// We calculate the allowed text width according to the select button's width while ensuring symmetric placement of the map title.
const int32_t boxBorder = 6;
const int32_t halfBoxTextAreaWidth = ( scenarioBoxRoi.width - ( 2 * boxBorder ) ) / 2;
const int32_t rightSideAvailableTextWidth
= halfBoxTextAreaWidth > buttonSelectWidth ? halfBoxTextAreaWidth - buttonSelectWidth : buttonSelectWidth - halfBoxTextAreaWidth;

// Set up restorers.
fheroes2::ImageRestorer mapTitleArea( display, scenarioBoxRoi.x + 6, scenarioBoxRoi.y + 5, 279, scenarioBoxRoi.height );
fheroes2::ImageRestorer mapTitleArea( display, scenarioBoxRoi.x + boxBorder + buttonSelectWidth, scenarioBoxRoi.y + 5, 2 * rightSideAvailableTextWidth,
scenarioBoxRoi.height );
fheroes2::ImageRestorer opponentsArea( display, roi.x, pointOpponentInfo.y, roi.width, 65 );
fheroes2::ImageRestorer classArea( display, roi.x, pointClassInfo.y, roi.width, 69 );
fheroes2::ImageRestorer handicapArea( display, roi.x, pointClassInfo.y + 69, roi.width, 31 );
fheroes2::ImageRestorer ratingArea( display, buttonOk.area().x + buttonOk.area().width, buttonOk.area().y,
roi.width - buttonOk.area().width - buttonCancel.area().width - 20 * 2, buttonOk.area().height );

// Map name
RedrawMapTitle( scenarioBoxRoi );
RedrawMapTitle( mapTitleArea.rect() );

playersInfo.RedrawInfo( false );

Expand Down Expand Up @@ -318,7 +326,7 @@ namespace
conf.setCurrentMapInfo( *fi );

mapTitleArea.restore();
RedrawMapTitle( scenarioBoxRoi );
RedrawMapTitle( mapTitleArea.rect() );
Game::LoadPlayers( fi->filename, players );

opponentsArea.restore();
Expand Down

0 comments on commit dddf722

Please sign in to comment.