Text alignment for dropdown, make sign UI nice

This commit is contained in:
Simon Robertshaw 2012-05-13 21:11:02 +01:00
parent 41e1d28c56
commit 4bb90d0d79
3 changed files with 50 additions and 7 deletions

View File

@ -48,7 +48,7 @@ SignWindow::SignWindow(SignTool * tool_, Simulation * sim_, int signID_, ui::Poi
sim(sim_),
signPosition(position_)
{
ui::Label * messageLabel = new ui::Label(ui::Point(4, 18), ui::Point(Size.X-8, 60), "New sign");
ui::Label * messageLabel = new ui::Label(ui::Point(4, 2), ui::Point(Size.X-8, 14), "New sign");
messageLabel->SetAlignment(AlignLeft, AlignTop);
AddComponent(messageLabel);
@ -58,14 +58,14 @@ SignWindow::SignWindow(SignTool * tool_, Simulation * sim_, int signID_, ui::Poi
okayButton->SetActionCallback(new OkayAction(this));
AddComponent(okayButton);
justification = new ui::DropDown(ui::Point(4, 18), ui::Point(50, 16));
justification = new ui::DropDown(ui::Point(8, 38), ui::Point(50, 16));
AddComponent(justification);
justification->AddOption(std::pair<std::string, int>("Left", (int)sign::Left));
justification->AddOption(std::pair<std::string, int>("Centre", (int)sign::Centre));
justification->AddOption(std::pair<std::string, int>("Right", (int)sign::Right));
justification->SetOption(0);
textField = new ui::Textbox(ui::Point(4, 32), ui::Point(Size.X-8, 16), "");
textField = new ui::Textbox(ui::Point(8, 17), ui::Point(Size.X-16, 16), "");
textField->SetAlignment(AlignLeft, AlignBottom);
AddComponent(textField);

View File

@ -80,6 +80,9 @@ DropDown::DropDown(Point position, Point size):
Component(position, size),
isMouseInside(false),
optionIndex(-1),
textPosition(ui::Point(0, 0)),
textVAlign(AlignMiddle),
textHAlign(AlignLeft),
callback(NULL)
{
background = activeBackground = Colour(0, 0, 0);
@ -101,20 +104,52 @@ 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);
g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y, options[optionIndex].first, 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);
g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y, options[optionIndex].first, text.Red, text.Green, text.Blue, 255);
}
}
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;
}
}
std::pair<std::string, int> DropDown::GetOption()
{
if(optionIndex!=-1)
@ -131,6 +166,7 @@ void DropDown::Draw(const Point& screenPos)
if(options[i].first == option)
{
optionIndex = i;
TextPosition();
return;
}
}
@ -142,6 +178,7 @@ void DropDown::Draw(const Point& screenPos)
if(options[i].second == option)
{
optionIndex = i;
TextPosition();
return;
}
}
@ -162,6 +199,8 @@ void DropDown::Draw(const Point& screenPos)
{
if(options[i].first == option)
{
if(i == optionIndex)
optionIndex = -1;
options.erase(options.begin()+i);
goto start;
}

View File

@ -27,13 +27,17 @@ class DropDown: public ui::Component {
Colour background, activeBackground;
Colour border, activeBorder;
Colour text, activeText;
Point textPosition;
bool isMouseInside;
int optionIndex;
DropDownAction * callback;
std::vector<std::pair<std::string, int> > options;
HorizontalAlignment textHAlign;
VerticalAlignment textVAlign;
public:
DropDown(Point position, Point size);
std::pair<std::string, int> GetOption();
void TextPosition();
void SetOption(int option);
void SetOption(std::string option);
void AddOption(std::pair<std::string, int> option);