-- File: SVViewerUserImplA.mesa
-- Last edited by Bier on July 7, 1983 11:28 am
-- Author: Eric Bier in October 1982
-- Contents: Code to respond to button presses made in an SVViewer

DIRECTORY
 BitMap3d,
 CastRays,
 CGDevice,
 Containers,
 CoordSys,
CSG,
 CSGGraphics,
 DisplayList3d,
 Draw3d,
 Graphics,
 GraphicsBasic,
 GraphicsColor,
IO,
 Matrix3d,
 Menus,
 MessageWindow,
 Preprocess3d,
 Real,
 Rope,
 SV2d,
 SVBoundBox,
 SVEditUser,
 SVFiles,
 SVVector3d,
 SVViewerTool,
 SVViewerTools,
 SVViewerUser,
 System,
 TFI3d,
 TFO3d,
 Time,
 ViewerClasses,
 ViewerOps,
 ViewerTools;

SVViewerUserImplA: CEDAR PROGRAM
IMPORTS BitMap3d, CastRays, CoordSys, DisplayList3d, Draw3d, Graphics, IO, Matrix3d, MessageWindow, Preprocess3d, Real, Rope, SVEditUser, SVFiles, SVViewerTools, SVViewerUser, TFI3d, TFO3d, Time, ViewerOps, ViewerTools
EXPORTS GraphicsBasic, SVViewerUser =

BEGIN

Assembly: TYPE = DisplayList3d.Assembly;
BoundBox: TYPE = SVBoundBox.BoundBox;
Camera: TYPE = CSGGraphics.Camera;
Color: TYPE = GraphicsColor.Color;
CoordSysGenerator: TYPE = DisplayList3d.CoordSysGenerator;
CoordSystem: TYPE = CoordSys.CoordSystem;
CSGTree: TYPE = DisplayList3d.CSGTree;
DeviceObject: PUBLIC TYPE = CGDevice.Rep;
DrawStyle: TYPE = CSGGraphics.DrawStyle;
EditToolData: TYPE = SVEditUser.EditToolData;
MasterObject: TYPE = DisplayList3d.MasterObject;
Matrix4by4: TYPE = Matrix3d.Matrix4by4;
MouseButton: TYPE = Menus.MouseButton;
Point2d: TYPE = SV2d.Point2d;
Primitive: TYPE = CSG.Primitive;
Scene: TYPE = DisplayList3d.Scene;
Vector: TYPE = SVVector3d.Vector;
Viewer: TYPE = ViewerClasses.Viewer;

ViewerToolData: TYPE = REF ViewerToolDataObj;
ViewerToolDataObj: TYPE = SVViewerTool.ViewerToolDataObj;

ViewerTextData: TYPE = SVViewerTool.ViewerTextData;

ViewerPictureData: TYPE = REF ViewerPictureDataObj;
ViewerPictureDataObj: TYPE = SVViewerUser.ViewerPictureDataObj;

InteractionMode: TYPE = SVViewerUser.InteractionMode; -- {select, cast};

DCProc: TYPE = SVViewerUser.DCProc;


ReadTwoReals: PUBLIC PROC [textViewer: Viewer] RETURNS [x, y: REAL] = TRUSTED {
 wholeRope: Rope.ROPE ← ViewerTools.GetContents[textViewer];
 wholeStream: IO.STREAMIO.RIS[wholeRope];
 TFI3d.ReadBlank[wholeStream];
 x ← TFI3d.ReadReal[wholeStream];
 TFI3d.ReadBlankAndRope[wholeStream, ","];
 y ← TFI3d.ReadReal[wholeStream]; };

