summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSimon Robertshaw <simon@hardwired.org.uk>2012-01-22 23:24:49 (GMT)
committer Simon Robertshaw <simon@hardwired.org.uk>2012-01-22 23:24:49 (GMT)
commit8c0678fa48f9598a5ade2d4960c46bfea7e6abef (patch)
tree880535e7984c24948d345932f680ef806baa4cec /src
parent19c1fa5dcb4c4a2ba9d692e136b17da316a2631b (diff)
downloadpowder-8c0678fa48f9598a5ade2d4960c46bfea7e6abef.zip
powder-8c0678fa48f9598a5ade2d4960c46bfea7e6abef.tar.gz
Begining menu, tool
Diffstat (limited to 'src')
-rw-r--r--src/game/GameController.cpp22
-rw-r--r--src/game/GameController.h2
-rw-r--r--src/game/GameModel.cpp82
-rw-r--r--src/game/GameModel.h18
-rw-r--r--src/game/GameView.cpp56
-rw-r--r--src/game/GameView.h6
-rw-r--r--src/game/Menu.h49
-rw-r--r--src/game/Tool.h53
-rw-r--r--src/interface/Button.h1
9 files changed, 278 insertions, 11 deletions
diff --git a/src/game/GameController.cpp b/src/game/GameController.cpp
index b9c534c..5749fd4 100644
--- a/src/game/GameController.cpp
+++ b/src/game/GameController.cpp
@@ -50,8 +50,19 @@ void GameController::AdjustBrushSize(int direction)
void GameController::DrawPoints(queue<ui::Point*> & 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<ui::Point*> & 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 03d12e9..d83c459 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<ui::Point*> & 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 3070093..090f46a 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);
+}
+
+void GameModel::SetActiveMenu(Menu * menu)
+{
+ for(int i = 0; i < menuList.size(); i++)
+ {
+ if(menuList[i]==menu)
+ {
+ activeMenu = menu;
+ toolList = menu->GetToolList();
+ notifyToolListChanged();
+ }
+ }
}
-int GameModel::GetActiveElement()
+vector<Tool*> GameModel::GetToolList()
{
- return activeElement;
+ return toolList;
}
-void GameModel::SetActiveElement(int element)
+Menu * GameModel::GetActiveMenu()
{
- activeElement = element;
+ return activeMenu;
+}
+
+Tool * GameModel::GetActiveTool()
+{
+ return activeTool;
+}
+
+void GameModel::SetActiveTool(Tool * tool)
+{
+ activeTool = tool;
+}
+
+vector<Menu*> 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 753e19d..d65165b 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<GameView*> observers;
+ vector<Tool*> toolList;
+ vector<Menu*> 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<Menu*> GetMenuList();
+ vector<Tool*> GetToolList();
+ void SetActiveMenu(Menu * menu);
+ Menu * GetActiveMenu();
Simulation * GetSimulation();
Renderer * GetRenderer();
diff --git a/src/game/GameView.cpp b/src/game/GameView.cpp
index 7fe4fa5..ed2094d 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<Menu*> 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 85a96e7..c27c2cc 100644
--- a/src/game/GameView.h
+++ b/src/game/GameView.h
@@ -1,6 +1,7 @@
#ifndef GAMEVIEW_H
#define GAMEVIEW_H
+#include <vector>
#include <queue>
#include "GameController.h"
#include "GameModel.h"
@@ -22,6 +23,8 @@ private:
Renderer * ren;
Brush * activeBrush;
//UI Elements
+ vector<ui::Button*> menuButtons;
+ vector<ui::Button*> 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 0000000..1824190
--- /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<Tool*> tools;
+public:
+ Menu(char icon_, string description_):
+ icon(icon_),
+ description(description_),
+ tools(vector<Tool*>())
+ {
+
+ }
+
+ vector<Tool*> 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 0000000..842f4a2
--- /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 <iostream>
+
+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 7728218..a137aac 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; }