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

Author Topic: Mouse Drag Box tutorial with SFML?  (Read 4115 times)

0 Members and 1 Guest are viewing this topic.

Mars_999

  • Full Member
  • ***
  • Posts: 103
    • View Profile
    • Email
Mouse Drag Box tutorial with SFML?
« on: May 26, 2012, 05:23:46 pm »
Anyone have a tutorial on doing a mouse drag box with SFML? I don't remember seeing one, and find it hard to believe their hasn't been any desires to see one...

Thanks!

Perde

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
Re: Mouse Drag Box tutorial with SFML?
« Reply #1 on: May 26, 2012, 05:40:21 pm »
Well, all you need to do is (very) basically this.

Have an object with the sprite you want to drag and a flag-variable.
When the mouse is clicked and the cursor is on the sprite set the flag to true.
When the flag is true the sprite is set to the position of the cursor whenever it's moved, before the sprite is drawn or whenever you choose to do so.
When the mouse is released you set the flag to false again.

At least that's how I would do it. You can of course add more stuff that defines the behaviour, like interaction with other objects or the borders of the window.

Mars_999

  • Full Member
  • ***
  • Posts: 103
    • View Profile
    • Email
Re: Mouse Drag Box tutorial with SFML?
« Reply #2 on: May 26, 2012, 05:43:12 pm »
I am thinking more of a RTS type box, where I click and drag and draw a box with lines to select units... in the box... but now I think about it, both situations would be nice to see.

1. Click drag box to select units
2. Click on unit to drag it around like you said...


Perde

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
Re: Mouse Drag Box tutorial with SFML?
« Reply #3 on: May 26, 2012, 05:50:26 pm »
Ooh, I see. How about this? When you get a click-event and the cursor is in the area where the dragging is possible you save the starting coords (you can use them as a flag, for instance if you're not dragging the coords are set to 0/0 or -1/-1 or something, you get the idea) and redraw it from the starting point to the current cursor position whenever the mouse is moved. When the key is released again you do the calculation what's inside the box and continue with whatever you wanna do.

I've never done something like this before though, it's just an idea.

 

anything