DrawSceneEtc: PUBLIC PROC [dc: Graphics.Context, viewerToolData: ViewerToolData] = TRUSTED {
-- may also draw coordinate systems, drawing planes, etc depending on the current selected modes.
 viewerPictureData: ViewerPictureData ← NARROW[viewerToolData.viewerPicture.data];
 scene: Scene ← viewerPictureData.scene;
 camera: Camera ← viewerPictureData.camera;
IF viewerPictureData.doubleBuffer THEN {
  BitMap3d.Erase[viewerPictureData.altDC];
  DisplayList3d.DrawScene[viewerPictureData.altDC.dc, scene, camera];
  IF viewerPictureData.showCoordSys
   THEN DrawAllCS[viewerPictureData.altDC.dc, viewerToolData];
  SVEditUser.DrawAnySelections[dc, viewerToolData];
  BitMap3d.DrawAltDisplayContext[dc, viewerPictureData.altDC];
  }
ELSE {
  editToolData: EditToolData ← NARROW[viewerToolData.editToolData];
  assem: Assembly;
  success: BOOL;
  DisplayList3d.DrawScene[dc, scene, camera];
  Graphics.SetColor[dc, GraphicsColor.black];
  -- IF viewerPictureData.showCoordSys THEN DrawAllCS[dc, viewerToolData];
  IF viewerPictureData.showCoordSys THEN
   IF editToolData.sceneSection.currentScene = scene THEN {
    [assem, success] ← SVEditUser.GetAssembly[editToolData.sceneSection.assemblyName, scene];
    IF success THEN DrawOneCS[dc, viewerToolData, assem.coordSys];
    };
   SVEditUser.DrawAnySelections[dc, viewerToolData];
  };
 };

EraseAndDrawSceneEtc: PUBLIC PROC [dc: Graphics.Context, viewerToolData: ViewerToolData] = TRUSTED {
-- may also draw coordinate systems, drawing planes, etc depending on the current selected modes.
 viewerPictureData: ViewerPictureData ← NARROW[viewerToolData.viewerPicture.data];
 scene: Scene ← viewerPictureData.scene;
 camera: Camera ← viewerPictureData.camera;
IF viewerPictureData.doubleBuffer THEN {
  BitMap3d.Erase[viewerPictureData.altDC];
  DisplayList3d.DrawScene[viewerPictureData.altDC.dc, scene, camera];
  IF viewerPictureData.showCoordSys
   THEN DrawAllCS[viewerPictureData.altDC.dc, viewerToolData];
  SVEditUser.DrawAnySelections[dc, viewerToolData];
  BitMap3d.DrawAltDisplayContext[dc, viewerPictureData.altDC];
  }
ELSE {
  Graphics.SetColor[dc, GraphicsColor.white];
  Graphics.DrawBox[dc, Graphics.GetBounds[dc]];
  DisplayList3d.DrawScene[dc, scene, camera];
  Graphics.SetColor[dc, GraphicsColor.black];
  IF viewerPictureData.showCoordSys THEN DrawAllCS[dc, viewerToolData];
  SVEditUser.DrawAnySelections[dc, viewerToolData];
  };
 };

DrawAllCS: PUBLIC PROC [dc: Graphics.Context, viewerToolData: ViewerToolData] = TRUSTED {
 viewerPictureData: ViewerPictureData ← NARROW[viewerToolData.viewerPicture.data];
 scene: Scene ← viewerPictureData.scene;
 camera: Camera ← viewerPictureData.camera;
 g: CoordSysGenerator ← DisplayList3d.GetCoordSysGenerator[scene];
 mat: Matrix4by4;
FOR cs: CoordSystem ← DisplayList3d.NextCoordSys[g], DisplayList3d.NextCoordSys[g]
UNTIL cs = NIL DO
  mat ← CoordSys.FindInTermsOfCamera[cs, camera.coordSys];
  -- make sure you have up to date coordSys positions
  Draw3d.DrawCoordSys[dc, mat, camera];
ENDLOOP;
 };

DrawOneCS: PUBLIC PROC [dc: Graphics.Context, viewerToolData: ViewerToolData, cs: CoordSystem] = TRUSTED {
 viewerPictureData: ViewerPictureData ← NARROW[viewerToolData.viewerPicture.data];
 camera: Camera ← viewerPictureData.camera;
-- assume that cs has valid cs.wrtCamera and cs.wrtWorld fields.
 Draw3d.DrawCoordSys[dc, cs.wrtCamera, camera];
 };

