Skip to content

Commit

Permalink
Add support for updating a string.
Browse files Browse the repository at this point in the history
  • Loading branch information
acaudwell committed Jun 24, 2012
1 parent 6370107 commit ebdf030
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
28 changes: 27 additions & 1 deletion ui/label.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

//UILabel

UILabel::UILabel(const std::string& text, bool editable, float width, UIAction* action) : text(text), width(width), action(action), UIElement() {
UILabel::UILabel(const std::string& text, bool editable, float width, UIAction* action, std::string* value) : text(text), value(value), width(width), action(action), UIElement() {

slider = 0;
text_colour = vec4(0.0f);
Expand Down Expand Up @@ -60,6 +60,14 @@ void UILabel::backspace() {
void UILabel::tab() {
}

bool UILabel::submit() {
if(value != 0) {
*value = text;
return true;
}
return false;
}

bool UILabel::keyPress(SDL_KeyboardEvent *e, char c) {

if(!c) return false;
Expand Down Expand Up @@ -128,6 +136,13 @@ bool UILabel::keyPress(SDL_KeyboardEvent *e, char c) {

void UILabel::update(float dt) {

if(editable && !selected && value != 0) {
if(*value != text) {
text = *value;
text_changed = true;
}
}

if(selected && editable) {
cursor_anim += dt;
if(cursor_anim>=2.0f) cursor_anim=0.0f;
Expand Down Expand Up @@ -234,6 +249,17 @@ void UILabel::drawContent() {
// expanded = 0.0f;
}

//

UILabelString::UILabelString(const std::string& label, std::string* value, bool editable, UIAction* action) : UILayout(true) {

addElement(new UILabel(label, false, 120.0f));
addElement(new UILabel(*value, editable, 213.0f, 0, value));

padding = vec2(5.0f, 0.0f);
scrollable = true;
}

//UIIntLabel

UIIntLabel::UIIntLabel(int* value, bool editable, UIAction* action) : value(value), UILabel("", editable, -1.0f, action) {
Expand Down
14 changes: 13 additions & 1 deletion ui/label.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
#define UI_LABEL_H

#include "element.h"
#include "layout.h"
#include "action.h"

class UISlider;
class UIFloatSlider;
class UIColourSlider;
class UILayout;

class UILabel : public UIElement {
protected:
Expand All @@ -17,6 +19,8 @@ class UILabel : public UIElement {

UIAction* action;

std::string* value;

void drawBackground();
public:
vec4 bgcolour;
Expand All @@ -28,7 +32,7 @@ class UILabel : public UIElement {
std::string text;
std::string display_text;

UILabel(const std::string& text, bool editable = false, float width = -1.0f, UIAction* action = 0);
UILabel(const std::string& text, bool editable = false, float width = -1.0f, UIAction* action = 0, std::string* value = 0);

float width;

Expand All @@ -37,8 +41,11 @@ class UILabel : public UIElement {
int getType() { return UI_LABEL; }

virtual void tab();

virtual void backspace();

bool submit();

void setWidth(float width);
void setText(const std::string& text);
void setTextColour(const vec4& colour);
Expand Down Expand Up @@ -94,4 +101,9 @@ class UIColourLabel : public UIFloatLabel {
void updateContent();
};

class UILabelString : public UILayout {
public:
UILabelString(const std::string& label, std::string* value, bool editable = false, UIAction* action = 0);
};

#endif

0 comments on commit ebdf030

Please sign in to comment.