unfocusing widgets

Started by Maksat, 24 May 2019, 11:26:24

Maksat

Hi!
Code (cpp) Select

auto button = tgui::Button::create();
button->setRenderer(theme.getRenderer("Button"));
button->connect("pressed", [](){std::cout<<"button pressed" << endl;});
gui->add(button);

/* --- */

sf::Event event;
while (window->isOpen())
{
while (window->pollEvent(event))
{
/* --- */
                gui.handleEvent(event);
               
                if (event->type == Event::KeyPressed)
                switch (event->key.code)
{
case Keyboard::Space:
cout<< "space key pressed"<<endl;
break;
                        /* --- */
}

/* --- */
}
}


if you press the button and then the space key, you will see:
button pressed
button pressed
space key pressed

I use many buttons and i have another function bind to the space key. I have to unfocus widget every time i pressed it. Is there any option to disable auto focusing last pressed widget?

texus

QuoteIs there any option to disable auto focusing last pressed widget?
No. You can disable it visually so that the user can't tell the difference between focused widgets but that won't solve the issue.
I can see how what you ask makes sense for buttons, but not allowing focusing widgets isn't a good solution in the general case because then you would no longer be able to use edit boxes and similar widgets that rely on the focusing.

If you need some events to go to TGUI while other events should be handled in your code and there is some overlap in the events then you should probably just not call gui.handleEvent for events you want to handle. So basically call gui.handleEvent for every event except for the space key press event.

Maksat

Quote from: texus on 24 May 2019, 11:34:06
So basically call gui.handleEvent for every event except for the space key press event.
how can I do that?
Code (cpp) Select

if(event.type == Event::KeyPressed && event.key.code == Keyboard::Space)
{
    handleSpaceKeyEvent();
    return;
}
gui.handleEvent();
/* handle other events */


like this?

texus

Yes, something like that.

Maksat


billarhos

Maybe it would be good for some more functionality for buttons or other widgets:

button->addEventCodeTrigger(const sf::Keyboard & code); // add this code to a vector

button->removeEventCodeTriiger(const sf::Keyboard & code); remove this code from vector if exist

Texus can you tell me when this button functionality added. I mean from what version exist?


void Button::keyPressed(const sf::Event::KeyEvent& event)
    {
        if ((event.code == sf::Keyboard::Space) || (event.code == sf::Keyboard::Return))
            onPress.emit(this, m_text.getString());
    }

texus

The oldest version that I can check is TGUI 0.4 and even there it already responded to pressing space or enter.

What would happen after adding a key to addEventCodeTrigger? I don't see any use case except for this one so I might as well add a function to disable using space and return keys to buttons.

The issue is a bit related to the question of which widgets should be focusable. Some people might e.g. want to be able to focus a slider and use the arrow keys to change the value while other might want the tab key to skip the slider. So if I could find a good way to allow the user to decide which widgets can gain focus then you would be able to solve it by making the buttons unfocusable.

billarhos

I thought for an instance that space and enter is a new functionality and maybe i should check my codes.

Quoteso I might as well add a function to disable using space and return keys to buttons
yes, this is also something i thought about.

QuoteSo if I could find a good way to allow the user to decide which widgets can gain focus

Perhaps a simple boolean is good enough. And you can leave up to user!

widget->setFocusable(bool onOff)


texus

QuotePerhaps a simple boolean is good enough.
Perhaps. But I don't like that the user would have to do it for every widget he creates. There will always be cases where the default doesn't match what you want and where you need to set the value each time, but I was hoping to find a way on a slightly higher level (e.g. a single function to disable it on all buttons). Maybe something in the theme. Although that sounds similar to earlier TGUI versions where widgets were only focusable if it had a focus texture (with EditBox as exception which could gain focus even without such texture).

The biggest problem is always figuring out what would work best without actually knowing what people want or how they want it. So maybe I should indeed just add the simple boolean toggle to already allow it now and leave the better designs for later.

texus

I have added a setFocusable function for all widgets. The updated TGUI version can be downloaded from github.

billarhos

You can not satisfy every user but having a default status and also giving the ability to change the capability of focus (setFocusable) then you can catch every possible senario with much more cleaner and maintanaible source code.

thanks Texus