UpdateHeader: PUBLIC PROC [pictureFile: Rope.ROPE, viewerToolData: ViewerToolData] = TRUSTED {
 viewerPictureData: ViewerPictureData ← NARROW[viewerToolData.viewerPicture.data];
 headerRope: Rope.ROPE;
 aisName: Rope.ROPE;
IF pictureFile = NIL THEN pictureFile ← viewerPictureData.scene.name;
 headerRope ← Rope.Concat["Solid Scene: ", pictureFile];
 aisName ← Rope.Concat[SVFiles.FilenameMinusExtension[pictureFile], ".ais"];
 viewerToolData.outer.name ← headerRope;
 ViewerTools.SetContents[viewerToolData.textSection.ais, aisName, TRUE];
 ViewerOps.PaintViewer[viewerToolData.outer, caption, FALSE, NIL];
 };

NotifyOfSplit: PUBLIC PROC [viewer: Viewer] = TRUSTED {
 viewerToolData: ViewerToolData ← NARROW[viewer.data];
 viewerPictureData: ViewerPictureData ← NARROW[viewerToolData.viewerPicture.data];
 UpdateHeader[viewerPictureData.scene.name, viewerToolData];
 };

UnSplit: PUBLIC PROC [viewerToolData: ViewerToolData] = TRUSTED {
 viewerPictureData: ViewerPictureData ← NARROW[viewerToolData.viewerPicture.data];
 UpdateHeader[viewerPictureData.scene.name, viewerToolData];
 };

DrawOneCSInViewer: PUBLIC PROC [viewerToolData: ViewerToolData, cs: CoordSystem] = TRUSTED {
DoDrawOneCSInViewer: PROC [dc: Graphics.Context] = TRUSTED {
  DrawOneCS[dc, viewerToolData, cs];
  };
 Painter[DoDrawOneCSInViewer, viewerToolData];
 };

Painter: PUBLIC PROC [proc: DCProc, viewerToolData: ViewerToolData ← NIL] = TRUSTED {
 viewerPictureData: ViewerPictureData ← NARROW[viewerToolData.viewerPicture.data];
 scene: Scene ← viewerPictureData.scene;
 camera: Camera ← viewerPictureData.camera;
 viewerPictureData.proc ← proc;
 ViewerOps.PaintViewer[viewer: viewerToolData.viewerPicture,
        hint: client,
        whatChanged: viewerToolData,
        clearClient: FALSE];
 }; -- end of Painter

DrawScene: PUBLIC PROC [parent: REF ANY, clientData: REF ANY, mouseButton: MouseButton, shift, control: BOOL] = TRUSTED {
-- Menus.MenuProc
-- viewer is a SolidWindow. clientData is viewerToolData
 viewerToolData: ViewerToolData ← NARROW[clientData];
 viewerPictureData: ViewerPictureData ← NARROW[viewerToolData.viewerPicture.data];
DoDrawScene: PROC [dc: Graphics.Context] = TRUSTED {
  DrawSceneEtc[dc, viewerToolData];
  };
 Painter[DoDrawScene, viewerToolData];
 };

DrawSceneInternal: PUBLIC PROC [parent: REF ANY, clientData: REF ANY, mouseButton: MouseButton, shift, control: BOOL] = TRUSTED {-- like DrawScene but erases the screen before drawing.
-- Menus.MenuProc
-- viewer is a SolidWindow. clientData is viewerToolData
 viewerToolData: ViewerToolData ← NARROW[clientData];
DoDrawScene: PROC [dc: Graphics.Context] = TRUSTED {
  EraseAndDrawSceneEtc[dc, viewerToolData];
  };
 Painter[DoDrawScene, viewerToolData];
 };

PlaceOrigin: PUBLIC PROC [viewer: Viewer] = TRUSTED {
-- viewer is the viewerPicture
-- find the center of the solid window in solid window coordinates
 originX, originY: REAL;
 transMat: Matrix4by4;
 viewerPictureData: ViewerPictureData ← NARROW[viewer.data];
 screenCS: CoordSystem;
 camera: Camera ← viewerPictureData.camera;
 screenCS ← camera.screenCS;
 originX ← viewer.cw; -- convert window width to real
 originY ← viewer.ch; -- convert window height to real
 originX ← originX/2.0; -- find the midpoint in window coords
 originY ← originY/2.0;
 transMat ← Matrix3d.MakeTranslateMat[-originX,-originY,0];
 CoordSys.TPlaceAwrtB[screenCS,camera.coordSys,transMat];
 }; -- end of PlaceOrigin

