From 36b2aa01918344d91df30c4a6722ac39eaca0642 Mon Sep 17 00:00:00 2001 From: Simon Robertshaw Date: Fri, 3 Aug 2012 15:29:18 +0100 Subject: [PATCH] QuickOptions!! #46 --- src/game/GameController.cpp | 1 + src/game/GameModel.cpp | 47 ++++++++++++++++++ src/game/GameModel.h | 7 +++ src/game/GameView.cpp | 53 ++++++++++++++++++++ src/game/GameView.h | 4 ++ src/game/QuickOption.h | 76 ++++++++++++++++++++++++++++ src/game/QuickOptions.h | 99 +++++++++++++++++++++++++++++++++++++ src/graphics/Renderer.cpp | 8 ++- src/graphics/Renderer.h | 1 + 9 files changed, 294 insertions(+), 2 deletions(-) create mode 100644 src/game/QuickOption.h create mode 100644 src/game/QuickOptions.h diff --git a/src/game/GameController.cpp b/src/game/GameController.cpp index 5cf765404..c992120da 100644 --- a/src/game/GameController.cpp +++ b/src/game/GameController.cpp @@ -74,6 +74,7 @@ public: OptionsCallback(GameController * cc_) { cc = cc_; } virtual void ControllerExit() { + cc->gameModel->UpdateQuickOptions(); //cc->gameModel->SetUser(cc->loginWindow->GetUser()); } }; diff --git a/src/game/GameModel.cpp b/src/game/GameModel.cpp index 7c2ddeb1e..28ed5501d 100644 --- a/src/game/GameModel.cpp +++ b/src/game/GameModel.cpp @@ -10,6 +10,7 @@ #include "client/Client.h" #include "game/DecorationTool.h" #include "GameModelException.h" +#include "QuickOptions.h" GameModel::GameModel(): sim(NULL), @@ -68,6 +69,7 @@ GameModel::GameModel(): } BuildMenus(); + BuildQuickOptionMenu(); //Set default decoration colour unsigned char colourR = min(Client::Ref().GetPrefInteger("Decoration.Red", 200), 255); @@ -114,6 +116,33 @@ GameModel::~GameModel() // delete[] activeTools; } +void GameModel::UpdateQuickOptions() +{ + for(std::vector::iterator iter = quickOptions.begin(), end = quickOptions.end(); iter != end; ++iter) + { + QuickOption * option = *iter; + option->Update(); + } +} + +void GameModel::BuildQuickOptionMenu() +{ + for(std::vector::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() { //Empty current menus @@ -246,6 +275,8 @@ void GameModel::AddObserver(GameView * observer){ observer->NotifyZoomChanged(this); observer->NotifyColourSelectorVisibilityChanged(this); observer->NotifyColourSelectorColourChanged(this); + observer->NotifyQuickOptionsChanged(this); + UpdateQuickOptions(); } void GameModel::SetActiveMenu(Menu * menu) @@ -282,6 +313,11 @@ void GameModel::SetActiveTool(int selection, Tool * tool) notifyActiveToolsChanged(); } +vector GameModel::GetQuickOptions() +{ + return quickOptions; +} + vector GameModel::GetMenuList() { return menuList; @@ -320,6 +356,7 @@ void GameModel::SetSave(SaveInfo * newSave) sim->Load(saveData); } notifySaveChanged(); + UpdateQuickOptions(); } void GameModel::SetSaveFile(SaveFile * newSave) @@ -348,6 +385,7 @@ void GameModel::SetSaveFile(SaveFile * newSave) delete newSave; notifySaveChanged(); + UpdateQuickOptions(); } Simulation * GameModel::GetSimulation() @@ -476,6 +514,7 @@ void GameModel::SetDecoration(bool decorationState) { ren->decorations_enable = decorationState?1:0; notifyDecorationChanged(); + UpdateQuickOptions(); } bool GameModel::GetDecoration() @@ -749,4 +788,12 @@ void GameModel::notifyToolTipChanged() { observers[i]->NotifyToolTipChanged(this); } +} + +void GameModel::notifyQuickOptionsChanged() +{ + for(int i = 0; i < observers.size(); i++) + { + observers[i]->NotifyQuickOptionsChanged(this); + } } \ No newline at end of file diff --git a/src/game/GameModel.h b/src/game/GameModel.h index 2c8636a97..e41ff2b58 100644 --- a/src/game/GameModel.h +++ b/src/game/GameModel.h @@ -21,6 +21,7 @@ class GameView; class Simulation; class Renderer; +class QuickOption; class ToolSelection { public: @@ -43,6 +44,7 @@ private: vector observers; vector toolList; vector menuList; + vector quickOptions; Menu * activeMenu; int currentBrush; vector brushList; @@ -76,6 +78,7 @@ private: void notifyLogChanged(string entry); void notifyInfoTipChanged(); void notifyToolTipChanged(); + void notifyQuickOptionsChanged(); public: GameModel(); ~GameModel(); @@ -92,6 +95,9 @@ public: std::string GetInfoTip(); void BuildMenus(); + void BuildQuickOptionMenu(); + + void UpdateQuickOptions(); void SetVote(int direction); SaveInfo * GetSave(); @@ -108,6 +114,7 @@ public: void ClearSimulation(); vector GetMenuList(); vector GetToolList(); + vector GetQuickOptions(); void SetActiveMenu(Menu * menu); Menu * GetActiveMenu(); void FrameStep(int frames); diff --git a/src/game/GameView.cpp b/src/game/GameView.cpp index d86f2b694..39211fc45 100644 --- a/src/game/GameView.cpp +++ b/src/game/GameView.cpp @@ -12,6 +12,7 @@ #include "interface/Slider.h" #include "search/Thumbnail.h" #include "simulation/SaveRenderer.h" +#include "QuickOption.h" GameView::GameView(): 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 { 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 optionList = sender->GetQuickOptions(); + for(vector::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) { int currentY = YRES+MENUSIZE-48;//-(sender->GetMenuList().size()*16); diff --git a/src/game/GameView.h b/src/game/GameView.h index 4c0ad85d9..15bf0dcc5 100644 --- a/src/game/GameView.h +++ b/src/game/GameView.h @@ -54,6 +54,7 @@ private: Renderer * ren; Brush * activeBrush; //UI Elements + vector quickOptionButtons; vector menuButtons; vector toolButtons; vector notificationComponents; @@ -139,6 +140,7 @@ public: void NotifyLogChanged(GameModel * sender, string entry); void NotifyToolTipChanged(GameModel * sender); void NotifyInfoTipChanged(GameModel * sender); + void NotifyQuickOptionsChanged(GameModel * sender); virtual void ToolTip(ui::Component * sender, ui::Point mousePosition, std::string toolTip); @@ -163,6 +165,8 @@ public: class MenuAction; class ToolAction; + class OptionAction; + class OptionListener; }; #endif // GAMEVIEW_H diff --git a/src/game/QuickOption.h b/src/game/QuickOption.h new file mode 100644 index 000000000..838302905 --- /dev/null +++ b/src/game/QuickOption.h @@ -0,0 +1,76 @@ +#pragma once + +#include +#include + +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 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::iterator iter = listeners.begin(), end = listeners.end(); iter != end; ++iter) + // delete *iter; + } + + std::vector 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::iterator iter = listeners.begin(), end = listeners.end(); iter != end; ++iter) + (*iter)->OnValueChanged(this); + } + void Update() + { + for(std::vector::iterator iter = listeners.begin(), end = listeners.end(); iter != end; ++iter) + (*iter)->OnValueChanged(this); + } +}; + diff --git a/src/game/QuickOptions.h b/src/game/QuickOptions.h new file mode 100644 index 000000000..f194c1c18 --- /dev/null +++ b/src/game/QuickOptions.h @@ -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; + } +}; \ No newline at end of file diff --git a/src/graphics/Renderer.cpp b/src/graphics/Renderer.cpp index 73d9b63fa..f18dbbb33 100644 --- a/src/graphics/Renderer.cpp +++ b/src/graphics/Renderer.cpp @@ -39,9 +39,9 @@ void Renderer::RenderBegin() { draw_air(); + draw_grav(); render_parts(); render_fire(); - draw_grav(); DrawWalls(); DrawSigns(); #ifndef OGLR @@ -1899,6 +1899,9 @@ void Renderer::draw_grav() int x, y, i, ca; float nx, ny, dist; + if(!gravifyFieldEnabled) + return; + for (y=0; yg = g; this->sim = sim; diff --git a/src/graphics/Renderer.h b/src/graphics/Renderer.h index c292b80b3..721d39ec3 100644 --- a/src/graphics/Renderer.h +++ b/src/graphics/Renderer.h @@ -42,6 +42,7 @@ public: char * plasma_data; int emp_decor; // + bool gravifyFieldEnabled; int decorations_enable; Simulation * sim; Graphics * g;