-- File: SVArtworkUserImpl.mesa
-- Last edited by Bier on December 18, 1982 1:42 am
-- Author: Eric Bier on March 22, 1983 2:18 pm
-- Contents: Viewer for Two dimensional artwork for mappings

DIRECTORY
 BandsClient,
 Containers,
 Graphics,
 GraphicsColor,
IO,
 Menus,
 MessageWindow,
 PadGraphics,
 Rope,
 SVArtwork,
 SVArtworkUser,
 SVCoordSys2d,
 SVMatrix2d,
 SVViewerTools,
 System,
 Time,
 ViewerClasses,
 ViewerTools,
 ViewerOps;

SVArtworkUserImpl: CEDAR PROGRAM
IMPORTS BandsClient, GraphicsColor, IO, MessageWindow, PadGraphics, SVArtwork, SVMatrix2d, SVCoordSys2d, SVViewerTools, Time, ViewerOps, ViewerTools
EXPORTS SVArtworkUser =
BEGIN

Artwork: TYPE = SVArtwork.Artwork;
MouseButton: TYPE = Menus.MouseButton;
Viewer: TYPE = ViewerClasses.Viewer;


ArtworkToolData: TYPE = REF ArtworkToolDataObj;
ArtworkToolDataObj: TYPE = SVArtworkUser.ArtworkToolDataObj;

UpdateHeader: PUBLIC PROC [assemblyName: Rope.ROPE, sceneName: Rope.ROPE, artworkToolData: ArtworkToolData] = {
 headerRope: Rope.ROPE;
 headerStream: IO.STREAM;
 headerStream ← IO.CreateOutputStreamToRope[];
IF artworkToolData.artwork.source = NIL THEN { -- pure color artwork
  r,g,b: REAL;
  [r,g,b] ← GraphicsColor.ColorToRGB[artworkToolData.artwork.color];
  headerStream.PutF["Artwork: (pure color) [%g, %g, %g]", [real[r]], [real[g]], [real[b]]];
  }
ELSE {
  r,g,b: REAL;
  [r,g,b] ← GraphicsColor.ColorToRGB[artworkToolData.artwork.color];
  headerStream.PutF["Artwork: %g %g backColor: [%g, %g, %g]", [rope[sceneName]], [rope[assemblyName]], [real[r]], [real[g]], [real[b]]];
  };
 headerRope ← IO.GetOutputStreamRope[headerStream];
 artworkToolData.outer.name ← headerRope;
 ViewerOps.PaintViewer[artworkToolData.outer, caption, FALSE, NIL];
 };

Painter: PUBLIC PROC [proc: PROC [dc: Graphics.Context], artworkToolData: ArtworkToolData] = TRUSTED {
 artworkToolData.proc ← proc;
 ViewerOps.PaintViewer[viewer: artworkToolData.picture,
        hint: client,
        whatChanged: artworkToolData,
        clearClient: FALSE];
 }; -- end of Painter

-- SVEditTool / ArtworkTool communication

SetArtwork: PUBLIC PROC [artworkTool: Viewer, artwork: Artwork, sceneName: Rope.ROPE, assemblyName: Rope.ROPE] = {
 artworkToolData: ArtworkToolData ← NARROW[artworkTool.data];
 artworkToolData.artwork ← artwork;
 UpdateHeader[assemblyName, sceneName, artworkToolData];
 DrawArtwork[NIL, artworkToolData, red, FALSE, FALSE];
 };

RotateArtwork: PUBLIC PROC [artworkTool: Viewer, degrees: REAL] = TRUSTED {
 artworkToolData: ArtworkToolData ← NARROW[artworkTool.data];
 artwork: Artwork ← artworkToolData.artwork;
 SVCoordSys2d.RotateCCWAwrtB[artwork.artWRTPad, artwork.artWRTPad, degrees];
 DrawArtwork[NIL, artworkToolData, red, FALSE, FALSE];
 };

NormalizeArtwork: PUBLIC PROC [artworkTool: Viewer] = TRUSTED {
 artworkToolData: ArtworkToolData ← NARROW[artworkTool.data];
 artwork: Artwork ← artworkToolData.artwork;
 SVCoordSys2d.PlaceAwrtB[artwork.artWRTPad, NIL, SVMatrix2d.Identity[]];
 };

