Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [Solved]Get the focus of a window  (Read 3564 times)

0 Members and 1 Guest are viewing this topic.

Steef435

  • Newbie
  • *
  • Posts: 12
    • View Profile
[Solved]Get the focus of a window
« on: November 10, 2012, 01:50:44 pm »
Hello all,

At the moment I'm trying to put everything I've learned in a small project, just a point and click game.
It's like duck hunt, a duck flies through the air and you have to "shoot" it by clicking on it.
Here's my problem:
Every loop (1/25sec) I check if the left mouse button is down and if the "cooldown" of the gun is passed.
If that's true I check if the duck is shot etc.
This all works fine.
But when I click outside of my window, my program takes that as a attempt to shoot the duck, this ruins my whole system because I want to keep track of the longest sequence of good shots etc.
What I'm trying to achieve is this:
if ((WindowIsFocussed) && (MouseButtonIsDown) && (CooldownPassed))
//Check if duck is shot etc.

So I'm searching for a function like bool sf::Window::isFocussed() or getFocus, whatever.

If I search for this function I find a request topic, so I guess the function is not yet implemented.

Is there any workaround for me to do this? I don't like to use the event system because I want to be able to just hold the mouse button.
I can think of using a bool to keep track of every time I get an LostFocus and GainedFocus event, but I was just wondering if there was a more neat way to achieve this.
I also thought of creating my own class derived from sf::RenderWindow, but after a look at the files my head hurt ;D

Thanks in advance!
Steef
« Last Edit: November 13, 2012, 05:45:45 pm by Steef435 »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Get the focus of a window
« Reply #1 on: November 10, 2012, 01:56:16 pm »
If you rely on events rather than direct inputs for your "MouseButtonIsDown" condition, it should be ok. Mouse clicks outside the window don't trigger events.

There's no direct way to check if the window is focused, so tracking the LostFocus and GainedFocus event is the only solution if you go this way.
Laurent Gomila - SFML developer

Steef435

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Get the focus of a window
« Reply #2 on: November 10, 2012, 04:33:50 pm »
Thanks for your reply.

So if I keep my mouse down for 10 seconds, will the MouseButtonPressed event be given every loop in that 10 seconds?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Get the focus of a window
« Reply #3 on: November 10, 2012, 05:00:22 pm »
So if I keep my mouse down for 10 seconds, will the MouseButtonPressed event be given every loop in that 10 seconds?
Depends on what you imagine under 'every loop'.
The event MouseButtonPressed will get triggered with a certain interval (usually configurable in your OS settings) and thus it won't get process every frame iteration. If you want a more accurate way use sf::Mouse.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Get the focus of a window
« Reply #4 on: November 10, 2012, 05:23:14 pm »
Quote
The event MouseButtonPressed will get triggered with a certain interval (usually configurable in your OS settings)
This applies only to keys, not mouse buttons ;)
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Get the focus of a window
« Reply #5 on: November 10, 2012, 06:37:35 pm »
This applies only to keys, not mouse buttons ;)
Oh well, right. :D
I was unsure if it really worked like that, but then I thought of the key event which has the same naming scheme, thus I guessed it would work the same.

So if I keep my mouse down for 10 seconds, will the MouseButtonPressed event be given every loop in that 10 seconds?
No the MouseButtonPressed event will only get triggered once, you press down the mouse button. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Steef435

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Get the focus of a window
« Reply #6 on: November 12, 2012, 10:13:39 pm »
I get it, thanks.
I fixed the problem with a bool to keep track of the focus using the events.

It works pretty well, but when I try to move the window by the titlebar(drag the window), it takes this as a mousedown.
But my widgets aren't drawn on(under) the titlebar, so there's no way to actually "shoot" at the titlebar.
Is there a way for me to fix this too(while keeping the titlebar)? Is the size of the titlebar a part of the size of the window I give at creation?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Get the focus of a window
« Reply #7 on: November 12, 2012, 10:40:17 pm »
I'm not sure if the mouse event should get triggered, but it at least doesn't count to the window size, thus you could check if the sf::Mouse::getPosition().y is bigger than 0 and only then react to the event.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Steef435

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Get the focus of a window
« Reply #8 on: November 13, 2012, 05:45:25 pm »
Ok, thanks!
I'm using XFCE 4.10, if you're interested.
I'll set this topic to solved, since I don't know if this is a bug or not.
Thanks again for the help.