• Aucun résultat trouvé

frame drawn around the scrolling area has a small hole at the lower left corner

Dans le document in utilities, in (Page 77-84)

The hole appears when SCROLL first executes. The size of the hole is related to the number of pixels skipped by the scroll-x parameter. We're not sure why this hole appears, but it seems to be unavoidable. Be aware of this if your pro gram must display important information near the scrolling area.

Program 3-2. TinySketch

"Graphics.bmap", included on the Amiga BASIC disk in the BasicDemos directory must be present in the current disk directory when TinySketch is run.

'Tinysketch*

•*

'Integer variables are faster-*

DEFINT A-z*

4,

•initialize custom screen*

GOSUB Init.Screen*

PRINT "I'll be with you in a moment...11*

*

'Initialize custom menus*

GOSUB Define.Menus*

*

'Borrow SetDrMd from ROM Kernel*

'* "graphics.bmap" must be in same*

'folder as this program*

*

LIBRARY "graphics.library"*

*

'Default drawing color*

CurrColor=l*

*

•Convenient constants*

TRUE=-1*

FALSE=0*

*

'ROM Kernel SetDrMd can be*

'used for special graphics*

•effects not supported by BASIC*

•inverse uses the Complement*

•drawing mode, handy for*

1 "rubber-banding."*

*

SUB Inverse STATIC*

CALL SetDrMd&(WINDOW(8),3)*

END SUB*

*

•Subprogram to reset drawing mode*

'to REPLACE, the normal setting*

*

SUB Normal STATIC*

