QuickOptions!! #46

This commit is contained in:
Simon Robertshaw 2012-08-03 15:29:18 +01:00
parent 80dfc96c7c
commit 36b2aa0191
9 changed files with 294 additions and 2 deletions

View File

@ -74,6 +74,7 @@ public:
OptionsCallback(GameController * cc_) { cc = cc_; } OptionsCallback(GameController * cc_) { cc = cc_; }
virtual void ControllerExit() virtual void ControllerExit()
{ {
cc->gameModel->UpdateQuickOptions();
//cc->gameModel->SetUser(cc->loginWindow->GetUser()); //cc->gameModel->SetUser(cc->loginWindow->GetUser());
} }
}; };

View File

@ -10,6 +10,7 @@
#include "client/Client.h" #include "client/Client.h"
#include "game/DecorationTool.h" #include "game/DecorationTool.h"
#include "GameModelException.h" #include "GameModelException.h"
#include "QuickOptions.h"
GameModel::GameModel(): GameModel::GameModel():
sim(NULL), sim(NULL),
@ -68,6 +69,7 @@ GameModel::GameModel():
} }
BuildMenus(); BuildMenus();
BuildQuickOptionMenu();
//Set default decoration colour //Set default decoration colour
unsigned char colourR = min(Client::Ref().GetPrefInteger("Decoration.Red", 200), 255); unsigned char colourR = min(Client::Ref().GetPrefInteger("Decoration.Red", 200), 255);
@ -114,6 +116,33 @@ GameModel::~GameModel()
// delete[] activeTools; // delete[] activeTools;
} }
void GameModel::UpdateQuickOptions()
{
for(std::vector<QuickOption*>::iterator iter = quickOptions.begin(), end = quickOptions.end(); iter != end; ++iter)
{
QuickOption * option = *iter;
option->Update();
}
}
void GameModel::BuildQuickOptionMenu()
{
for(std::vector<QuickOption*>::iterator iter = quickOptions.begin(), end = quickOptions.end(); iter != end; ++iter)
{
delete *iter;
}
quickOptions.clear();
quickOptions.push_back(new SandEffectOption(this));
quickOptions.push_back(new DrawGravOption(this));
quickOptions.push_back(new DecorationsOption(this));
quickOptions.push_back(new NGravityOption(this));
quickOptions.push_back(new AHeatOption(this));
notifyQuickOptionsChanged();
UpdateQuickOptions();
}
void GameModel::BuildMenus() void GameModel::BuildMenus()
{ {
//Empty current menus //Empty current menus
@ -246,6 +275,8 @@ void GameModel::AddObserver(GameView * observer){
observer->NotifyZoomChanged(this); observer->NotifyZoomChanged(this);
observer->NotifyColourSelectorVisibilityChanged(this); observer->NotifyColourSelectorVisibilityChanged(this);
observer->NotifyColourSelectorColourChanged(this); observer->NotifyColourSelectorColourChanged(this);
observer->NotifyQuickOptionsChanged(this);
UpdateQuickOptions();
} }
void GameModel::SetActiveMenu(Menu * menu) void GameModel::SetActiveMenu(Menu * menu)
@ -282,6 +313,11 @@ void GameModel::SetActiveTool(int selection, Tool * tool)
notifyActiveToolsChanged(); notifyActiveToolsChanged();
} }
vector<QuickOption*> GameModel::GetQuickOptions()
{
return quickOptions;
}
vector<Menu*> GameModel::GetMenuList() vector<Menu*> GameModel::GetMenuList()
{ {
return menuList; return menuList;
@ -320,6 +356,7 @@ void GameModel::SetSave(SaveInfo * newSave)
sim->Load(saveData); sim->Load(saveData);
} }
notifySaveChanged(); notifySaveChanged();
UpdateQuickOptions();
} }
void GameModel::SetSaveFile(SaveFile * newSave) void GameModel::SetSaveFile(SaveFile * newSave)
@ -348,6 +385,7 @@ void GameModel::SetSaveFile(SaveFile * newSave)
delete newSave; delete newSave;
notifySaveChanged(); notifySaveChanged();
UpdateQuickOptions();
} }
Simulation * GameModel::GetSimulation() Simulation * GameModel::GetSimulation()
@ -476,6 +514,7 @@ void GameModel::SetDecoration(bool decorationState)
{ {
ren->decorations_enable = decorationState?1:0; ren->decorations_enable = decorationState?1:0;
notifyDecorationChanged(); notifyDecorationChanged();
UpdateQuickOptions();
} }
bool GameModel::GetDecoration() bool GameModel::GetDecoration()
@ -750,3 +789,11 @@ void GameModel::notifyToolTipChanged()
observers[i]->NotifyToolTipChanged(this); observers[i]->NotifyToolTipChanged(this);
} }
} }
void GameModel::notifyQuickOptionsChanged()
{
for(int i = 0; i < observers.size(); i++)
{
observers[i]->NotifyQuickOptionsChanged(this);
}
}

