diff --git a/src/game/GameController.cpp b/src/game/GameController.cpp index b9c534cb5..5749fd49c 100644 --- a/src/game/GameController.cpp +++ b/src/game/GameController.cpp @@ -50,8 +50,19 @@ void GameController::AdjustBrushSize(int direction) void GameController::DrawPoints(queue & pointQueue) { Simulation * sim = gameModel->GetSimulation(); - int activeElement = gameModel->GetActiveElement(); + Tool * activeTool = gameModel->GetActiveTool(); Brush * cBrush = gameModel->GetBrush(); + if(!activeTool || !cBrush) + { + if(!pointQueue.empty()) + { + while(!pointQueue.empty()) + { + delete pointQueue.front(); + pointQueue.pop(); + } + } + } if(!pointQueue.empty()) { ui::Point * sPoint = NULL; @@ -61,12 +72,12 @@ void GameController::DrawPoints(queue & pointQueue) pointQueue.pop(); if(sPoint) { - sim->create_line(fPoint->X, fPoint->Y, sPoint->X, sPoint->Y, 1, 1, activeElement, 0, cBrush); + activeTool->DrawLine(sim, cBrush, *fPoint, *sPoint); delete sPoint; } else { - sim->create_parts(fPoint->X, fPoint->Y, 1, 1, activeElement, 0, cBrush); + activeTool->Draw(sim, cBrush, *fPoint); } sPoint = fPoint; } @@ -85,6 +96,11 @@ void GameController::SetPaused(bool pauseState) gameModel->SetPaused(pauseState); } +void GameController::SetActiveMenu(Menu * menu) +{ + gameModel->SetActiveMenu(menu); +} + void GameController::OpenSearch() { search = new SearchController(); diff --git a/src/game/GameController.h b/src/game/GameController.h index 03d12e9e6..d83c4597e 100644 --- a/src/game/GameController.h +++ b/src/game/GameController.h @@ -7,6 +7,7 @@ #include "interface/Point.h" #include "simulation/Simulation.h" #include "search/SearchController.h" +#include "Menu.h" using namespace std; @@ -27,6 +28,7 @@ public: void DrawPoints(queue & pointQueue); void Tick(); void SetPaused(bool pauseState); + void SetActiveMenu(Menu * menu); void OpenSearch(); void OpenLogin(); void OpenTags(); diff --git a/src/game/GameModel.cpp b/src/game/GameModel.cpp index 307009300..090f46ada 100644 --- a/src/game/GameModel.cpp +++ b/src/game/GameModel.cpp @@ -7,7 +7,7 @@ #include "Brush.h" GameModel::GameModel(): - activeElement(1), + activeTool(NULL), sim(NULL), ren(NULL), currentSave(NULL), @@ -15,10 +15,35 @@ GameModel::GameModel(): { sim = new Simulation(); ren = new Renderer(ui::Engine::Ref().g, sim); + + menuList.clear(); + for(int i = 0; i < 12; i++) + { + menuList.push_back(new Menu('q', "Simon")); + } + //Build menus from Simulation elements + for(int i = 0; i < PT_NUM; i++) + { + if(sim->ptypes[i].menusection < 12) + { + Tool * tempTool = new ElementTool(i, sim->ptypes[i].name, 0, 0, 0); + menuList[sim->ptypes[i].menusection]->AddTool(tempTool); + } + } + + activeTool = new ElementTool(1, "TURD", 0, 0, 0); } GameModel::~GameModel() { + for(int i = 0; i < menuList.size(); i++) + { + for(int j = 0; i < menuList[i]->GetToolList().size(); i++) + { + delete menuList[i]->GetToolList()[j]; + } + delete menuList[i]; + } delete sim; delete ren; } @@ -36,22 +61,53 @@ void GameModel::AddObserver(GameView * observer){ observer->NotifyPausedChanged(this); observer->NotifySaveChanged(this); observer->NotifyBrushChanged(this); + observer->NotifyMenuListChanged(this); + observer->NotifyToolListChanged(this); } -int GameModel::GetActiveElement() +void GameModel::SetActiveMenu(Menu * menu) { - return activeElement; + for(int i = 0; i < menuList.size(); i++) + { + if(menuList[i]==menu) + { + activeMenu = menu; + toolList = menu->GetToolList(); + notifyToolListChanged(); + } + } } -void GameModel::SetActiveElement(int element) +vector GameModel::GetToolList() { - activeElement = element; + return toolList; +} + +Menu * GameModel::GetActiveMenu() +{ + return activeMenu; +} + +Tool * GameModel::GetActiveTool() +{ + return activeTool; +} + +void GameModel::SetActiveTool(Tool * tool) +{ + activeTool = tool; +} + +vector GameModel::GetMenuList() +{ + return menuList; } Save * GameModel::GetSave() { return currentSave; } + void GameModel::SetSave(Save * newSave) { currentSave = newSave; @@ -123,3 +179,19 @@ void GameModel::notifyBrushChanged() observers[i]->NotifyBrushChanged(this); } } + +void GameModel::notifyMenuListChanged() +{ + for(int i = 0; i < observers.size(); i++) + { + observers[i]->NotifyMenuListChanged(this); + } +} + +void GameModel::notifyToolListChanged() +{ + for(int i = 0; i < observers.size(); i++) + { + observers[i]->NotifyToolListChanged(this); + } +} diff --git a/src/game/GameModel.h b/src/game/GameModel.h index 753e19d1b..d65165b24 100644 --- a/src/game/GameModel.h +++ b/src/game/GameModel.h @@ -8,6 +8,9 @@ #include "GameView.h" #include "Brush.h" +#include "Tool.h" +#include "Menu.h" + using namespace std; class GameView; @@ -18,16 +21,21 @@ class GameModel { private: vector observers; + vector toolList; + vector menuList; + Menu * activeMenu; Brush * currentBrush; Save * currentSave; Simulation * sim; Renderer * ren; - int activeElement; + Tool * activeTool; void notifyRendererChanged(); void notifySimulationChanged(); void notifyPausedChanged(); void notifySaveChanged(); void notifyBrushChanged(); + void notifyMenuListChanged(); + void notifyToolListChanged(); public: GameModel(); ~GameModel(); @@ -35,11 +43,15 @@ public: Brush * GetBrush(); void SetSave(Save * newSave); void AddObserver(GameView * observer); - int GetActiveElement(); - void SetActiveElement(int element); + Tool * GetActiveTool(); + void SetActiveTool(Tool * tool); bool GetPaused(); void SetPaused(bool pauseState); void ClearSimulation(); + vector GetMenuList(); + vector GetToolList(); + void SetActiveMenu(Menu * menu); + Menu * GetActiveMenu(); Simulation * GetSimulation(); Renderer * GetRenderer(); diff --git a/src/game/GameView.cpp b/src/game/GameView.cpp index 7fe4fa5c7..ed2094d96 100644 --- a/src/game/GameView.cpp +++ b/src/game/GameView.cpp @@ -175,6 +175,62 @@ GameView::GameView(): AddComponent(pauseButton); } +class GameView::MenuAction: public ui::ButtonAction +{ + GameView * v; +public: + Menu * menu; + MenuAction(GameView * _v, Menu * menu_) { v = _v; menu = menu_; } + void ActionCallback(ui::Button * sender) + { + v->c->SetActiveMenu(menu); + } +}; + +void GameView::NotifyMenuListChanged(GameModel * sender) +{ + int currentY = YRES+MENUSIZE-36; + for(int i = 0; i < menuButtons.size(); i++) + { + RemoveComponent(menuButtons[i]); + delete menuButtons[i]; + } + menuButtons.clear(); + for(int i = 0; i < toolButtons.size(); i++) + { + RemoveComponent(toolButtons[i]); + delete toolButtons[i]; + } + toolButtons.clear(); + vector menuList = sender->GetMenuList(); + for(int i = 0; i < menuList.size(); i++) + { + std::string tempString = ""; + tempString += menuList[i]->GetIcon(); + ui::Button * tempButton = new ui::Button(ui::Point(XRES+BARSIZE-18, currentY), ui::Point(16, 16), tempString); + tempButton->SetTogglable(true); + tempButton->SetActionCallback(new MenuAction(this, menuList[i])); + currentY-=18; + AddComponent(tempButton); + menuButtons.push_back(tempButton); + } +} + +void GameView::NotifyToolListChanged(GameModel * sender) +{ + for(int i = 0; i < menuButtons.size(); i++) + { + if(((MenuAction*)menuButtons[i]->GetActionCallback())->menu==sender->GetActiveMenu()) + { + menuButtons[i]->SetToggleState(true); + } + else + { + menuButtons[i]->SetToggleState(false); + } + } +} + void GameView::NotifyRendererChanged(GameModel * sender) { ren = sender->GetRenderer(); diff --git a/src/game/GameView.h b/src/game/GameView.h index 85a96e735..c27c2cc02 100644 --- a/src/game/GameView.h +++ b/src/game/GameView.h @@ -1,6 +1,7 @@ #ifndef GAMEVIEW_H #define GAMEVIEW_H +#include #include #include "GameController.h" #include "GameModel.h" @@ -22,6 +23,8 @@ private: Renderer * ren; Brush * activeBrush; //UI Elements + vector menuButtons; + vector toolButtons; ui::Button * searchButton; ui::Button * reloadButton; ui::Button * saveSimulationButton; @@ -41,6 +44,8 @@ public: void NotifyPausedChanged(GameModel * sender); void NotifySaveChanged(GameModel * sender); void NotifyBrushChanged(GameModel * sender); + void NotifyMenuListChanged(GameModel * sender); + void NotifyToolListChanged(GameModel * sender); virtual void OnMouseMove(int x, int y, int dx, int dy); virtual void OnMouseDown(int x, int y, unsigned button); virtual void OnMouseUp(int x, int y, unsigned button); @@ -49,6 +54,7 @@ public: //virtual void OnKeyRelease(int key, bool shift, bool ctrl, bool alt) {} virtual void OnTick(float dt); virtual void OnDraw(); + class MenuAction; }; #endif // GAMEVIEW_H diff --git a/src/game/Menu.h b/src/game/Menu.h new file mode 100644 index 000000000..1824190fc --- /dev/null +++ b/src/game/Menu.h @@ -0,0 +1,49 @@ +/* + * Menu.h + * + * Created on: Jan 22, 2012 + * Author: Simon + */ + +#ifndef MENU_H_ +#define MENU_H_ + +#include "Tool.h" + +class Menu +{ + char icon; + string description; + vector tools; +public: + Menu(char icon_, string description_): + icon(icon_), + description(description_), + tools(vector()) + { + + } + + vector GetToolList() + { + return tools; + } + + char GetIcon() + { + return icon; + } + + string GetDescription() + { + return description; + } + + void AddTool(Tool * tool_) + { + tools.push_back(tool_); + } +}; + + +#endif /* MENU_H_ */ diff --git a/src/game/Tool.h b/src/game/Tool.h new file mode 100644 index 000000000..842f4a25f --- /dev/null +++ b/src/game/Tool.h @@ -0,0 +1,53 @@ +/* + * Tool.h + * + * Created on: Jan 22, 2012 + * Author: Simon + */ + +#ifndef TOOL_H_ +#define TOOL_H_ + +#include + +using namespace std; + +class Tool +{ +protected: + int toolID, colRed, colBlue, colGreen; + string toolName; +public: + Tool(int id, string name, int r, int b, int g): + toolID(id), + toolName(name), + colRed(r), + colGreen(g), + colBlue(b) + { + } + virtual ~Tool() {} + virtual void Draw(Simulation * sim, Brush * brush, ui::Point position) {} + virtual void DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2) {} + virtual void DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2) {} +}; + +class ElementTool: public Tool +{ +public: + ElementTool(int id, string name, int r, int b, int g): + Tool(id, name, r, g, b) + { + } + virtual ~ElementTool() {} + virtual void Draw(Simulation * sim, Brush * brush, ui::Point position){ + sim->create_parts(position.X, position.Y, 1, 1, toolID, 0, brush); + } + virtual void DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2) { + std::cout << position1.X << toolID << brush << std::endl; + sim->create_line(position1.X, position1.Y, position2.X, position2.Y, 1, 1, toolID, 0, brush); + } + virtual void DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2) {} +}; + +#endif /* TOOL_H_ */ diff --git a/src/interface/Button.h b/src/interface/Button.h index 772821828..a137aac37 100644 --- a/src/interface/Button.h +++ b/src/interface/Button.h @@ -53,6 +53,7 @@ public: inline bool GetToggleState(); inline void SetToggleState(bool state); void SetActionCallback(ButtonAction * action); + ButtonAction * GetActionCallback() { return actionCallback; } void TextPosition(); void SetText(std::string buttonText); HorizontalAlignment GetHAlignment() { return textHAlign; }