Author Topic: How to create a rectangle selection? (selection box)  (Read 3412 times)

vexe

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 153
    • View Profile
How to create a rectangle selection? (selection box)
« on: September 04, 2013, 05:03:51 AM »
Hello again :D

As you probably know, I'm working on a file manager. Some very basic thing that you should be able to do, is to select multiple files/folders with your mouse using a rectangle selection. I have set up Multiple selection just fine (click on file, hold ctrl, click on others to add them to the current selection) - See attachments.

What I thought about is, draw a simple Sprite, let its beginning (start position) be where the mouse clicks first, and then when you move the mouse, the sprite scales along with you. There are of course tricky things that I have to watch out for, like a negative scale which is why I think should change the sprite's pivot point appropriately, like:

  1. #Psudo code
  2.  
  3. var start = mouse.pos;
  4. if (mouse moves right and up 'start')
  5.     sprite.pivot = bottomLeft;
  6. (... some more ifs covering the rest of possibilities ...)
  7. whenever the mouse moves:
  8.     sprite.scale = dist(cursor, sprite);
  9.  

Or something along that - And then, the way I'm thinking to detect collision between my files/folders and the rect selection, is via the colliders - if (there is a collision between the rect and a file/folder => add the colliding file/folder to the selection)
Prob solved.

Now I'm asking because I believe there is a better and maybe easier way to draw the rect, the way I mentioned isn't all that walk in the park.

So, what are you ideas/suggestions in achieving a rectangle selection? in other words having a sprite follow the cursor and scaling nicely with it? in other words draw a rectangle between two points?

- Thanks a bunch for any help/ideas!