|
Post by eyfenna on Aug 11, 2019 3:59:26 GMT -6
Heya,
with the following code several white boxes can be drawn on screen with leftshift and pushing mousebutton 1, one drawn box can be recolloured to red by clicking inside it. Additional with holding the mousebutton while being inside a box is going to move it:
dim squares[5000,5] creation = false current = -1
sub resetsquares() for i = 0 to 4999 if squares[i,0] = 0 then for j = 1 to 4 squares[i,j] = -1 next end if next end sub
sub drawsquare() for i = 0 to 4999 if squares[i,0] = 1 then SetColor( RGB ( 255, 255, 255 ) ) box(squares[i,1],squares[i,2],squares[i,3],squares[i,4]) elseif squares[i,0] = 2 then SetColor(RGB(255,50,50)) box(squares[i,1],squares[i,2],squares[i,3],squares[i,4]) end if next end sub
sub marksquare(mx,my,mb) if mb then for i = 0 to 4999 if squares[i,0] = 1 then if mx >= squares[i,1] and mx <= squares[i,3] and my >= squares[i,2] and my <= squares[i,4] then squares[i,0] = 2 end if elseif squares[i,0] = 2 and ( mx <= squares[i,1] or mx >= squares[i,3] or my <= squares[i,2] or my > squares[i,4]) then squares[i,0] = 1 end if next end if end sub
sub createsquare(mx,my, byref acurrent) for i = 0 to 4999 if squares[i,0] = 0 then if not creation then squares[i,0] = 1 acurrent = i squares[i,1] = mx squares[i,2] = my squares[i,3] = squares[i,1] squares[i,4] = squares[i,2] creation = true end if end if next end sub
sub dragsquare(mx,my, acurrent) if squares[current,1] > -1 then if creation then squares[acurrent,3] = mx squares[acurrent,4] = my end if end if end sub
WindowOpen ( 0, "TEST WINDOW", 100, 100, 640, 480, 0 ) CanvasOpen ( 0, 640, 480, 0, 0, 640, 480, 0 )
dim mx dim my dim mb1 dim mb2 dim mb3 while true clearcanvas getmouse(mx,my,mb1,mb2,mb3) resetsquares() if not key(K_LSHIFT) then marksquare(mx,my,mb1) creation = false else if mb1 and not creation then createsquare(mx,my,current) elseif creation then dragsquare(mx,my,current) end if if not mb1 then print("Ending drawing") creation = false current = -1 end if end if drawsquare() update() wend
|
|
|
Post by tbird on Aug 15, 2019 7:32:37 GMT -6
I finally got to try this, neat, it works except the dragging of the square wasn't working for me. I haven't investigated as I am assuming I am doing something wrong lol.
|
|
|
Post by johnno56 on Aug 15, 2019 7:41:58 GMT -6
Cool... Nicely done... Does it come in blue? lol
|
|
|
Post by eyfenna on Aug 17, 2019 3:33:03 GMT -6
I did some cleaning of the above code following a specific coding style guidline I am experimenting with. Basically the above functionallity with an added subroutine. Added functionality: when any square is highligthed red, pressing delete leads to this square "disappearing"
dim squares[5000,5] squarecreation = false squarecurrent = -1
sub SquareResetSquares() for i = 0 to 4999 if squares[i,0] = 0 then for j = 1 to 4 squares[i,j] = -1 next end if next end sub
sub SquareDraw() for i = 0 to 4999 if squares[i,0] = 1 then SetColor( RGB ( 255, 255, 255 ) ) box(squares[i,1],squares[i,2],squares[i,3],squares[i,4]) elseif squares[i,0] = 2 then SetColor(RGB(255,50,50)) box(squares[i,1],squares[i,2],squares[i,3],squares[i,4]) end if next end sub
sub SquareHighlight(mx,my,mb) if mb then for i = 0 to 4999 if squares[i,0] = 1 then if mx >= squares[i,1] and mx <= squares[i,3] and my >= squares[i,2] and my <= squares[i,4] then squares[i,0] = 2 end if elseif squares[i,0] = 2 and ( mx <= squares[i,1] or mx >= squares[i,3] or my <= squares[i,2] or my > squares[i,4]) then squares[i,0] = 1 end if next end if end sub
sub SquareCreate(mx,my) for i = 0 to 4999 if squares[i,0] = 0 then if not squarecreation then squares[i,0] = 1 squarecurrent = i squares[i,1] = mx squares[i,2] = my squares[i,3] = squares[i,1] squares[i,4] = squares[i,2] squarecreation = true end if end if next end sub
sub SquareDrag(mx,my) if squares[squarecurrent,1] > -1 then if squarecreation then squares[squarecurrent,3] = mx squares[squarecurrent,4] = my end if end if end sub
sub SquareResetTrigger() squarecurrent = -1 squarecreation = false end sub
sub SquareDelete() for i = 0 to 4999 if squares[i,0] = 2 then for j = 0 to 4 squares[i,j] = 0 next end if next end sub
WindowOpen ( 0, "TEST WINDOW", 100, 100, 640, 480, 0 ) CanvasOpen ( 0, 640, 480, 0, 0, 640, 480, 0 )
dim mx dim my dim mb1 dim mb2 dim mb3 while true clearcanvas SquareResetSquares() getmouse(mx,my,mb1,mb2,mb3) if not key(K_LSHIFT) then SquareHighlight(mx,my,mb1) SquareResetTrigger() else SquareCreate(mx,my) SquareDrag(mx,my) end if if key(K_DELETE) then SquareDelete() end if SquareDraw() update() wend
|
|