Just a little snippet I played around with when I was bored.
Thought it might be useful to someone, so here it is...
Basically you get some crosshairs controlled by the mouse, and you can select an area of the screen using the
left mouse button.
You can de-select the area using the
right mouse button, or by selecting a new area...
// Crosshairs and Area Selection
// LMB = Left Mouse Button
// RMB = Right Mouse Button
// A type to hold our selected area coordinates...
type Area_Type
x1 as integer
y1 as integer
x2 as integer
y2 as integer
endtype
set window on
sync on : sync rate 0
hide mouse
selX as integer // Holds coords for where you started holding the LMB.
selY as integer
lmHold as boolean // Bool to see if the LMB is being held down.
lmHold = 0
global sArea as Area_Type // Holds coordinates for the the selected area after you release the LMB.
global AreaSelected as boolean // Lets us know if there is a selected area to use, for some purpose...
AreaSelected = 0
// Main Loop
do
cls
msx = mousex()
msy = mousey()
mclk = mouseclick()
if mclk = 2 then AreaSelected = 0 // De-select using RMB.
// Some logic to allow us to hold down the left mouse button to start selecting...
if mclk = 1 and lmHold = 0
lmHold = 1
selX = msx
selY = msy
AreaSelected = 0
endif
// Stores the selected area when the LMB is released.
if mclk = 0 and lmHold = 1
lmHold = 0
sArea.x1 = selX
sArea.y1 = selY
sArea.x2 = msx
sArea.y2 = msy
AreaSelected = 1
if sArea.x1 > sArea.x2 // Makes sure that the
cTemp = sArea.x1 // coordinates are stored
sArea.x1 = sArea.x2 // in the right
sArea.x2 = cTemp // order...
endif
if sArea.y1 > sArea.y2 // Same, but for the Y coords...
cTemp = sArea.y1
sArea.y1 = sArea.y2
sArea.y2 = cTemp
endif
endif
// If the left mouse button is held down, draw the selection box and an extra crosshair...
if lmHold = 1
DrawSelection(selX,selY,msx,msy,rgb(0,63,0))
DrawCrosshairs(selX, selY, rgb(0,127,0))
endif
// If there is an area selected, draw it in a dark gray color...
if AreaSelected = 1
DrawSelection(sArea.x1, sArea.y1, sArea.x2, sArea.y2, rgb(31,31,31))
endif
// Draws the crosshair that follows the mouse around.
DrawCrosshairs(msx, msy, rgb(0,255,0))
ink rgb(255,255,255),0
text 0,0, "Top-Left : " + str$(sArea.x1) + ", " + str$(sArea.y1) // Prints the selected coords
text 0,15, "Bottom-Right: " + str$(sArea.x2) + ", " + str$(sArea.y2) // in the top left corner...
if AreaSelected = 1
text 0,30, "Active Selection: YES"
else
text 0,30, "Active Selection: NO"
endif
sync
loop
// Draws the crosshairs.
function DrawCrosshairs(cx as integer, cy as integer, col as dword)
ink col,0
line cx,0,cx,1050
line 0,cy,1680,cy
endfunction
// Draws a box over the selected area.
function DrawSelection(x1 as integer, y1 as integer, x2 as integer, y2 as integer, col as dword)
temp as integer
if x1 > x2 // Switches the coordinates
temp = x1 // around so that the
x1 = x2 // box is drawn
x2 = temp // correctly...
endif
if y1 > y2 // Same here, but for the Y coordinates...
temp = y1
y1 = y2
y2 = temp
endif
ink col,0
box x1,y1,x2,y2
endfunction
- enderleit