Begining menu, tool

This commit is contained in:
Simon Robertshaw 2012-01-22 23:24:49 +00:00
parent 19c1fa5dcb
commit 8c0678fa48
9 changed files with 278 additions and 11 deletions

View File

@ -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();

View File

@ -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();

View File

@ -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<Tool*> GameModel::GetToolList()
{
activeElement = element;
return toolList;
}
Menu * GameModel::GetActiveMenu()
{
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);
}
}

View File

@ -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();

View File

@ -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();

View File

@ -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

49
src/game/Menu.h Normal file
View File

@ -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_ */

53
src/game/Tool.h Normal file
View File

@ -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_ */

View File

@ -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; }