moved word wrap function from Vertical Scroller into utils.
moved MTG specific functions out of utils.cpp into AllAbilities added word wrapping to descriptions while viewing deck information.
This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
#include "utils.h"
|
||||
#include "MTGDefinitions.h"
|
||||
#include "Subtypes.h"
|
||||
#include "WResourceManager.h"
|
||||
#include "WFont.h"
|
||||
|
||||
using std::vector;
|
||||
|
||||
@@ -236,80 +238,55 @@ std::vector<std::string> split(const std::string &s, char delim)
|
||||
return split(s, delim, elems);
|
||||
}
|
||||
|
||||
std::string wordWrap(std::string sentence, int width)
|
||||
// This is a customized word wrap based on pixel width. It tries it's best
|
||||
// to wrap strings using spaces as delimiters.
|
||||
// Not sure how this translates into non-english fonts.
|
||||
std::string wordWrap(std::string sentence, float width, int fontId)
|
||||
{
|
||||
std::string::iterator it = sentence.begin();
|
||||
|
||||
//remember how long next word is
|
||||
int nextWordLength = 0;
|
||||
int distanceFromWidth = width;
|
||||
|
||||
while (it != sentence.end())
|
||||
WFont * mFont = resources.GetWFont(fontId);
|
||||
float lineWidth = mFont->GetStringWidth( sentence.c_str() );
|
||||
string retVal = sentence;
|
||||
if ( lineWidth < width ) return sentence;
|
||||
|
||||
int numLines = 1;
|
||||
int breakIdx = 0;
|
||||
for( size_t idx = 0; idx < sentence.length(); idx ++ )
|
||||
{
|
||||
while (*it != ' ')
|
||||
{
|
||||
nextWordLength++;
|
||||
distanceFromWidth--;
|
||||
|
||||
++it;
|
||||
|
||||
// check if done
|
||||
if (it == sentence.end())
|
||||
{
|
||||
return sentence;
|
||||
}
|
||||
}
|
||||
|
||||
if (nextWordLength > distanceFromWidth)
|
||||
{
|
||||
*it = '\n';
|
||||
distanceFromWidth = width;
|
||||
nextWordLength = 0;
|
||||
}
|
||||
|
||||
//skip the space
|
||||
++it;
|
||||
if ( sentence[idx] == ' ' )
|
||||
{
|
||||
string currentSentence = sentence.substr(breakIdx, idx - breakIdx);
|
||||
float stringLength = mFont->GetStringWidth( currentSentence.c_str() );
|
||||
if (stringLength >= width)
|
||||
{
|
||||
if ( stringLength > width )
|
||||
{
|
||||
while ( sentence[idx-1] != ' ' )
|
||||
idx--;
|
||||
}
|
||||
retVal[idx-1] = '\n';
|
||||
breakIdx = idx;
|
||||
numLines++;
|
||||
}
|
||||
}
|
||||
else if ( sentence[idx] == '\n' )
|
||||
{
|
||||
string currentSentence = sentence.substr(breakIdx, idx - breakIdx);
|
||||
float stringLength = mFont->GetStringWidth( currentSentence.c_str() );
|
||||
if (stringLength >= width)
|
||||
{
|
||||
if ( stringLength > width )
|
||||
{
|
||||
while ( sentence[idx-1] != ' ' )
|
||||
idx--;
|
||||
retVal[idx-1] = '\n';
|
||||
}
|
||||
numLines++;
|
||||
}
|
||||
breakIdx = idx;
|
||||
numLines++;
|
||||
}
|
||||
}
|
||||
|
||||
return sentence;
|
||||
return retVal;
|
||||
}
|
||||
|
||||
// Given a delimited string of abilities, add the ones to the list that are "Basic" MTG abilities
|
||||
void PopulateAbilityIndexVector( list<int>& abilities, const string& abilityStringList, char delimiter )
|
||||
{
|
||||
vector<string> abilitiesList = split( abilityStringList, delimiter);
|
||||
for ( vector<string>::iterator iter = abilitiesList.begin(); iter != abilitiesList.end(); ++iter)
|
||||
{
|
||||
int abilityIndex = Constants::GetBasicAbilityIndex( *iter );
|
||||
|
||||
if (abilityIndex != -1)
|
||||
abilities.push_back( abilityIndex );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void PopulateColorIndexVector( list<int>& colors, const string& colorStringList, char delimiter )
|
||||
{
|
||||
vector<string> abilitiesList = split( colorStringList, delimiter);
|
||||
for ( vector<string>::iterator iter = abilitiesList.begin(); iter != abilitiesList.end(); ++iter)
|
||||
{
|
||||
for (int colorIndex = Constants::MTG_COLOR_ARTIFACT; colorIndex < Constants::MTG_NB_COLORS; ++colorIndex)
|
||||
{
|
||||
// if the text is not a basic ability but contains a valid color add it to the color vector
|
||||
if ( (Constants::GetBasicAbilityIndex( *iter ) != -1) && ((*iter).find( Constants::MTGColorStrings[ colorIndex ] ) != string::npos) )
|
||||
colors.push_back(colorIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PopulateSubtypesIndexVector( list<int>& types, const string& subTypesStringList, char delimiter)
|
||||
{
|
||||
vector<string> subTypesList = split( subTypesStringList, delimiter);
|
||||
for (vector<string>::iterator it = subTypesList.begin(); it != subTypesList.end(); ++it)
|
||||
{
|
||||
string subtype = *it;
|
||||
size_t id = Subtypes::subtypesList->find( subtype );
|
||||
if ( id != string::npos )
|
||||
types.push_back(id);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user