View File

@ -21,6 +21,7 @@ class GameView;
class Simulation; class Simulation;
class Renderer; class Renderer;
class QuickOption;
class ToolSelection class ToolSelection
{ {
public: public:
@ -43,6 +44,7 @@ private:
vector<GameView*> observers; vector<GameView*> observers;
vector<Tool*> toolList; vector<Tool*> toolList;
vector<Menu*> menuList; vector<Menu*> menuList;
vector<QuickOption*> quickOptions;
Menu * activeMenu; Menu * activeMenu;
int currentBrush; int currentBrush;
vector<Brush *> brushList; vector<Brush *> brushList;
@ -76,6 +78,7 @@ private:
void notifyLogChanged(string entry); void notifyLogChanged(string entry);
void notifyInfoTipChanged(); void notifyInfoTipChanged();
void notifyToolTipChanged(); void notifyToolTipChanged();
void notifyQuickOptionsChanged();
public: public:
GameModel(); GameModel();
~GameModel(); ~GameModel();
@ -92,6 +95,9 @@ public:
std::string GetInfoTip(); std::string GetInfoTip();
void BuildMenus(); void BuildMenus();
void BuildQuickOptionMenu();
void UpdateQuickOptions();
void SetVote(int direction); void SetVote(int direction);
SaveInfo * GetSave(); SaveInfo * GetSave();
@ -108,6 +114,7 @@ public:
void ClearSimulation(); void ClearSimulation();
vector<Menu*> GetMenuList(); vector<Menu*> GetMenuList();
vector<Tool*> GetToolList(); vector<Tool*> GetToolList();
vector<QuickOption*> GetQuickOptions();
void SetActiveMenu(Menu * menu); void SetActiveMenu(Menu * menu);
Menu * GetActiveMenu(); Menu * GetActiveMenu();
void FrameStep(int frames); void FrameStep(int frames);

View File

@ -12,6 +12,7 @@
#include "interface/Slider.h" #include "interface/Slider.h"
#include "search/Thumbnail.h" #include "search/Thumbnail.h"
#include "simulation/SaveRenderer.h" #include "simulation/SaveRenderer.h"
#include "QuickOption.h"
GameView::GameView(): GameView::GameView():
ui::Window(ui::Point(0, 0), ui::Point(XRES+BARSIZE, YRES+MENUSIZE)), ui::Window(ui::Point(0, 0), ui::Point(XRES+BARSIZE, YRES+MENUSIZE)),
@ -362,6 +363,33 @@ public:
} }
}; };
class GameView::OptionAction: public ui::ButtonAction
{
QuickOption * option;
public:
OptionAction(QuickOption * _option) { option = _option; }
void ActionCallback(ui::Button * sender)
{
option->Perform();
}
};
class GameView::OptionListener: public QuickOptionListener
{
ui::Button * button;
public:
OptionListener(ui::Button * _button) { button = _button; }
virtual void OnValueChanged(QuickOption * option)
{
switch(option->GetType())
{
case QuickOption::Toggle:
button->SetTogglable(true);
button->SetToggleState(option->GetToggle());
}
}
};
class GameView::ToolAction: public ui::ButtonAction class GameView::ToolAction: public ui::ButtonAction
{ {
GameView * v; GameView * v;
@ -376,6 +404,31 @@ public:
} }
}; };
void GameView::NotifyQuickOptionsChanged(GameModel * sender)
{
for(int i = 0; i < quickOptionButtons.size(); i++)
{
RemoveComponent(quickOptionButtons[i]);
delete quickOptionButtons[i];
}
int currentY = 1;
vector<QuickOption*> optionList = sender->GetQuickOptions();
for(vector<QuickOption*>::iterator iter = optionList.begin(), end = optionList.end(); iter != end; ++iter)
{
QuickOption * option = *iter;
ui::Button * tempButton = new ui::Button(ui::Point(XRES+BARSIZE-16, currentY), ui::Point(15, 15), option->GetIcon(), option->GetDescription());
//tempButton->Appearance.Margin = ui::Border(0, 2, 3, 2);
tempButton->SetTogglable(true);
tempButton->SetActionCallback(new OptionAction(option));
option->AddListener(new OptionListener(tempButton));
AddComponent(tempButton);
quickOptionButtons.push_back(tempButton);
currentY += 16;
}
}
void GameView::NotifyMenuListChanged(GameModel * sender) void GameView::NotifyMenuListChanged(GameModel * sender)
{ {
int currentY = YRES+MENUSIZE-48;//-(sender->GetMenuList().size()*16); int currentY = YRES+MENUSIZE-48;//-(sender->GetMenuList().size()*16);

View File

@ -54,6 +54,7 @@ private:
Renderer * ren; Renderer * ren;
Brush * activeBrush; Brush * activeBrush;
//UI Elements //UI Elements
vector<ui::Button*> quickOptionButtons;
vector<ui::Button*> menuButtons; vector<ui::Button*> menuButtons;
vector<ToolButton*> toolButtons; vector<ToolButton*> toolButtons;
vector<ui::Component*> notificationComponents; vector<ui::Component*> notificationComponents;
@ -139,6 +140,7 @@ public:
void NotifyLogChanged(GameModel * sender, string entry); void NotifyLogChanged(GameModel * sender, string entry);
void NotifyToolTipChanged(GameModel * sender); void NotifyToolTipChanged(GameModel * sender);
void NotifyInfoTipChanged(GameModel * sender); void NotifyInfoTipChanged(GameModel * sender);
void NotifyQuickOptionsChanged(GameModel * sender);
virtual void ToolTip(ui::Component * sender, ui::Point mousePosition, std::string toolTip); virtual void ToolTip(ui::Component * sender, ui::Point mousePosition, std::string toolTip);
@ -163,6 +165,8 @@ public:
class MenuAction; class MenuAction;
class ToolAction; class ToolAction;
class OptionAction;
class OptionListener;
}; };
#endif // GAMEVIEW_H #endif // GAMEVIEW_H

