diff options
| author | Simon Robertshaw <simon@hardwired.org.uk> | 2012-05-13 19:00:22 (GMT) |
|---|---|---|
| committer | Simon Robertshaw <simon@hardwired.org.uk> | 2012-05-13 19:00:22 (GMT) |
| commit | 7758fe52cb9ef78b562bc2587b17b6344d8829fe (patch) | |
| tree | 92f45320d240148f6e518ed07aec480e329d4a25 /src | |
| parent | 4032a0469b1f40f7197468f34986e365bd6e7314 (diff) | |
| download | powder-7758fe52cb9ef78b562bc2587b17b6344d8829fe.zip powder-7758fe52cb9ef78b562bc2587b17b6344d8829fe.tar.gz | |
DropDown UI component
Diffstat (limited to 'src')
| -rw-r--r-- | src/interface/DropDown.cpp | 122 | ||||
| -rw-r--r-- | src/interface/DropDown.h | 19 | ||||
| -rw-r--r-- | src/interface/Window.cpp | 1 | ||||
| -rw-r--r-- | src/options/OptionsController.cpp | 8 | ||||
| -rw-r--r-- | src/options/OptionsController.h | 2 | ||||
| -rw-r--r-- | src/options/OptionsModel.cpp | 21 | ||||
| -rw-r--r-- | src/options/OptionsModel.h | 4 | ||||
| -rw-r--r-- | src/options/OptionsView.cpp | 41 | ||||
| -rw-r--r-- | src/simulation/SaveLoader.cpp | 7 | ||||
| -rw-r--r-- | src/simulation/Simulation.h | 2 |
10 files changed, 205 insertions, 22 deletions
diff --git a/src/interface/DropDown.cpp b/src/interface/DropDown.cpp index 6944e6f..ea7d56a 100644 --- a/src/interface/DropDown.cpp +++ b/src/interface/DropDown.cpp @@ -5,26 +5,53 @@ * Author: Simon */ +#include <iostream> +#include "Button.h" #include "DropDown.h" namespace ui { +class ItemSelectedAction; class DropDownWindow: public ui::Window { + friend class ItemSelectedAction; Colour background, activeBackground; Colour border, activeBorder; Colour text, activeText; + DropDown * dropDown; + std::vector<Button> buttons; bool isMouseInside; public: - DropDownWindow(Point position, Point size, Colour background, Colour activeBackground, Colour border, Colour activeBorder, Colour text, Colour activeText): - Window(position, size), + class ItemSelectedAction: public ButtonAction + { + DropDownWindow * window; + std::string option; + public: + ItemSelectedAction(DropDownWindow * window, std::string option): window(window), option(option) { } + virtual void ActionCallback(ui::Button *sender) + { + ui::Engine::Ref().CloseWindow(); + window->setOption(option); + window->SelfDestruct(); + } + }; + DropDownWindow(DropDown * dropDown): + Window(ui::Point(dropDown->Position.X+dropDown->GetParentWindow()->Position.X-5, dropDown->Position.Y+dropDown->GetParentWindow()->Position.Y-3), ui::Point(dropDown->Size.X+10, dropDown->options.size()*13)), + dropDown(dropDown), background(background), - activeBackground(activeBackground), - border(border), - activeBorder(activeBorder), - text(text), - activeText(activeText) + activeBackground(dropDown->activeBackground), + border(dropDown->border), + activeBorder(dropDown->activeBorder), + text(dropDown->text), + activeText(dropDown->activeText) { - + int currentY = 0; + for(int i = 0; i < dropDown->options.size(); i++) + { + Button * tempButton = new Button(Point(0, currentY), Point(Size.X, 14), dropDown->options[i].first); + tempButton->SetActionCallback(new ItemSelectedAction(this, dropDown->options[i].first)); + AddComponent(tempButton); + currentY += 13; + } } virtual void OnDraw() { @@ -32,20 +59,36 @@ public: g->fillrect(Position.X, Position.Y, Size.X, Size.Y, background.Red, background.Green, background.Blue, 255); g->drawrect(Position.X, Position.Y, Size.X, Size.Y, border.Red, border.Green, border.Blue, 255); } + void setOption(std::string option) + { + dropDown->SetOption(option); + if(dropDown->callback) + { + int optionIndex = 0; + for(optionIndex = 0; optionIndex < dropDown->options.size(); optionIndex++) + { + if(option == dropDown->options[optionIndex].first) + break; + } + dropDown->callback->OptionChanged(dropDown, dropDown->options[optionIndex]); + } + } virtual ~DropDownWindow() {} }; DropDown::DropDown(Point position, Point size): Component(position, size), - isMouseInside(false) + isMouseInside(false), + optionIndex(-1) { - activeText = background = Colour(0, 0, 0); - text = activeBackground = border = activeBorder = Colour(255, 255, 255); + background = activeBackground = Colour(0, 0, 0); + activeText = text = activeBackground = border = activeBorder = Colour(255, 255, 255); } void DropDown::OnMouseClick(int x, int y, unsigned int button) { - ui::Engine().Ref().ShowWindow(new DropDownWindow(ui::Point(50, 50), ui::Point(50, 50), background, activeBackground, border, activeBorder, text, activeText)); + DropDownWindow * newWindow = new DropDownWindow(this); + ui::Engine().Ref().ShowWindow(newWindow); } void DropDown::Draw(const Point& screenPos) @@ -56,20 +99,73 @@ void DropDown::Draw(const Point& screenPos) { g->fillrect(Position.X-1, Position.Y-1, Size.X+2, Size.Y+2, activeBackground.Red, activeBackground.Green, activeBackground.Blue, 255); g->drawrect(Position.X, Position.Y, Size.X, Size.Y, activeBorder.Red, activeBorder.Green, activeBorder.Blue, 255); + if(optionIndex!=-1) + g->drawtext(Position.X, Position.Y+1, options[optionIndex].first, activeText.Red, activeText.Green, activeText.Blue, 255); //g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y+1, displayText, activeText.Red, activeText.Green, activeText.Blue, 255); } else { g->fillrect(Position.X, Position.Y, Size.X, Size.Y, background.Red, background.Green, background.Blue, 255); g->drawrect(Position.X, Position.Y, Size.X, Size.Y, border.Red, border.Green, border.Blue, 255); + if(optionIndex!=-1) + g->drawtext(Position.X, Position.Y+1, options[optionIndex].first, text.Red, text.Green, text.Blue, 255); //g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y+1, displayText, text.Red, text.Green, text.Blue, 255); } } + + void DropDown::SetOption(std::string option) + { + for(int i = 0; i < options.size(); i++) + { + if(options[i].first == option) + { + optionIndex = i; + return; + } + } + } + void DropDown::SetOption(int option) + { + for(int i = 0; i < options.size(); i++) + { + if(options[i].second == option) + { + optionIndex = i; + return; + } + } + } + void DropDown::AddOption(std::pair<std::string, int> option) + { + for(int i = 0; i < options.size(); i++) + { + if(options[i] == option) + return; + } + options.push_back(option); + } + void DropDown::RemoveOption(std::string option) + { + start: + for(int i = 0; i < options.size(); i++) + { + if(options[i].first == option) + { + options.erase(options.begin()+i); + goto start; + } + } + } + void DropDown::SetOptions(std::vector<std::pair<std::string, int> > options) + { + this->options = options; + } DropDown::~DropDown() { - // TODO Auto-generated destructor stub + if(callback) + delete callback; } } /* namespace ui */ diff --git a/src/interface/DropDown.h b/src/interface/DropDown.h index 619ec5a..34310df 100644 --- a/src/interface/DropDown.h +++ b/src/interface/DropDown.h @@ -8,18 +8,37 @@ #ifndef DROPDOWN_H_ #define DROPDOWN_H_ +#include <utility> #include "Component.h" #include "Colour.h" namespace ui { +class DropDown; +class DropDownWindow; +class DropDownAction +{ +public: + virtual void OptionChanged(DropDown * sender, std::pair<std::string, int> newOption) {} + virtual ~DropDownAction() {} +}; class DropDown: public ui::Component { + friend class DropDownWindow; Colour background, activeBackground; Colour border, activeBorder; Colour text, activeText; bool isMouseInside; + int optionIndex; + DropDownAction * callback; + std::vector<std::pair<std::string, int> > options; public: DropDown(Point position, Point size); + void SetOption(int option); + void SetOption(std::string option); + void AddOption(std::pair<std::string, int> option); + void RemoveOption(std::string option); + void SetOptions(std::vector<std::pair<std::string, int> > options); + void SetActionCallback(DropDownAction * action) { callback = action;} virtual void Draw(const Point& screenPos); virtual void OnMouseClick(int x, int y, unsigned int button); virtual ~DropDown(); diff --git a/src/interface/Window.cpp b/src/interface/Window.cpp index 2de6463..86221ab 100644 --- a/src/interface/Window.cpp +++ b/src/interface/Window.cpp @@ -26,6 +26,7 @@ Window::~Window() if(Components[i]==focusedComponent_) focusedComponent_ = NULL; } + Components.clear(); } void Window::AddComponent(Component* c) diff --git a/src/options/OptionsController.cpp b/src/options/OptionsController.cpp index b39509c..85f2a2f 100644 --- a/src/options/OptionsController.cpp +++ b/src/options/OptionsController.cpp @@ -38,6 +38,14 @@ void OptionsController::SetWaterEqualisation(bool state) { model->SetWaterEqualisation(state); } +void OptionsController::SetGravityMode(int gravityMode) +{ + model->SetGravityMode(gravityMode); +} +void OptionsController::SetAirMode(int airMode) +{ + model->SetAirMode(airMode); +} OptionsView * OptionsController::GetView() { diff --git a/src/options/OptionsController.h b/src/options/OptionsController.h index 18bbf4e..19f0e9b 100644 --- a/src/options/OptionsController.h +++ b/src/options/OptionsController.h @@ -26,6 +26,8 @@ public: void SetAmbientHeatSimulation(bool state); void SetNewtonianGravity(bool state); void SetWaterEqualisation(bool state); + void SetGravityMode(int gravityMode); + void SetAirMode(int airMode); void Exit(); OptionsView * GetView(); virtual ~OptionsController(); diff --git a/src/options/OptionsModel.cpp b/src/options/OptionsModel.cpp index c0a69f5..3891d41 100644 --- a/src/options/OptionsModel.cpp +++ b/src/options/OptionsModel.cpp @@ -5,6 +5,7 @@ * Author: Simon */ +#include "Air.h" #include "OptionsModel.h" OptionsModel::OptionsModel(Simulation * sim_) { @@ -64,6 +65,26 @@ void OptionsModel::SetWaterEqualisation(bool state) notifySettingsChanged(); } +int OptionsModel::GetAirMode() +{ + return sim->air->airMode; +} +void OptionsModel::SetAirMode(int airMode) +{ + sim->air->airMode = airMode; + notifySettingsChanged(); +} + +int OptionsModel::GetGravityMode() +{ + return sim->gravityMode; +} +void OptionsModel::SetGravityMode(int gravityMode) +{ + sim->gravityMode = gravityMode; + notifySettingsChanged(); +} + void OptionsModel::notifySettingsChanged() { for(int i = 0; i < observers.size(); i++) diff --git a/src/options/OptionsModel.h b/src/options/OptionsModel.h index 903f3b7..8f83fc8 100644 --- a/src/options/OptionsModel.h +++ b/src/options/OptionsModel.h @@ -28,6 +28,10 @@ public: void SetNewtonianGravity(bool state); bool GetWaterEqualisation(); void SetWaterEqualisation(bool state); + int GetAirMode(); + void SetAirMode(int airMode); + int GetGravityMode(); + void SetGravityMode(int gravityMode); virtual ~OptionsModel(); }; diff --git a/src/options/OptionsView.cpp b/src/options/OptionsView.cpp index d863844..d1066a8 100644 --- a/src/options/OptionsView.cpp +++ b/src/options/OptionsView.cpp @@ -8,6 +8,7 @@ #include "OptionsView.h" #include "interface/Button.h" #include "interface/Label.h" +#include "interface/DropDown.h" OptionsView::OptionsView(): ui::Window(ui::Point(-1, -1), ui::Point(300, 300)){ @@ -77,14 +78,42 @@ OptionsView::OptionsView(): tempLabel->SetAlignment(AlignLeft, AlignMiddle); AddComponent(tempLabel); - airMode = new ui::DropDown(ui::Point(Size.X-55, 143), ui::Point(50, 16));//, "Water equalisation \bgIntroduced in version 61"); - //airMode->SetActionCallback(new WaterEqualisationAction(this)); + class AirModeChanged: public ui::DropDownAction + { + OptionsView * v; + public: + AirModeChanged(OptionsView * v): v(v) { } + virtual void OptionChanged(ui::DropDown * sender, std::pair<std::string, int> option) { v->c->SetAirMode(option.second); } + }; + airMode = new ui::DropDown(ui::Point(Size.X-85, 143), ui::Point(80, 16)); AddComponent(airMode); - tempLabel = new ui::Label(ui::Point(3, 143), ui::Point(Size.X-24, 16), "Air Simulation Mode"); + airMode->AddOption(std::pair<std::string, int>("On", 0)); + airMode->AddOption(std::pair<std::string, int>("Pressure off", 1)); + airMode->AddOption(std::pair<std::string, int>("Velocity off", 2)); + airMode->AddOption(std::pair<std::string, int>("Off", 3)); + airMode->AddOption(std::pair<std::string, int>("No Update", 4)); + airMode->SetActionCallback(new AirModeChanged(this)); + + tempLabel = new ui::Label(ui::Point(3, 143), ui::Point(Size.X-90, 16), "Air Simulation Mode"); tempLabel->SetAlignment(AlignLeft, AlignMiddle); AddComponent(tempLabel); - - tempLabel = new ui::Label(ui::Point(3, 163), ui::Point(Size.X-24, 16), "Gravity Simulation Mode"); + + class GravityModeChanged: public ui::DropDownAction + { + OptionsView * v; + public: + GravityModeChanged(OptionsView * v): v(v) { } + virtual void OptionChanged(ui::DropDown * sender, std::pair<std::string, int> option) { v->c->SetGravityMode(option.second); } + }; + + gravityMode = new ui::DropDown(ui::Point(Size.X-85, 163), ui::Point(80, 16)); + AddComponent(gravityMode); + gravityMode->AddOption(std::pair<std::string, int>("Vertical", 0)); + gravityMode->AddOption(std::pair<std::string, int>("Off", 1)); + gravityMode->AddOption(std::pair<std::string, int>("Radial", 2)); + gravityMode->SetActionCallback(new GravityModeChanged(this)); + + tempLabel = new ui::Label(ui::Point(3, 163), ui::Point(Size.X-90, 16), "Gravity Simulation Mode"); tempLabel->SetAlignment(AlignLeft, AlignMiddle); AddComponent(tempLabel); @@ -111,6 +140,8 @@ void OptionsView::NotifySettingsChanged(OptionsModel * sender) ambientHeatSimulation->SetChecked(sender->GetAmbientHeatSimulation()); newtonianGravity->SetChecked(sender->GetNewtonianGravity()); waterEqualisation->SetChecked(sender->GetWaterEqualisation()); + airMode->SetOption(sender->GetAirMode()); + gravityMode->SetOption(sender->GetGravityMode()); } void OptionsView::AttachController(OptionsController * c_) diff --git a/src/simulation/SaveLoader.cpp b/src/simulation/SaveLoader.cpp index b07ba08..7db6895 100644 --- a/src/simulation/SaveLoader.cpp +++ b/src/simulation/SaveLoader.cpp @@ -7,6 +7,7 @@ #include <bzlib.h> #include <cmath> +#include "Air.h" #include "SaveLoader.h" //!TODO: enum for LoadSave return @@ -118,7 +119,7 @@ int SaveLoader::PSVLoad(unsigned char * data, int dataLength, Simulation * sim, } if (ver>=46 && replace) { sim->gravityMode = ((c[3]>>2)&0x03);// | ((c[3]>>2)&0x01); - sim->airMode = ((c[3]>>4)&0x07);// | ((c[3]>>4)&0x02) | ((c[3]>>4)&0x01); + sim->air->airMode = ((c[3]>>4)&0x07);// | ((c[3]>>4)&0x02) | ((c[3]>>4)&0x01); } if (ver>=49 && replace) { tempGrav = ((c[3]>>7)&0x01); @@ -170,7 +171,7 @@ int SaveLoader::PSVLoad(unsigned char * data, int dataLength, Simulation * sim, { if (ver<46) { sim->gravityMode = 0; - sim->airMode = 0; + sim->air->airMode = 0; } sim->clear_sim(); } @@ -904,7 +905,7 @@ unsigned char * SaveLoader::PSVBuild(int & dataLength, Simulation * sim, int ori c[0] = 0x50; //0x66; c[1] = 0x53; //0x75; c[2] = 0x76; //0x43; - c[3] = sim->legacy_enable|((sim->sys_pause<<1)&0x02)|((sim->gravityMode<<2)&0x0C)|((sim->airMode<<4)&0x70)|((sim->ngrav_enable<<7)&0x80); + c[3] = sim->legacy_enable|((sim->sys_pause<<1)&0x02)|((sim->gravityMode<<2)&0x0C)|((sim->air->airMode<<4)&0x70)|((sim->ngrav_enable<<7)&0x80); c[4] = SAVE_VERSION; c[5] = CELL; c[6] = bw; diff --git a/src/simulation/Simulation.h b/src/simulation/Simulation.h index f4f8f5b..d200106 100644 --- a/src/simulation/Simulation.h +++ b/src/simulation/Simulation.h @@ -202,7 +202,7 @@ public: int photons[YRES][XRES]; // int gravityMode; - int airMode; + //int airMode; int ngrav_enable; int legacy_enable; int aheat_enable; |
