QCard DLL has a function that automatically drags a card from its current position to the xy position specified. It is called ReturnDrag. It might be used to return acard to its original location after a user attempts to drop it in an illegal spot. It can also be used to move a card so that it lines up properly in a row or column or on a pile of other cards. It might also be used for special effects.
Dude, where's my card?
If we want to return a card to its original location, we need to know the values for x and y. We can do this with GetCardX and GetCardY in the DLL.
calldll#qc,"GetCardX",_
nC aslong,_ 'index of card
CardX aslong'x location of upper cornercalldll#qc,"GetCardY",_
nC aslong,_ 'index of card
CardY aslong'y location of upper corner
InitDrag returns the index of the card under the mouse, so we can use that value to get the X and Y location of the card.
cardIndex=InitDrag(hBox,MouseX,MouseY)
We'll use the return from InitCard to see if the card is blocked. If it is blocked, we'll call AbortDrag.
isBlocked=GetCardBlocked(cardIndex)'see if card is blockedif isBlocked<>0then'if card is blocked, inform user and abort drag operationcall AbortDrag
#1.g "place 10 420;\BLOCKED Card Index is ";cardIndex;space$(60)waitendif
If the card is not blocked we'll allow the program to continue. We'll keep track of the index of the dragged card in a variable called dragCard and we'll get the X and Y location in case we need to return the card later.
'document card index and location for return drag
dragCard=cardIndex
cardX=GetCardX(dragCard)
cardY=GetCardY(dragCard)
Is the move legal?
For this demo we've eliminated the shuffle to simplify things. We've also drawn a box in the corner as a target for the user to drag a card. Cards must land in the box or the move is illegal.
We allow drag. When the user releases the right mouse button we check with GetCardX and GetCardY
to see if the card is completely within the borders of the box.
#1.g "when rightButtonUp"
cardIndex=EndDrag(hBox,MouseX,MouseY)
cX=GetCardX(dragCard)'get x location of card
cY=GetCardY(dragCard)'get y location of card
Here is the routine that checks the location and uses the ReturnDrag function to return the card if it is not in the box. If it is within the box, we use ReturnDrag to line it up with previous cards in the pile.
if(cX<450)or(cY<250)then'return card if it is not in boxcall ReturnDrag hBox, dragCard,cardX,cardY
#1.g "place 10 420;\Illegal move.";space$(60)else'put it on pile in boxcall ReturnDrag hBox, dragCard,470,260#1.g "place 10 420;\Card placed in box. Thanks. ";space$(50)endif
ReturnDrag
Here is the ReturnDrag function which causes the automatic dragging operation. It requires the handle of the graphicbox, the index of the card to drag, and the xy target location.
calldll#qc,"ReturnDrag",_ 'automatic dragging
hndle asulong,_ 'handle of graphicbox
nC aslong,_ 'card to drag
nx aslong,_ 'x location to drag to
ny aslong,_ 'y location to drag to
re asvoid'no return
DEMO
See Lesson 1 for QCard DLL and WAV files needed for the demo code.
'An open project card game, begun by Alyce Watson, May 27, 2003.'Uses Qcard32.dll, a freeware library of playing card images.'DLL by Stephen Murphy. Qcard32.DLL website:'http://www.telusplanet.net/public/stevem/dim card(24)'array to hold cardsgosub[fillCardArray]'fill array with card values[varSetup]
i=0'i will be our counter var in for/next loops
design=1'default design is circles
offset=20'offset for card vertical spacing for dragging purposesnomainwinWindowWidth=640:WindowHeight=480UpperLeftX=1:UpperLeftY=1menu#1,"&File","&New",[new],"E&xit",[quit]graphicbox#1.g,0,0,640,440open"Dragging Cards"for window_nf as#1#1"trapclose [quit]"'get graphicbox handle
hBox=hwnd(#1.g)'open the dllopen"qcard32.dll"fordllas#qc
'initialize the deckCall InitializeDeck hBox
[new]Call SetDefaultValues
'draw a nice background#1.g "down; fill 10 190 225"#1.g "backcolor 10 190 225"'draw a box in lower right corner#1.g "place 450 250;box 700 500"#1.g "place 450 230;\Drag Cards Here."'temp message for this demo only#1.g "place 10 420"#1.g "\Right-click on a card, then move mouse to drag and drop it."
offset=20call SetOffSet offset 'set offset to 20 pixels - default is 16'set xy location to start deal
x=10:y=2:row=1for i =1to24Call DealCard hBox,card(i),x,y
'When creating a row or pile of cards,'only the topmost card should have an'IsBlocked value of FALSE.if row<4thencall AdjustCardBlocked card(i),1
x=x+100if x>510then'move to next row
x=10
y=y+offset
row=row+1endifplaywave"card.wav",sync
'pause 100 milliseconds between cardscall Pause 100scannext#1.g "when rightButtonDown [initDrag]"wait[initDrag]
cardIndex=InitDrag(hBox,MouseX,MouseY)
isBlocked=GetCardBlocked(cardIndex)'see if card is blockedif isBlocked<>0then'if card is blocked, inform user and abort drag operationcall AbortDrag
#1.g "place 10 420;\BLOCKED Card Index is ";cardIndex;space$(60)waitendif#1.g "place 10 420;\Drag Card Index is ";cardIndex;space$(60)#1.g "when rightButtonMove [doDrag]"'document card index and location for return drag
dragCard=cardIndex
cardX=GetCardX(dragCard)
cardY=GetCardY(dragCard)wait[doDrag]call DoDrag hBox,MouseX,MouseY#1.g "when rightButtonUp [endDrag]"wait[endDrag]#1.g "when rightButtonUp"
cardIndex=EndDrag(hBox,MouseX,MouseY)
cX=GetCardX(dragCard)'get x location of card
cY=GetCardY(dragCard)'get y location of cardif(cX<450)or(cY<250)then'return card if it is not in boxcall ReturnDrag hBox, dragCard,cardX,cardY
#1.g "place 10 420;\Illegal move.";space$(60)else'put it on pile in boxcall ReturnDrag hBox, dragCard,470,260#1.g "place 10 420;\Card placed in box. Thanks. ";space$(50)endifwait[fillCardArray]'fill card array'cards 1 to 52 are in the first deck'cards 53 to 104 are in the second deck'use cards Jack through King in each suit, first deck
card(1)=11'jack of clubs
card(2)=12'queen
card(3)=13'king
card(4)=24'jack of diamonds
card(5)=25'queen
card(6)=26'king
card(7)=37'jack of hearts
card(8)=38'queen
card(9)=39'king
card(10)=50'jack of spades
card(11)=51'queen
card(12)=52'king'now use second deck, to fill second half of arrayfor i =1to12
card(i+12)=card(i)+52nextRETURN[quit]close#qc:close#1:end'''''''''''''''''''''subs and functions:Sub Pause ms
'pause ms number of millisecondscalldll#kernel32,"Sleep",_
ms aslong, re asvoidEndSubFunction GetCardX(nC)calldll#qc,"GetCardX",_
nC aslong,_ 'index of card
GetCardX aslong'x location of upper cornerendfunctionFunction GetCardY(nC)calldll#qc,"GetCardY",_
nC aslong,_ 'index of card
GetCardY aslong'y location of upper cornerendfunctionSub SetOffSet offset
calldll#qc,"SetOffSet",offset aslong,_
re asvoidendsubSub AdjustCardBlocked nC, bValue
calldll#qc,"AdjustCardBlocked",_
nC aslong, bValue aslong, re asvoidendsubFunction GetCardBlocked(nC)calldll#qc,"GetCardBlocked",nC aslong,_
GetCardBlocked aslongendfunctionSub ReturnDrag hndle,nC,nx,ny
calldll#qc,"ReturnDrag",_ 'automatic dragging
hndle asulong,_ 'handle of graphicbox
nC aslong,_ 'card to drag
nx aslong,_ 'x location to drag to
ny aslong,_ 'y location to drag to
re asvoid'no returnendsubFunction InitDrag(hndle, x, y)calldll#qc,"InitDrag",_
hndle asulong, x aslong, y aslong,_
InitDrag aslongendfunctionSub AbortDrag
calldll#qc,"AbortDrag",re asvoidendsubSub DoDrag hndle,x,y
calldll#qc,"DoDrag",hndle asulong,_
x aslong, y aslong, r asvoidendsubFunction EndDrag(hndle,x,y)calldll#qc,"EndDrag",hndle asulong,_
x aslong, y aslong, EndDrag aslongendfunctionSub InitializeDeck hndle
calldll#qc,"InitializeDeck",_
hndle asulong,r aslongEndSubSub DealCard hndle,nC,x,y
'places card on window whose handle is hndle at x,y'nC is number of card - 1-52 in first deck and'53-104 in second deck, if usedcalldll#qc,"DealCard",hndle asulong,nC aslong,_
x aslong,y aslong,r asvoidEndSubSub SetDefaultValues
'reset all card properties back to their default values.calldll#qc,"SetDefaultValues",r asvoidEndSub
QCard DLL Lesson 12
Lesson 11 Lesson 13-
QCard DLL Lesson 12 | Dragging Cards Automatically | Dude, where's my card? | Is the move legal? | ReturnDrag | DEMO
See Lesson 1 for QCard DLL and WAV files needed for the demo code.
Dragging Cards Automatically
QCard DLL has a function that automatically drags a card from its current position to the xy position specified. It is called ReturnDrag. It might be used to return acard to its original location after a user attempts to drop it in an illegal spot. It can also be used to move a card so that it lines up properly in a row or column or on a pile of other cards. It might also be used for special effects.Dude, where's my card?
If we want to return a card to its original location, we need to know the values for x and y. We can do this with GetCardX and GetCardY in the DLL.InitDrag returns the index of the card under the mouse, so we can use that value to get the X and Y location of the card.
We'll use the return from InitCard to see if the card is blocked. If it is blocked, we'll call AbortDrag.
If the card is not blocked we'll allow the program to continue. We'll keep track of the index of the dragged card in a variable called dragCard and we'll get the X and Y location in case we need to return the card later.
Is the move legal?
For this demo we've eliminated the shuffle to simplify things. We've also drawn a box in the corner as a target for the user to drag a card. Cards must land in the box or the move is illegal.We allow drag. When the user releases the right mouse button we check with GetCardX and GetCardY
to see if the card is completely within the borders of the box.
Here is the routine that checks the location and uses the ReturnDrag function to return the card if it is not in the box. If it is within the box, we use ReturnDrag to line it up with previous cards in the pile.
ReturnDrag
Here is the ReturnDrag function which causes the automatic dragging operation. It requires the handle of the graphicbox, the index of the card to drag, and the xy target location.DEMO
See Lesson 1 for QCard DLL and WAV files needed for the demo code.QCard DLL Lesson 12 | Dragging Cards Automatically | Dude, where's my card? | Is the move legal? | ReturnDrag | DEMO
Lesson 11 Lesson 13