From 04aa3a8eabad95ca92a8708fbb7ccd2b9c086274 Mon Sep 17 00:00:00 2001 From: Matt Gingold Date: Tue, 6 May 2014 18:47:15 +1000 Subject: [PATCH 1/3] Added variable binding for strings --- src/ofxUICanvas.cpp | 25 +++++++++++ src/ofxUICanvas.h | 2 + src/ofxUITextInput.cpp | 95 +++++++++++++++++++++++------------------- src/ofxUITextInput.h | 11 +++-- 4 files changed, 86 insertions(+), 47 deletions(-) diff --git a/src/ofxUICanvas.cpp b/src/ofxUICanvas.cpp index 7b5be48a8..59cd7b2fa 100644 --- a/src/ofxUICanvas.cpp +++ b/src/ofxUICanvas.cpp @@ -1664,6 +1664,20 @@ ofxUITextInput* ofxUICanvas::addTextInput(string _name, string _textstring, int return widget; } +ofxUITextInput* ofxUICanvas::addTextInput(string _name, string *_textstring, int _size) +{ + float h = 0; + float x = 0; + float y = 0; + if(_size == -1) + { + _size = widgetFontSize; + } + ofxUITextInput* widget = new ofxUITextInput(_name, _textstring, rect->getWidth()-widgetSpacing*2, h, x, y, _size); + addWidgetPosition(widget, widgetPosition, widgetAlign); + return widget; +} + ofxUITextInput* ofxUICanvas::addTextInput(string _name, string _textstring, float w, float h, float x, float y, int _size) { if(_size == -1) @@ -1675,6 +1689,17 @@ ofxUITextInput* ofxUICanvas::addTextInput(string _name, string _textstring, floa return widget; } +ofxUITextInput* ofxUICanvas::addTextInput(string _name, string *_textstring, float w, float h, float x, float y, int _size) +{ + if(_size == -1) + { + _size = widgetFontSize; + } + ofxUITextInput* widget = new ofxUITextInput(_name, _textstring, w, h, x, y, _size); + addWidgetPosition(widget, widgetPosition, widgetAlign); + return widget; +} + ofxUILabelToggle* ofxUICanvas::addLabelToggle(string _name, bool _value, bool _justifyLeft) { ofxUILabelToggle* widget = new ofxUILabelToggle(_name, _value, rect->getWidth()-widgetSpacing*2, globalButtonDimension, 0, 0, widgetFontSize, _justifyLeft); diff --git a/src/ofxUICanvas.h b/src/ofxUICanvas.h index 2bf7251a0..ee339851d 100755 --- a/src/ofxUICanvas.h +++ b/src/ofxUICanvas.h @@ -195,7 +195,9 @@ class ofxUICanvas : public ofxUIWidget, public ofxUIAppCBGlue ofxUI2DPad* add2DPad(string _name, ofxUIVec3f _rangeX, ofxUIVec3f _rangeY, ofxUIVec3f *_value, float w, float h, float x = 0, float y = 0); ofxUITextInput* addTextInput(string _name, string _textstring, int _size = -1); + ofxUITextInput* addTextInput(string _name, string *_textstring, int _size = -1); ofxUITextInput* addTextInput(string _name, string _textstring, float w, float h = 0, float x = 0, float y = 0, int _size = -1); + ofxUITextInput* addTextInput(string _name, string *_textstring, float w, float h = 0, float x = 0, float y = 0, int _size = -1); ofxUILabelToggle* addLabelToggle(string _name, bool _value, bool _justifyLeft = false); ofxUILabelToggle* addLabelToggle(string _name, bool _value, float w, float h = 0, float x = 0, float y = 0, bool _justifyLeft = false); diff --git a/src/ofxUITextInput.cpp b/src/ofxUITextInput.cpp index 6fb532afe..756b35678 100644 --- a/src/ofxUITextInput.cpp +++ b/src/ofxUITextInput.cpp @@ -28,17 +28,36 @@ ofxUITextInput::ofxUITextInput(string _name, string _textstring, float w, float h, float x, float y, int _size) : ofxUIWidgetWithLabel() { + useReference = false; + init(_name, &_textstring, w, h, x, y, _size); +} + +ofxUITextInput::ofxUITextInput(string _name, string *_textstring, float w, float h, float x, float y, int _size) : ofxUIWidgetWithLabel() +{ + useReference = true; init(_name, _textstring, w, h, x, y, _size); } -void ofxUITextInput::init(string _name, string _textstring, float w, float h, float x, float y, int _size) +void ofxUITextInput::init(string _name, string *_textstring, float w, float h, float x, float y, int _size) { initRect(x,y,w,h); name = string(_name); kind = OFX_UI_WIDGET_TEXTINPUT; - textstring = string(_textstring); - defaultstring = string(_textstring); - displaystring = string(_textstring); + + textstring = *_textstring; + + if(useReference) + { + textstringRef = _textstring; + } + else + { + textstringRef = new string(); + *textstringRef = textstring; + } + + defaultstring = string(textstring); + displaystring = string(textstring); clicked = false; //the widget's value autoclear = true; @@ -148,7 +167,19 @@ void ofxUITextInput::mousePressed(int x, int y, int button) theta = 0; hit = true; #endif - cursorPosition = label->getLabel().length(); + + // place cursor where we click on the text input + string textatcursor = ""; + + while((x - rect->x) > label->getStringWidth(textatcursor)){ + if(textatcursor.length() >= displaystring.size()){ + textatcursor = displaystring + "."; + break; + } + textatcursor += displaystring.at(textatcursor.length()); + } + + cursorPosition = textstring.length() - displaystring.length() + textatcursor.length() - 1; state = OFX_UI_STATE_DOWN; inputTriggerType = OFX_UI_TEXTINPUT_ON_FOCUS; @@ -191,7 +222,7 @@ void ofxUITextInput::mouseReleased(int x, int y, int button) void ofxUITextInput::keyPressed(int key) { - if(clicked) + if(isClicked()) { switch (key) { @@ -223,13 +254,13 @@ void ofxUITextInput::keyPressed(int key) { clicked = false; } - + *textstringRef = textstring; triggerEvent(this); if(autoclear) { - cursorPosition = 0; - textstring.clear(); - recalculateDisplayString(); + setTextString(""); + }else{ + setTextString(textstring); } break; @@ -246,6 +277,7 @@ void ofxUITextInput::keyPressed(int key) case OF_KEY_UP: if(cursorPosition > 0) { + cout << cursorPosition << " " << textstring.length() << endl; cursorPosition--; recalculateDisplayString(); } @@ -363,31 +395,15 @@ int ofxUITextInput::getInputTriggerType() void ofxUITextInput::setTextString(string s) { - textstring = ""; - string temp = ""; + textstring = s; + defaultstring = textstring; + displaystring = textstring; - int length = s.length(); + cursorPosition = textstring.length(); - if(length > 0) - { - for(int i = 0; i < length; i++) - { - temp+=s.at(i); - float newWidth = label->getStringWidth(temp); - - if(newWidth < rect->getWidth()-padding*2.0) - { - textstring+=s.at(i); - label->setLabel(textstring); - } - } - } - else - { - textstring = s; - label->setLabel(textstring); - } - displaystring = textstring; + firstVisibleCharacterIndex = 0; + + recalculateDisplayString(); } void ofxUITextInput::setParent(ofxUIWidget *_parent) @@ -408,17 +424,10 @@ void ofxUITextInput::setParent(ofxUIWidget *_parent) defaultX = labelrect->getX(false); cursorWidth = label->getStringWidth("."); - while(label->getStringWidth(textstring) > rect->getWidth()-padding*2.0) - { - string::iterator it; - it=textstring.begin(); - textstring.erase (it); - } - - defaultstring = textstring; - displaystring = textstring; - setTextString(textstring); + calculatePaddingRect(); + setTextString(textstring); + } void ofxUITextInput::setAutoClear(bool _autoclear) diff --git a/src/ofxUITextInput.h b/src/ofxUITextInput.h index 3e040f22f..34f7fca4a 100755 --- a/src/ofxUITextInput.h +++ b/src/ofxUITextInput.h @@ -31,7 +31,8 @@ class ofxUITextInput : public ofxUIWidgetWithLabel { public: ofxUITextInput(string _name, string _textstring, float w, float h = 0, float x = 0, float y = 0, int _size = OFX_UI_FONT_SMALL); - void init(string _name, string _textstring, float w, float h = 0, float x = 0, float y = 0, int _size = OFX_UI_FONT_SMALL); + ofxUITextInput(string _name, string *_textstring, float w, float h = 0, float x = 0, float y = 0, int _size = OFX_UI_FONT_SMALL); + void init(string _name, string *_textstring, float w, float h = 0, float x = 0, float y = 0, int _size = OFX_UI_FONT_SMALL); virtual void setDrawPadding(bool _draw_padded_rect); virtual void setDrawPaddingOutline(bool _draw_padded_rect_outline); virtual void drawFill(); @@ -61,9 +62,11 @@ class ofxUITextInput : public ofxUIWidgetWithLabel #endif protected: - string textstring; - string defaultstring; - string displaystring; + string * textstringRef; + string textstring; + string defaultstring; + string displaystring; + bool useReference; bool clicked; bool autoUnfocus; float theta; From 2287cf106c233f30b398079a2d05f498f3da51eb Mon Sep 17 00:00:00 2001 From: Matt Gingold Date: Tue, 6 May 2014 18:48:02 +1000 Subject: [PATCH 2/3] Added hack to more correctly calculate spaces ' ' using getStringWidth --- src/ofxUILabel.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ofxUILabel.cpp b/src/ofxUILabel.cpp index d849d4537..02d6814f2 100644 --- a/src/ofxUILabel.cpp +++ b/src/ofxUILabel.cpp @@ -146,6 +146,7 @@ void ofxUILabel::drawStringShadow(float x, float y, string _string) float ofxUILabel::getStringWidth(string s) { + replace(s.begin(), s.end(), ' ', '_'); // hack to more correctly calculate spaces ' ' as oF currently doesn't do this correctly return font->stringWidth(s); } From 7531ed326832baac1cde6a8a660f304a0780fda2 Mon Sep 17 00:00:00 2001 From: Matt Gingold Date: Tue, 6 May 2014 18:49:49 +1000 Subject: [PATCH 3/3] Allow text input widget to correctly de-focus/unclick --- src/ofxUIScrollableCanvas.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ofxUIScrollableCanvas.cpp b/src/ofxUIScrollableCanvas.cpp index a94d16949..80f2b06b5 100644 --- a/src/ofxUIScrollableCanvas.cpp +++ b/src/ofxUIScrollableCanvas.cpp @@ -524,7 +524,7 @@ void ofxUIScrollableCanvas::mousePressed(int x, int y, int button) { if((*it)->isVisible()) { - if((*it)->isHit(x, y)) + if((*it)->isHit(x, y) || (*it)->getKind() == OFX_UI_WIDGET_TEXTINPUT) // allows text input widget to correctly de-focus/unclick { if((*it)->isDraggable()) {