Skip to content
This repository has been archived by the owner on Dec 1, 2020. It is now read-only.

Feature text input with variable binding to reference #202

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/ofxUICanvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/ofxUICanvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/ofxUILabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/ofxUIScrollableCanvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand Down
95 changes: 52 additions & 43 deletions src/ofxUITextInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -191,7 +222,7 @@ void ofxUITextInput::mouseReleased(int x, int y, int button)

void ofxUITextInput::keyPressed(int key)
{
if(clicked)
if(isClicked())
{
switch (key)
{
Expand Down Expand Up @@ -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;

Expand All @@ -246,6 +277,7 @@ void ofxUITextInput::keyPressed(int key)
case OF_KEY_UP:
if(cursorPosition > 0)
{
cout << cursorPosition << " " << textstring.length() << endl;
cursorPosition--;
recalculateDisplayString();
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
11 changes: 7 additions & 4 deletions src/ofxUITextInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down