Hi,
I've made this functions today, because I was a bit bored... this function collection, called "mouse plugin" (short "mop", also the mainly used prefix at the beginning of all used functions, arrays, types and variables), can handle different mouse-operations, and offers different features for your programs.
It's easy to implement, you just have to include the code and then you can use the functions.
At the beginning you should call "StartMop()" to setup all variables etc.
Features:
-Management of Mouse-Position and -Movement
-Click-Management for right mousebutton, left button and the wheel (Key pressed, click started, click stopped, editable doubleclick (time-delay between two clicks, both clicks on the same position etc.))
-Field/Zone-Functions (Create different Trigger-Zones and easily check if they are hovered or clicked)
-Mouse can be locked at a defined position (usefull for shooters etc)
-Simple Drag-and-Drop-System
Anyone can use the code, if he wants, and edit it, and can name me in the credits - if he wants.. doesn't really matter, I'd just be happy if I could help someone with it...
The functions:
rem **This function have to be called before all other mop-functions. Without it other functions will crash
rem or return wrong values
function StartMop()
global mop_x as integer `mouse-x
global mop_y as integer `mouse-y
global mop_mx as integer `move-x
global mop_my as integer `move-y
global mop_mz as integer `move-z
global mop_left as integer `mouseclick left
global mop_right as integer `mouseclick right
global mop_top as integer `mouse-wheel pressed
global mop_nleft as integer `leftbutton new pressed
global mop_nright as integer
global mop_ntop as integer
global mop_lleft as integer `leftbutton was pressed last round
global mop_lright as integer
global mop_ltop as integer
global mop_lcl as integer `last left-click (timer)
global mop_lcr as integer
global mop_lct as integer
global mop_lclx as integer `x-pos of the last click
global mop_lcly as integer
global mop_lcrx as integer
global mop_lcry as integer
global mop_lctx as integer
global mop_lcty as integer
global mop_dcl as integer `double-click left
global mop_dcr as integer
global mop_dct as integer
global mop_dcDelay as dword `Double-click-delay (time between to clicks)
global mop_dcSamePos as boolean `if both clicks have to be on the same position
mop_dcDelay = 250
mop_dcSamePos = 1
rem Drag&Drop
global mop_dragged as boolean
global mop_dragstartx as integer
global mop_dragstarty as integer
global mop_dragdifx as integer
global mop_dragdify as integer
global mop_dragrelx as integer
global mop_dragrely as integer
rem Mouse-Locking
global mop_locked as boolean
global mop_LockX as integer
global mop_LockY as integer
rem Zones
global mop_Zones as integer
global mop_ZoneFillColor as dword
global mop_ZoneBorderColor as dword
global mop_ZoneTextColor as dword
dim mop_Zone(-1) as mop_ZoneType
mop_Zones = -1
mop_ZoneBorderColor = rgb(255,255,255)
mop_ZoneFillColor = rgb(128,128,128)
mop_ZoneTextColor = rgb(255,0,0)
endfunction
type mop_ZoneType
x1 as integer
y1 as integer
x2 as integer
y2 as integer
endtype
function mop_SetDoubleClickDelay(time)
mop_dcDelay = time
endfunction
function mop_SetDoubleClickSamePos(SamePos as boolean)
mop_dcSamePos = SamePos
endfunction
function mop_Update()
mx = mousex()
my = mousey()
mop_mx = mx-mop_x
mop_my = my-mop_y
mop_mz = mousemovez()
mop_x = mx
mop_y = my
rem Lock mouse
if mop_Locked = 1
position mouse mop_LockX, mop_LockY
endif
rem Buttons pressed
mc = mouseclick()
left = ((mc=1) or (mc=3) or (mc=5))
right = ((mc=2) or (mc=3) or (mc=6))
top = ((mc=4) or (mc=5) or (mc=6))
if mc=7 then left=1 : right=1 : top=1
rem Left button -----
if mop_dcl = 1 then mop_dcl = 0
if left = 1 `left pressed
if mop_nleft=1 then mop_nleft = 0
if mop_lleft=0 then mop_nleft = 1
mop_left = 1
else `left is not pressed
lc = mop_lcl : lcx = mop_lclx : lcy = mop_lcly
if mop_lleft = 1 `left was pressed last round
mop_left = 0
mop_nleft = -1
mop_lclx = mop_x : mop_lcly = mop_y
mop_lcl = timer()
if (((timer()-lc)<=mop_dcdelay) and ((lcx=mop_x) and (lcy=mop_y) or (mop_dcSamePos=0))) then mop_dcl = 1 else mop_dcl = 0
else `left was not pressed
mop_nleft = 0
endif
endif
mop_lleft = mop_left
rem right button -----
if mop_dcr = 1 then mop_dcr = 0
if right = 1 `right pressed
if mop_nright=1 then mop_nright = 0
if mop_lright=0 then mop_nright = 1
mop_right = 1
else `right is not pressed
lc = mop_lcr : lcx = mop_lcrx : lcy = mop_lcry
if mop_lright = 1 `right was pressed last round
mop_right = 0
mop_nright = -1
mop_lcrx = mop_x : mop_lcry = mop_y
mop_lcr = timer()
if (((timer()-lc)<=mop_dcdelay) and ((lcx=mop_x) and (lcy=mop_y) or (mop_dcSamePos=0))) then mop_dcr = 1 else mop_dcr = 0
else `right was not pressed
mop_nright = 0
endif
endif
mop_lright = mop_right
rem top button -----
if mop_dct = 1 then mop_dct = 0
if top = 1 `top pressed
if mop_ntop=1 then mop_ntop = 0
if mop_ltop=0 then mop_ntop = 1
mop_top = 1
else `top is not pressed
lc = mop_lct : lcx = mop_lctx : lcy = mop_lcty
if mop_ltop = 1 `top was pressed last round
mop_top = 0
mop_ntop = -1
mop_lctx = mop_x : mop_lcty = mop_y
mop_lct = timer()
if (((timer()-lc)<=mop_dcdelay) and ((lcx=mop_x) and (lcy=mop_y) or (mop_dcSamePos=0))) then mop_dct = 1 else mop_dct = 0
else `top was not pressed
mop_ntop = 0
endif
endif
mop_ltop = mop_top
rem End of this part -----
endfunction
function mop_PosX()
endfunction mop_x
function mop_PosY()
endfunction mop_y
function mop_MoveX()
endfunction mop_mx
function mop_MoveY()
endfunction mop_my
function mop_MoveZ()
endfunction mop_mz
function mop_Click(Button)
select Button
case 1 : exitfunction mop_left : endcase
case 2 : exitfunction mop_right : endcase
case 3 : exitfunction mop_top : endcase
endselect
endfunction 0
function mop_NewClick(Button)
select Button
case 1 : exitfunction mop_nleft : endcase
case 2 : exitfunction mop_nright : endcase
case 3 : exitfunction mop_ntop : endcase
endselect
endfunction 0
function mop_DoubleClick(Button)
select Button
case 1 : exitfunction mop_dcl : endcase
case 2 : exitfunction mop_dcr : endcase
case 3 : exitfunction mop_dct : endcase
endselect
endfunction 0
function mop_InField(x1,y1,x2,y2)
if mop_y <= y2
if mop_y => y1
if mop_x <= x2
if mop_x => x1
exitfunction 1
endif
endif
endif
endif
endfunction 0
function mop_CreateZone(x1,y1,w,h)
x2 = x1+w
y2 = y1+h
inc mop_Zones
array insert at bottom mop_Zone(0), mop_Zones
mop_Zone(mop_Zones).x1 = x1
mop_Zone(mop_Zones).y1 = y1
mop_Zone(mop_Zones).x2 = x2
mop_Zone(mop_Zones).y2 = y2
endfunction mop_Zones
function mop_GetZoneX(ID)
x = mop_Zone(ID).x1
endfunction x
function mop_GetZoneY(ID)
y = mop_Zone(ID).y1
endfunction y
function mop_GetZoneX2(ID)
x2 = mop_Zone(ID).x2
endfunction x2
function mop_GetZoneY2(ID)
y2 = mop_Zone(ID).y2
endfunction y2
function mop_GetZoneWidth(ID)
w = mop_Zone(ID).x2 - mop_Zone(ID).x1
endfunction w
function mop_GetZoneHeight(ID)
h = mop_Zone(ID).y2 - mop_Zone(ID).y1
endfunction h
function mop_PositionZone(ID,X,Y)
w = mop_Zone(ID).x2-mop_Zone(ID).x1
h = mop_Zone(ID).y2-mop_Zone(ID).y1
mop_Zone(ID).x1 = x
mop_Zone(ID).y1 = y
mop_Zone(ID).x2 = x+w
mop_Zone(ID).y2 = y+h
endfunction
function mop_RelPositionZone(ID,xadd,yadd)
inc mop_Zone(ID).x1, xadd
inc mop_Zone(ID).y1, yadd
inc mop_Zone(ID).x2, xadd
inc mop_Zone(ID).y2, yadd
endfunction
function mop_InZone(ID)
if mop_y <= mop_Zone(ID).y2
if mop_y => mop_Zone(ID).y1
if mop_x <= mop_Zone(ID).x2
if mop_x => mop_Zone(ID).x1
exitfunction 1
endif
endif
endif
endif
endfunction 0
function mop_ShowZone(ID,t$,Filled as byte) `Info> Filled: 0=lines, 1=box, 2=lines+box
if Filled > 0
ink mop_ZoneFillColor, 0
box mop_Zone(ID).x1+1, mop_Zone(ID).y1+1, mop_Zone(ID).x2, mop_Zone(ID).y2
endif
if Filled <> 1
ink mop_ZoneBorderColor, 0
line mop_Zone(ID).x1,mop_Zone(ID).y1,mop_Zone(ID).x2,mop_Zone(ID).y1
line mop_Zone(ID).x1,mop_Zone(ID).y1,mop_Zone(ID).x1,mop_Zone(ID).y2
line mop_Zone(ID).x2,mop_Zone(ID).y2,mop_Zone(ID).x1,mop_Zone(ID).y2
line mop_Zone(ID).x2,mop_Zone(ID).y2,mop_Zone(ID).x2,mop_Zone(ID).y1
endif
if t$<>""
ink mop_ZoneTextColor, 0
center text mop_Zone(ID).x1+(mop_Zone(ID).x2-mop_Zone(ID).x1)/2, mop_Zone(ID).y1+(mop_Zone(ID).y2-mop_Zone(ID).y1)/2-text size()/2, t$
endif
endfunction
function mop_SetZoneColors(Txt as dword, Fill as dword, Border as dword)
mop_ZoneTextColor = Txt
mop_ZoneFillColor = Fill
mop_ZoneBorderColor = Border
endfunction
function mop_LockMouse(x,y)
mop_Locked = 1
mop_LockX = x
mop_LockY = y
endfunction
function mop_UnlockMouse()
mop_Locked = 0
endfunction
function mop_GetLocked()
endfunction mop_Locked
function mop_GetLockX()
endfunction mop_LockX
function mop_GetLockY()
endfunction mop_LockY
function mop_GetDoubleClickSamePos()
endfunction mop_dcSamePos
function mop_GetDoubleClickDelay()
endfunction mop_dcDelay
function mop_Drag()
mop_dragged = 1
mop_dragstartx = mop_x
mop_dragstarty = mop_y
endfunction
function mop_DragObject(ObjX,ObjY)
mop_dragged = 1
mop_dragrelx = mop_x-ObjX
mop_dragrely = mop_y-ObjY
endfunction
function mop_GetDraggedObjectX()
x = mop_x - mop_dragrelx
endfunction x
function mop_GetDraggedObjectY()
y = mop_y - mop_dragrely
endfunction y
function mop_Drop()
mop_dragged = 0
mop_dragdifx = mop_x-mop_dragstartx
mop_dragdify = mop_y-mop_dragstarty
endfunction
function mop_GetDragged()
endfunction mop_dragged
function mop_GetDragX()
endfunction mop_dragstartx
function mop_GetDragY()
endfunction mop_dragstarty
function mop_GetDropXDif()
endfunction mop_dragdifx
function mop_GetDropYDif()
endfunction mop_dragdify
My actual example-code is really bad written, but while I'm writing on a new, better one I'll post this here:
set display mode 800,600,32
sync on : sync rate 0
set window on
StartMop()
mop_SetDoubleClickDelay(300)
SamePos = 1
EndButton = mop_CreateZone(300,400,100,25)
Toggle = mop_CreateZone(300,5,100,25)
do
cls
Mop_Update()
print "FPS: ", screen fps()
print
print "MouseX: ", mop_posx()
print "MouseY: ", mop_posy()
print
print "Move-X: ", mop_MoveX()
print "Move-Y: ", mop_MoveY()
print "Move-Z: ", mop_MoveZ()
print
print "Mouseclick: ", mouseclick()
print "Left: ", mop_Click(1)
print "Right: ", mop_Click(2)
print "Top: ", mop_Click(3)
print
print "Newclick:"
print "Left: ", mop_NewClick(1)
print "Right: ", mop_NewClick(2)
print "Top: ", mop_NewClick(3)
print
print "Doubleclick:"
print "SamePos: ", mop_GetDoubleClickSamePos()
print "Delay: ", mop_GetDoubleClickDelay()
print "Left: ", mop_DoubleClick(1)
print "Right: ", mop_DoubleClick(2)
print "Top: ", mop_DoubleClick(3)
print
print "Zone:"
print "Hover: ", mop_InZone(EndButton)
print
print "-Use RMB/TMB to move Button"
print "-Doubleclick for events"
print "-Press 'Toggle' to change SamePos"
rem Double-Click-Events
mop_ShowZone(Toggle,"Toggle",0)
if mop_InZone(Toggle) and mop_NewClick(1)=1 then SamePos = 1-SamePos
mop_SetDoubleclickSamePos(SamePos)
if mop_DoubleClick(1)=1 : for x = 1 to 10 : print rnd(99) : sync : wait 100 : next x : endif
if mop_DoubleClick(2)=1 then for x = 1 to 30 : cls rnd(0xFFFFFF) : sync : wait 10 : next x
if mop_DoubleClick(3)=1 `lock/unlock mouse
if mop_GetLocked()=1
mop_UnlockMouse()
else
mop_LockMouse(mop_posx(),mop_posy())
endif
endif
rem End-Event
mop_ShowZone(EndButton,"End",2)
if mop_NewClick(1)=1 and mop_InZone(EndButton) then end
if spacekey() then mop_PositionZone(EndButton,mousex(),mousey())
rem Drag&Drop Button (rightkey)
rem Drag
if mop_NewClick(2)=1 and mop_InZone(EndButton)
LastX = mop_PosX() : LastY = mop_PosY() : Selected1 = 1
else
rem Drop
if mop_NewClick(2)=-1 and Selected1 = 1
_xdif = mop_PosX()-LastX
_ydif = mop_PosY()-LastY
mop_RelPositionZone(EndButton,_xdif,_ydif)
Selected1 = 0
endif
endif
rem Show Line
if mop_Click(2)=1 and Selected1=1 then line mop_PosX(),mop_PosY(),LastX, LastY
rem Drag&Drop Button (topkey)
rem Drag
if mop_NewClick(3)=1 and mop_InZone(EndButton)
XDif = mop_PosX()-mop_Zone(EndButton).x1
YDif = mop_PosY()-mop_Zone(EndButton).y1
Selected2 = 1
else
rem Drop
if mop_Click(3)=1 and Selected2=1
mop_PositionZone(EndButton,mop_PosX()-XDif,mop_PosY()-YDif)
else
Selected2 = 0
endif
endif
rem Drag&Drop toggle-Button
rem Drag
if (mop_NewClick(2)=1) and (mop_InZone(Toggle)=1)
mop_DragObject(mop_GetZoneX(Toggle),mop_GetZoneY(Toggle))
endif
rem Position/Drop
if mop_GetDragged()=1
if mop_NewClick(2)=-1 then mop_Drop()
mop_PositionZone(Toggle,mop_GetDraggedObjectX(),mop_GetDraggedObjectY())
endif
sync
loop
The code shows the main-features. For example, you can drag and drop the two buttons, using the right/top mousebutton.
Also much information about the mouse is showed on the screen.
I know, that most of the features are easy to do in dark basic, but for example the doubleclick-management is something I often wished to have, and didn't really know how to do it. (I think it's also possible to do it over windows DLLs.. but I'm not sure)
So I'm going to write another example-code now.
I'd apreciate criticism, questions, feature-ideas etc., thanks.
Mr Kohlenstoff
Edit:
I made a new demo-program, the code is better structured, and most features are used again.
Here the code:
rem Display settings
set display mode 1024,768,32
sync on : sync rate 0
randomize timer()
rem Start Mouse-Plugin
StartMoP()
rem Create different zones for the menu
x = 412 : y = 200 : dis = 100 : w = 200 : h = 40
NewGame = mop_CreateZone(x,y,w,h)
LoadGame = mop_CreateZone(x,y+dis,w,h)
Options = mop_CreateZone(x,y+dis*2,w,h)
Credits = mop_CreateZone(x,y+dis*3,w,h)
EndGame = mop_CreateZone(x,y+dis*5,w,h)
Help = mop_CreateZone(10,10,150,30)
rem Change the Zone-Colors
mop_SetZoneColors(rgb(255,255,255),rgb(0,0,200),rgb(255,255,255))
rem Main-Loop
do
rem Clear screen
cls
rem Headline
center text 512,40,"Main-Menu"
rem Update Mouse-Plugin
mop_Update()
rem Show Zones
style = 2 `This is the style, in which the zones are drawn, 0,1 and 2 are possible
mop_ShowZone(NewGame,"SamePos: "+str$(mop_GetDoubleClickSamePos()),style)
if mop_GetLocked()=1 then load$ = "Doubleclick" else load$ = "Click me!"
mop_ShowZone(LoadGame,load$,style)
if (mop_GetDragged()=1 and Dragged=Options) then opt$ = "Drop me!" else opt$ = "Drag me!"
mop_ShowZone(Options,opt$,style)
mop_ShowZone(Credits,"Change Colors",style)
mop_ShowZone(EndGame,"End",style)
mop_ShowZone(Help,"Click/Drag",0)
rem Events
gosub MouseEvents
rem Render screen
sync
loop
rem Labels
MouseEvents:
rem Simple button-clicks
if mop_NewClick(1)=1
rem End
if mop_InZone(EndGame)
end
endif
rem change colors if credits-button is pressed
if mop_InZone(Credits)
c1 = rgb(rnd(255),rnd(255),rnd(255))
c2 = rnd(0xFFFFFF)
c3 = rnd(0xFFFFFF)
mop_SetZoneColors(c1,c2,c3)
endif
rem Load Game-Button -> mouse is locked, until user doubleclicks
if mop_InZone(LoadGame) and mop_GetLocked()=0
mop_LockMouse(mop_PosX(),mop_PosY())
endif
rem New Game-Button -> DoubleClickSamePos is toggled (SamePos=1->both clicks have to hit the same pixel)
if mop_InZone(NewGame) and mop_NewClick(1)
mop_SetDoubleClickSamePos(1-mop_GetDoubleClickSamePos())
endif
endif
rem Doubleclick
if mop_DoubleClick(1)
rem Unlock mouse (if it's locked on the load-button)
mop_UnlockMouse()
rem Help-Button
if mop_InZone(Help) then mop_RelPositionZone(Help,5-rnd(10),5-rnd(10))
endif
rem Drag and Drop Options-Button
if mop_NewClick(1)=1
if mop_InZone(Options)=1
Dragged = Options
mop_DragObject(mop_GetZoneX(Options),mop_GetZoneY(Options))
endif
endif
rem Positioning and Dropping
if mop_GetDragged()=1 and Dragged = Options
mop_PositionZone(Options,mop_GetDraggedObjectX(),mop_GetDraggedObjectY())
rem Drop if mouse-click stops
if mop_NewClick(1)=-1 then mop_Drop()
endif
rem Other Drag-and-Drop-System
if mop_NewClick(1)=1
if mop_InZone(Help)=1
Dragged = Help
mop_Drag()
endif
endif
rem Showing new Position
if mop_GetDragged()=1 and Dragged = Help
line mop_PosX(), mop_PosY(), mop_GetDragX(), mop_GetDragY()
rem Drop
if mop_NewClick(1)=-1
mop_Drop()
mop_RelPositionZone(Help,mop_GetDropXDif(),mop_GetDropYDif())
endif
endif
return
And, finally, an image of this demo.
Have fun!
Visit the DBPro - Speed up your game-Thread. http://forum.thegamecreators.com/?m=forum_view&t=88661&b=1