diff --git a/CMakeLists.txt b/CMakeLists.txt index 812123e..7b08430 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,7 +9,7 @@ cmake_policy(SET CMP0015 NEW) set(STATES activeGameState.cpp inGameAppState.cpp guiAppState.cpp mapEditorAppState.cpp) set(UTIL util.cpp binds.h) -set(GUI tooltip.cpp optionsButton.cpp exitButton.cpp consoleCommand.cpp) +set(GUI tooltip.cpp mapEditorButton.cpp optionsButton.cpp exitButton.cpp consoleCommand.cpp) set(CONTROLLERS unitFrameController.cpp cameraController.cpp) set(CORE gameManager.cpp defConfigs.cpp ${CONTROLLERS}) set(PROJECTILES projectile.cpp) diff --git a/mapEditorAppState.cpp b/mapEditorAppState.cpp index 1c3847e..d83c2ab 100644 --- a/mapEditorAppState.cpp +++ b/mapEditorAppState.cpp @@ -44,13 +44,15 @@ namespace battleship{ ufCtr->setPlacingFrames(true); } - MapEditorAppState::MapEditor::MapEditor(string name, Vector2 size){ + MapEditorAppState::MapEditor::MapEditor(string name, Vector2 size, bool newMap){ map = Map::getSingleton(); - map->load(name, true); + map->load(name, newMap); - map->addPlayer(new Player(0, 0, 0)); + if(newMap){ + map->addPlayer(new Player(0, 0, 0)); + generatePlane(size); + } - generatePlane(size); prepareGui(); string basePath = GameManager::getSingleton()->getPath(); @@ -322,13 +324,14 @@ namespace battleship{ } } - MapEditorAppState::MapEditorAppState(string name, Vector2 size) : AbstractAppState( + MapEditorAppState::MapEditorAppState(string name, Vector2 size, bool newMap) : AbstractAppState( AppStateType::MAP_EDITOR, configData::calcSumBinds(AppStateType::MAP_EDITOR, true), configData::calcSumBinds(AppStateType::MAP_EDITOR, false), GameManager::getSingleton()->getPath() + "Scripts/options.lua"){ mapName = name; mapSize = size; + this->newMap = newMap; } void MapEditorAppState::update(){ @@ -347,7 +350,7 @@ namespace battleship{ void MapEditorAppState::onAttached(){ AbstractAppState::onAttached(); - mapEditor = new MapEditor(mapName, mapSize); + mapEditor = new MapEditor(mapName, mapSize, newMap); Root *root = Root::getSingleton(); Material *mat = new Material(root->getLibPath() + "text"); diff --git a/mapEditorAppState.h b/mapEditorAppState.h index 75bcbe5..f604eb6 100644 --- a/mapEditorAppState.h +++ b/mapEditorAppState.h @@ -35,7 +35,7 @@ namespace battleship{ Z_AXIS }; - MapEditor(std::string, vb01::Vector2); + MapEditor(std::string, vb01::Vector2, bool); void updateCircleRadius(bool); void pushLandmassVerts(float); void castSelectionRay(); @@ -70,7 +70,7 @@ namespace battleship{ UnitListbox *vehicleListbox = nullptr, *structureListbox = nullptr; }; - MapEditorAppState(std::string, vb01::Vector2); + MapEditorAppState(std::string, vb01::Vector2, bool); void update(); void onAttached(); void onDettached(); @@ -81,6 +81,7 @@ namespace battleship{ MapEditor *mapEditor = nullptr; std::string mapName; vb01::Vector2 mapSize; + bool newMap; vb01::Text *radiusText = nullptr; }; } diff --git a/mapEditorButton.cpp b/mapEditorButton.cpp new file mode 100644 index 0000000..d7b2f3d --- /dev/null +++ b/mapEditorButton.cpp @@ -0,0 +1,124 @@ +#include "mapEditorButton.h" +#include "guiAppState.h" +#include "inGameAppState.h" +#include "mapEditorAppState.h" + +#include + +namespace battleship{ + using namespace std; + using namespace vb01; + using namespace vb01Gui; + using namespace gameBase; + + void MapEditorButton::onClick(){ + class NewMapButton : public Button{ + public: + NewMapButton(Vector2 pos, Vector2 size) : Button(pos, size, "New map", GameManager::getSingleton()->getPath() + "Fonts/batang.ttf"){} + void onClick(){ + class OkButton : public Button{ + public: + OkButton(Vector2 pos, Vector2 size, Textbox *name, Textbox *sx, Textbox *sy) : Button(pos, size, "Ok", GameManager::getSingleton()->getPath() + "Fonts/batang.ttf", -1, true){ + this->name = name; + this->sizeX = sx; + this->sizeY = sy; + } + void onClick(){ + GameManager *gm = GameManager::getSingleton(); + StateManager *stateManager = gm->getStateManager(); + GuiAppState *state = (GuiAppState*)stateManager->getAppStateByType((int)AppStateType::GUI_STATE); + + state->removeAllTextboxes(); + state->removeAllButtons(vector{this}); + + Vector2 size = Vector2( + atof(wstringToString(sizeX->getText()).c_str()), + atof(wstringToString(sizeY->getText()).c_str()) + ); + stateManager->attachAppState(new MapEditorAppState(wstringToString(name->getText()), size, true)); + + state->removeButton(this); + } + private: + Textbox *name, *sizeX, *sizeY; + }; + + GameManager *gm = GameManager::getSingleton(); + StateManager *stateManager = gm->getStateManager(); + GuiAppState *state = (GuiAppState*)stateManager->getAppStateByType((int)AppStateType::GUI_STATE); + state->removeAllButtons(vector{this}); + + Vector2 size = Vector2(140, 20); + + string fontPath = gm->getPath() + "Fonts/batang.ttf"; + Textbox *name = new Textbox(Vector2(100, 150), size, fontPath); + Textbox *sizeX = new Textbox(Vector2(100, 150 + (size.y + 10)), size, fontPath); + Textbox *sizeY = new Textbox(Vector2(100, 150 + 2 * (size.y + 10)), size, fontPath); + state->addTextbox(name); + state->addTextbox(sizeX); + state->addTextbox(sizeY); + + state->addButton(new OkButton(Vector2(200, gm->getHeight() - 150), Vector2(140, 50), name, sizeX, sizeY)); + + state->removeButton(this); + } + private: + }; + + class LoadMapButton : public Button{ + public: + LoadMapButton(Vector2 pos, Vector2 size) : Button(pos, size, "Load map", GameManager::getSingleton()->getPath() + "Fonts/batang.ttf"){} + void onClick(){ + class OkButton : public Button{ + public: + OkButton(Vector2 pos, Vector2 size, Listbox *listbox) : Button(pos, size, "Ok", GameManager::getSingleton()->getPath() + "Fonts/batang.ttf"){ + this->listbox = listbox; + } + + void onClick(){ + string name = wstringToString(listbox->getContents()[listbox->getSelectedOption()]); + + StateManager *sm = GameManager::getSingleton()->getStateManager(); + GuiAppState *guiState = (GuiAppState*)sm->getAppStateByType((int)AppStateType::GUI_STATE); + guiState->removeAllListboxes(); + guiState->removeAllButtons(vector{this}); + + sm->attachAppState(new MapEditorAppState(name, Vector2::VEC_ZERO, false)); + + guiState->removeButton(this); + } + private: + Listbox *listbox; + }; + + GameManager *gm = GameManager::getSingleton(); + StateManager *sm = gm->getStateManager(); + GuiAppState *guiState = (GuiAppState*)sm->getAppStateByType((int)AppStateType::GUI_STATE); + guiState->removeAllButtons(vector{this}); + + string basePath = gm->getPath(); + vector maps = readDir(basePath + "Models/Maps/", true); + int displayLimit = 2; + Listbox *mapsListbox = new Listbox(Vector2(100, 150), Vector2(140, 20), maps, (maps.size() > displayLimit ? maps.size() : displayLimit), basePath + "Fonts/batang.ttf"); + guiState->addListbox(mapsListbox); + guiState->addButton(new OkButton(Vector2(100, 350), Vector2(100, 20), mapsListbox)); + + guiState->removeButton(this); + } + private: + }; + + GameManager *gm = GameManager::getSingleton(); + GuiAppState *guiState = (GuiAppState*)gm->getStateManager()->getAppStateByType((int)AppStateType::GUI_STATE); + guiState->removeAllButtons(vector{this}); + + int width = gm->getWidth(), height = gm->getHeight(); + Vector2 size = Vector2(150, 40); + + NewMapButton *newMapButton = new NewMapButton(Vector2(width / 16, height / 12), size); + LoadMapButton *loadMapButton = new LoadMapButton(Vector2(width / 16, height / 12 * 2), size); + guiState->addButton(newMapButton); + guiState->addButton(loadMapButton); + guiState->removeButton(this); + } +} diff --git a/mapEditorButton.h b/mapEditorButton.h new file mode 100644 index 0000000..5f34227 --- /dev/null +++ b/mapEditorButton.h @@ -0,0 +1,17 @@ +#ifndef MAP_EDITOR_BUTTON_H +#define MAP_EDITOR_BUTTON_H + +#include + +#include "gameManager.h" + +namespace battleship{ + class MapEditorButton : public vb01Gui::Button{ + public: + MapEditorButton(vb01::Vector2 pos, vb01::Vector2 size) : vb01Gui::Button(pos, size, "Map editor", GameManager::getSingleton()->getPath() + "Fonts/batang.ttf"){} + void onClick(); + private: + }; +} + +#endif diff --git a/util.cpp b/util.cpp index 0dfeab1..e5c263f 100755 --- a/util.cpp +++ b/util.cpp @@ -18,7 +18,7 @@ #include "gameManager.h" #include "guiAppState.h" #include "inGameAppState.h" -#include "mapEditorAppState.h" +#include "mapEditorButton.h" using namespace std; using namespace glm; @@ -205,72 +205,6 @@ namespace battleship{ }; }; - class NewMapButton : public Button{ - public: - NewMapButton(Vector2 pos, Vector2 size) : Button(pos, size, "New map", GameManager::getSingleton()->getPath() + "Fonts/batang.ttf", -1, true){} - void onClick(){ - class OkButton : public Button{ - public: - OkButton(Vector2 pos, Vector2 size, Textbox *name, Textbox *sx, Textbox *sy) : Button(pos, size, "Ok", GameManager::getSingleton()->getPath() + "Fonts/batang.ttf", -1, true){ - this->name = name; - this->sizeX = sx; - this->sizeY = sy; - } - void onClick(){ - GameManager *gm = GameManager::getSingleton(); - StateManager *stateManager = gm->getStateManager(); - GuiAppState *state = (GuiAppState*)stateManager->getAppStateByType((int)AppStateType::GUI_STATE); - - state->removeAllTextboxes(); - state->removeAllButtons(vector{this}); - - stateManager->attachAppState(new MapEditorAppState( - wstringToString(name->getText()), - Vector2( - atof(wstringToString(sizeX->getText()).c_str()), - atof(wstringToString(sizeY->getText()).c_str())) - ) - ); - - state->removeButton(this); - } - private: - Textbox *name, *sizeX, *sizeY; - }; - - GameManager *gm = GameManager::getSingleton(); - StateManager *stateManager = gm->getStateManager(); - GuiAppState *state = (GuiAppState*)stateManager->getAppStateByType((int)AppStateType::GUI_STATE); - state->removeAllButtons(vector{this}); - - Vector2 size = Vector2(140, 20); - - string fontPath = gm->getPath() + "Fonts/batang.ttf"; - Textbox *name = new Textbox(Vector2(100, 150), size, fontPath); - Textbox *sizeX = new Textbox(Vector2(100, 150 + (size.y + 10)), size, fontPath); - Textbox *sizeY = new Textbox(Vector2(100, 150 + 2 * (size.y + 10)), size, fontPath); - state->addTextbox(name); - state->addTextbox(sizeX); - state->addTextbox(sizeY); - - state->addButton(new OkButton(Vector2(200, gm->getHeight() - 150), Vector2(140, 50), name, sizeX, sizeY)); - - state->removeButton(this); - } - private: - }; - - class LoadMapButton : public Button{ - public: - LoadMapButton(Vector2 pos, Vector2 size) : Button(pos, size, "Load map", GameManager::getSingleton()->getPath() + "Fonts/batang.ttf", -1, true){} - void onClick(){ - StateManager *stateManager = GameManager::getSingleton()->getStateManager(); - GuiAppState *state = (GuiAppState*)stateManager->getAppStateByType((int)AppStateType::GUI_STATE); - state->removeAllButtons(); - } - private: - }; - GameManager *gm = GameManager::getSingleton(); string font = gm->getPath() + "Fonts/batang.ttf"; int width = gm->getWidth(), height = gm->getHeight(); @@ -280,13 +214,11 @@ namespace battleship{ SpButton *spButton = new SpButton(state, Vector2(width / 16, height / 12), size, "Singleplayer", true); MainMenuOptionsButton *optionsButton = new MainMenuOptionsButton(state, Vector2(width / 16, height / 12 * 2), size, "Options", true); - NewMapButton *newMapButton = new NewMapButton(Vector2(width / 16, height / 12 * 3), size); - LoadMapButton *loadMapButton = new LoadMapButton(Vector2(width / 16, height / 12 * 4), size); - ExitButton *exitButton = new ExitButton(Vector2(width / 16, height / 12 * 5), size); + MapEditorButton *editorButton = new MapEditorButton(Vector2(width / 16, height / 12 * 3), size); + ExitButton *exitButton = new ExitButton(Vector2(width / 16, height / 12 * 4), size); state->addButton(spButton); state->addButton(optionsButton); - state->addButton(newMapButton); - state->addButton(loadMapButton); + state->addButton(editorButton); state->addButton(exitButton); }