ofxUI icon indicating copy to clipboard operation
ofxUI copied to clipboard

Focus on Multiple TextInputs

Open noio opened this issue 11 years ago • 5 comments

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 avatar Jun 19 '14 12:06 noio

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

Wazhup avatar Jul 17 '14 22:07 Wazhup

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 avatar Jul 18 '14 08:07 noio

@noio yap, i did the same :D

Wazhup avatar Jul 24 '14 20:07 Wazhup

@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;

moebiussurfing avatar Dec 03 '14 00:12 moebiussurfing

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.

chuckleplant avatar Feb 24 '15 12:02 chuckleplant