CALL SetDrMd&(WIND0W(8)#l)*

END SUB*

A

CLS:BEEP 'The beep shows we're readyI*

A

'Endless loop to check for mouse*

'button and menu status*

A

MainLoop:*

WHILE TRUE 'i.e. forever*

Menuld=0*

WHILE MOUSE(0)=0 AND Menuld=0*

MenuId=MENU(0)*

WEND*

IF Menuld<>0 THEN GOSUB MenuHandler:GOTO MainLoop*

sx=MOUSE(1):sy=MOUSE(2)*

GOSUB MouseHandler*

WEND*

*

'The left mouse button has been*

•pressed. Execute appropriate*

'tool.*

A

MouseHandler:*

'*

'Since we don't have any way to*

'change colors, we'll just use*

'random colors*

A

CurrColor=INT(31*RND+l)*

U

'Toolnum goes from 1-4*

ON ToolNum GOSUB Sketch,DrwLine,Rectangle,Oval*

RETURN*

*

'Freehand drawing mode*

*

Sketch:*

'Set initial point*

PSET(sx,sy),CurrColor*

A

'if and while the button is*

'still held down, connect the*

'successive positions of the mouse*

*

WHILE MOUSE(0)<>0*

LINE -(MOUSE(1),MOUSE(2)),CurrColor*

WEND*

RETURN*

A

'Preview line until mouse button*

'is released, then set down*

'the line.*

*

DrwLine:*

'First point is actually the "ending point"*

•point of the line*

ex=sx:ey=sy*

*As long as the mouse button is held down.,.*

WHILE MOUSE(0)<>0*

•Draw ghost line*

Inverse*

LINE (sx,sy)-(ex,ey)*

'we save this position*

cx=MOUSE(1):cy=MOUSE(2)*

•and erase the ghost line*

LINE (sx,sy)-(ex,cy)*

•set up for next ghost line*

ex=cx:ey=cy*

WEND*

•Back to normal drawing mode*

Normal*

•Set down the line at the last*

•position ghosted*

LINE (sx,sy)-(ex,ey),CurrColor*

RETURN*

'Structurally, think of Oval*

'as a substitution of a*

'"Circloid" command for the*

'LINE command in the DrwLine*

'routine above*

•Circloid computes the shape*

'of the oval, simplifying*

'this loop considerably*

*

Oval:*

ex=sx:ey=sy*

WHILE MOUSE(0)<>0*

Inverse*

CALL Circloid(ex,ey)*

cx=MOUSE(1):cy=MOUSE(2)*

CALL Circloid(cx,cy)*

ex=cx:ey=cy*

WEND*

Normal*

CALL Circloid(ex, ey)*

RETURN*

A

SUB Circloid(x,y) STATIC*

SHARED sx,sy,CurrColor,xr,yr*

xr=ABS(sx-x)+1:yr=ABS(sy-y)+1:ratio#=xr/yr*

IF xr>yr THEN radius=xr ELSE radius=yr:ratio#=yr/xr*

CIRCLE (sx+xr/2,sy+yr/2), radius,CurrColor,,,ratio#*.44*

END SUB*

*

'A general-purpose rectangle*

'previewing routine.*

'As long as the mouse is held down,*

'it previews a "rubber rectangle11*

'and when the button is released,*

'it exits with the coordinates*

'(sx,sy)-(ex,ey)*

*

RubberBox:*

ex=sx:ey=sy*

WHILE MOUSE(0)<>0*

Inverse*

LINE (sx,sy)-(ex,ey),,b*

cx=MOUSE(1):cy=MOUSE(2)*

LINE (sx,sy)-(cx,cy),,b*

ex=cx:ey=cy*

WEND*

LINE (sx,sy)-(ex,ey),,b*

Normal*

RETURN*

*

'See how easy this is?*

'This small routine implements*

'the rectangle command:*

*

Rectangle:*

GOSUB RubberBox*

LINE (sx,sy)-(ex,ey),CurrColor,b*

RETURN*

*

'Process the selected menu item*

'Menuld holds which menu was*

'selected, and Menultem holds*

'the selection within the menu.*

■*

MenuHandler:*

Menultem=MENU(1)*

'Branch to appropriate menu routine*

ON Menuld GOSUB Project,Tools*

RETURN*

*

'The Project Menu handler*

'Depending on Menultem (1 or 2)*

'we'll branch to New Picture or Quit*

*

Project:*

ON Menultem GOSUB NewPict,Quit*

RETURN*

*

'Pretty simple, eh?*

NewPict:*

CLS*

RETURN*

*

•Restore BASIC'S menus (a necessity, not a feature),*

•close the screen (ditto),*

•and exit to the listing*

Quit:*

MENU RESET*

SCREEN CLOSE 1*

CLS*

•Change LIST to SYSTEM to exit*

•directly to the Workbench*

*

LIST*

END*

*

'The Tools menu is handled differently*

'than Edit's. We just select*

'the tool number directly from*

'the menu item, checkmark the tool,*

'and deselect all the other tools.*

*

Tools:*

ToolNum=MenuItem*

FOR i=l TO NumToolsxMENU 2,i,l:NEXT*

MENU 2,ToolNum,2*

RETURN*

*

Init.Screen:*

•initialize custom screen and*

•window, a 32-color 320x200*

•screen,*

xsize=320:ysize=200*

SCREEN l,xsize,ysize,5,l*

WINDOW 1,"TinySketch",,16,1*

'Set background color to*

•zero, menu colors to dark cyan*

*on cyan*

PALETTE 0,0,0,0*

PALETTE 30,0,.5,.5*

PALETTE 31,.5,1,1*

CLS*

RETURN*

*

'It's pretty easy to declare your*

'menus.*

Define.Menus:*

MENU 1,0,1,"Picture"*

MENU 1,1,1,"New "*

MENU 1,2,1,"Quit "*

MENU 2,0,1,"Tools"*

MENU 2,1,2," Sketch "*

MENU 2,2,1," Line "*

MENU 2,3,1," Rectangle"*

MENU 2,4,1," Oval "*

ToolNum=l:NumTools=4*

'Disable unused menus*

MENU 3,0,0,"":MENU 4,0,0,""*

RETURN*

Program 3-3. Shapelt

'Shapelt * DEFINT a-Z*

DEF FNl(h/w,p)=INT(3+2*(h+l)*INT((w+16)/l6)*p/2)*

DIM Shape%(2000),work%(400) 'required for Intuits*

DIM xl(20),yl(20),x2(20),y2(20)*

TRUE=-l:SideBorder=9:BotBorder=19*

filename$="untitledM*

maxlen=25 'length of text fields*

GOSUB InitMenu*

SCREEN l,320,200,2,l:depth=2*

xsize=320:ysize=200*

Scrld=l 'Workbench screen=-lr else custom*

Drawmode=TRUE*

id=WINDOW(l):wx=311:wy=200-16 'adjust these*

GOSUB DoWin*

GOSUB DoDraw*

ACTIVE=TRUE*

1 4,

Main:*

WHILE ACTIVE*

WINDOW 3*

Menuld=0:Button=0:x=-l*

WHILE Button=0 AND MenuId=0*

Button=MOUSE(0)*

IF Got AND (x<>MOUSE(l) OR y<>MOUSE(2)) THEN*

WINDOW OUTPUT 3*

IF x>=0 THEN PUT(x/8,y/8),Shape%*

x=MOUSE(1):y=MOUSE(2)*

PUT(x/8,y/8)#Shape%*

END IF*

MenuId=MENU(0)*

WEND*

IF Got THEN PUT (x/8,y/8),Shape%*

WINDOW OUTPUT 1*

IF Menuld THEN GOSUB MenuHandler:GOTO Main*

sx=MOUSE(1):sy=MOUSE(2):GOSUB MouseHandler*

WEND*

■*

MouseHandler:*

WINDOW OUTPUT 1:PATTERN &HFFFF*

IF sy>=cy THEN*

LINE (Currcolor*cw,cy)-(Currcolor*cw+cw,wy),0,b*

Currcolor=INT(sx/cw):IF Currcolor>maxcolor THEN Currcolor=maxcol

or*

LINE (Currcolor*cw,cy)-(Currcolor*cw+cw,wy),1,b*

PATTERN &HAAAA*

LINE (0fcy)-(wx,cy),l*

PATTERN &HFFFF*

IF GetMode=TRUE AND MOUSE(0)=-l THEN*

GOSUB RubberBox*

WINDOW OUTPUT 3*

GET (xl,yl)-(x2,y2),Shape%:Got=TRUE*

swidth=ABS(x2-xl):sheight=ABS(y2-yl)*

MENU 1,2,1:MENU 1,3,1:MENU 1,6,1*

GetMode=0*

END IF*

ON Tool GOSUB Draw,Solid,Hollow*

RETURN*

FOR x=mxl TO mx2+8 STEP 8*

LINE (x,myl)-(x,my2+8),l*

NEXT*

FOR y=myl TO my2+8 STEP 8*

LINE (mxl,y)-(mx2+8,y),l*

FOR y=myl TO my2 STEP 8*

IF ey>cy-8 THEN ey=cy-8*

tx=INT(ex/8):ty=INT(ey/8)4 IF tx>=xl THEN x2=tx«

IF ty>=yl THEN y2=ty*

mx2=x2*8:my2=y2*8*

IF mxl>mx2 THEN SWAP mxl,mx2:SWAP xl#x2*

IF myl>my2 THEN SWAP myl,my2:SWAP yl,y2«

RETURN*

*

MenuHandler:*

MenuItem=MENU(l)*

ON Menuld GOSUB Shape,Tools,CScreen*

RETURN*

*

Shape:*

ON Menultem GOSUB Pick,Discard,ReDraw,SNew,SOpen,SSave,SQuit*

RETURN*

MENU 1,2,0:MENU 1,3,0:MENU 1,6,0*

RETURN*

Dans le document in utilities, in (Page 77-84)