diff --git a/projects/mtg/include/AllAbilities.h b/projects/mtg/include/AllAbilities.h index 17b759309..4547ff335 100644 --- a/projects/mtg/include/AllAbilities.h +++ b/projects/mtg/include/AllAbilities.h @@ -5138,4 +5138,12 @@ public: } }; +// utility functions + +void PopulateColorIndexVector( list& colors, const string& colorsString, char delimiter = ','); +void PopulateAbilityIndexVector( list& abilities, const string& abilitiesString, char delimiter = ','); +void PopulateSubtypesIndexVector( list& subtypes, const string& subtypesString, char delimiter = ' '); + + + #endif diff --git a/projects/mtg/include/TextScroller.h b/projects/mtg/include/TextScroller.h index 5e55f4758..6305d6598 100644 --- a/projects/mtg/include/TextScroller.h +++ b/projects/mtg/include/TextScroller.h @@ -39,7 +39,6 @@ class VerticalTextScroller: { private: size_t mNbItemsShown; - bool mScrollerInitialized; float mHeight; // maximum height availble for display float mMarginX; float mMarginY; // margin used to allow text to scroll off screen without @@ -47,9 +46,6 @@ private: // for at least one line of text ( mY - line height of current font ) float mOriginalY; // mY initially, used to restore scroller to original position after update -protected: - string wordWrap(string sentence, float width); - public: VerticalTextScroller(int fontId, float x, float y, float width, float height, float scrollSpeed = 30, size_t _minimumItems = 1); void Render(); diff --git a/projects/mtg/include/utils.h b/projects/mtg/include/utils.h index 2dea3b0c9..b862750ec 100644 --- a/projects/mtg/include/utils.h +++ b/projects/mtg/include/utils.h @@ -43,12 +43,7 @@ std::string join(vector &v, string delim = " "); std::vector &split(const std::string &s, char delim, std::vector &elems); std::vector split(const std::string &s, char delim); //splits a string with "delim" and returns a vector of strings. -std::string wordWrap(std::string s, int width); - -void PopulateColorIndexVector( list& colors, const string& colorsString, char delimiter = ','); -void PopulateAbilityIndexVector( list& abilities, const string& abilitiesString, char delimiter = ','); -void PopulateSubtypesIndexVector( list& subtypes, const string& subtypesString, char delimiter = ' '); - +std::string wordWrap(std::string s, float width, int fontId); int loadRandValues(string s); int filesize(const char * filename); diff --git a/projects/mtg/src/AllAbilities.cpp b/projects/mtg/src/AllAbilities.cpp index 1fc8fabff..84a85a45a 100644 --- a/projects/mtg/src/AllAbilities.cpp +++ b/projects/mtg/src/AllAbilities.cpp @@ -1991,3 +1991,44 @@ AUpkeep::~AUpkeep() } +// utility functions + +// Given a delimited string of abilities, add the ones to the list that are "Basic" MTG abilities +void PopulateAbilityIndexVector( list& abilities, const string& abilityStringList, char delimiter ) +{ + vector abilitiesList = split( abilityStringList, delimiter); + for ( vector::iterator iter = abilitiesList.begin(); iter != abilitiesList.end(); ++iter) + { + int abilityIndex = Constants::GetBasicAbilityIndex( *iter ); + + if (abilityIndex != -1) + abilities.push_back( abilityIndex ); + } +} + + +void PopulateColorIndexVector( list& colors, const string& colorStringList, char delimiter ) +{ + vector abilitiesList = split( colorStringList, delimiter); + for ( vector::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& types, const string& subTypesStringList, char delimiter) +{ + vector subTypesList = split( subTypesStringList, delimiter); + for (vector::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); + } +} diff --git a/projects/mtg/src/DeckMenu.cpp b/projects/mtg/src/DeckMenu.cpp index 8d8edc06c..ca4bb249d 100644 --- a/projects/mtg/src/DeckMenu.cpp +++ b/projects/mtg/src/DeckMenu.cpp @@ -49,7 +49,7 @@ JGuiController(id, listener), fontId(fontId), mShowDetailsScreen( showDetailsOve descX = 260 + kDescriptionVerticalBoxPadding; descY = 100 + kDescriptionHorizontalBoxPadding; descHeight = 145; - descWidth = 220; + descWidth = 200; detailedInfoBoxX = 400; detailedInfoBoxY = 235; @@ -211,7 +211,7 @@ void DeckMenu::Render() if (quad) renderer->RenderQuad(quad, avatarX, avatarY); } // fill in the description part of the screen - string text = currentMenuItem->desc; + string text = wordWrap(currentMenuItem->desc, descWidth, mainFont->mFontID ); mainFont->DrawString(text.c_str(), descX, descY); mFont->SetColor(ARGB(255,255,255,255)); diff --git a/projects/mtg/src/GameStateDuel.cpp b/projects/mtg/src/GameStateDuel.cpp index 6e7d6bc7c..3361918c3 100644 --- a/projects/mtg/src/GameStateDuel.cpp +++ b/projects/mtg/src/GameStateDuel.cpp @@ -275,27 +275,21 @@ bool GameStateDuel::MusicExist(string FileName) void GameStateDuel::ensureOpponentMenu() { - if (opponentMenu == NULL) - { - opponentMenu = NEW DeckMenu(DUEL_MENU_CHOOSE_OPPONENT, this, Fonts::OPTION_FONT, "Choose Opponent", - GameStateDuel::selectedAIDeckId, true); - opponentMenu->Add(MENUITEM_RANDOM_AI, "Random"); - if (options[Options::EVILTWIN_MODE_UNLOCKED].number) opponentMenu->Add(MENUITEM_EVIL_TWIN, "Evil Twin", _( - "Can you play against yourself?").c_str()); - DeckManager * deckManager = DeckManager::GetInstance(); - vector opponentDeckList; - if(!options[Options::CHEATMODEAIDECK].number) - { - opponentDeckList = fillDeckMenu(opponentMenu, JGE_GET_RES("ai/baka"), "ai_baka", mPlayers[0], options[Options::AIDECKS_UNLOCKED].number); - } - else - { - opponentDeckList = fillDeckMenu(opponentMenu, JGE_GET_RES("ai/baka"), "ai_baka", mPlayers[0],1000); - } - deckManager->updateMetaDataList(&opponentDeckList, true); - opponentMenu->Add(MENUITEM_CANCEL, "Cancel", _("Choose a different player deck").c_str()); - opponentDeckList.clear(); - } + if (opponentMenu == NULL) + { + opponentMenu = NEW DeckMenu(DUEL_MENU_CHOOSE_OPPONENT, this, Fonts::OPTION_FONT, "Choose Opponent", + GameStateDuel::selectedAIDeckId, true); + opponentMenu->Add(MENUITEM_RANDOM_AI, "Random"); + if (options[Options::EVILTWIN_MODE_UNLOCKED].number) opponentMenu->Add(MENUITEM_EVIL_TWIN, "Evil Twin", + _("Can you defeat yourself?").c_str()); + DeckManager * deckManager = DeckManager::GetInstance(); + vector opponentDeckList; + int nbUnlockedDecks = options[Options::CHEATMODEAIDECK].number ? 1000 : options[Options::AIDECKS_UNLOCKED].number; + opponentDeckList = fillDeckMenu(opponentMenu, JGE_GET_RES("ai/baka"), "ai_baka", mPlayers[0], nbUnlockedDecks); + deckManager->updateMetaDataList(&opponentDeckList, true); + opponentMenu->Add(MENUITEM_CANCEL, "Cancel", _("Choose a different player deck").c_str()); + opponentDeckList.clear(); + } } void GameStateDuel::Update(float dt) diff --git a/projects/mtg/src/TextScroller.cpp b/projects/mtg/src/TextScroller.cpp index 8a22e7c89..dd7880c91 100644 --- a/projects/mtg/src/TextScroller.cpp +++ b/projects/mtg/src/TextScroller.cpp @@ -96,7 +96,6 @@ TextScroller( fontId, x, y, width, scrollSpeed) mHeight = height; mNbItemsShown = numItemsShown; mMarginX = 0; - mScrollerInitialized = false; timer=0; WFont *mFont = resources.GetWFont(fontId); mOriginalY = mY; @@ -109,7 +108,7 @@ TextScroller( fontId, x, y, width, scrollSpeed) void VerticalTextScroller::Add( string text ) { strings.push_back( text ); - string wrappedText = wordWrap(text, mWidth); + string wrappedText = wordWrap(text, mWidth, fontId); mText.append(wrappedText); } @@ -147,48 +146,4 @@ void VerticalTextScroller::Render() { WFont * mFont = resources.GetWFont(fontId); mFont->DrawString(mText.c_str(), mX, mY); -} - - - -// 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 VerticalTextScroller::wordWrap(std::string sentence, float width) -{ - 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 ++ ) - { - 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' ) - { - numLines++; - breakIdx = idx; - } - } - if ( numLines * mFont->GetHeight() > mHeight ) - mScrollerInitialized = true; - - return retVal; -} +} \ No newline at end of file diff --git a/projects/mtg/src/utils.cpp b/projects/mtg/src/utils.cpp index a75f6013b..9f3f466d3 100644 --- a/projects/mtg/src/utils.cpp +++ b/projects/mtg/src/utils.cpp @@ -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 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& abilities, const string& abilityStringList, char delimiter ) -{ - vector abilitiesList = split( abilityStringList, delimiter); - for ( vector::iterator iter = abilitiesList.begin(); iter != abilitiesList.end(); ++iter) - { - int abilityIndex = Constants::GetBasicAbilityIndex( *iter ); - - if (abilityIndex != -1) - abilities.push_back( abilityIndex ); - } -} - - -void PopulateColorIndexVector( list& colors, const string& colorStringList, char delimiter ) -{ - vector abilitiesList = split( colorStringList, delimiter); - for ( vector::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& types, const string& subTypesStringList, char delimiter) -{ - vector subTypesList = split( subTypesStringList, delimiter); - for (vector::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); - } -}