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/interface/DropDown.cpp | |
| parent | 4032a0469b1f40f7197468f34986e365bd6e7314 (diff) | |
| download | powder-7758fe52cb9ef78b562bc2587b17b6344d8829fe.zip powder-7758fe52cb9ef78b562bc2587b17b6344d8829fe.tar.gz | |
DropDown UI component
Diffstat (limited to 'src/interface/DropDown.cpp')
| -rw-r--r-- | src/interface/DropDown.cpp | 122 |
1 files changed, 109 insertions, 13 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 */ |
