diff options
| author | Simon Robertshaw <simon@hardwired.org.uk> | 2012-01-20 22:07:49 (GMT) |
|---|---|---|
| committer | Simon Robertshaw <simon@hardwired.org.uk> | 2012-01-20 22:07:49 (GMT) |
| commit | c8073657fcbfd1bfa72538d7babe4964857e7101 (patch) | |
| tree | f3488e993c9828121b2f89ed2f639d2ebbe54dc9 /src/interface | |
| parent | c5e8b345219cd7d8ca4b0aa638f59a1fed2cd83b (diff) | |
| download | powder-c8073657fcbfd1bfa72538d7babe4964857e7101.zip powder-c8073657fcbfd1bfa72538d7babe4964857e7101.tar.gz | |
More stuff, need to fix memory leak
Diffstat (limited to 'src/interface')
| -rw-r--r-- | src/interface/Button.cpp | 30 | ||||
| -rw-r--r-- | src/interface/Keys.h | 8 | ||||
| -rw-r--r-- | src/interface/Label.cpp | 63 | ||||
| -rw-r--r-- | src/interface/Label.h | 14 | ||||
| -rw-r--r-- | src/interface/SaveButton.cpp | 1 | ||||
| -rw-r--r-- | src/interface/Textbox.cpp | 192 | ||||
| -rw-r--r-- | src/interface/Textbox.h | 44 |
7 files changed, 312 insertions, 40 deletions
diff --git a/src/interface/Button.cpp b/src/interface/Button.cpp index affa75d..783c297 100644 --- a/src/interface/Button.cpp +++ b/src/interface/Button.cpp @@ -130,34 +130,6 @@ void Button::Draw(const Point& screenPos) g->drawrect(Position.X, Position.Y, Size.X, Size.Y, 255, 255, 255, 255); g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y, ButtonText, 255, 255, 255, 255); } - /*sf::RenderWindow* rw = reinterpret_cast<sf::RenderWindow*>(userdata); //it better be a RenderWindow or so help your god - - //Draw component here - sf::Text textGraphic(ButtonText); - textGraphic.SetCharacterSize(11); - if(isButtonDown) - textGraphic.SetColor(sf::Color::Black); - else - textGraphic.SetColor(sf::Color::White); - sf::FloatRect tempRect = textGraphic.GetRect(); - textGraphic.SetPosition(ceil(X + Width/2 - tempRect.Width/2), ceil(Y + Height/2 - tempRect.Height/2)); - - if(isMouseInside) - { - if(isButtonDown) - rw->Draw(sf::Shape::Rectangle(X+2, Y+2, Width-4, Width-4, sf::Color::White, 2.f, sf::Color::Black)); - else - rw->Draw(sf::Shape::Rectangle(X+2, Y+2, Width-4, Width-4, sf::Color::Black, 2.f, sf::Color::White)); - } - else - { - if(isButtonDown) - rw->Draw(sf::Shape::Rectangle(X+2, Y+2, Width-4, Width-4, sf::Color::White, 2.f, sf::Color::Black)); - else - rw->Draw(sf::Shape::Rectangle(X+1, Y+1, Width-2, Width-2, sf::Color::Black, 1.f, sf::Color::White)); - } - - rw->Draw(textGraphic);*/ } void Button::OnMouseUnclick(int x, int y, unsigned int button) @@ -218,6 +190,8 @@ void Button::SetActionCallback(ButtonAction * action) Button::~Button() { + if(actionCallback) + delete actionCallback; } } /* namespace ui */ diff --git a/src/interface/Keys.h b/src/interface/Keys.h new file mode 100644 index 0000000..e923703 --- /dev/null +++ b/src/interface/Keys.h @@ -0,0 +1,8 @@ +#define KEY_UP SDLK_UP +#define KEY_DOWN SDLK_DOWN +#define KEY_RIGHT SDLK_RIGHT +#define KEY_LEFT SDLK_LEFT +#define KEY_HOME SDLK_HOME +#define KEY_END SDLK_END +#define KEY_BACKSPACE SDLK_BACKSPACE +#define KEY_DELETE SDLK_DELETE diff --git a/src/interface/Label.cpp b/src/interface/Label.cpp index cf09d4e..6fc47e2 100644 --- a/src/interface/Label.cpp +++ b/src/interface/Label.cpp @@ -1,30 +1,39 @@ #include <string> #include "Config.h" #include "Global.h" -#include "interface/Point.h" -#include "interface/Label.h" +#include "Point.h" +#include "Label.h" using namespace ui; Label::Label(Window* parent_state, std::string labelText): Component(parent_state), - LabelText(labelText) + text(labelText), + textPosition(ui::Point(0, 0)), + textVAlign(AlignMiddle), + textHAlign(AlignCentre) { - + TextPosition(); } Label::Label(Point position, Point size, std::string labelText): Component(position, size), - LabelText(labelText) + text(labelText), + textPosition(ui::Point(0, 0)), + textVAlign(AlignMiddle), + textHAlign(AlignCentre) { - + TextPosition(); } Label::Label(std::string labelText): Component(), - LabelText(labelText) + text(labelText), + textPosition(ui::Point(0, 0)), + textVAlign(AlignMiddle), + textHAlign(AlignCentre) { - + TextPosition(); } Label::~Label() @@ -32,9 +41,45 @@ Label::~Label() } +void Label::TextPosition() +{ + //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; + } +} + +void Label::SetText(std::string text) +{ + this->text = text; + TextPosition(); +} void Label::Draw(const Point& screenPos) { Graphics * g = Engine::Ref().g; - g->drawtext(Position.X+(Size.X-Graphics::textwidth((char *)LabelText.c_str()))/2, Position.Y+(Size.Y-10)/2, LabelText, 255, 255, 255, 255); + g->drawtext(screenPos.X+textPosition.X, screenPos.Y+textPosition.Y, text, 255, 255, 255, 255); } + diff --git a/src/interface/Label.h b/src/interface/Label.h index e56852e..9b5a454 100644 --- a/src/interface/Label.h +++ b/src/interface/Label.h @@ -4,20 +4,28 @@ #include <string> #include "Component.h" +#include "Misc.h" namespace ui { class Label : public Component { + std::string text; + ui::Point textPosition; + HorizontalAlignment textHAlign; + VerticalAlignment textVAlign; public: Label(Window* parent_state, std::string labelText); - Label(Point position, Point size, std::string labelText); - Label(std::string labelText); virtual ~Label(); - std::string LabelText; + void TextPosition(); + 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 void Draw(const Point& screenPos); }; diff --git a/src/interface/SaveButton.cpp b/src/interface/SaveButton.cpp index 18f3281..cf1c0f2 100644 --- a/src/interface/SaveButton.cpp +++ b/src/interface/SaveButton.cpp @@ -58,6 +58,7 @@ void SaveButton::Tick(float dt) if(tempThumb) { thumbnail = tempThumb; //Store a local copy of the thumbnail + cout << (void *)(tempThumb) << " " << (void *)(&thumbnail) << endl; if(thumbnail->Data) { if(thumbnail->Size.Y > (Size.Y-25)) diff --git a/src/interface/Textbox.cpp b/src/interface/Textbox.cpp new file mode 100644 index 0000000..3cedc15 --- /dev/null +++ b/src/interface/Textbox.cpp @@ -0,0 +1,192 @@ +#include <string> +#include <stdexcept> +#include "Config.h" +#include "Global.h" +#include "interface/Point.h" +#include "interface/Textbox.h" +#include "interface/Keys.h" + +using namespace ui; + +Textbox::Textbox(Window* parent_state, std::string textboxText): + Component(parent_state), + text(textboxText), + textPosition(ui::Point(0, 0)), + textVAlign(AlignMiddle), + textHAlign(AlignCentre), + actionCallback(NULL) +{ + TextPosition(); + cursor = text.length(); +} + +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) +{ + TextPosition(); + cursor = text.length(); +} + +Textbox::Textbox(std::string textboxText): + Component(), + text(textboxText), + textPosition(ui::Point(0, 0)), + textVAlign(AlignMiddle), + textHAlign(AlignCentre), + actionCallback(NULL) +{ + TextPosition(); + cursor = text.length(); +} + +Textbox::~Textbox() +{ + if(actionCallback) + delete actionCallback; +} + +void Textbox::TextPosition() +{ + std::string tempText = text; + if(tempText.length() && cursor) + { + tempText.erase(cursor, tempText.length()-cursor); + cursorPosition = Graphics::textwidth((char *)tempText.c_str()); + } + 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 *)text.c_str()))/2; + break; + case AlignRight: + textPosition.X = (Size.X-Graphics::textwidth((char *)text.c_str()))-2; + break; + } +} + +void Textbox::SetText(std::string text) +{ + this->text = text; + TextPosition(); +} + +std::string Textbox::GetText() +{ + return text; +} + +void Textbox::OnKeyPress(int key, bool shift, bool ctrl, bool alt) +{ + bool changed = false; + try + { + switch(key) + { + case KEY_HOME: + cursor = 0; + break; + case KEY_END: + cursor = text.length(); + break; + case KEY_LEFT: + if(cursor > 0) + cursor--; + break; + case KEY_RIGHT: + if(cursor < text.length()) + cursor++; + break; + case KEY_DELETE: + if(text.length() && cursor < text.length()) + { + if(ctrl) + text.erase(cursor, text.length()-cursor); + else + text.erase(cursor, 1); + changed = true; + } + break; + case KEY_BACKSPACE: + if(text.length() && cursor > 0) + { + if(ctrl) + { + text.erase(0, cursor); + cursor = 0; + } + else + { + text.erase(cursor-1, 1); + cursor--; + } + changed = true; + } + break; + default: + if(key >= ' ' && key < 127) + { + if(cursor == text.length()) + { + text += key; + } + else + { + text.insert(cursor, 1, (char)key); + } + cursor++; + changed = true; + } + } + if(changed && actionCallback) + { + actionCallback->TextChangedCallback(this); + } + } + catch(std::out_of_range e) + { + cursor = 0; + text = ""; + } + TextPosition(); +} + +void Textbox::Draw(const Point& screenPos) +{ + Graphics * g = Engine::Ref().g; + if(IsFocused()) + { + g->drawrect(screenPos.X, screenPos.Y, Size.X, Size.Y, 255, 255, 255, 255); + g->draw_line(screenPos.X+textPosition.X+cursorPosition, screenPos.Y+3, screenPos.X+textPosition.X+cursorPosition, screenPos.Y+12, 255, 255, 255, XRES+BARSIZE); + } + else + { + g->drawrect(screenPos.X, screenPos.Y, Size.X, Size.Y, 160, 160, 160, 255); + } + g->drawtext(screenPos.X+textPosition.X, screenPos.Y+textPosition.Y, text, 255, 255, 255, 255); +} diff --git a/src/interface/Textbox.h b/src/interface/Textbox.h new file mode 100644 index 0000000..5cd14f4 --- /dev/null +++ b/src/interface/Textbox.h @@ -0,0 +1,44 @@ +#ifndef TEXTBOX_H +#define TEXTBOX_H + +#include <string> + +#include "Component.h" +#include "Misc.h" + +namespace ui +{ +class Textbox; +class TextboxAction +{ +public: + virtual void TextChangedCallback(ui::Textbox * sender) {} +}; +class Textbox : public Component +{ + std::string text; + ui::Point textPosition; + HorizontalAlignment textHAlign; + VerticalAlignment textVAlign; + int cursor, cursorPosition; + TextboxAction *actionCallback; +public: + Textbox(Window* parent_state, std::string textboxText); + Textbox(Point position, Point size, std::string textboxText); + Textbox(std::string textboxText); + virtual ~Textbox(); + + void TextPosition(); + void SetText(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, bool shift, bool ctrl, bool alt); + + virtual void Draw(const Point& screenPos); +}; +} + +#endif // TEXTBOX_H |
