From dd59978a058bed4c11b0843dad40b1a111850f01 Mon Sep 17 00:00:00 2001 From: Joao Marcos <40339986+JoaoMarcosCSilva@users.noreply.github.com> Date: Wed, 11 Jul 2018 20:18:54 -0300 Subject: [PATCH] Fixes problem when choosing the faction of an AI (#186) * Fixes problem when choosing the faction of an AI (fixes #184) --- AUTHORS.md | 1 + source/glest_game/facilities/components.h | 2 +- .../menu/menu_state_custom_game.cpp | 112 +++++++++++++++++- .../glest_game/menu/menu_state_custom_game.h | 16 ++- 4 files changed, 128 insertions(+), 3 deletions(-) diff --git a/AUTHORS.md b/AUTHORS.md index 2850ec5ba..2452ce0ec 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -10,6 +10,7 @@ which includes the _MegaGlest_ commit log. * [Andy Alt](https://github.com/andy5995) * [James Sherratt](https://github.com/Jammyjamjamman) +* [João Marcos](https://github.com/JoaoMarcosCSilva) ### php || mysql diff --git a/source/glest_game/facilities/components.h b/source/glest_game/facilities/components.h index a734b1d7f..cd094347f 100644 --- a/source/glest_game/facilities/components.h +++ b/source/glest_game/facilities/components.h @@ -370,7 +370,7 @@ namespace Glest { static const int defH; static const int defW; - private: + protected: GraphicButton graphButton1, graphButton2; vector items; vector translated_items; diff --git a/source/glest_game/menu/menu_state_custom_game.cpp b/source/glest_game/menu/menu_state_custom_game.cpp index 8e3d3509e..1b349044c 100644 --- a/source/glest_game/menu/menu_state_custom_game.cpp +++ b/source/glest_game/menu/menu_state_custom_game.cpp @@ -68,7 +68,116 @@ namespace Glest { s = formatString(s); } }; + // ===================================================== + // class GraphicListBoxFactions + // ===================================================== + GraphicListBoxFactions::GraphicListBoxFactions(const std::string &containerName, const std::string &objName) : GraphicListBox::GraphicListBox(containerName, objName) {} + + void GraphicListBoxFactions::addInformation(MenuStateCustomGame *menu, int index) { + this->menu = menu; + this->index = index; + } + bool GraphicListBoxFactions::mouseClick(int x, int y, string advanceToItemStartingWith) { + if (this->getVisible() == false) { + return false; + } + + if (!items.empty()) { + + bool b1 = graphButton1.mouseClick(x, y); + bool b2 = graphButton2.mouseClick(x, y); + + int superj = selectedItemIndex; + + if (b1) { + bool bFound = false; + if (advanceToItemStartingWith != "") { + for (int i = selectedItemIndex - 1; i >= 0; --i) { + string item = items[i]; + if ((int)translated_items.size() > i) item = translated_items[i]; + if (StartsWith(toLower(item), toLower(advanceToItemStartingWith)) == true) { + bFound = true; + selectedItemIndex = i; + break; + } + } + if (bFound == false) { + for (int i = (int)items.size() - 1; i >= selectedItemIndex; --i) { + string item = items[i]; + if ((int)translated_items.size() > i) item = translated_items[i]; + if (StartsWith(toLower(item), toLower(advanceToItemStartingWith)) == true) { + bFound = true; + selectedItemIndex = i; + break; + } + } + } + } + if (bFound == false) { + --selectedItemIndex %= items.size(); + + int type = 0; + std::string faction = ""; + if (menu != NULL) { + type = (menu->listBoxControls)[index].getSelectedItemIndex(); + faction = menu->factionFiles[getSelectedItemIndex()]; + } + if (menu != NULL + && faction == formatString(GameConstants::OBSERVER_SLOTNAME) + && (type == ctCpuEasy || type == ctCpu || type == ctCpuUltra || type == ctCpuZeta)) { + --selectedItemIndex %= items.size(); + } + + + } + + } + else if (b2) { + bool bFound = false; + if (advanceToItemStartingWith != "") { + for (int i = selectedItemIndex + 1; i < (int)items.size(); ++i) { + string item = items[i]; + if ((int)translated_items.size() > i) item = translated_items[i]; + if (StartsWith(toLower(item), toLower(advanceToItemStartingWith)) == true) { + bFound = true; + selectedItemIndex = i; + break; + } + } + if (bFound == false) { + for (int i = 0; i <= selectedItemIndex; ++i) { + string item = items[i]; + if ((int)translated_items.size() > i) item = translated_items[i]; + if (StartsWith(toLower(item), toLower(advanceToItemStartingWith)) == true) { + bFound = true; + selectedItemIndex = i; + break; + } + } + } + } + if (bFound == false) { + ++selectedItemIndex %= items.size(); + + int type = 0; + std::string faction = ""; + if (menu != NULL) { + type = (menu->listBoxControls)[index].getSelectedItemIndex(); + faction = menu->factionFiles[getSelectedItemIndex()]; + } + if (menu != NULL + && faction == formatString(GameConstants::OBSERVER_SLOTNAME) + && (type == ctCpuEasy || type == ctCpu || type == ctCpuUltra || type == ctCpuZeta)) { + ++selectedItemIndex %= items.size(); + } + } + } + setText(getSelectedItem()); + return b1 || b2; + } + return false; + } // ===================================================== // class MenuStateCustomGame // ===================================================== @@ -1979,7 +2088,7 @@ namespace Glest { } //printf("checkControTypeClicked = %d selectedControlItemIndex = %d i = %d\n",checkControTypeClicked,selectedControlItemIndex,i); - + listBoxFactions[i].addInformation(this, i); if (selectedControlItemIndex != ctHuman && checkControTypeClicked == true && listBoxControls[i].mouseClick(x, y)) { @@ -5795,6 +5904,7 @@ namespace Glest { //printf("DONE Setting scenario faction i = %d [ %s]\n",i,scenarioInfo.factionTypeNames[i].c_str()); // Disallow CPU players to be observers + listBoxFactions[i].addInformation(this, i); if (factionFiles [listBoxFactions[i].getSelectedItemIndex()] == formatString(GameConstants::OBSERVER_SLOTNAME) diff --git a/source/glest_game/menu/menu_state_custom_game.h b/source/glest_game/menu/menu_state_custom_game.h index 3fb829f96..f6aef3352 100644 --- a/source/glest_game/menu/menu_state_custom_game.h +++ b/source/glest_game/menu/menu_state_custom_game.h @@ -46,19 +46,33 @@ namespace Glest { class SwitchSetupRequest; class ServerInterface; class TechTree; + class MenuStateCustomGame; enum ParentMenuState { pNewGame, pMasterServer, pLanGame }; + // ===================================================== + // class GraphicListBoxFactions + // ===================================================== + class GraphicListBoxFactions : public GraphicListBox { + private: + MenuStateCustomGame * menu; + int index; + public: + GraphicListBoxFactions(const std::string &containerName = "", const std::string &objName = ""); + void addInformation(MenuStateCustomGame* m, int i); + bool mouseClick(int x, int y, string advanceToItemStartingWith); + }; // =============================== // class MenuStateCustomGame // =============================== class MenuStateCustomGame : public MenuState, public SimpleTaskCallbackInterface { + friend class GraphicListBoxFactions; private: GraphicButton buttonReturn; GraphicButton buttonPlayNow; @@ -93,7 +107,7 @@ namespace Glest { GraphicListBox listBoxControls[GameConstants::maxPlayers]; GraphicButton buttonBlockPlayers[GameConstants::maxPlayers]; GraphicListBox listBoxRMultiplier[GameConstants::maxPlayers]; - GraphicListBox listBoxFactions[GameConstants::maxPlayers]; + GraphicListBoxFactions listBoxFactions[GameConstants::maxPlayers]; GraphicListBox listBoxTeams[GameConstants::maxPlayers]; GraphicLabel labelNetStatus[GameConstants::maxPlayers]; MapInfo mapInfo;