ResizeBitMap: PUBLIC PROC [viewer: Viewer] = TRUSTED {
-- viewer is the viewerPicture
-- resize its bitMap
 viewerPictureData: ViewerPictureData ← NARROW[viewer.data];
 BitMap3d.ResizeBitMap[viewer, viewerPictureData.altDC];
 };

Empty: PUBLIC PROC [parent: REF ANY, clientData: REF ANY, mouseButton: MouseButton, shift, control: BOOL] = TRUSTED {
-- Creates a new empty scene for the viewer to view
 viewerToolData: ViewerToolData ← NARROW[clientData];
 viewerPictureData: ViewerPictureData ← NARROW[viewerToolData.viewerPicture.data];
 oldScene: Scene ← viewerPictureData.scene;
 viewerPictureData.scene ← DisplayList3d.CreateScene["NoName.pic"];
 UpdateHeader[viewerPictureData.scene.name, viewerToolData];
 ViewerTools.SetContents[viewerToolData.textSection.fileName, viewerPictureData.scene.name];
 SVViewerUser.Selected[parent, clientData, mouseButton, shift, control];
 Erase[parent, clientData, mouseButton, shift, control];
 SVEditUser.NotifyOfEmpty[viewerToolData: viewerToolData, from: oldScene, to: viewerPictureData.scene];
 SceneOldVersion[viewerToolData]; -- this must be done after NotifyOfEmpty since SVEditUser expects to register to the newly created NoName scene before it resets the [New Version] headers
 };

ConfirmEmpty: PUBLIC PROC [parent: REF ANY, clientData: REF ANY, mouseButton: MouseButton, shift, control: BOOL] = TRUSTED {
 viewerToolData: ViewerToolData ← NARROW[clientData];
 viewerPictureData: ViewerPictureData ← NARROW[viewerToolData.viewerPicture.data];
 scene: Scene ← viewerPictureData.scene;
 picName: Rope.ROPE ← ViewerTools.GetContents[viewerToolData.textSection.fileName];
 stream: IO.STREAM;
 rope: Rope.ROPE;
 dirty: BOOL;
 stream ← IO.CreateOutputStreamToRope[];
 dirty ← viewerToolData.outer.newVersion;
IF dirty THEN
  stream.PutF["Confirm discard of edits"]
ELSE stream.PutF["Confirm emptying of viewer"];
 rope ← IO.GetOutputStreamRope[stream];
 MessageWindow.Append[rope, TRUE];
 };

ClearScene: PUBLIC PROC [parent: REF ANY, clientData: REF ANY, mouseButton: MouseButton, shift, control: BOOL] = TRUSTED {
 viewerToolData: ViewerToolData ← NARROW[clientData];
 viewerPictureData: ViewerPictureData ← NARROW[viewerToolData.viewerPicture.data];
 DisplayList3d.ClearScene[viewerPictureData.scene];
 };

Erase: PUBLIC PROC [parent: REF ANY, clientData: REF ANY, mouseButton: MouseButton, shift, control: BOOL] = TRUSTED {
 viewerToolData: ViewerToolData ← NARROW[clientData];
 viewerPictureData: ViewerPictureData ← NARROW[viewerToolData.viewerPicture.data];
DoErase: PROC [dc: Graphics.Context] = TRUSTED {
  mark: Graphics.Mark ← Graphics.Save[dc];
  Graphics.SetColor[dc, Graphics.white];
  Graphics.DrawBox[dc,Graphics.GetBounds[dc]];
  Graphics.SetColor[dc, Graphics.white];
  Graphics.Restore[dc,mark];
  };
 Painter[DoErase, viewerToolData];
};

