DropDown UI component
This commit is contained in:
parent
4032a0469b
commit
7758fe52cb
@ -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),
|
||||
background(background),
|
||||
activeBackground(activeBackground),
|
||||
border(border),
|
||||
activeBorder(activeBorder),
|
||||
text(text),
|
||||
activeText(activeText)
|
||||
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(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 */
|
||||
|
@ -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();
|
||||
|
@ -26,6 +26,7 @@ Window::~Window()
|
||||
if(Components[i]==focusedComponent_)
|
||||
focusedComponent_ = NULL;
|
||||
}
|
||||
Components.clear();
|
||||
}
|
||||
|
||||
void Window::AddComponent(Component* c)
|
||||
|
@ -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()
|
||||
{
|
||||
|
@ -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();
|
||||
|
@ -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++)
|
||||
|
@ -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();
|
||||
};
|
||||
|
||||
|
@ -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);
|
||||
|
||||
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-24, 16), "Gravity Simulation Mode");
|
||||
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_)
|
||||
|
@ -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;
|
||||
|
@ -202,7 +202,7 @@ public:
|
||||
int photons[YRES][XRES];
|
||||
//
|
||||
int gravityMode;
|
||||
int airMode;
|
||||
//int airMode;
|
||||
int ngrav_enable;
|
||||
int legacy_enable;
|
||||
int aheat_enable;
|
||||
|
Reference in New Issue
Block a user