2017-10-09 15:40:08 +02:00
# include "global.h"
2017-11-19 22:48:46 +01:00
# include "international_string_util.h"
2018-11-28 00:21:23 +01:00
# include "list_menu.h"
# include "pokedex.h"
# include "script_menu.h"
# include "string_util.h"
# include "strings.h"
# include "text.h"
# include "window.h"
2017-10-09 15:40:08 +02:00
2018-11-28 00:21:23 +01:00
extern const struct PokedexEntry gPokedexEntries [ ] ;
2017-10-09 15:40:08 +02:00
2018-11-28 00:21:23 +01:00
int GetStringCenterAlignXOffset ( int fontId , const u8 * str , int totalWidth )
2017-10-09 15:40:08 +02:00
{
return GetStringCenterAlignXOffsetWithLetterSpacing ( fontId , str , totalWidth , 0 ) ;
}
2018-11-28 00:21:23 +01:00
int GetStringRightAlignXOffset ( int fontId , const u8 * str , int totalWidth )
2017-10-09 15:40:08 +02:00
{
return GetStringWidthDifference ( fontId , str , totalWidth , 0 ) ;
}
2018-11-28 00:21:23 +01:00
int GetStringCenterAlignXOffsetWithLetterSpacing ( int fontId , const u8 * str , int totalWidth , int letterSpacing )
2017-10-09 15:40:08 +02:00
{
return GetStringWidthDifference ( fontId , str , totalWidth , letterSpacing ) / 2 ;
}
2018-11-28 00:21:23 +01:00
int GetStringWidthDifference ( int fontId , const u8 * str , int totalWidth , int letterSpacing )
2017-10-09 15:40:08 +02:00
{
2018-11-28 00:21:23 +01:00
int stringWidth = GetStringWidth ( fontId , str , letterSpacing ) ;
2017-10-09 15:40:08 +02:00
if ( totalWidth > stringWidth )
return totalWidth - stringWidth ;
else
return 0 ;
}
2021-10-18 03:51:11 +02:00
int GetMaxWidthInMenuTable ( const struct MenuAction * actions , int numActions )
2017-10-09 15:40:08 +02:00
{
2021-10-18 03:51:11 +02:00
int i , maxWidth ;
2017-10-09 15:40:08 +02:00
2021-10-18 03:51:11 +02:00
for ( maxWidth = 0 , i = 0 ; i < numActions ; i + + )
2017-10-09 15:40:08 +02:00
{
2021-10-30 22:47:37 +02:00
int stringWidth = GetStringWidth ( FONT_NORMAL , actions [ i ] . text , 0 ) ;
2021-10-18 03:51:11 +02:00
if ( stringWidth > maxWidth )
maxWidth = stringWidth ;
2017-10-09 15:40:08 +02:00
}
2021-10-18 03:51:11 +02:00
return ConvertPixelWidthToTileWidth ( maxWidth ) ;
2017-10-09 15:40:08 +02:00
}
2022-07-29 16:17:58 +02:00
int GetMaxWidthInSubsetOfMenuTable ( const struct MenuAction * actions , const u8 * actionIds , int numActions )
2017-10-09 15:40:08 +02:00
{
2021-10-18 03:51:11 +02:00
int i , maxWidth ;
2017-10-09 15:40:08 +02:00
2021-10-18 03:51:11 +02:00
for ( maxWidth = 0 , i = 0 ; i < numActions ; i + + )
2017-10-09 15:40:08 +02:00
{
2021-10-30 22:47:37 +02:00
int stringWidth = GetStringWidth ( FONT_NORMAL , actions [ actionIds [ i ] ] . text , 0 ) ;
2021-10-18 03:51:11 +02:00
if ( stringWidth > maxWidth )
maxWidth = stringWidth ;
2017-10-09 15:40:08 +02:00
}
2021-10-18 03:51:11 +02:00
return ConvertPixelWidthToTileWidth ( maxWidth ) ;
2017-10-09 15:40:08 +02:00
}
2018-11-28 00:21:23 +01:00
2019-04-01 00:59:52 +02:00
int Intl_GetListMenuWidth ( const struct ListMenuTemplate * listMenu )
2018-11-28 00:21:23 +01:00
{
int i , maxWidth , finalWidth ;
const struct ListMenuItem * items = listMenu - > items ;
maxWidth = 0 ;
for ( i = 0 ; i < listMenu - > totalItems ; i + + )
{
int width = GetStringWidth ( listMenu - > fontId , items [ i ] . name , 0 ) ;
if ( width > maxWidth )
maxWidth = width ;
}
finalWidth = maxWidth + listMenu - > item_X + 9 ;
if ( finalWidth < 0 )
finalWidth + = 7 ;
finalWidth > > = 3 ;
if ( finalWidth > 28 )
finalWidth = 28 ;
return finalWidth ;
}
void CopyMonCategoryText ( int dexNum , u8 * dest )
{
u8 * str = StringCopy ( dest , gPokedexEntries [ dexNum ] . categoryName ) ;
* str = CHAR_SPACE ;
StringCopy ( str + 1 , gText_Pokemon ) ;
}
2021-10-18 03:51:11 +02:00
u8 * GetStringClearToWidth ( u8 * dest , int fontId , const u8 * str , int totalStringWidth )
2018-11-28 00:21:23 +01:00
{
u8 * buffer ;
int width ;
int clearWidth ;
2021-10-18 03:51:11 +02:00
if ( str )
2018-11-28 00:21:23 +01:00
{
2021-10-18 03:51:11 +02:00
buffer = StringCopy ( dest , str ) ;
width = GetStringWidth ( fontId , str , 0 ) ;
2018-11-28 00:21:23 +01:00
}
else
{
2021-10-18 03:51:11 +02:00
buffer = dest ;
2018-11-28 00:21:23 +01:00
width = 0 ;
}
clearWidth = totalStringWidth - width ;
if ( clearWidth > 0 )
{
2021-10-18 03:51:11 +02:00
* ( buffer + + ) = EXT_CTRL_CODE_BEGIN ;
* ( buffer + + ) = EXT_CTRL_CODE_CLEAR ;
* ( buffer + + ) = clearWidth ;
2018-11-28 00:21:23 +01:00
* buffer = EOS ;
}
return buffer ;
}
void PadNameString ( u8 * dest , u8 padChar )
{
u8 length ;
StripExtCtrlCodes ( dest ) ;
length = StringLength ( dest ) ;
if ( padChar = = EXT_CTRL_CODE_BEGIN )
{
while ( length < PLAYER_NAME_LENGTH - 1 )
{
dest [ length ] = EXT_CTRL_CODE_BEGIN ;
2022-11-24 03:13:00 +01:00
dest [ length + 1 ] = EXT_CTRL_CODE_RESET_FONT ;
2018-11-28 00:21:23 +01:00
length + = 2 ;
}
}
else
{
while ( length < PLAYER_NAME_LENGTH - 1 )
{
dest [ length ] = padChar ;
length + + ;
}
}
dest [ length ] = EOS ;
}
2021-02-20 21:15:38 +01:00
void ConvertInternationalPlayerName ( u8 * str )
2018-11-28 00:21:23 +01:00
{
if ( StringLength ( str ) < PLAYER_NAME_LENGTH - 1 )
ConvertInternationalString ( str , LANGUAGE_JAPANESE ) ;
else
StripExtCtrlCodes ( str ) ;
}
2021-02-20 21:15:38 +01:00
void ConvertInternationalPlayerNameStripChar ( u8 * str , u8 removeChar )
2018-11-28 00:21:23 +01:00
{
u8 * buffer ;
if ( StringLength ( str ) < PLAYER_NAME_LENGTH - 1 )
{
ConvertInternationalString ( str , LANGUAGE_JAPANESE ) ;
}
2021-02-20 21:15:38 +01:00
else if ( removeChar = = EXT_CTRL_CODE_BEGIN )
2018-11-28 00:21:23 +01:00
{
StripExtCtrlCodes ( str ) ;
}
else
{
buffer = str ;
while ( buffer [ 1 ] ! = EOS )
buffer + + ;
2021-02-20 21:15:38 +01:00
while ( buffer > = str & & buffer [ 0 ] = = removeChar )
2018-11-28 00:21:23 +01:00
{
buffer [ 0 ] = EOS ;
buffer - - ;
}
}
}
2021-04-22 20:30:45 +02:00
void ConvertInternationalContestantName ( u8 * str )
2018-11-28 00:21:23 +01:00
{
if ( * str + + = = EXT_CTRL_CODE_BEGIN & & * str + + = = EXT_CTRL_CODE_JPN )
{
while ( * str ! = EOS )
{
if ( str [ 0 ] = = EXT_CTRL_CODE_BEGIN & & str [ 1 ] = = EXT_CTRL_CODE_ENG )
return ;
str + + ;
}
* str + + = EXT_CTRL_CODE_BEGIN ;
* str + + = EXT_CTRL_CODE_ENG ;
* str = EOS ;
}
}
void TVShowConvertInternationalString ( u8 * dest , const u8 * src , int language )
{
StringCopy ( dest , src ) ;
ConvertInternationalString ( dest , language ) ;
}
2022-07-26 15:39:29 +02:00
// It's impossible to distinguish between Latin languages just from a string alone, so the function defaults to LANGUAGE_ENGLISH. This is the case in all of the versions of the game.
2021-10-18 03:51:11 +02:00
int GetNicknameLanguage ( u8 * str )
2018-11-28 00:21:23 +01:00
{
if ( str [ 0 ] = = EXT_CTRL_CODE_BEGIN & & str [ 1 ] = = EXT_CTRL_CODE_JPN )
return LANGUAGE_JAPANESE ;
else
2022-07-23 12:23:04 +02:00
return LANGUAGE_ENGLISH ;
2018-11-28 00:21:23 +01:00
}
2021-11-01 23:06:15 +01:00
// Used by Pokénav's Match Call to erase the previous trainer's flavor text when switching between their info pages.
void FillWindowTilesByRow ( int windowId , int columnStart , int rowStart , int numFillTiles , int numRows )
2018-11-28 00:21:23 +01:00
{
u8 * windowTileData ;
2021-11-01 23:06:15 +01:00
int fillSize , windowRowSize , i ;
2018-11-28 00:21:23 +01:00
struct Window * window = & gWindows [ windowId ] ;
fillSize = numFillTiles * TILE_SIZE_4BPP ;
windowRowSize = window - > window . width * TILE_SIZE_4BPP ;
windowTileData = window - > tileData + ( rowStart * windowRowSize ) + ( columnStart * TILE_SIZE_4BPP ) ;
if ( numRows > 0 )
{
2021-11-01 23:06:15 +01:00
for ( i = numRows ; i ! = 0 ; i - - )
2018-11-28 00:21:23 +01:00
{
CpuFastFill8 ( 0x11 , windowTileData , fillSize ) ;
windowTileData + = windowRowSize ;
}
}
}