DrawCoordSystems: PUBLIC PROC [parent: REF ANY, clientData: REF ANY, mouseButton: MouseButton, shift, control: BOOL] = TRUSTED {
 viewerToolData: ViewerToolData ← NARROW[clientData];
DoDrawCoordSys: PROC [dc: Graphics.Context] = TRUSTED {
  DrawAllCS[dc, viewerToolData];
  };
 Painter[DoDrawCoordSys, viewerToolData];
 };

DrawBoundBoxes: PUBLIC PROC [parent: REF ANY, clientData: REF ANY, mouseButton: MouseButton, shift, control: BOOL] = TRUSTED {
-- viewer is Container
 viewerToolData: ViewerToolData ← NARROW[clientData];
 viewerPictureData: ViewerPictureData ← NARROW[viewerToolData.viewerPicture.data];
 scene: Scene ← viewerPictureData.scene;
 camera: Camera ← viewerPictureData.camera;
 tree: CSG.CSGTree;
DoDrawBoundBoxes: PROC [dc: Graphics.Context] = TRUSTED {
  Draw3d.DrawBoundBoxes[dc, tree, camera];
  };
 tree ← DisplayList3d.SceneToTree[scene, camera]; -- does a TellAboutCameraAndWorld
 [] ← Preprocess3d.Preprocess[tree, camera];
 Painter[DoDrawBoundBoxes, viewerToolData];
 }; -- end of DrawBoundBoxes

SceneNewVersion: PUBLIC PROC [viewerToolData: ViewerToolData] = TRUSTED {
 viewerPictureData: ViewerPictureData ← NARROW[viewerToolData.viewerPicture.data];
 scene: Scene ← viewerPictureData.scene;
 SVEditUser.SceneNewVersion[viewerToolData, scene];
 };

NewVersion: PUBLIC PROC [viewer: Viewer] = TRUSTED {
 viewerToolData: ViewerToolData ← NARROW[viewer.data];
 viewerPictureData: ViewerPictureData ← NARROW[viewerToolData.viewerPicture.data];
 scene: Scene ← viewerPictureData.scene;
 scene.dirty ← TRUE;
 viewerToolData.outer.newVersion ← TRUE;
 UpdateHeader[scene.name, viewerToolData];
 };

SceneOldVersion: PUBLIC PROC [viewerToolData: ViewerToolData] = TRUSTED {
 viewerPictureData: ViewerPictureData ← NARROW[viewerToolData.viewerPicture.data];
 scene: Scene ← viewerPictureData.scene;
 SVEditUser.SceneOldVersion[viewerToolData, scene];
 };

OldVersion: PUBLIC PROC [viewer: Viewer] = TRUSTED {
 viewerToolData: ViewerToolData ← NARROW[viewer.data];
 viewerPictureData: ViewerPictureData ← NARROW[viewerToolData.viewerPicture.data];
 scene: Scene ← viewerPictureData.scene;
 scene.dirty ← FALSE;
 viewerToolData.outer.newVersion ← FALSE;
 UpdateHeader[scene.name, viewerToolData];
 };

Reset: PUBLIC PROC [parent: REF ANY, clientData: REF ANY, mouseButton: MouseButton, shift, control: BOOL] = TRUSTED {
 viewerToolData: ViewerToolData ← NARROW[clientData];
 viewerPictureData: ViewerPictureData ← NARROW[viewerToolData.viewerPicture.data];
 success: BOOL;
 scene: Scene ← viewerPictureData.scene;
 [scene, success] ← SVEditUser.LoadScene[viewerToolData, scene.name];
-- Reload this scene from its backing file.
IF success THEN {viewerPictureData.scene ← scene;
  SceneOldVersion[viewerToolData];
  UpdateHeader[scene.name, viewerToolData];
  DrawSceneInternal[parent, clientData, mouseButton, shift, control];
  SVViewerUser.Selected[parent, clientData, mouseButton, shift, control];
  };
 }; -- end of Reset

