<> <> <> <> <<>> DIRECTORY BasicTime, CubicSplines, FS, GGAlign, GGButtons, GGCaret, GGDescribe, GGDrawProcess, GGError, GGEvent, GGGraphicsButton, GGGravity, GGInterfaceTypes, GGModelTypes, GGObjects, GGRefresh, GGSegment, GGSelect, GGTouch, GGVector, GGViewerOps, GGWindow, Imager, ImagerColor, ImagerInterpress, IO, Random, Real, Rope, Rosary, TiogaButtons, ViewerClasses, ViewerIO, ViewerOps, ViewerTools; GGEventImpl: CEDAR PROGRAM IMPORTS BasicTime, FS, GGAlign, GGButtons, GGCaret, GGDescribe, GGDrawProcess, GGError, GGGraphicsButton, GGGravity, GGObjects, GGRefresh, GGSegment, GGSelect, GGTouch, GGVector, GGViewerOps, GGWindow, Imager, ImagerColor, ImagerInterpress, IO, Random, Real, Rope, Rosary, TiogaButtons, ViewerIO, ViewerOps, ViewerTools EXPORTS GGEvent = BEGIN Cluster: TYPE = GGModelTypes.Cluster; EntityGenerator: TYPE = GGModelTypes.EntityGenerator; GargoyleData: TYPE = GGInterfaceTypes.GargoyleData; Outline: TYPE = GGModelTypes.Outline; Point: TYPE = GGModelTypes.Point; ScalarButtonClient: TYPE = GGInterfaceTypes.ScalarButtonClient; Segment: TYPE = GGModelTypes.Segment; Sequence: TYPE = GGModelTypes.Sequence; Traj: TYPE = GGModelTypes.Traj; TrajGenerator: TYPE = GGModelTypes.TrajGenerator; TwoState: TYPE = GGInterfaceTypes.TwoState; SegmentGenerator: TYPE = GGModelTypes.SegmentGenerator; SequenceGenerator: TYPE = GGModelTypes.SequenceGenerator; Vector: TYPE = GGModelTypes.Vector; Viewer: TYPE = ViewerClasses.Viewer; <<>> <> <<>> Clear: PUBLIC PROC [event: LIST OF REF ANY, gargoyleData: GargoyleData] = {}; Reset: PUBLIC PROC [event: LIST OF REF ANY, gargoyleData: GargoyleData] = {}; Get: PUBLIC PROC [event: LIST OF REF ANY, gargoyleData: GargoyleData] = {}; Store: PUBLIC PROC [event: LIST OF REF ANY, gargoyleData: GargoyleData] = {}; Split: PUBLIC PROC [event: LIST OF REF ANY, gargoyleData: GargoyleData] = {}; Interpress: PUBLIC PROC [event: LIST OF REF ANY, gargoyleData: GargoyleData] = { ipRef: ImagerInterpress.Ref; wdir, ipName, fullName: Rope.ROPE; success: BOOL; pixelsPerMeter: REAL _ 0.0254 / 72.0; <> startTime: BasicTime.GMT; endTime: BasicTime.GMT; totalTime: INT; msgRope: Rope.ROPE; <> DoMakeInterpress: PROC [dc: Imager.Context] = { Imager.ScaleT[dc, pixelsPerMeter]; GGRefresh.InterpressEntireScene[dc, gargoyleData]; }; ipName _ ViewerTools.GetSelectionContents[]; IF Rope.Length[ipName] = 0 THEN RETURN; wdir _ gargoyleData.originalWorkingDirectory; success _ TRUE; [fullName,,] _ FS.ExpandName[ipName, wdir ! FS.Error => IF error.group = user THEN { success _ FALSE; CONTINUE; } ]; IF NOT success THEN RETURN; ipRef _ ImagerInterpress.Create[fullName]; msgRope _ IO.PutFR["Writing to interpress file: %g . . . ", [rope[fullName]]]; GGError.Append[msgRope, begin]; <> startTime _ BasicTime.Now[]; ImagerInterpress.DoPage[ipRef, DoMakeInterpress, 1.0]; ImagerInterpress.Close[ipRef]; endTime _ BasicTime.Now[]; totalTime _ BasicTime.Period[startTime, endTime]; msgRope _ IO.PutFR[" Done in time (%r)", [integer[totalTime]]]; GGError.Append[msgRope, end]; }; <<>> <> <> Delete: PUBLIC PROC [event: LIST OF REF ANY, gargoyleData: GargoyleData] = { parentOutline: Outline; entityGen: EntityGenerator; entityGen _ GGSelect.SelectedEntities[gargoyleData, normal]; FOR entity: REF ANY _ GGObjects.NextEntity[entityGen], GGObjects.NextEntity[entityGen] UNTIL entity = NIL DO GGSelect.DeselectEntity[entity, gargoyleData, normal]; GGSelect.DeselectEntity[entity, gargoyleData, copy]; GGSelect.DeselectEntity[entity, gargoyleData, hot]; GGSelect.DeselectEntity[entity, gargoyleData, active]; WITH entity SELECT FROM cluster: Cluster => ERROR NotYetImplemented; outline: Outline => { GGObjects.DeleteOutline[gargoyleData.scene, outline]; GGTouch.OutlineDeleted[outline, gargoyleData]; }; seq: Sequence => { parentOutline _ GGObjects.OutlineOfTraj[seq.traj]; IF GGObjects.HasHoles[parentOutline] THEN { SELECT seq.traj.role FROM hole => ERROR NotYetImplemented; open => ERROR; -- impossible since the outline has holes fence => { GGError.Append["You can't delete the fence trajectory of an outline with holes.", oneLiner]; LOOP; }; ENDCASE => ERROR; } ELSE { IF seq.all THEN { -- the whole trajectory is selected GGObjects.DeleteOutline[gargoyleData.scene, parentOutline]; GGTouch.OutlineDeleted[parentOutline, gargoyleData]; } ELSE { -- part of a trajectory is selected. Remove the parts in a sensible fashion. This may require breaking up the trajectory into pieces. Remove all of the pieces from touching lists. ERROR NotYetImplemented; }; }; }; ENDCASE => ERROR; ENDLOOP; GGWindow.Painter[$PaintEntireScene, gargoyleData]; }; SelectAll: PUBLIC PROC [event: LIST OF REF ANY, gargoyleData: GargoyleData] = { GGSelect.DeselectAll[gargoyleData, normal]; GGSelect.SelectAll[gargoyleData, normal]; GGWindow.Painter[$PaintEntireScene, gargoyleData]; }; NotYetImplemented: PUBLIC SIGNAL = CODE; <<>> <> SetStraight: PUBLIC PROC [event: LIST OF REF ANY, gargoyleData: GargoyleData] = { seqGen: SequenceGenerator; segmentGen: SegmentGenerator; traj: Traj; newSeg: Segment; seqGen _ GGSelect.SelectedSequences[gargoyleData, normal]; FOR seq: Sequence _ GGObjects.NextSequence[seqGen], GGObjects.NextSequence[seqGen] UNTIL seq = NIL DO segmentGen _ GGObjects.SegmentsInSequence[seq]; traj _ seq.traj; FOR next: GGObjects.SegAndIndex _ GGObjects.NextSegmentAndIndex[segmentGen], GGObjects.NextSegmentAndIndex[segmentGen] UNTIL next.seg = NIL DO newSeg _ GGSegment.MakeLine[next.seg.lo, next.seg.hi]; AddSegment[traj, newSeg, next]; ENDLOOP; ENDLOOP; GGWindow.Painter[$PaintEntireScene, gargoyleData]; }; SetArc: PUBLIC PROC [event: LIST OF REF ANY, gargoyleData: GargoyleData] = { seqGen: SequenceGenerator; segmentGen: SegmentGenerator; traj: Traj; newSeg: Segment; p0, p1, p2: Point; seqGen _ GGSelect.SelectedSequences[gargoyleData, normal]; FOR seq: Sequence _ GGObjects.NextSequence[seqGen], GGObjects.NextSequence[seqGen] UNTIL seq = NIL DO segmentGen _ GGObjects.SegmentsInSequence[seq]; traj _ seq.traj; FOR next: GGObjects.SegAndIndex _ GGObjects.NextSegmentAndIndex[segmentGen], GGObjects.NextSegmentAndIndex[segmentGen] UNTIL next.seg = NIL DO <> p0 _ next.seg.lo; p2 _ next.seg.hi; p1 _ GetPt[p0, GGVector.Sub[p2,p0], GGVector.Distance[p0,p1]]; newSeg _ GGSegment.MakeArc[p0, p1, p2]; AddSegment[traj, newSeg, next]; ENDLOOP; ENDLOOP; GGWindow.Painter[$PaintEntireScene, gargoyleData]; }; SetConic: PUBLIC PROC [event: LIST OF REF ANY, gargoyleData: GargoyleData] = { seqGen: SequenceGenerator; segmentGen: SegmentGenerator; traj: Traj; newSeg: Segment; p0, p1, p2: Point; seqGen _ GGSelect.SelectedSequences[gargoyleData, normal]; FOR seq: Sequence _ GGObjects.NextSequence[seqGen], GGObjects.NextSequence[seqGen] UNTIL seq = NIL DO segmentGen _ GGObjects.SegmentsInSequence[seq]; traj _ seq.traj; FOR next: GGObjects.SegAndIndex _ GGObjects.NextSegmentAndIndex[segmentGen], GGObjects.NextSegmentAndIndex[segmentGen] UNTIL next.seg = NIL DO p0 _ next.seg.lo; p2 _ next.seg.hi; p1 _ GetPt[p0, GGVector.Sub[p2,p0], GGVector.Distance[p0,p1]]; newSeg _ GGSegment.MakeConic[p0, p1, p2, 0.7]; AddSegment[traj, newSeg, next]; ENDLOOP; ENDLOOP; GGWindow.Painter[$PaintEntireScene, gargoyleData]; }; random: Random.RandomStream _ Random.Create[10000,0]; NewV: PROC RETURNS[REAL] = {RETURN[Random.NextInt[random]/10000.0]}; GetPt: PROC[p0, dir: Point, maxL: REAL] RETURNS [pt: Point] = { <> scale1: REAL _ NewV[]*maxL; angle: REAL _ 180*NewV[]-90; --range -90..90 pt _ GGVector.Add[GGVector.Scale[GGVector.VectorPlusAngle[dir,angle], scale1], p0]; }; GetPtSequence: PROC[p0,p1: Point, n: NAT] RETURNS [CubicSplines.KnotSequence] ={ points: CubicSplines.KnotSequence _ NEW[CubicSplines.KnotSequenceRec[n+2]]; dir: Point _ GGVector.Sub[p1,p0]; inc: REAL _ GGVector.Distance[p0,p1]/(n-2); maxL: REAL _ inc; points[0] _ p0; FOR i: NAT IN [1..points.length-1) DO points[i] _ GetPt[p0, dir, maxL]; maxL _ maxL+inc; ENDLOOP; points[points.length-1] _ p1; RETURN[points]; }; AddSegment: PROC[traj: Traj, newSeg: Segment, next: GGObjects.SegAndIndex] = { traj.segments _ Rosary.Cat[ Rosary.Substr[traj.segments, 0, next.index], Rosary.FromItem[newSeg], Rosary.Substr[traj.segments, next.index+1, GGObjects.HiSegment[traj]-next.index] ]; }; SetBezier: PUBLIC PROC [event: LIST OF REF ANY, gargoyleData: GargoyleData] = { seqGen: SequenceGenerator; segmentGen: SegmentGenerator; traj: Traj; newSeg: Segment; p0, p1, p2, p3: Point; seqGen _ GGSelect.SelectedSequences[gargoyleData, normal]; FOR seq: Sequence _ GGObjects.NextSequence[seqGen], GGObjects.NextSequence[seqGen] UNTIL seq = NIL DO segmentGen _ GGObjects.SegmentsInSequence[seq]; traj _ seq.traj; FOR next: GGObjects.SegAndIndex _ GGObjects.NextSegmentAndIndex[segmentGen], GGObjects.NextSegmentAndIndex[segmentGen] UNTIL next.seg = NIL DO <> length: REAL _ GGVector.Distance[next.seg.lo, next.seg.hi]; dir: Point _ GGVector.Sub[p3,p0]; p0 _ next.seg.lo; p1 _ GetPt[p0, dir,length*0.7]; p2 _ GetPt[p0, dir,length]; p3 _ next.seg.hi; newSeg _ GGSegment.MakeBezier[p0, p1, p2, p3]; AddSegment[traj, newSeg, next]; ENDLOOP; ENDLOOP; GGWindow.Painter[$PaintEntireScene, gargoyleData]; }; SetSpline: PUBLIC PROC [event: LIST OF REF ANY, gargoyleData: GargoyleData] = { seqGen: SequenceGenerator; segmentGen: SegmentGenerator; traj: Traj; newSeg: Segment; seqGen _ GGSelect.SelectedSequences[gargoyleData, normal]; FOR seq: Sequence _ GGObjects.NextSequence[seqGen], GGObjects.NextSequence[seqGen] UNTIL seq = NIL DO segmentGen _ GGObjects.SegmentsInSequence[seq]; traj _ seq.traj; FOR next: GGObjects.SegAndIndex _ GGObjects.NextSegmentAndIndex[segmentGen], GGObjects.NextSegmentAndIndex[segmentGen] UNTIL next.seg = NIL DO <> length: REAL _ GGVector.Distance[next.seg.lo, next.seg.hi]; type: CubicSplines.SplineType _ naturalAL; nKnots: INT _ Real.RoundLI[10*NewV[]]; cps: CubicSplines.KnotSequence _ GetPtSequence[next.seg.lo, next.seg.hi, nKnots]; newSeg _ GGSegment.MakeCubicSpline[cps, type]; AddSegment[traj, newSeg, next]; ENDLOOP; ENDLOOP; GGWindow.Painter[$PaintEntireScene, gargoyleData]; }; <<>> <> Close: PUBLIC PROC [event: LIST OF REF ANY, gargoyleData: GargoyleData] = { traj: Traj; firstPoint, lastPoint: Point; seg: Segment; [traj,----,----,----,----] _ GGCaret.GetChair[gargoyleData.caret]; IF traj = NIL THEN { GGError.Append["There is no caret trajectory to close.", oneLiner]; GGError.Blink[]; RETURN; }; GGError.Append["The caret trajectory will be closed.", oneLiner]; firstPoint _ GGObjects.FetchJointPos[traj, 0]; lastPoint _ GGObjects.LastJointPos[traj]; seg _ GGSegment.MakeLine[lastPoint, firstPoint]; GGObjects.CloseWithSegment[traj, seg, lo]; GGObjects.SetFillColor[GGObjects.OutlineOfTraj[traj], Imager.MakeGray[0.5]]; GGWindow.Painter[$PaintEntireScene, gargoyleData]; }; <<>> <> TestGravity: PUBLIC PROC [event: LIST OF REF ANY, gargoyleData: GargoyleData] = { <> xRandomStream, yRandomStream: Random.RandomStream; testPoint: Point; x, y: INT; totalCount: NAT _ 0; s: IO.STREAM; resultPoint: Point; feature: REF ANY; countRope: Rope.ROPE; environ: GGGravity.ObjectBag; xRandomStream _ Random.Create[gargoyleData.actionArea.cw]; yRandomStream _ Random.Create[gargoyleData.actionArea.ch]; environ _ GGGravity.CreateObjectBag[]; GGAlign.AddItemsForAction[gargoyleData, environ, $SelectPoint]; IF environ = NIL THEN { GGError.Append["Add some trajectories to test gravity.", oneLiner]; GGError.Blink[]; RETURN; }; gargoyleData.hitTest.environ _ environ; GGWindow.Painter[$PaintAlign, gargoyleData]; gargoyleData.hitTest.hitCount _ 0; UNTIL totalCount > 1000 DO IF gargoyleData.aborted THEN { gargoyleData.aborted _ FALSE; EXIT; }; x _ Random.NextInt[xRandomStream]; y _ Random.NextInt[yRandomStream]; testPoint _ [x, y]; testPoint _ GGWindow.ScreenToWorld[testPoint, gargoyleData.camera]; gargoyleData.refresh.spotPoint _ testPoint; [resultPoint, feature] _ GGGravity.Map[testPoint, gargoyleData.hitTest.tolerance, environ, gargoyleData]; IF feature # NIL THEN { gargoyleData.refresh.hitPoint _ resultPoint; GGWindow.Painter[$PaintHitLine, gargoyleData]; gargoyleData.hitTest.hitCount _ gargoyleData.hitTest.hitCount + 1; } ELSE { GGWindow.Painter[$PaintSpot, gargoyleData]; }; totalCount _ totalCount + 1; ENDLOOP; s _ IO.ROS[]; s.PutF["Tested %g total points. %g were hits", [integer[totalCount]], [integer[gargoyleData.hitTest.hitCount]]]; countRope _ IO.RopeFromROS[s]; GGError.Append[countRope, oneLiner]; }; DrawTouchPoints: PUBLIC PROC [event: LIST OF REF ANY, gargoyleData: GargoyleData] = { GGWindow.Painter[$PaintTouchPoints, gargoyleData]; }; DescribeTouchPoints: PUBLIC PROC [event: LIST OF REF ANY, gargoyleData: GargoyleData] = { GGTouch.DescribeAllTouchPoints[gargoyleData]; }; DrawBoundBoxes: PUBLIC PROC [event: LIST OF REF ANY, gargoyleData: GargoyleData] = { GGWindow.Painter[$PaintBoundBoxes, gargoyleData]; }; DrawSelectionBox: PUBLIC PROC [event: LIST OF REF ANY, gargoyleData: GargoyleData] = { GGWindow.Painter[$PaintSelectionBox, gargoyleData]; }; Typescript: PUBLIC PROC [event: LIST OF REF ANY, gargoyleData: GargoyleData] = { newViewer: Viewer; inHandle, outHandle: IO.STREAM; [inHandle, outHandle, newViewer] _ MakeNewViewerAndHandle[]; GGError.SetErrorStream[outHandle]; gargoyleData.debug.typescript _ newViewer; }; MakeNewViewerAndHandle: PRIVATE PROC [] RETURNS [in, out: IO.STREAM, newViewer: Viewer] = { newViewer _ ViewerOps.CreateViewer[ flavor: $TypeScript, info: [ name: "Gargoyle Script", menu: NIL, data: NIL, iconic: TRUE, column: right, scrollable: TRUE, icon: unInit ], paint: FALSE]; ViewerOps.SetOpenHeight[newViewer, 120]; ViewerOps.OpenIcon[icon: newViewer, closeOthers: FALSE, bottom: TRUE, paint: TRUE]; [in, out] _ ViewerIO.CreateViewerStreams["gargoyle script", newViewer, NIL, TRUE]; }; SlackLog: PUBLIC PROC [event: LIST OF REF ANY, gargoyleData: GargoyleData] = { GGDrawProcess.OutputLog[]; }; DescribeCaretObject: PUBLIC PROC [event: LIST OF REF ANY, gargoyleData: GargoyleData] = { chair: Traj; description, msgRope: Rope.ROPE; isJoint: BOOL; jointNum: NAT; seg: Segment; [chair, isJoint, ----, jointNum, seg] _ GGCaret.GetChair[gargoyleData.caret]; IF isJoint THEN { description _ GGDescribe.DescribeJoint[chair, jointNum]; } ELSE { segNum: INT; segNum _ GGObjects.IndexOfSegment[seg, chair]; IF segNum = -1 THEN description _ Rope.Concat["Nonexistent segment of ", GGDescribe.DescribeTraj[chair]] ELSE description _ GGDescribe.DescribeSegment[chair, segNum]; }; msgRope _ IO.PutFR["Caret is on %g.", [rope[description]]]; GGError.Append[msgRope, oneLiner]; }; <<>> <