diff options
| author | Simon Robertshaw <simon@hardwired.org.uk> | 2012-05-15 17:13:17 (GMT) |
|---|---|---|
| committer | Simon Robertshaw <simon@hardwired.org.uk> | 2012-05-15 17:13:17 (GMT) |
| commit | 136675b56a8a1862afb41ccee5c14e93e483b964 (patch) | |
| tree | 8f679477c5e1c0984a5cb9c169e339c1ca0d6e0c /src/interface | |
| parent | 45563e97e813cfd21724ad1111e5de3e04679e1a (diff) | |
| download | powder-136675b56a8a1862afb41ccee5c14e93e483b964.zip powder-136675b56a8a1862afb41ccee5c14e93e483b964.tar.gz | |
Move style into Component
Diffstat (limited to 'src/interface')
| -rw-r--r-- | src/interface/Appearance.cpp | 32 | ||||
| -rw-r--r-- | src/interface/Appearance.h | 53 | ||||
| -rw-r--r-- | src/interface/Border.h | 64 | ||||
| -rw-r--r-- | src/interface/Button.cpp | 85 | ||||
| -rw-r--r-- | src/interface/Button.h | 21 | ||||
| -rw-r--r-- | src/interface/Component.cpp | 73 | ||||
| -rw-r--r-- | src/interface/Component.h | 21 | ||||
| -rw-r--r-- | src/interface/DropDown.cpp | 81 | ||||
| -rw-r--r-- | src/interface/DropDown.h | 7 | ||||
| -rw-r--r-- | src/interface/Label.cpp | 44 | ||||
| -rw-r--r-- | src/interface/Label.h | 9 | ||||
| -rw-r--r-- | src/interface/Textbox.cpp | 53 | ||||
| -rw-r--r-- | src/interface/Textbox.h | 6 |
13 files changed, 292 insertions, 257 deletions
diff --git a/src/interface/Appearance.cpp b/src/interface/Appearance.cpp new file mode 100644 index 0000000..c089d76 --- /dev/null +++ b/src/interface/Appearance.cpp @@ -0,0 +1,32 @@ +// +// Appearance.cpp +// The Powder Toy +// +// Created by Simon Robertshaw on 15/05/2012. +// + +#include <iostream> +#include "Appearance.h" + +namespace ui +{ + Appearance::Appearance(): + HorizontalAlign(AlignCentre), + VerticalAlign(AlignMiddle), + + BackgroundHover(30, 30, 30), + BackgroundInactive(0, 0, 0), + BackgroundActive(255, 255, 255), + + TextHover(255, 255, 255), + TextInactive(255, 255, 255), + TextActive(0, 0, 0), + + BorderHover(255, 255, 255), + BorderInactive(200, 200, 200), + BorderActive(255, 255, 255), + Margin(1, 4), + + icon(NoIcon) + {}; +}
\ No newline at end of file diff --git a/src/interface/Appearance.h b/src/interface/Appearance.h new file mode 100644 index 0000000..6b3c44f --- /dev/null +++ b/src/interface/Appearance.h @@ -0,0 +1,53 @@ +// +// Appearance.h +// The Powder Toy +// +// Created by Simon Robertshaw on 15/05/2012. +// + +#ifndef The_Powder_Toy_Appearance_h +#define The_Powder_Toy_Appearance_h + +#include "Border.h" +#include "Colour.h" +#include "Graphics.h" + +namespace ui +{ + class Appearance + { + public: + enum HorizontalAlignment + { + AlignLeft, AlignCentre, AlignRight + }; + + enum VerticalAlignment + { + AlignTop, AlignMiddle, AlignBottom + }; + + VerticalAlignment VerticalAlign; + HorizontalAlignment HorizontalAlign; + + ui::Colour BackgroundHover; + ui::Colour BackgroundInactive; + ui::Colour BackgroundActive; + + ui::Colour TextHover; + ui::Colour TextInactive; + ui::Colour TextActive; + + ui::Colour BorderHover; + ui::Colour BorderInactive; + ui::Colour BorderActive; + + ui::Border Margin; + + Icon icon; + + Appearance(); + }; +} + +#endif diff --git a/src/interface/Border.h b/src/interface/Border.h new file mode 100644 index 0000000..b5ae505 --- /dev/null +++ b/src/interface/Border.h @@ -0,0 +1,64 @@ +#pragma once +#include "Platform.h" + +namespace ui +{ + + struct Border + { +#if ENABLE_FLOAT_UI +# define BORDER_T float +#else +# define BORDER_T int +#endif + + BORDER_T Top; + BORDER_T Right; + BORDER_T Bottom; + BORDER_T Left; + + Border(BORDER_T all): + Top(all), + Right(all), + Bottom(all), + Left(all) + { + } + + Border(BORDER_T v, BORDER_T h): + Top(v), + Right(h), + Bottom(v), + Left(h) + { + } + + Border(BORDER_T top, BORDER_T right, BORDER_T bottom, BORDER_T left): + Top(top), + Right(right), + Bottom(bottom), + Left(left) + { + } + + inline bool operator == (const Border& v) const + { + return (Top == v.Top || Right == v.Right || Bottom == v.Bottom || Left == v.Left); + } + + inline bool operator != (const Border& v) const + { + return (Top != v.Top || Right != v.Right || Bottom != v.Bottom || Left != v.Left); + } + + inline void operator = (const Border& v) + { + Top = v.Top; + Right = v.Right; + Bottom = v.Bottom; + Left = v.Left; + } + + }; + +} diff --git a/src/interface/Button.cpp b/src/interface/Button.cpp index 5f3a12a..93cf2a2 100644 --- a/src/interface/Button.cpp +++ b/src/interface/Button.cpp @@ -21,15 +21,8 @@ Button::Button(Point position, Point size, std::string buttonText): isTogglable(false), toggle(false), actionCallback(NULL), - textPosition(ui::Point(0, 0)), - textVAlign(AlignMiddle), - textHAlign(AlignLeft), - Enabled(true), - icon(NoIcon) + Enabled(true) { - activeText = background = Colour(0, 0, 0); - text = activeBackground = border = activeBorder = Colour(255, 255, 255); - TextPosition(); } @@ -38,63 +31,20 @@ void Button::TextPosition() buttonDisplayText = ButtonText; if(buttonDisplayText.length()) { - if(Graphics::textwidth((char *)buttonDisplayText.c_str()) > Size.X - (icon? 22 : 0)) + if(Graphics::textwidth((char *)buttonDisplayText.c_str()) > Size.X - (Appearance.icon? 22 : 0)) { - int position = Graphics::textwidthx((char *)buttonDisplayText.c_str(), Size.X - (icon? 38 : 22)); + int position = Graphics::textwidthx((char *)buttonDisplayText.c_str(), Size.X - (Appearance.icon? 38 : 22)); buttonDisplayText = buttonDisplayText.erase(position, buttonDisplayText.length()-position); buttonDisplayText += "..."; } } - // 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; - } - - if(icon) - { - switch(textHAlign) - { - case AlignLeft: - textPosition.X = 3+17; - break; - case AlignCentre: - textPosition.X = (((Size.X-14)-Graphics::textwidth((char *)buttonDisplayText.c_str()))/2)+17; - break; - case AlignRight: - textPosition.X = (((Size.X-14)-Graphics::textwidth((char *)buttonDisplayText.c_str()))-2)+17; - break; - } - } - else - { - switch(textHAlign) - { - case AlignLeft: - textPosition.X = 3; - break; - case AlignCentre: - textPosition.X = (Size.X-Graphics::textwidth((char *)buttonDisplayText.c_str()))/2; - break; - case AlignRight: - textPosition.X = (Size.X-Graphics::textwidth((char *)buttonDisplayText.c_str()))-2; - break; - } - } + Component::TextPosition(buttonDisplayText); } void Button::SetIcon(Icon icon) { - this->icon = icon; + Appearance.icon = icon; TextPosition(); } @@ -127,31 +77,36 @@ inline void Button::SetToggleState(bool state) void Button::Draw(const Point& screenPos) { + if(!drawn) + { + TextPosition(); + drawn = true; + } Graphics * g = ui::Engine::Ref().g; Point Position = screenPos; if(Enabled) { if(isButtonDown || (isTogglable && toggle)) { - 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); - g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y+1, buttonDisplayText, activeText.Red, activeText.Green, activeText.Blue, 255); + g->fillrect(Position.X+1, Position.Y+1, Size.X-2, Size.Y-2, Appearance.BackgroundActive.Red, Appearance.BackgroundActive.Green, Appearance.BackgroundActive.Blue, 255); + g->drawrect(Position.X, Position.Y, Size.X, Size.Y, Appearance.BorderActive.Red, Appearance.BorderActive.Green, Appearance.BorderActive.Blue, 255); + g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y+1, buttonDisplayText, Appearance.TextActive.Red, Appearance.TextActive.Green, Appearance.TextActive.Blue, 255); } else { - g->fillrect(Position.X+1, Position.Y+1, Size.X-2, Size.Y-2, background.Red, background.Green, background.Blue, 255); - g->drawrect(Position.X, Position.Y, Size.X, Size.Y, border.Red, border.Green, border.Blue, 255); - g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y+1, buttonDisplayText, text.Red, text.Green, text.Blue, 255); + g->fillrect(Position.X+1, Position.Y+1, Size.X-2, Size.Y-2, Appearance.BackgroundInactive.Red, Appearance.BackgroundInactive.Green, Appearance.BackgroundInactive.Blue, 255); + g->drawrect(Position.X, Position.Y, Size.X, Size.Y, Appearance.BorderInactive.Red, Appearance.BorderInactive.Green, Appearance.BorderInactive.Blue, 255); + g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y+1, buttonDisplayText, Appearance.TextInactive.Red, Appearance.TextInactive.Green, Appearance.TextInactive.Blue, 255); } } else { - g->fillrect(Position.X+1, Position.Y+1, Size.X-2, Size.Y-2, background.Red, background.Green, background.Blue, 180); + g->fillrect(Position.X+1, Position.Y+1, Size.X-2, Size.Y-2, Appearance.BackgroundInactive.Red, Appearance.BackgroundInactive.Green, Appearance.BackgroundInactive.Blue, 180); g->drawrect(Position.X, Position.Y, Size.X, Size.Y, 180, 180, 180, 255); - g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y+1, buttonDisplayText, 180, 180, 180, 255); + g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y, buttonDisplayText, 180, 180, 180, 255); } - if(icon) - g->draw_icon(Position.X+3, Position.Y+textPosition.Y, icon); + if(Appearance.icon) + g->draw_icon(Position.X+iconPosition.X, Position.Y+iconPosition.Y, Appearance.icon); } void Button::OnMouseUp(int x, int y, unsigned int button) diff --git a/src/interface/Button.h b/src/interface/Button.h index 6f485cb..61430ad 100644 --- a/src/interface/Button.h +++ b/src/interface/Button.h @@ -29,7 +29,6 @@ public: Button(Point position = Point(0, 0), Point size = Point(0, 0), std::string buttonText = ""); virtual ~Button(); - Icon icon; bool Toggleable; bool Enabled; @@ -42,6 +41,7 @@ public: virtual void Draw(const Point& screenPos); + virtual void TextPosition(); inline bool GetState() { return state; } virtual void DoAction(); //action of button what ever it may be void SetTogglable(bool isTogglable); @@ -50,34 +50,15 @@ public: inline void SetToggleState(bool state); void SetActionCallback(ButtonAction * action); ButtonAction * GetActionCallback() { return actionCallback; } - void TextPosition(); void SetText(std::string buttonText); - - HorizontalAlignment GetHAlignment() { return textHAlign; } - VerticalAlignment GetVAlignment() { return textVAlign; } - void SetAlignment(HorizontalAlignment hAlign, VerticalAlignment vAlign) { textHAlign = hAlign; textVAlign = vAlign; TextPosition(); } - - void SetBackgroundColour(Colour background) { this->background = background; } - void SetActiveBackgroundColour(Colour background) { this->activeBackground = background; } - void SetBorderColour(Colour border) { this->border = border; } - void SetActiveBorderColour(Colour border) { this->activeBorder = border; } - void SetTextColour(Colour text) { this->text = text; } - void SetActiveTextColour(Colour text) { this->activeText = text; } - void SetIcon(Icon icon); protected: - Colour background, activeBackground; - Colour border, activeBorder; - Colour text, activeText; std::string buttonDisplayText; std::string ButtonText; bool isButtonDown, state, isMouseInside, isTogglable, toggle; ButtonAction * actionCallback; - ui::Point textPosition; - HorizontalAlignment textHAlign; - VerticalAlignment textVAlign; }; } diff --git a/src/interface/Component.cpp b/src/interface/Component.cpp index 0efc2b1..aa9f8be 100644 --- a/src/interface/Component.cpp +++ b/src/interface/Component.cpp @@ -1,4 +1,5 @@ //#include "Platform.h" +#include <iostream> #include "interface/Component.h" #include "interface/Engine.h" #include "interface/Point.h" @@ -13,18 +14,24 @@ Component::Component(Window* parent_state): Position(Point(0,0)), Size(Point(0,0)), Locked(false), - Visible(true) + Visible(true), + textPosition(0, 0), + iconPosition(0, 0), + drawn(false) { } Component::Component(Point position, Point size): - parentstate_(NULL), + parentstate_(0), _parent(NULL), Position(position), Size(size), Locked(false), - Visible(true) + Visible(true), + textPosition(0, 0), + iconPosition(0, 0), + drawn(false) { } @@ -35,9 +42,66 @@ Component::Component(): Position(Point(0,0)), Size(Point(0,0)), Locked(false), - Visible(true) + Visible(true), + textPosition(0, 0), + iconPosition(0, 0), + drawn(false) +{ + +} + +void Component::Refresh() +{ + drawn = false; +} + +void Component::TextPosition(std::string displayText) { + textPosition = ui::Point(0, 0); + + int textWidth, textHeight = 10; + Graphics::textsize((char*)displayText.c_str(), textWidth, textHeight); + textHeight-=3; + textWidth-=1; + if(Appearance.icon) + { + textWidth += 15; + } + + int textAreaWidth = Size.X-(Appearance.Margin.Right+Appearance.Margin.Left); + int textAreaHeight = Size.Y-(Appearance.Margin.Top+Appearance.Margin.Bottom); + + switch(Appearance.VerticalAlign) + { + case ui::Appearance::AlignTop: + textPosition.Y = Appearance.Margin.Top; + break; + case ui::Appearance::AlignMiddle: + textPosition.Y = Appearance.Margin.Top+((textAreaHeight-textHeight)/2); + break; + case ui::Appearance::AlignBottom: + textPosition.Y = Size.Y-(textHeight+Appearance.Margin.Bottom); + break; + } + + switch(Appearance.HorizontalAlign) + { + case ui::Appearance::AlignLeft: + textPosition.X = Appearance.Margin.Left; + break; + case ui::Appearance::AlignCentre: + textPosition.X = Appearance.Margin.Left+((textAreaWidth-textWidth)/2); + break; + case ui::Appearance::AlignRight: + textPosition.X = Size.X-(textWidth+Appearance.Margin.Right); + break; + } + if(Appearance.icon) + { + iconPosition = textPosition-ui::Point(0, 1); + textPosition.X += 15; + } } bool Component::IsFocused() const @@ -88,6 +152,7 @@ void Component::SetParent(Panel* new_parent) void Component::Draw(const Point& screenPos) { + drawn = true; } void Component::Tick(float dt) diff --git a/src/interface/Component.h b/src/interface/Component.h index 2735736..a4fde39 100644 --- a/src/interface/Component.h +++ b/src/interface/Component.h @@ -1,5 +1,6 @@ #pragma once +#include "Appearance.h" #include "Point.h" #include "Window.h" #include "Platform.h" @@ -15,7 +16,14 @@ namespace ui * *See sys::XComponent */ class Component - { + { + private: + Window* parentstate_; + Panel* _parent; + protected: + bool drawn; + ui::Point textPosition; + ui::Point iconPosition; public: Component(Window* parent_state); Component(Point position, Point size); @@ -31,6 +39,13 @@ namespace ui bool Locked; bool Visible; + ui::Appearance Appearance; + //virtual void SetAppearance(ui::Appearance); + //ui::Appearance GetAppearance(); + virtual void TextPosition(std::string); + + void Refresh(); + /* See the parent of this component. * If new_parent is NULL, this component will have no parent. (THIS DOES NOT delete THE COMPONENT. See XComponent::RemoveChild) */ @@ -196,9 +211,5 @@ namespace ui // alt: Alternate key is released. /// virtual void OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt); - - private: - Window* parentstate_; - Panel* _parent; }; } diff --git a/src/interface/DropDown.cpp b/src/interface/DropDown.cpp index 5ad255a..9748d7a 100644 --- a/src/interface/DropDown.cpp +++ b/src/interface/DropDown.cpp @@ -15,9 +15,7 @@ namespace ui { class ItemSelectedAction; class DropDownWindow: public ui::Window { friend class ItemSelectedAction; - Colour background, activeBackground; - Colour border, activeBorder; - Colour text, activeText; + Appearance appearance; DropDown * dropDown; std::vector<Button> buttons; bool isMouseInside; @@ -38,19 +36,13 @@ public: 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, 1+dropDown->options.size()*15)), dropDown(dropDown), - background(dropDown->background), - activeBackground(dropDown->activeBackground), - border(dropDown->border), - activeBorder(dropDown->activeBorder), - text(dropDown->text), - activeText(dropDown->activeText) + appearance(dropDown->Appearance) { int currentY = 1; for(int i = 0; i < dropDown->options.size(); i++) { Button * tempButton = new Button(Point(1, currentY), Point(Size.X-2, 14), dropDown->options[i].first); - tempButton->SetTextColour(dropDown->text); - tempButton->SetBorderColour(dropDown->background); + tempButton->Appearance = appearance; tempButton->SetActionCallback(new ItemSelectedAction(this, dropDown->options[i].first)); AddComponent(tempButton); currentY += 15; @@ -60,7 +52,7 @@ public: { Graphics * g = ui::Engine::Ref().g; 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); + g->drawrect(Position.X, Position.Y, Size.X, Size.Y, appearance.BackgroundInactive.Red, appearance.BackgroundInactive.Green, appearance.BackgroundInactive.Blue, appearance.BackgroundInactive.Alpha); } void setOption(std::string option) { @@ -83,17 +75,8 @@ DropDown::DropDown(Point position, Point size): Component(position, size), isMouseInside(false), optionIndex(-1), - textPosition(ui::Point(0, 0)), - textVAlign(AlignMiddle), - textHAlign(AlignLeft), callback(NULL) { - activeText = text = Colour(255, 255, 255); - - border = style::Colour::InactiveBorder; - activeBorder = style::Colour::ActiveBorder; - background = style::Colour::InactiveBackground; - activeBackground = style::Colour::ActiveBackground; } void DropDown::OnMouseClick(int x, int y, unsigned int button) @@ -104,59 +87,31 @@ void DropDown::OnMouseClick(int x, int y, unsigned int button) void DropDown::Draw(const Point& screenPos) { + if(!drawn) + { + if(optionIndex!=-1) + TextPosition(options[optionIndex].first); + drawn = true; + } 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); + g->fillrect(Position.X-1, Position.Y-1, Size.X+2, Size.Y+2, Appearance.BackgroundActive.Red, Appearance.BackgroundActive.Green, Appearance.BackgroundActive.Blue, 255); + g->drawrect(Position.X, Position.Y, Size.X, Size.Y, Appearance.BorderActive.Red, Appearance.BorderActive.Green, Appearance.BorderActive.Blue, 255); if(optionIndex!=-1) - g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y, options[optionIndex].first, activeText.Red, activeText.Green, activeText.Blue, 255); + g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y, options[optionIndex].first, Appearance.TextActive.Red, Appearance.TextActive.Green, Appearance.TextActive.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); + g->fillrect(Position.X, Position.Y, Size.X, Size.Y, Appearance.BackgroundInactive.Red, Appearance.BackgroundInactive.Green, Appearance.BackgroundInactive.Blue, 255); + g->drawrect(Position.X, Position.Y, Size.X, Size.Y, Appearance.BorderInactive.Red, Appearance.BorderInactive.Green, Appearance.BorderInactive.Blue, 255); if(optionIndex!=-1) - g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y, options[optionIndex].first, text.Red, text.Green, text.Blue, 255); + g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y, options[optionIndex].first, Appearance.TextInactive.Red, Appearance.TextInactive.Green, Appearance.TextInactive.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) @@ -173,7 +128,7 @@ void DropDown::Draw(const Point& screenPos) if(options[i].first == option) { optionIndex = i; - TextPosition(); + TextPosition(options[optionIndex].first); return; } } @@ -185,7 +140,7 @@ void DropDown::Draw(const Point& screenPos) if(options[i].second == option) { optionIndex = i; - TextPosition(); + TextPosition(options[optionIndex].first); return; } } diff --git a/src/interface/DropDown.h b/src/interface/DropDown.h index dabd4ec..813c035 100644 --- a/src/interface/DropDown.h +++ b/src/interface/DropDown.h @@ -24,20 +24,13 @@ public: }; class DropDown: public ui::Component { friend class DropDownWindow; - 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); diff --git a/src/interface/Label.cpp b/src/interface/Label.cpp index b6fbb5f..ceb5a4e 100644 --- a/src/interface/Label.cpp +++ b/src/interface/Label.cpp @@ -18,12 +18,8 @@ using namespace ui; Label::Label(Point position, Point size, std::string labelText): Component(position, size), text(labelText), - textPosition(ui::Point(0, 0)), - textVAlign(AlignMiddle), - textHAlign(AlignCentre), textColour(255, 255, 255) { - TextPosition(); } /*Label::Label(std::string labelText): @@ -41,44 +37,24 @@ Label::~Label() } -void Label::TextPosition() +void Label::SetText(std::string text) { - //Position.X+(Size.X-Graphics::textwidth((char *)ButtonText.c_str()))/2, Position.Y+(Size.Y-10)/2 - switch(textVAlign) - { - case AlignTop: - textPosition.Y = 3; - break; - case AlignMiddle: - textPosition.Y = (Size.Y-10)/2; - break; - case AlignBottom: - textPosition.Y = Size.Y-11; - break; - } - - switch(textHAlign) - { - case AlignLeft: - textPosition.X = 3; - break; - case AlignCentre: - textPosition.X = (Size.X-Graphics::textwidth((char *)text.c_str()))/2; - break; - case AlignRight: - textPosition.X = (Size.X-Graphics::textwidth((char *)text.c_str()))-2; - break; - } + this->text = text; + TextPosition(text); } -void Label::SetText(std::string text) +std::string Label::GetText() { - this->text = text; - TextPosition(); + return this->text; } void Label::Draw(const Point& screenPos) { + if(!drawn) + { + TextPosition(text); + drawn = true; + } Graphics * g = Engine::Ref().g; g->drawtext(screenPos.X+textPosition.X, screenPos.Y+textPosition.Y, text, textColour.Red, textColour.Green, textColour.Blue, 255); } diff --git a/src/interface/Label.h b/src/interface/Label.h index 4f7ce7f..08c5fad 100644 --- a/src/interface/Label.h +++ b/src/interface/Label.h @@ -13,10 +13,6 @@ namespace ui { protected: std::string text; - ui::Point textPosition; - HorizontalAlignment textHAlign; - VerticalAlignment textVAlign; - Colour textColour; public: //Label(Window* parent_state, std::string labelText); @@ -24,11 +20,8 @@ namespace ui //Label(std::string labelText); virtual ~Label(); - virtual void TextPosition(); virtual void SetText(std::string text); - HorizontalAlignment GetHAlignment() { return textHAlign; } - VerticalAlignment GetVAlignment() { return textVAlign; } - void SetAlignment(HorizontalAlignment hAlign, VerticalAlignment vAlign) { textHAlign = hAlign; textVAlign = vAlign; TextPosition(); } + virtual std::string GetText(); void SetTextColour(Colour textColour) { this->textColour = textColour; } diff --git a/src/interface/Textbox.cpp b/src/interface/Textbox.cpp index 991ccac..8657e8e 100644 --- a/src/interface/Textbox.cpp +++ b/src/interface/Textbox.cpp @@ -12,14 +12,11 @@ Textbox::Textbox(Point position, Point size, std::string textboxText): Component(position, size), text(textboxText), textPosition(ui::Point(0, 0)), - textVAlign(AlignMiddle), - textHAlign(AlignCentre), actionCallback(NULL), masked(false), border(true) { SetText(textboxText); - TextPosition(); cursor = text.length(); } @@ -29,57 +26,19 @@ Textbox::~Textbox() delete actionCallback; } -void Textbox::TextPosition() -{ - if(cursor) - { - cursorPosition = Graphics::textnwidth((char *)displayText.c_str(), cursor); - } - else - { - cursorPosition = 0; - } - //Position.X+(Size.X-Graphics::textwidth((char *)ButtonText.c_str()))/2, Position.Y+(Size.Y-10)/2 - switch(textVAlign) - { - case AlignTop: - textPosition.Y = 3; - break; - case AlignMiddle: - textPosition.Y = (Size.Y-10)/2; - break; - case AlignBottom: - textPosition.Y = Size.Y-11; - 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; - } -} - void Textbox::SetText(std::string text) { cursor = text.length(); this->text = text; this->displayText = text; - TextPosition(); + TextPosition(displayText); } void Textbox::SetDisplayText(std::string text) { displayText = text; - TextPosition(); + TextPosition(displayText); } std::string Textbox::GetText() @@ -140,7 +99,6 @@ void Textbox::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool if(cursor == text.length()) { text += character; - //std::cout << key << std::endl; } else { @@ -171,11 +129,16 @@ void Textbox::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool if(actionCallback) actionCallback->TextChangedCallback(this); } - TextPosition(); + TextPosition(displayText); } void Textbox::Draw(const Point& screenPos) { + if(!drawn) + { + TextPosition(displayText); + drawn = true; + } Graphics * g = Engine::Ref().g; if(IsFocused()) { diff --git a/src/interface/Textbox.h b/src/interface/Textbox.h index c29e01b..e502768 100644 --- a/src/interface/Textbox.h +++ b/src/interface/Textbox.h @@ -22,8 +22,6 @@ protected: std::string text; std::string displayText; ui::Point textPosition; - HorizontalAlignment textHAlign; - VerticalAlignment textVAlign; int cursor, cursorPosition; TextboxAction *actionCallback; bool masked; @@ -32,13 +30,9 @@ public: Textbox(Point position, Point size, std::string textboxText); virtual ~Textbox(); - virtual void TextPosition(); virtual void SetText(std::string text); virtual void SetDisplayText(std::string text); std::string GetText(); - HorizontalAlignment GetHAlignment() { return textHAlign; } - VerticalAlignment GetVAlignment() { return textVAlign; } - void SetAlignment(HorizontalAlignment hAlign, VerticalAlignment vAlign) { textHAlign = hAlign; textVAlign = vAlign; TextPosition(); } void SetActionCallback(TextboxAction * action) { actionCallback = action; } virtual void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt); |
