Lock the mouse to a grid.

Source-code only - please, don't post questions here.
Pritchard
Posts: 5425
Joined: Sep 12, 2005 20:06
Location: Ohio, USA

Lock the mouse to a grid.

Postby Pritchard » Mar 15, 2007 22:08

This has been posted before in the forums. I asked help on this when I was making CHAOS Map Creator, but here's a much simplified version that puts the mouse into a blocked range. I just feel it's important to have it in tips & tricks somewhere.

Code: Select all

Type MapScreen
  BlockWidth      As Integer
  BlockHeight     As Integer
 
  ScreenWidth     As Integer
  ScreenHeight    As Integer
End Type

Type Mouse
  X               As Integer
  Y               As Integer
  Btn             As Integer
End Type

Dim As MapScreen Wnd
Dim As Mouse Ms

  '' Our current tile positions.  Locks the mouse to a grid.
Dim As Integer CurTileX, CurTileY

Wnd.BlockWidth = 64
Wnd.BlockHeight = 32

Wnd.ScreenWidth = 640
Wnd.ScreenHeight = 480

  '' Set up the screen
Screenres Wnd.ScreenWidth, Wnd.ScreenHeight, 32

  '' Lock the page so we can draw on it.
screenlock
Do
 
    '' Get the mouse position
  GetMouse Ms.X, Ms.Y, , Ms.Btn
 
    '' Set the current tile positions.
  CurTileX = Ms.X \ Wnd.BlockWidth
  CurTileY = Ms.Y \ Wnd.BlockHeight
 
 
  '' Loop through all of the blocks.
    '' Horizontal blocks
  For XVar As Integer = 0 To (Wnd.ScreenWidth - Wnd.BlockWidth ) Step Wnd.BlockWidth
      '' Vertical blocks
    For YVar As Integer = 0 To (Wnd.ScreenHeight - Wnd.BlockHeight ) Step Wnd.BlockHeight
     
        '' Draw a box around the block.
      Line (XVar, YVar)-(XVar + ( Wnd.BlockWidth - 1 ), YVar + ( Wnd.BlockHeight - 1 ) ), rgb(255, 0, 0), b

    Next
  Next
 
    '' Put the mouse in the proper block.
    ''
    '' Multiply current tile we're on by the tile's size to find the positions.
    ''
  Line ( CurTileX * Wnd.BlockWidth, CurTileY * Wnd.BlockHeight) - ( (CurTileX * Wnd.BlockWidth) + (Wnd.BlockWidth - 1), (CurTileY * Wnd.BlockHeight) + (Wnd.BlockHeight - 1) ), rgb(255, 255, 255), bf
 
    '' How do we exit this program anyways? :P
  Locate 1, 1
  Print "Click Left-Mouse to Exit"
  Print "Current Tile: " & CurTileX & ", " & CurTileY
 
    '' Page Flip
  screenunlock
  screenlock
  Cls
 
  '' Mouse is in window when the click is being done.
Loop Until ( ( Ms.X > 0 ) <> 0 ) And ( ( Ms.Btn And 1 ) <> 0 )
screenunlock


How it's Done:

- Integer Divide the mouse axis (either x or y) by the tile's size to find which tile we're on

- Multiply the tile we're on by tile size and we're on the tile's start position.

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 0 guests