home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Pascal / Snippets / SplatMaster / BalloonMacro.p < prev    next >
Encoding:
Text File  |  1992-03-09  |  8.2 KB  |  280 lines  |  [TEXT/PJMM]

  1. =================================================== }
  2. {Converts any angle to its positive equivalent angle : 0<=angle<360;}
  3. {this function takes an angle (can be negative or positive and may}
  4. {be greater than 360) and returns a positive angle such that any}
  5. {negative angles are reevaluated into positive ones (e.g.,-15 degrees}
  6. {gets returned as +345 degrees and any angles over 360 are }
  7. {factored down, (e.g., 395 degrees becomes 35 degrees).}
  8.     function MakeAngles (theAngle: real): real;
  9.         var
  10.             angleOffset: real;
  11.     begin
  12.         if theAngle < 0 then
  13.             angleOffset := (round(theAngle / 360.0) - 1) * 360
  14.         else
  15.             angleOffset := round(theAngle / 360.0) * 360;
  16.         MakeAngles := (theAngle - angleOffset);
  17.     end;
  18.  
  19.  
  20. {======================================================================================= }
  21.     procedure SetUpETs (MacroDLOGPtr: dialogPtr);
  22.         var
  23.             aStr: str255;
  24.             tipe, anInt, index: integer;
  25.             hdl: handle;
  26.             theRect: rect;
  27.     begin
  28.  
  29.         GetDItem(MacroDLOGPtr, rad1_Start_ET, tipe, hdl, theRect);         {get the handle}
  30.         aStr := Int2String(tempBalloon.rad1Start);
  31.         SetIText(hdl, aStr);
  32.  
  33.         GetDItem(MacroDLOGPtr, rad1_End_ET, tipe, hdl, theRect);         {get the handle}
  34.         aStr := Int2String(tempBalloon.rad1End);
  35.         SetIText(hdl, aStr);
  36.  
  37.         GetDItem(MacroDLOGPtr, rad2_Start_ET, tipe, hdl, theRect);         {get the handle}
  38.         aStr := Int2String(tempBalloon.rad2Start);
  39.         SetIText(hdl, aStr);
  40.  
  41.         GetDItem(MacroDLOGPtr, rad2_End_ET, tipe, hdl, theRect);         {get the handle}
  42.         aStr := Int2String(tempBalloon.rad2End);
  43.         SetIText(hdl, aStr);
  44.  
  45.         GetDItem(MacroDLOGPtr, theta_ET, tipe, hdl, theRect);         {get the handle}
  46.         aStr := Real2String(tempBalloon.thetaEndDegrees);
  47.         SetIText(hdl, aStr);
  48.  
  49.         GetDItem(MacroDLOGPtr, iterations_ET, tipe, hdl, theRect);         {get the handle}
  50.         aStr := Int2String(tempBalloon.iterations);
  51.         SetIText(hdl, aStr);
  52.  
  53.         GetDItem(MacroDLOGPtr, divs_ET, tipe, hdl, theRect);         {get the handle}
  54.         aStr := Int2String(tempBalloon.divisions);
  55.         SetIText(hdl, aStr);
  56.  
  57.  
  58.     end;{SetUpETs}
  59.  
  60. {======================================================================================= }
  61. {Get text and numbers out of  the ETs, error check them, return TRUE if they were}
  62. {okay, FALSE if there was an error}
  63.     function GetETs (MacroDLOGPtr: dialogPtr): boolean;
  64.         var
  65.             aStr: str255;
  66.             tipe, anInt, index: integer;
  67.             hdl: handle;
  68.             theRect: rect;
  69.             okay: boolean;
  70.             aReal: real;
  71.         function GetStr (theItem: integer): str255;
  72.             var
  73.                 aStr2: str255;
  74.                 anInt: integer;
  75.         begin
  76.             GetDItem(MacroDLOGPtr, theItem, tipe, hdl, theRect);         {get the handle}
  77.             GetIText(hdl, aStr2);
  78.             GetStr := (aStr2);
  79.         end;
  80.     begin
  81.         GetETs := FALSE;
  82.  
  83.         aStr := GetStr(rad1_Start_ET);
  84.         anInt := String2Int(aStr);
  85.         tempBalloon.rad1Start := abs(anInt);
  86.  
  87.         aStr := GetStr(rad1_End_ET);
  88.         anInt := String2Int(aStr);
  89.         tempBalloon.rad1End := abs(anInt);
  90.  
  91.         aStr := GetStr(rad2_Start_ET);
  92.         anInt := String2Int(aStr);
  93.         tempBalloon.rad2Start := abs(anInt);
  94.  
  95.         aStr := GetStr(rad2_End_ET);
  96.         anInt := String2Int(aStr);
  97.         tempBalloon.rad2End := abs(anInt);
  98.  
  99.         aStr := GetStr(theta_ET);
  100.         aReal := String2Real(aStr);
  101.         tempBalloon.thetaEndDegrees := MakeAngles(aReal);
  102.  
  103.         aStr := GetStr(iterations_ET);
  104.         anInt := String2Int(aStr);
  105.         tempBalloon.iterations := abs(anInt);
  106.  
  107.         aStr := GetStr(divs_ET);
  108.         anInt := String2Int(aStr);
  109.         tempBalloon.divisions := abs(anInt);
  110.  
  111.  
  112.         GetETs := TRUE;
  113.     end;{GetETs}
  114.  
  115.  
  116. {======================================================================================= }
  117.     function MacroFilter (theDialog: DialogPtr; var theEvent: EventRecord; var itemHit: integer): boolean;
  118.         label
  119.             10;
  120.         var
  121.             tempRect: Rect;
  122.             aHandle: handle;
  123.             tipe, i: integer;
  124.             myPt: point;
  125.             KeyCh: Char;
  126.     begin
  127.         MacroFilter := FALSE;
  128.         ItemHit := 0;
  129.  
  130.         GetMouse(myPt);
  131. {Let's see if we're over any of the ET rects}
  132.         for i := rad1_Start_ET to iterations_ET do
  133.             begin
  134.                 GetDItem(theDialog, i, tipe, aHandle, tempRect);         {get the handle}
  135.                 if PtInRect(myPt, tempRect) and (tipe = EditText) then
  136.                     begin
  137.                         SetCursor(gIbeam^^);
  138.                         goto 10;
  139.                     end;{if PtInRect}
  140.             end;{for i}
  141.         InitCursor;                                    {make cursor Arrow if not over any ET rect}
  142. 10:
  143.         case theEvent.what of
  144.             keydown, autoKey: 
  145.                 begin    { trap key down events }
  146.                     KeyCh := Chr(BitAnd(theEvent.message, charCodeMask));
  147.                     if BitAnd(theEvent.modIFiers, CmdKey) <> 0 then    {Cmd key is down}
  148.                         begin
  149.                             ItemHit := -1;    {so the 'v','c', or 'x' won't be 'typed' into text box}
  150.                             if theEvent.what <> AutoKey then    {we don't want to auto-key cmd-key equivs}
  151.                                 case KeyCh of
  152.                                     'x', 'X': 
  153.                                         DlgCut(theDialog);
  154.                                     'c', 'C': 
  155.                                         DlgCopy(theDialog);
  156.                                     'v', 'V': 
  157.                                         DlgPaste(theDialog);
  158.                                     '.': 
  159.                                         begin
  160.                                             itemHit := 2;
  161.                                             ClickButton(theDialog, Cancel);
  162.                                         end;
  163.                                     otherwise
  164.                                         SysBeep(1);    {not a valid Cmd-Key}
  165.                                 end;    {case KeyCh of}
  166.                         end    {if Cmd-key is down}
  167.                     else
  168.                         begin
  169.                             ItemHit := 0;
  170.                             if KeyCh in [char(k_CR), char(k_Enter)] then
  171.                                 ItemHit := Okay;                                { Button equivalents }
  172.                         end;
  173.                 end;{keydown, autokey}
  174.             otherwise
  175.                 ;
  176.         end;{case theEvent.what of}
  177.  
  178.         if ItemHit <> 0 then
  179.             MacroFilter := True;           { don't pass back to modal }
  180.  
  181.         if ItemHit = Okay then       { Simulate clicking of the OK button }
  182.             ClickButton(theDialog, Okay);
  183.  
  184.     end;
  185.  
  186. {======================================================================================= }
  187. { function MakeMacro (var aBalloon: BalloonRec): boolean;}
  188.     function MakeMacro;
  189.         var
  190.             OldPort: GrafPtr;
  191.             MacroDLOGPtr: dialogPtr;
  192.             itemHit, Tipe, anInt: integer;
  193.             aHdl: Handle;
  194.             aRect: rect;
  195.             aStr: Str255;
  196.             aReal: Real;
  197.             finished: boolean;
  198.  
  199.     begin
  200.         SetCursor(gwatch^^);
  201.         GetPort(OldPort);
  202.         tempBalloon := aBalloon;
  203.  
  204.         MacroDLOGPtr := GetNewDialog(MacroDLOG_ID, nil, Pointer(-1));
  205.  
  206.         CenterWindow(MacroDLOGPtr);
  207.         SetPort(MacroDLOGPtr);
  208.         SetUpETs(MacroDLOGPtr);
  209.  
  210.         InitCursor;
  211.         ShowWindow(MacroDLOGPtr);
  212.         DrawDefaultBtn(Okay, MacroDLOGPtr);    {Outline Default Button}
  213.         SelIText(MacroDLOGPtr, rad1_Start_ET, 0, 32767);    {pre-doubleclick the item}
  214.  
  215.         finished := FALSE;
  216.  
  217.         repeat
  218.             begin
  219.                 ModalDialog(@MacroFilter, itemHit);{Wait until an item is hit}
  220.                 case itemHit of
  221.                     Okay: 
  222.                         begin
  223.                             finished := GetETs(MacroDLOGPtr);{GetETs; if the ETs were okay then we 're done}
  224.                             if finished then
  225.                                 begin
  226.                                     aBalloon := tempBalloon;
  227.                                 end;
  228.                             MakeMacro := TRUE;
  229.                         end;
  230.                     Cancel: 
  231.                         begin
  232.                             finished := TRUE;
  233.                             MakeMacro := FALSE;
  234.                         end;
  235.                     otherwise
  236.                         ;
  237.                 end{case}
  238.             end;{begin}
  239.         until finished;
  240.  
  241.  
  242.         DisposDialog(MacroDLOGPtr);{Flush the dialog out of memory}
  243.         setPort(oldPort);
  244.     end;    {MakeMacro}
  245.  
  246.  
  247. {======================================================================================= }
  248. {    procedure DoMultiSplat (var aBalloon:BalloonRec);}
  249.     procedure DoMultiSplat;
  250.         var
  251.             curStep: integer;
  252.             rad1Diff, rad2Diff, curTheta, thetaStep: real;
  253.     begin
  254.         tempBalloon := aBalloon;
  255.         curTheta := 0.0;
  256.         if tempBalloon.thetaEndDegrees <> 0.0 then
  257.             thetaStep := (twoPi * (tempBalloon.thetaEndDegrees / 360.0)) / tempBalloon.divisions
  258.         else
  259.             thetaStep := 0.0;
  260.  
  261.         with tempBalloon do
  262.             begin
  263.                 rad1Diff := (rad1End - rad1Start) / iterations;
  264.                 rad2Diff := (rad2End - rad2Start) / iterations;
  265.             end;
  266.  
  267.         for curStep := 1 to tempBalloon.iterations do
  268.             begin
  269.                 tempBalloon.innerRadius := round(tempBalloon.innerRadius + rad1Diff);
  270.                 tempBalloon.outerRadius := round(tempBalloon.outerRadius + rad2Diff);
  271.                 DrawBalloon(tempBalloon);
  272.                 curTheta := curTheta + thetaStep;
  273.                 tempBalloon.theta := curTheta;
  274.             end;
  275.         aBalloon.numPtsSoFar := tempBalloon.numPtsSoFar;
  276.         aBalloon.numSplats := aBalloon.numSplats + tempBalloon.iterations;
  277.  
  278.     end;    {DoMultiSplat}
  279.  
  280. end.