Element menu

This commit is contained in:
Simon Robertshaw 2012-01-23 10:50:48 +00:00
parent 8c0678fa48
commit 8a65c395f4
10 changed files with 107 additions and 9 deletions

View File

@ -6,7 +6,7 @@ OBJS := $(patsubst src/%.cpp,build/obj/%.o,$(SOURCES))
FOLDERS :=
CFLAGS := -w -Isrc/ -Idata/
OFLAGS := -fkeep-inline-functions #-O3 -ffast-math -ftree-vectorize -funsafe-math-optimizations -msse2
OFLAGS := -fkeep-inline-functions -O3 -ffast-math -ftree-vectorize -funsafe-math-optimizations -msse2
CPPC := g++
CPPC_WIN := i686-w64-mingw32-gcc

View File

@ -88,7 +88,7 @@ void GameController::DrawPoints(queue<ui::Point*> & pointQueue)
void GameController::Tick()
{
//gameModel->GetSimulation()->update_particles();
gameModel->GetSimulation()->update_particles();
}
void GameController::SetPaused(bool pauseState)
@ -96,11 +96,21 @@ void GameController::SetPaused(bool pauseState)
gameModel->SetPaused(pauseState);
}
void GameController::SetPaused()
{
gameModel->SetPaused(!gameModel->GetPaused());
}
void GameController::SetActiveMenu(Menu * menu)
{
gameModel->SetActiveMenu(menu);
}
void GameController::SetActiveTool(Tool * tool)
{
gameModel->SetActiveTool(tool);
}
void GameController::OpenSearch()
{
search = new SearchController();

View File

@ -28,7 +28,9 @@ public:
void DrawPoints(queue<ui::Point*> & pointQueue);
void Tick();
void SetPaused(bool pauseState);
void SetPaused();
void SetActiveMenu(Menu * menu);
void SetActiveTool(Tool * tool);
void OpenSearch();
void OpenLogin();
void OpenTags();

View File

@ -26,7 +26,7 @@ GameModel::GameModel():
{
if(sim->ptypes[i].menusection < 12)
{
Tool * tempTool = new ElementTool(i, sim->ptypes[i].name, 0, 0, 0);
Tool * tempTool = new ElementTool(i, sim->ptypes[i].name, PIXR(sim->ptypes[i].pcolors), PIXG(sim->ptypes[i].pcolors), PIXB(sim->ptypes[i].pcolors));
menuList[sim->ptypes[i].menusection]->AddTool(tempTool);
}
}
@ -96,6 +96,7 @@ Tool * GameModel::GetActiveTool()
void GameModel::SetActiveTool(Tool * tool)
{
activeTool = tool;
notifyActiveToolChanged();
}
vector<Menu*> GameModel::GetMenuList()
@ -195,3 +196,11 @@ void GameModel::notifyToolListChanged()
observers[i]->NotifyToolListChanged(this);
}
}
void GameModel::notifyActiveToolChanged()
{
for(int i = 0; i < observers.size(); i++)
{
observers[i]->NotifyActiveToolChanged(this);
}
}

View File

@ -36,6 +36,7 @@ private:
void notifyBrushChanged();
void notifyMenuListChanged();
void notifyToolListChanged();
void notifyActiveToolChanged();
public:
GameModel();
~GameModel();

View File

