Path: utzoo!attcan!uunet!cs.utexas.edu!uwm.edu!psuvax1!xavier!jackiw From: jackiw@cs.swarthmore.edu (Nick Jackiw) Newsgroups: comp.sys.mac.programmer Subject: Re: Dragging Bitmaps? Message-ID: Date: 5 Jul 90 17:11:31 GMT References: <3398@husc6.harvard.edu> Sender: news@xavier.swarthmore.edu (Usenet News) Reply-To: jackiw@cs.swarthmore.edu (Nick Jackiw) Organization: Visual Geometry Project, Swarthmore College, PA Lines: 65 siegel@endor.harvard.edu (Rich Siegel) writes: > > Does anyone have any suggestions on how to drag small bitmaps around, in the > same fashion as MacPaint? Currently I drag outlines around, but it would be > much nicer to move the object as a whole. > > R. Sure: you'll need to use some offscreen bitmaps though. The major design decisions that affect the process are: (1) do you want it to be really smooth? (2) must your bitmaps be of eccentric shape or can they be limited to rects? (1) For rectangles The Cheap Way: Get two offscreen bitmaps the size of the one to drag, and fill them both with the bitimages. Call these SOURCE and BUFFER. Now watch the mouse. Every time it moves, calculate the new location (presumably you measure the offset into the bitmap at which the mouse went down, and then calculate the new location based on this offset and the vector of mouse movement, so the mouse "sticks" to the same part of the rectangle). Copy BUFFER to the old location, copy the rect of "underlaid" bits at the new location to BUFFER, and then copy SOURCE to the new location. Repeat until mouseup. The problem with this is that you'll have flicker--you'll be overwriting background bits with source bits and source bits with background bits. The more expensive way: Get an offscreen copy of the area in which you wish to permit dragging, without the dragable image in it. Call this BACKBITS. Now get an offscreen copy of the draggable image, call this SOURCE. Now get a third bitmap, the size of BACKBITS, left blank; call this SCRATCH. For each new mouse move, copy the UnionRect of the oldLocation and the newLocation from BackBits to Scratch, and then throw in (to Scratch) a copy of the SOURCE at new location. Copy this unionRect to the screen. (2) For irregularly-shaped bitmaps You know about BitMap2Rgn, I presume. It's on the DTS cds in object code, or you can count on it being available if you'll only run on 32Bit QD machines. (There may be licensing hassles concerning the object code; read the docs or write your own.) With BitMap2Rgn, get a region that includes/excludes the appropriate parts of your SOURCE. On a blank bitmap the size of SOURCE, do a FillRgn(bitsRgn,black). This gives you a mask, which you should use in either of the above schemes: wherever you formerly did CopyBits(source,somePlace...) now do a CopyMask(source, somePlace,maskBits,...). If you want source code, I can whip something up. There are slightly more efficient ways to do it than the above, but that's the basic scheme as I understand it. Call this BACKBITS. -- +-------------------+-jackiw@cs.swarthmore.edu / !rutgers!bpa!swatsun!jackiw-+ | nicholas jackiw | jackiw%campus.swarthmore.edu@swarthmr.bitnet | +-------------------+-VGP/MathDept/Swarthmore College, Swarthmore, PA 19081--+ "Ah...I've got this CHRONIC pain." _True Believer_