2012-04-17 09:56:57 -05:00
|
|
|
/*
|
|
|
|
* DropDown.cpp
|
|
|
|
*
|
|
|
|
* Created on: Apr 16, 2012
|
|
|
|
* Author: Simon
|
|
|
|
*/
|
|
|
|
|
2012-05-13 14:00:22 -05:00
|
|
|
#include <iostream>
|
2012-05-14 14:47:14 -05:00
|
|
|
#include "Style.h"
|
2012-05-13 14:00:22 -05:00
|
|
|
#include "Button.h"
|
2012-04-17 09:56:57 -05:00
|
|
|
#include "DropDown.h"
|
|
|
|
|
|
|
|
namespace ui {
|
|
|
|
|
2012-05-13 14:00:22 -05:00
|
|
|
class ItemSelectedAction;
|
2012-04-17 09:56:57 -05:00
|
|
|
class DropDownWindow: public ui::Window {
|
2012-05-13 14:00:22 -05:00
|
|
|
friend class ItemSelectedAction;
|
2012-04-17 09:56:57 -05:00
|
|
|
Colour background, activeBackground;
|
|
|
|
Colour border, activeBorder;
|
|
|
|
Colour text, activeText;
|
2012-05-13 14:00:22 -05:00
|
|
|
DropDown * dropDown;
|
|
|
|
std::vector<Button> buttons;
|
2012-04-17 09:56:57 -05:00
|
|
|
bool isMouseInside;
|
|
|
|
public:
|
2012-05-13 14:00:22 -05:00
|
|
|
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):
|
2012-05-14 14:47:14 -05:00
|
|
|
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, 1+dropDown->options.size()*15)),
|
2012-05-13 14:00:22 -05:00
|
|
|
dropDown(dropDown),
|
2012-05-14 14:47:14 -05:00
|
|
|
background(dropDown->background),
|
2012-05-13 14:00:22 -05:00
|
|
|
activeBackground(dropDown->activeBackground),
|
|
|
|
border(dropDown->border),
|
|
|
|
activeBorder(dropDown->activeBorder),
|
|
|
|
text(dropDown->text),
|
|
|
|
activeText(dropDown->activeText)
|
2012-04-17 09:56:57 -05:00
|
|
|
{
|
2012-05-14 14:47:14 -05:00
|
|
|
int currentY = 1;
|
2012-05-13 14:00:22 -05:00
|
|
|
for(int i = 0; i < dropDown->options.size(); i++)
|
|
|
|
{
|
2012-05-14 14:47:14 -05:00
|
|
|
Button * tempButton = new Button(Point(1, currentY), Point(Size.X-2, 14), dropDown->options[i].first);
|
|
|
|
tempButton->SetTextColour(dropDown->text);
|
|
|
|
tempButton->SetBorderColour(dropDown->background);
|
2012-05-13 14:00:22 -05:00
|
|
|
tempButton->SetActionCallback(new ItemSelectedAction(this, dropDown->options[i].first));
|
|
|
|
AddComponent(tempButton);
|
2012-05-14 14:47:14 -05:00
|
|
|
currentY += 15;
|
2012-05-13 14:00:22 -05:00
|
|
|
}
|
2012-04-17 09:56:57 -05:00
|
|
|
}
|
|
|
|
virtual void OnDraw()
|
|
|
|
{
|
|
|
|
Graphics * g = ui::Engine::Ref().g;
|
2012-05-14 14:47:14 -05:00
|
|
|
g->fillrect(Position.X, Position.Y, Size.X, Size.Y, 100, 100, 100, 255);
|
|
|
|
g->drawrect(Position.X, Position.Y, Size.X, Size.Y, border.Red, border.Green, border.Blue, border.Alpha);
|
2012-04-17 09:56:57 -05:00
|
|
|
}
|
2012-05-13 14:00:22 -05:00
|
|
|
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]);
|
|
|
|
}
|
|
|
|
}
|
2012-04-17 09:56:57 -05:00
|
|
|
virtual ~DropDownWindow() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
DropDown::DropDown(Point position, Point size):
|
|
|
|
Component(position, size),
|
2012-05-13 14:00:22 -05:00
|
|
|
isMouseInside(false),
|
2012-05-13 14:54:29 -05:00
|
|
|
optionIndex(-1),
|
2012-05-13 15:11:02 -05:00
|
|
|
textPosition(ui::Point(0, 0)),
|
|
|
|
textVAlign(AlignMiddle),
|
|
|
|
textHAlign(AlignLeft),
|
2012-05-13 14:54:29 -05:00
|
|
|
callback(NULL)
|
2012-04-17 09:56:57 -05:00
|
|
|
{
|
2012-05-14 14:47:14 -05:00
|
|
|
activeText = text = Colour(255, 255, 255);
|
|
|
|
|
|
|
|
border = style::Colour::InactiveBorder;
|
|
|
|
activeBorder = style::Colour::ActiveBorder;
|
|
|
|
background = style::Colour::InactiveBackground;
|
|
|
|
activeBackground = style::Colour::ActiveBackground;
|
2012-04-17 09:56:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void DropDown::OnMouseClick(int x, int y, unsigned int button)
|
|
|
|
{
|
2012-05-13 14:00:22 -05:00
|
|
|
DropDownWindow * newWindow = new DropDownWindow(this);
|
|
|
|
ui::Engine().Ref().ShowWindow(newWindow);
|
2012-04-17 09:56:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void DropDown::Draw(const Point& screenPos)
|
|
|
|
{
|
|
|
|
Graphics * g = ui::Engine::Ref().g;
|
|
|
|
Point Position = screenPos;
|
|
|
|
if(isMouseInside)
|
|
|
|
{
|
|
|
|
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);
|
2012-05-13 14:00:22 -05:00
|
|
|
if(optionIndex!=-1)
|
2012-05-13 15:11:02 -05:00
|
|
|
g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y, options[optionIndex].first, activeText.Red, activeText.Green, activeText.Blue, 255);
|
2012-04-17 09:56:57 -05:00
|
|
|
}
|
|
|
|
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);
|
2012-05-13 14:00:22 -05:00
|
|
|
if(optionIndex!=-1)
|
2012-05-13 15:11:02 -05:00
|
|
|
g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y, options[optionIndex].first, text.Red, text.Green, text.Blue, 255);
|
2012-04-17 09:56:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2012-05-13 14:00:22 -05:00
|
|
|
|
2012-05-13 15:11:02 -05:00
|
|
|
void DropDown::TextPosition()
|
|
|
|
{
|
|
|
|
std::string displayText;
|
|
|
|
if(optionIndex!=-1)
|
|
|
|
displayText = options[optionIndex].first;
|
|
|
|
|
|
|
|
// Values 3 and 10 are for vertical padding of 3 pixels, middle uses 7 as that's the height of a capital
|
|
|
|
switch(textVAlign)
|
|
|
|
{
|
|
|
|
case AlignTop:
|
|
|
|
textPosition.Y = 3;
|
|
|
|
break;
|
|
|
|
case AlignMiddle:
|
|
|
|
textPosition.Y = (Size.Y-10)/2;
|
|
|
|
break;
|
|
|
|
case AlignBottom:
|
|
|
|
textPosition.Y = Size.Y-10;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(textHAlign)
|
|
|
|
{
|
|
|
|
case AlignLeft:
|
|
|
|
textPosition.X = 3;
|
|
|
|
break;
|
|
|
|
case AlignCentre:
|
|
|
|
textPosition.X = (Size.X-Graphics::textwidth((char *)displayText.c_str()))/2;
|
|
|
|
break;
|
|
|
|
case AlignRight:
|
|
|
|
textPosition.X = (Size.X-Graphics::textwidth((char *)displayText.c_str()))-2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-13 14:54:29 -05:00
|
|
|
std::pair<std::string, int> DropDown::GetOption()
|
|
|
|
{
|
|
|
|
if(optionIndex!=-1)
|
|
|
|
{
|
|
|
|
return options[optionIndex];
|
|
|
|
}
|
|
|
|
return std::pair<std::string, int>("", -1);
|
|
|
|
}
|
|
|
|
|
2012-05-13 14:00:22 -05:00
|
|
|
void DropDown::SetOption(std::string option)
|
|
|
|
{
|
|
|
|
for(int i = 0; i < options.size(); i++)
|
|
|
|
{
|
|
|
|
if(options[i].first == option)
|
|
|
|
{
|
|
|
|
optionIndex = i;
|
2012-05-13 15:11:02 -05:00
|
|
|
TextPosition();
|
2012-05-13 14:00:22 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void DropDown::SetOption(int option)
|
|
|
|
{
|
|
|
|
for(int i = 0; i < options.size(); i++)
|
|
|
|
{
|
|
|
|
if(options[i].second == option)
|
|
|
|
{
|
|
|
|
optionIndex = i;
|
2012-05-13 15:11:02 -05:00
|
|
|
TextPosition();
|
2012-05-13 14:00:22 -05:00
|
|
|
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)
|
|
|
|
{
|
2012-05-13 15:11:02 -05:00
|
|
|
if(i == optionIndex)
|
|
|
|
optionIndex = -1;
|
2012-05-13 14:00:22 -05:00
|
|
|
options.erase(options.begin()+i);
|
|
|
|
goto start;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void DropDown::SetOptions(std::vector<std::pair<std::string, int> > options)
|
|
|
|
{
|
|
|
|
this->options = options;
|
|
|
|
}
|
2012-04-17 09:56:57 -05:00
|
|
|
|
|
|
|
|
|
|
|
DropDown::~DropDown() {
|
2012-05-13 14:00:22 -05:00
|
|
|
if(callback)
|
|
|
|
delete callback;
|
2012-04-17 09:56:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
} /* namespace ui */
|