ConfirmReset
: PUBLIC PROC [parent: REF ANY, clientData: REF ANY, mouseButton: MouseButton, shift, control: BOOL] = TRUSTED {
 viewerToolData: ViewerToolData ← NARROW[clientData];
 viewerPictureData: ViewerPictureData ← NARROW[viewerToolData.viewerPicture.data];
 scene: Scene ← viewerPictureData.scene;
 stream: IO.STREAM;
 rope: Rope.ROPE;
 dirty: BOOL;
 stream ← IO.CreateOutputStreamToRope[];
 dirty ← viewerToolData.outer.newVersion;
IF dirty THEN
  stream.PutF["Confirm discard of edits"]
ELSE stream.PutF["Confirm reset of %g", [rope[scene.name]]];
 rope ← IO.GetOutputStreamRope[stream];
 MessageWindow.Append[rope, TRUE];
 }; -- end of ConfirmReset

Save: PUBLIC PROC [parent: REF ANY, clientData: REF ANY, mouseButton: MouseButton, shift, control: BOOL] = TRUSTED {
 viewerToolData: ViewerToolData ← NARROW[clientData];
 viewerPictureData: ViewerPictureData ← NARROW[viewerToolData.viewerPicture.data];
 success: BOOL;
 scene: Scene ← viewerPictureData.scene;
 success ← SVEditUser.SaveScene[viewerToolData, scene.name];
IF success THEN {
  SVViewerUser.Selected[parent, clientData, mouseButton, shift, control];
  SceneOldVersion[viewerToolData];
  UpdateHeader[scene.name, viewerToolData];
  };
 }; -- end of Save

ConfirmSave: PUBLIC PROC [parent: REF ANY, clientData: REF ANY, mouseButton: MouseButton, shift, control: BOOL] = TRUSTED {
 viewerToolData: ViewerToolData ← NARROW[clientData];
 viewerPictureData: ViewerPictureData ← NARROW[viewerToolData.viewerPicture.data];
 scene: Scene ← viewerPictureData.scene;
 exists: BOOL;
 stream: IO.STREAM;
 rope: Rope.ROPE;
 stream ← IO.CreateOutputStreamToRope[];
 exists ← SVFiles.FileExists[scene.name];
IF exists THEN
  stream.PutF["Confirm Save to %g [Old File]", [rope[scene.name]]]
ELSE {stream.PutF["%g doesn't exist. Use Store", [rope[scene.name]]];
  MessageWindow.Blink[]
  };
 rope ← IO.GetOutputStreamRope[stream];
 MessageWindow.Append[rope, TRUE];
 };
  
Load: PUBLIC PROC [parent: REF ANY, clientData: REF ANY, mouseButton: MouseButton, shift, control: BOOL] = TRUSTED {
 viewerToolData: ViewerToolData ← NARROW[clientData];
 viewerPictureData: ViewerPictureData ← NARROW[viewerToolData.viewerPicture.data];
 success: BOOL;
 picName: Rope.ROPE ← ViewerTools.GetContents[viewerToolData.textSection.fileName];
 scene: Scene;
 [scene, success] ← SVEditUser.LoadScene[viewerToolData, picName];
IF success THEN {viewerPictureData.scene ← scene;
  SceneOldVersion[viewerToolData];
  UpdateHeader[picName, viewerToolData];
  DrawSceneInternal[parent, clientData, mouseButton, shift, control];
  SVViewerUser.Selected[parent, clientData, mouseButton, shift, control];
  };
 }; -- end of Load

ConfirmLoad: PUBLIC PROC [parent: REF ANY, clientData: REF ANY, mouseButton: MouseButton, shift, control: BOOL] = TRUSTED {
 viewerToolData: ViewerToolData ← NARROW[clientData];
 viewerPictureData: ViewerPictureData ← NARROW[viewerToolData.viewerPicture.data];
 scene: Scene ← viewerPictureData.scene;
 picName: Rope.ROPE ← ViewerTools.GetContents[viewerToolData.textSection.fileName];
 stream: IO.STREAM;
 rope: Rope.ROPE;
 dirty: BOOL;
 stream ← IO.CreateOutputStreamToRope[];
 dirty ← viewerToolData.outer.newVersion;
IF dirty THEN
  stream.PutF["Confirm discard of edits"]
ELSE stream.PutF["Confirm load of %g", [rope[picName]]];
 rope ← IO.GetOutputStreamRope[stream];
 MessageWindow.Append[rope, TRUE];
 }; -- end of ConfirmLoad
   