@ -187,6 +187,18 @@ public:
}
};
class GameView::ToolAction: public ui::ButtonAction
{
GameView * v;
public:
Tool * tool;
ToolAction(GameView * _v, Tool * tool_) { v = _v; tool = tool_; }
void ActionCallback(ui::Button * sender)
{
v->c->SetActiveTool(tool);
}
};
void GameView::NotifyMenuListChanged(GameModel * sender)
{
int currentY = YRES+MENUSIZE-36;
@ -216,8 +228,24 @@ void GameView::NotifyMenuListChanged(GameModel * sender)
}
}
void GameView::NotifyActiveToolChanged(GameModel * sender)
{
for(int i = 0; i < toolButtons.size(); i++)
{
if(((ToolAction*)toolButtons[i]->GetActionCallback())->tool==sender->GetActiveTool())
{
toolButtons[i]->SetToggleState(true);
}
else
{
toolButtons[i]->SetToggleState(false);
}
}
}
void GameView::NotifyToolListChanged(GameModel * sender)
{
int currentX = XRES+BARSIZE-56;
for(int i = 0; i < menuButtons.size(); i++)
{
if(((MenuAction*)menuButtons[i]->GetActionCallback())->menu==sender->GetActiveMenu())
@ -229,6 +257,25 @@ void GameView::NotifyToolListChanged(GameModel * sender)
menuButtons[i]->SetToggleState(false);
}
}
for(int i = 0; i < toolButtons.size(); i++)
{
RemoveComponent(toolButtons[i]);
delete toolButtons[i];
}
toolButtons.clear();
vector<Tool*> toolList = sender->GetToolList();
for(int i = 0; i < toolList.size(); i++)
{
ui::Button * tempButton = new ui::Button(ui::Point(currentX, YRES), ui::Point(32, 16), toolList[i]->GetName());
currentX -= 36;
tempButton->SetTogglable(true);
tempButton->SetActionCallback(new ToolAction(this, toolList[i]));
tempButton->SetBackgroundColour(toolList[i]->colRed, toolList[i]->colGreen, toolList[i]->colBlue);
tempButton->SetAlignment(AlignCentre, AlignBottom);
AddComponent(tempButton);
toolButtons.push_back(tempButton);
}
}
void GameView::NotifyRendererChanged(GameModel * sender)
@ -313,6 +360,15 @@ void GameView::OnMouseWheel(int x, int y, int d)
}
}
void GameView::OnKeyPress(int key, bool shift, bool ctrl, bool alt)
{
switch(key)
{
case ' ':
c->SetPaused();
}
}
void GameView::OnTick(float dt)
{
if(!pointQueue.empty())
@ -327,6 +383,8 @@ void GameView::OnDraw()
if(ren)
{
ren->render_parts();
ren->render_fire();
ren->render_signs();
}
if(activeBrush)
{

View File

@ -46,15 +46,18 @@ public:
void NotifyBrushChanged(GameModel * sender);
void NotifyMenuListChanged(GameModel * sender);
void NotifyToolListChanged(GameModel * sender);
void NotifyActiveToolChanged(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);
virtual void OnMouseWheel(int x, int y, int d);
virtual void OnKeyPress(int key, bool shift, bool ctrl, bool alt);
//virtual void OnKeyPress(int key, bool shift, bool ctrl, bool alt) {}
//virtual void OnKeyRelease(int key, bool shift, bool ctrl, bool alt) {}
virtual void OnTick(float dt);
virtual void OnDraw();
class MenuAction;
class ToolAction;
};
#endif // GAMEVIEW_H

View File

@ -15,10 +15,10 @@ using namespace std;
class Tool
{
protected:
int toolID, colRed, colBlue, colGreen;
int toolID;
string toolName;
public:
Tool(int id, string name, int r, int b, int g):
Tool(int id, string name, int r, int g, int b):
toolID(id),
toolName(name),
colRed(r),
@ -26,16 +26,18 @@ public:
colBlue(b)
{
}
string GetName() { return toolName; }
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) {}
int colRed, colBlue, colGreen;
};
class ElementTool: public Tool
{
public:
ElementTool(int id, string name, int r, int b, int g):
ElementTool(int id, string name, int r, int g, int b):
Tool(id, name, r, g, b)
{
}

View File

@ -26,7 +26,10 @@ Button::Button(Window* parent_state, std::string buttonText):
textPosition(ui::Point(0, 0)),
textVAlign(AlignMiddle),
textHAlign(AlignCentre),
Enabled(true)
Enabled(true),
colr(0),
colg(0),
colb(0)
{
TextPosition();
}
@ -42,7 +45,10 @@ Button::Button(Point position, Point size, std::string buttonText):
textPosition(ui::Point(0, 0)),
textVAlign(AlignMiddle),
textHAlign(AlignCentre),
Enabled(true)
Enabled(true),
colr(0),
colg(0),
colb(0)
{
TextPosition();
}
@ -58,7 +64,10 @@ Button::Button(std::string buttonText):
textPosition(ui::Point(0, 0)),
textVAlign(AlignMiddle),
textHAlign(AlignCentre),
Enabled(true)
Enabled(true),
colr(0),
colg(0),
colb(0)
{
TextPosition();
}
@ -135,6 +144,8 @@ void Button::Draw(const Point& screenPos)
{
if(isMouseInside)
g->fillrect(Position.X, Position.Y, Size.X, Size.Y, 20, 20, 20, 255);
else
g->fillrect(Position.X, Position.Y, Size.X, Size.Y, colr, colg, colb, 255);
g->drawrect(Position.X, Position.Y, Size.X, Size.Y, 255, 255, 255, 255);
g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y, ButtonText, 255, 255, 255, 255);
}

View File

@ -59,7 +59,9 @@ public:
HorizontalAlignment GetHAlignment() { return textHAlign; }
VerticalAlignment GetVAlignment() { return textVAlign; }
void SetAlignment(HorizontalAlignment hAlign, VerticalAlignment vAlign) { textHAlign = hAlign; textVAlign = vAlign; TextPosition(); }
void SetBackgroundColour(int colr, int colg, int colb) { this->colr = colr; this->colg = colg; this->colb = colb; }
protected:
int colr, colg, colb;
bool isButtonDown, state, isMouseInside, isTogglable, toggle;
ButtonAction * actionCallback;
ui::Point textPosition;