rezaali/ofxUI

Focus on Multiple TextInputs

Opened this issue · 5 comments

noio commented

When creating a UI with multiple textinputs, the default behavior is for each textfield to stay selected after it's clicked. This often leads to the case where multiple textinputs receive input text.

Would it be an idea to have the Canvas keep track of which textfield is in focus? I might be able to contribute.

@noio do you solve it? if yes how? thanks for your time

noio commented

I wrote some code "manually" to unselect all textinputs (except one) when a new one is selected:

void ofApp::unfocusAllTextInputs(ofxUITextInput* except){
    for (int i = 0; i < textInputs.size(); i ++){
        if (except != textInputs[i]){
            textInputs[i]->setFocus(false);
        }
    }
}

Then I call this whenever a new textinput is selected in ofApp::guiEvent(ofxUIEventArgs& e):

if (kind == OFX_UI_WIDGET_TEXTINPUT){
    ofxUITextInput *ti = (ofxUITextInput *) e.widget;
    if (ti->getInputTriggerType() == OFX_UI_TEXTINPUT_ON_FOCUS){
        unfocusAllTextInputs(ti);
    }
}

This does mean you have to keep track of the textinputs:

windowHTextInput = gui->addTextInput("WINDOW_H", ofToString(ofGetHeight()));
windowHTextInput->setAutoClear(false);
textInputs.push_back(windowHTextInput);

@noio yap, i did the same :D

@noio . Thanks. it helped me too. After some doubts, finally I defined textInputs and my variables in ofApp.h like this:

void unfocusAllTextInputs(ofxUITextInput* except);
vector<ofxUITextInput *> textInputs;
ofxUITextInput *textInputUserKitLAB;
ofxUITextInput *textInputSeedPresetKit;

This happens to me with the ofxUIScrollableCanvas, quick fix:

void ofxUIScrollableCanvas::mousePressed(int x, int y, int button)
{
    if(sRect->inside(x, y))
    {
        hit = true;
        for(vector<ofxUIWidget *>::iterator it = widgets.begin(); it != widgets.end(); ++it)
        {
            if((*it)->isVisible())
            {
                if((*it)->isHit(x, y))
                {
                    if((*it)->isDraggable())
                    {
                        hitWidget = true;
                    }
                    (*it)->mousePressed(x, y, button);
                }
                else if((*it)->getKind() == ofxUIWidgetType::OFX_UI_WIDGET_TEXTINPUT)
                {
                     (*it)->mousePressed(x, y, button);
                }
            }
        }
    }

    isScrolling = false;
    vel.set(0,0);
}

I think the solution would be making ofxUITextInput always listen to mouse events, then checking for their rects. This way it would be globally fixed, even if you have multiple ofxUICanvas.