Store: PUBLIC PROC [parent: REF ANY, clientData: REF ANY, mouseButton: MouseButton, shift, control: BOOL] = TRUSTED {
 viewerToolData: ViewerToolData ← NARROW[clientData];
 viewerPictureData: ViewerPictureData ← NARROW[viewerToolData.viewerPicture.data];
 success: BOOL;
 picName: Rope.ROPE ← ViewerTools.GetContents[viewerToolData.textSection.fileName];
 scene: Scene ← viewerPictureData.scene;
 success ← SVEditUser.StoreScene[viewerToolData, scene, picName];
IF success THEN {UpdateHeader[picName, viewerToolData];
  SVViewerUser.Selected[parent, clientData, mouseButton, shift, control];
  SceneOldVersion[viewerToolData];
  UpdateHeader[scene.name, viewerToolData];};
 }; -- end of Store

ConfirmStore: PUBLIC PROC [parent: REF ANY, clientData: REF ANY, mouseButton: MouseButton, shift, control: BOOL] = TRUSTED {
 viewerToolData: ViewerToolData ← NARROW[clientData];
 viewerPictureData: ViewerPictureData ← NARROW[viewerToolData.viewerPicture.data];
 scene: Scene ← viewerPictureData.scene;
 picName: Rope.ROPE ← ViewerTools.GetContents[viewerToolData.textSection.fileName];
 exists: BOOL;
 stream: IO.STREAM;
 rope: Rope.ROPE;
 stream ← IO.CreateOutputStreamToRope[];
 exists ← SVFiles.FileExists[picName];
IF exists THEN
  stream.PutF["Confirm Store of %g [Old File]", [rope[picName]]]
ELSE stream.PutF["Confirm Store to %g [New File]", [rope[picName]]];
 rope ← IO.GetOutputStreamRope[stream];
 MessageWindow.Append[rope, TRUE];
 };
   
Split: PUBLIC PROC [parent: REF ANY, clientData: REF ANY, mouseButton: MouseButton, shift, control: BOOL] = TRUSTED {
-- Create a new viewer onto this scene. Update the ring of links joining all viewers onto this scene,
 viewerToolData: ViewerToolData ← NARROW[clientData];
 viewerPictureData: ViewerPictureData ← NARROW[viewerToolData.viewerPicture.data];
 scene: Scene ← viewerPictureData.scene;
 SVEditUser.Split[viewerToolData, scene];
 };

RayCast: PUBLIC PROC [parent: REF ANY, clientData: REF ANY, mouseButton: MouseButton, shift, control: BOOL] = TRUSTED {
 viewerToolData: ViewerToolData ← NARROW[clientData];
 viewerPictureData: ViewerPictureData ← NARROW[viewerToolData.viewerPicture.data];
 editToolData: EditToolData ← NARROW[viewerToolData.editToolData];
 scene: Scene ← viewerPictureData.scene;
 camera: Camera ← viewerPictureData.camera;
 tree: CSGTree;
 startTime: System.GreenwichMeanTime;
 endTime: System.GreenwichMeanTime;
 totalTime: LONG CARDINAL;
 timeStream: IO.STREAMIO.CreateOutputStreamToRope[];
 outStream: IO.STREAM ← SVEditUser.GetOutHandle[];
 timeRope: Rope.ROPE;
 aisFileRope: Rope.ROPE ← ViewerTools.GetContents[viewerToolData.textSection.ais];
 resolution: REAL ← SVViewerTools.GetReal[editToolData.cameraSection.resolution, 72.0];
-- For now, I delete any old file if it exists.
 startTime ← Time.Current[];
 tree ← DisplayList3d.SceneToTree[scene, camera];
 CastRays.DrawTree[NIL, tree, scene.lightSources, viewerPictureData.camera, aisFileRope, mouseButton = blue, resolution, SVViewerUser.RayCastProgress, viewerToolData, outStream];
 endTime ← Time.Current[];
 totalTime ← endTime - startTime;
 outStream.PutF[" Raycast took (%r)\n",[cardinal[totalTime]]];
 timeStream.PutF[" Raycast took (%r)",[cardinal[totalTime]]];
 timeRope ← IO.GetOutputStreamRope[timeStream];
 MessageWindow.Append[timeRope, TRUE];
 SVViewerUser.DrawBlackAndWhite[parent, clientData, mouseButton, shift, control];
 }; -- end of RayCast