76
src/game/QuickOption.h Normal file
View File

@ -0,0 +1,76 @@
#pragma once
#include <string>
#include <vector>
class GameModel;
class QuickOption;
class QuickOptionListener
{
protected:
QuickOptionListener() {}
public:
virtual ~QuickOptionListener() {}
virtual void OnValueChanged(QuickOption * sender) {}
};
class QuickOption
{
public:
enum Type {
Toggle, Multi
};
protected:
std::vector<QuickOptionListener*> listeners;
GameModel * m;
Type type;
std::string icon;
std::string description;
QuickOption(std::string icon, std::string description, GameModel * m, Type type) :
icon(icon),
description(description),
m(m),
type(type)
{
}
virtual void perform() {}
public:
virtual ~QuickOption()
{
//for(std::vector<QuickOptionListener*>::iterator iter = listeners.begin(), end = listeners.end(); iter != end; ++iter)
// delete *iter;
}
std::vector<QuickOptionListener*> GetListeners()
{
return listeners;
}
void AddListener(QuickOptionListener * listener)
{
listeners.push_back(listener);
}
Type GetType() { return type; }
virtual bool GetToggle() {}
virtual int GetMutli() {}
virtual int GetMultiCount() {}
std::string GetIcon() { return icon; }
void SetIcon(std::string icon) { this->icon = icon; }
std::string GetDescription() { return description; }
void SetDescription(std::string description) { this->description = description; }
void Perform()
{
perform();
for(std::vector<QuickOptionListener*>::iterator iter = listeners.begin(), end = listeners.end(); iter != end; ++iter)
(*iter)->OnValueChanged(this);
}
void Update()
{
for(std::vector<QuickOptionListener*>::iterator iter = listeners.begin(), end = listeners.end(); iter != end; ++iter)
(*iter)->OnValueChanged(this);
}
};

