Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!att!tut.cis.ohio-state.edu!zaphod.mps.ohio-state.edu!rpi!uupsi!sunic!news.funet.fi!funic!santra!hila.hut.fi!jmunkki From: jmunkki@hila.hut.fi (Juri Munkki) Newsgroups: comp.sys.mac.programmer Subject: Re: An itsy bitsy question... Message-ID: <1991Jan8.043323.954@santra.uucp> Date: 8 Jan 91 04:33:23 GMT References: <27725.2787e3d0@kuhub.cc.ukans.edu> Sender: news@santra.uucp (Cnews - USENET news system) Reply-To: jmunkki@hila.hut.fi (Juri Munkki) Organization: Helsinki University of Technology, FINLAND Lines: 99 In article <27725.2787e3d0@kuhub.cc.ukans.edu> mlab2@kuhub.cc.ukans.edu writes: >In article , aberno@questor.wimsey.bc.ca (Anthony Berno) writes: >> How is the "rubberbanding" done in programs like Superpaint when you are >> drawing a line on the screen? There isn't an obvious solution in Inside >> Macintosh, and any solutions I have come up with are hideously difficult. >> There must be a relatively simple way of doing it. Does anyone out there >> know? >> >procedure RubberBand; > var > originalPt, lastPt, newPt : Point; >begin > GetMouse(originalPoint); > PenMode(patXOr); > GetMouse(lastPt); > MoveTo(originalPt.h, originalPt.v); > LineTo(lastPt.h, lastPt.v); > repeat > MoveTo(originalPt.h, originalPt.v); > LineTo(lastPt.h, lastPt); > GetMouse(newPt); > MoveTo(originalPt.h,originalPt.v); > LineTo(newPt.h, newPt.v); > lastPt:=newPt; > until (not Button); > PenNormal; >end; Don't do this. I can point out several faults in the above code. First of all, it creates a quickly flickering line that can be invisible to the user, if the timing happens to be right. Using exclusive or mode should be ok in non-paint programs, but inverting a line when the user is trying to paint a line is not good. Using an offscreen bitmap to restore the background will look much nicer. Superpaint certainly doesn't use XOR mode. Most importantly, even loops like this one should use GetNextEvent. GetNextEvent will tell you the mouse position as well as the mouse button state. The nice thing about it is that it can keep track of button events better than your loop. Your loop fails, if the user rapidly releases and presses the mouse button. If you have more than just two lines drawn in the loop, it's quite probable that this will happen. This also means that you never find out where the mouse button really came up. Moving the mouse quickly while releasing the button will get you this result. I also discovered that the GetMouse/Button approach doesn't really work with QuicKeys (or at least the version QuicKeys that I have). Here's what I'm using in Dizzy (a digital circuit simulator that compiles for Think C and X/Motif on unix machines. It will be soon released as free software WITH SOURCE CODE.): /* >> Gets the mouse position while in a tracking loop. >> Returns true when the mouse button comes up. */ int GetMouseTrackEvent(pt) Point *pt; { int r=0; r=GetNextEvent(mUpMask+mDownMask,&MyEvent); *pt=MyEvent.where; GlobalToLocal(pt); if(r) r=(MyEvent.what==mouseUp); return !r; } You also have to check if the mouse moved, if you want to avoid flicker. Of course this is easy to do with just an extra variable: do /* Loop until button is released. */ { downflag = GetMouseTrackEvent(&MousePoint); if(MousePoint.h!=OldSpot.h || MousePoint.v !=OldSpot.v) { /* Ok, mouse moved, do the rubberbanding... blaa blaa... OldSpot = MousePoint; } } while(downflag); I use XOR mode in dizzy, but that's because it has rubberbanding operations that do not need to be wysiwyg. The interesting thing about loops like the one above is that they will work with X toolkit just as well, if you rewrite GetMouseTrackEvent. As a bonus, this kind of loop allows for multifinder background operations. If you have terminal program in the background, it will still be able to run while you rubberband. If you want absolute multifinder compatibility, you just rewrite GetMouseTrackEvent to use WaitNextEvent. (Sorry, I don't have a version like that... The only multifinder documents I have are in some issue of Byte. I wonder why Apple doesn't make stuff like this available in bookstores or at least technotes.) ____________________________________________________________________________ / Juri Munkki / Helsinki University of Technology / Wind / Project / / jmunkki@hut.fi / Computing Center Macintosh Support / Surf / STORM / ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~