unfocusing widgets

  • 10 Replies
  • 255 Views
unfocusing widgets
« on: 24 May 2019, 11:26:24 »
Hi!
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

  • *****
  • 1463
    • View Profile
    • Texus's Blog
Re: unfocusing widgets
« Reply #1 on: 24 May 2019, 11:34:06 »
Quote
Is 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.

Re: unfocusing widgets
« Reply #2 on: 24 May 2019, 14:11:02 »
So basically call gui.handleEvent for every event except for the space key press event.
how can I do that?
if(event.type == Event::KeyPressed && event.key.code == Keyboard::Space)
{
    handleSpaceKeyEvent();
    return;
}
gui.handleEvent();
/* handle other events */
 

like this?

*

texus

  • *****
  • 1463
    • View Profile
    • Texus's Blog
Re: unfocusing widgets
« Reply #3 on: 24 May 2019, 14:13:26 »
Yes, something like that.

Re: unfocusing widgets
« Reply #4 on: 24 May 2019, 15:10:07 »
Ok, thank you!

Re: unfocusing widgets
« Reply #5 on: 24 May 2019, 16:11:33 »
Maybe it would be good for some more functionality for buttons or other widgets:

Code: [Select]
button->addEventCodeTrigger(const sf::Keyboard & code); // add this code to a vector
Code: [Select]
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?

Code: [Select]
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

  • *****
  • 1463
    • View Profile
    • Texus's Blog
Re: unfocusing widgets
« Reply #6 on: 24 May 2019, 16:46:51 »
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.

Re: unfocusing widgets
« Reply #7 on: 24 May 2019, 16:58:20 »
I thought for an instance that space and enter is a new functionality and maybe i should check my codes.

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

Quote
So 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!

Code: [Select]
widget->setFocusable(bool onOff)

*

texus

  • *****
  • 1463
    • View Profile
    • Texus's Blog
Re: unfocusing widgets
« Reply #8 on: 24 May 2019, 17:13:35 »
Quote
Perhaps 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

  • *****
  • 1463
    • View Profile
    • Texus's Blog
Re: unfocusing widgets
« Reply #9 on: 24 May 2019, 18:04:37 »
I have added a setFocusable function for all widgets. The updated TGUI version can be downloaded from github.

Re: unfocusing widgets
« Reply #10 on: 24 May 2019, 19:07:38 »
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