-- Button procs

ScalarSetPrompt: PUBLIC PROC [parent: REF ANY, clientData: REF ANY, mouseButton: MouseButton,
  shift, control: BOOL] = TRUSTED {
 artworkToolData: ArtworkToolData ← NARROW[clientData];
 scalar: REAL ← SVViewerTools.GetReal[artworkToolData.textSection.scalar, 1];
 artworkToolData.scalar ← scalar;
 DrawArtwork[NIL, artworkToolData, red, FALSE, FALSE];
 };

ScalarPrompt: PUBLIC PROC [parent: REF ANY, clientData: REF ANY, mouseButton: MouseButton,
  shift, control: BOOL] = TRUSTED {
 artworkToolData: ArtworkToolData ← NARROW[clientData];
 ViewerTools.SetSelection[artworkToolData.textSection.scalar];
 };

MakeBands: PUBLIC PROC [parent: REF ANY, clientData: REF ANY, mouseButton: MouseButton,
  shift, control: BOOL] = TRUSTED {
 artworkToolData: ArtworkToolData ← NARROW[clientData];
 scalar: REAL ← artworkToolData.scalar;
-- TIMING VARABLES
 startTime: System.GreenwichMeanTime;
 endTime: System.GreenwichMeanTime;
 totalTime: LONG CARDINAL;
 timeStream: IO.STREAM;
 timeRope: Rope.ROPE;
-- BANDS VARIABLES
 bandContext: Graphics.Context;
 bandDevice: BandsClient.Device;
-- START TIMING
 startTime ← Time.Current[];
-- START PRESSING
 BandsClient.SetUpSeparation[none, Tentfilter];
 [bandContext, bandDevice] ← BandsClient.OpenBands[hornet];

 SVArtwork.DrawArtwork[bandContext, artworkToolData.artwork, artworkToolData.origin, scalar];
  
 BandsClient.CloseBands[bandDevice, hornet];
 endTime ← Time.Current[];
 totalTime ← endTime - startTime;
 timeStream ← IO.CreateOutputStreamToRope[];
 timeStream.PutF[" Done. MakeBands took (%r)",[cardinal[totalTime]]];
 timeRope ← timeStream.GetOutputStreamRope[];
 MessageWindow.Append[timeRope, TRUE];
 };

Tentfilter: PROC [x,y: REAL] RETURNS [val: REAL] = TRUSTED {
RETURN [1.0-ABS[x]];
};

DrawArtwork: PUBLIC PROC [parent: REF ANY, clientData: REF ANY, mouseButton: MouseButton, shift, control: BOOL] = TRUSTED {
 artworkToolData: ArtworkToolData ← NARROW[clientData];
 artwork: Artwork ← artworkToolData.artwork;
 scalar: REAL ← artworkToolData.scalar;
DoDrawArtwork: PROC [dc: Graphics.Context] = TRUSTED {
   SVArtwork.DrawArtwork[dc, artwork, artworkToolData.origin, scalar];
  };
 Painter[DoDrawArtwork, artworkToolData];
 CrossHairs[parent, clientData, mouseButton, shift, control];
 };
    
PlaceOrigin: PUBLIC PROC [parent: REF ANY, clientData: REF ANY, mouseButton: MouseButton,
  shift, control: BOOL] = TRUSTED {
-- parent is the picture
-- find the center of the picture in picture coordinates
 picture: Viewer ← NARROW[parent];
 originX, originY: INTEGER;
 artworkToolData: ArtworkToolData ← NARROW[clientData];
 originX ← picture.cw; -- convert window width to real
 originY ← picture.ch; -- convert window height to real
 originX ← originX/2; -- find the midpoint in window coords
 originY ← originY/2;
 artworkToolData.origin ← [originX, originY];
 };

CrossHairs: PUBLIC PROC [parent: REF ANY, clientData: REF ANY, mouseButton: MouseButton, shift, control: BOOL] = TRUSTED {
DoCrossHairs: PROC [dc: Graphics.Context] = TRUSTED {
  PadGraphics.CrossHairs[dc, artworkToolData.origin];
  };
 artworkToolData: ArtworkToolData ← NARROW[clientData];
 Painter[DoCrossHairs, artworkToolData];
 };


  

END.