# Detecting mouse in box? In Blitz Plus.

This topic is 2206 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am trying to make a tile map editor in Blitz Plus. To do this, I divided the screen up into a grid. However, I am having trouble actually telling where the mouse is. I have tried a couple methods, but none of them worked. Each tile is 40 pixels high and 40 pixels wide.

First we would actually draw the grid using this code:

Function DrawBoxes()
;draw the horizontal rectangles
For horiz = 40 To 600 Step 40 ;-----------
Rect 0,horiz,800,5,1
Next

;draw the vertical rectangles
For verti = 40 To 800 Step 40
Rect verti,0,5,600,1
Next

End Function

Here's my first method for detecting where the mouse is:
For columns = 40 To 800 Step 40 ;we will actually be using the coordinate positions for this
For rows = 40 To 600 Step 40
If MouseX() < (rows + 20) And MouseX() > (rows - 20) ;if it is inside the box horizontally ;we are testing this from the center of the box
If MouseY() < (columns + 20) And MouseY() > (columns - 20) ;if it is inside the box vertically
Color 255,0,0
Rect rows,columns,40,40,1 ;draw a red box so the user knows it is selected
EndIf
EndIf
;EndIf
Next
Next

However, this method lets the user select multiple boxes at once, and if you decrease the size of the hitbox, it barely works at all.

Method 2:
For columns = 40 To 800 Step 40 ;we will actually be using the coordinate positions for this
For rows = 40 To 600 Step 40
If RectsOverlap(rows,columns,25,25,MouseX(),MouseY(),12,21) ;use a smaller hitbox for the rectangle
Color 255,0,0
Rect rows,columns,40,40,1
Endif
Next
Next

However, this method barely works either, and if you increase the size of the hitbox, it lets the user select multiple boxes at once.

Any ideas on how to tell where the mouse is in multiple boxes that are right beside each other? Thanks.

##### Share on other sites
The mouse is in box MouseX() / 40 and MouseY() / 40 you'll have to add in the offset if your grid's start is something other than 0,0. You can can get the screen coordinates of the upper left hand corner of square the mouse is by multiplying the result of the previous operation by 40 the adding in any offset for where your grid begins. Hope that helps and I haven't completely misunderstood(4am here lol)

You could also keep track of the box information in an array, storing the x,y of the upper left corner and check the mouse position against the array when needed, say a click.

##### Share on other sites
Here is a way that I use to find where the mouse is located in the grid:

Vector2f getGridLoc(int tileSize)
{
Vector2f gridLoc;
gridLoc.x = std::floor(mouse.x / tileSize);
gridLoc.y = std::floor(mouse.y / tileSize);
return gridLoc;
}

1. 1
2. 2
frob
15
3. 3
4. 4
5. 5
Rutin
12

• 12
• 12
• 58
• 14
• 15
• ### Forum Statistics

• Total Topics
632119
• Total Posts
3004208

×