ConfirmRayCast: PUBLIC PROC [parent: REF ANY, clientData: REF ANY, mouseButton: MouseButton, shift, control: BOOL] = TRUSTED {
 viewerToolData: ViewerToolData ← NARROW[clientData];
 exists: BOOL;
 aisFileRope: Rope.ROPE ← ViewerTools.GetContents[viewerToolData.textSection.ais];
 redRope: Rope.ROPE ← Rope.Concat[SVFiles.FilenameMinusExtension[aisFileRope], "-red.ais"];
 greenRope: Rope.ROPE ← Rope.Concat[SVFiles.FilenameMinusExtension[aisFileRope], "-green.ais"];
 blueRope: Rope.ROPE ← Rope.Concat[SVFiles.FilenameMinusExtension[aisFileRope], "-blue.ais"];
 stream: IO.STREAM;
 rope: Rope.ROPE;
 stream ← IO.CreateOutputStreamToRope[];
IF mouseButton = blue THEN { -- black and white only
  exists ← SVFiles.FileExists[aisFileRope];
  stream.PutF["Confirm raycast to %g.", [rope[aisFileRope]]];
  IF exists THEN stream.PutF[" [Old File]"]
   ELSE stream.PutF[" [New File]"];
  }
ELSE {
  exists ← SVFiles.FileExists[aisFileRope] OR SVFiles.FileExists[redRope] OR SVFiles.FileExists[greenRope] OR SVFiles.FileExists[blueRope];
 stream.PutF["Confirm raycast to %g [-red, -green, -blue, plain].ais",
  [rope[SVFiles.FilenameMinusExtension[aisFileRope]]]];
  IF exists THEN stream.PutF[" [Old Files]"]
  ELSE stream.PutF[" [New Files]"];
  };
 rope ← IO.GetOutputStreamRope[stream];
 MessageWindow.Append[rope, TRUE];
 };

Stop: PUBLIC PROC [parent: REF ANY, clientData: REF ANY, mouseButton: MouseButton, shift, control: BOOL] = TRUSTED {
 viewerToolData: ViewerToolData ← NARROW[clientData];
 viewerPictureData: ViewerPictureData ← NARROW[viewerToolData.viewerPicture.data];
 camera: Camera ← viewerPictureData.camera;
 camera.abort ← TRUE;
 };

ARay: PUBLIC PROC [parent: REF ANY, clientData: REF ANY, mouseButton: MouseButton, shift, control: BOOL] = TRUSTED {
 viewerToolData: ViewerToolData ← NARROW[clientData];
 viewerPictureData: ViewerPictureData ← NARROW[viewerToolData.viewerPicture.data];
 scene: Scene ← viewerPictureData.scene;
 camera: Camera ← viewerPictureData.camera;
-- get x and y from appropriate slots. Interpret this as camera coordinates and draw a cross.
 x, y: REAL;
 xInt, yInt: INTEGER;
 outHandle: IO.STREAM;
 tree: CSGTree ← DisplayList3d.SceneToTree[scene, camera];
 color: Color;
 [x, y] ← ReadTwoReals[viewerToolData.textSection.xyz];
 xInt ← Real.FixI[x]; yInt ← Real.FixI[y];
 outHandle ← SVEditUser.GetOutHandle[];
 color ← CastRays.SingleRay[xInt, yInt,
       tree, scene.lightSources, viewerPictureData.camera, TRUE, outHandle];
 outHandle.PutF["[%g,%g]: ",[real[x]], [real[y]]];
 TFO3d.FileoutColor[outHandle, color];
 }; -- end of ARay

END.