99
src/game/QuickOptions.h Normal file
View File

@ -0,0 +1,99 @@
#include "QuickOption.h"
#include "GameModel.h"
class SandEffectOption: public QuickOption
{
public:
SandEffectOption(GameModel * m):
QuickOption("P", "Sand effect", m, Toggle)
{
}
virtual bool GetToggle()
{
return m->GetSimulation()->pretty_powder;
}
virtual void perform()
{
m->GetSimulation()->pretty_powder = !m->GetSimulation()->pretty_powder;
}
};
class DrawGravOption: public QuickOption
{
public:
DrawGravOption(GameModel * m):
QuickOption("G", "Draw gravity field", m, Toggle)
{
}
virtual bool GetToggle()
{
return m->GetRenderer()->gravifyFieldEnabled;
}
virtual void perform()
{
m->GetRenderer()->gravifyFieldEnabled = !m->GetRenderer()->gravifyFieldEnabled;
}
};
class DecorationsOption: public QuickOption
{
public:
DecorationsOption(GameModel * m):
QuickOption("D", "Draw decorations", m, Toggle)
{
}
virtual bool GetToggle()
{
return m->GetRenderer()->decorations_enable;
}
virtual void perform()
{
m->GetRenderer()->decorations_enable = !m->GetRenderer()->decorations_enable;
}
};
class NGravityOption: public QuickOption
{
public:
NGravityOption(GameModel * m):
QuickOption("N", "Newtonian Gravity", m, Toggle)
{
}
virtual bool GetToggle()
{
return m->GetSimulation()->grav->ngrav_enable;
}
virtual void perform()
{
if(m->GetSimulation()->grav->ngrav_enable)
{
m->GetSimulation()->grav->stop_grav_async();
}
else
{
m->GetSimulation()->grav->start_grav_async();
}
}
};
class AHeatOption: public QuickOption
{
public:
AHeatOption(GameModel * m):
QuickOption("A", "Ambient heat", m, Toggle)
{
}
virtual bool GetToggle()
{
return m->GetSimulation()->aheat_enable;
}
virtual void perform()
{
m->GetSimulation()->aheat_enable = !m->GetSimulation()->aheat_enable;
}
};

View File

@ -39,9 +39,9 @@ void Renderer::RenderBegin()
{ {
draw_air(); draw_air();
draw_grav();
render_parts(); render_parts();
render_fire(); render_fire();
draw_grav();
DrawWalls(); DrawWalls();
DrawSigns(); DrawSigns();
#ifndef OGLR #ifndef OGLR
@ -1899,6 +1899,9 @@ void Renderer::draw_grav()
int x, y, i, ca; int x, y, i, ca;
float nx, ny, dist; float nx, ny, dist;
if(!gravifyFieldEnabled)
return;
for (y=0; y<YRES/CELL; y++) for (y=0; y<YRES/CELL; y++)
{ {
for (x=0; x<XRES/CELL; x++) for (x=0; x<XRES/CELL; x++)
@ -2096,7 +2099,8 @@ Renderer::Renderer(Graphics * g, Simulation * sim):
zoomScopeSize(32), zoomScopeSize(32),
ZFACTOR(8), ZFACTOR(8),
zoomEnabled(false), zoomEnabled(false),
decorations_enable(1) decorations_enable(1),
gravifyFieldEnabled(false)
{ {
this->g = g; this->g = g;
this->sim = sim; this->sim = sim;

View File

@ -42,6 +42,7 @@ public:
char * plasma_data; char * plasma_data;
int emp_decor; int emp_decor;
// //
bool gravifyFieldEnabled;
int decorations_enable; int decorations_enable;
Simulation * sim; Simulation * sim;
Graphics * g; Graphics * g;