MODULE testgraph;
(* test simple graphics functions from module graph *)
(* Author: Andrew Tridgell, March 1994 *)


IMPORT Graph;
IMPORT InOut;
IMPORT StdStrings;
IMPORT Random;

FROM InOut IMPORT WriteString, WriteLn;

FROM Graph IMPORT 
  (* import some important types from the graph module *)
  EventSet, Event, LineStyle, DrawingFunction;

PROCEDURE WaitClick;
(* wait for a mouse click *)
BEGIN (* WaitClick *)
  Graph.DrawString(10,Graph.WindowHeight() - 50,"Click a button to continue");
  Graph.WaitForButtonPress;
  Graph.ClearWindow;
END WaitClick;

PROCEDURE RandomCard() : INTEGER;
(* useful for pretty patterns *)
BEGIN (* RandomCard *)
RETURN(TRUNC(Random.Random()*65535.0));
END RandomCard;


PROCEDURE TestColors;
(* test of colors *)
VAR
  X,Y,Radius,MaxX : INTEGER;
  Red, Green, Blue : INTEGER;
  
BEGIN (* TestColors *)

  Graph.SaveSettings;

  MaxX := Graph.WindowWidth();
  X := MaxX DIV 2;
  Y := Graph.WindowHeight() DIV 2;

  Red := 0; Green := 0; Blue := 0;

  FOR Radius := 1 TO (MaxX DIV 3) DO

    Graph.SetForeground(Graph.GetColorRGB(Red,Green,Blue));

    Graph.DrawCircle(X,Y,Radius);
    
    Red := Red + 3;
    Green := Green + 9;
    Blue := Blue + 27;
    Graph.Sleep(1.0);
  END;

  Graph.RestoreSettings;

END TestColors;


PROCEDURE TestLines;
(* test of line drawing *)
VAR
  X1,Y1,X2,Y2 : INTEGER;
  Step : INTEGER;
    
BEGIN (* TestLines *)

  X1 := Graph.WindowWidth() DIV 5;
  Y1 := Graph.WindowHeight() DIV 5;
  X2 := 3 * X1;
  Step := Graph.WindowHeight() DIV 20;
  
  FOR Y2 := Y1 TO (3*Y1) BY 10 DO 
    Graph.DrawLine(X1,Y1,X2,Y2);
  END;
  
END TestLines;

PROCEDURE TestRectangles;
(* test of rectangle drawing *)
VAR
  MaxY, X,Y,Width,Height : INTEGER;
  Step : INTEGER;
    
BEGIN (* TestRectangles *)

  Graph.SaveSettings;

  IF (Graph.AvailableColors() > 2) THEN
    Graph.SetForeground(Graph.GetColor("blue"));
  END;    

  Width := Graph.WindowWidth() DIV 3;
  Height := Graph.WindowHeight() DIV 3;
  Step := Graph.WindowHeight() DIV 20;

  (* set a different line style, saving the old one *)
  Graph.SaveSettings;
  Graph.SetLineStyle(LineDoubleDash);
  
  FOR X := 0 TO Graph.WindowWidth() BY 20 DO 
    Y := X;
    Graph.DrawRectangle(X,Y,Width,Height);
  END;
  
  (* restore the saved line style *)
  Graph.RestoreSettings;
  
  MaxY := Graph.WindowHeight();
  
  IF (Graph.AvailableColors() > 2) THEN
    Graph.SetForeground(Graph.GetColor("red"));
  END;    

  FOR X := 0 TO Graph.WindowWidth() BY 20 DO 
    Y := MaxY - X;
    Graph.DrawRectangle(X,Y,Width,Height);
  END;

  IF (Graph.AvailableColors() > 2) THEN
    Graph.SetForeground(Graph.GetColor("green"));
  END;    

  FOR X := 0 TO Graph.WindowWidth() BY 20 DO   
    Graph.FillRectangle(X,MaxY DIV 2,15,(X MOD 7)*5);
  END;

  Graph.RestoreSettings;

END TestRectangles;



PROCEDURE TestFonts;
(* test font operations *)
VAR
  FontName, Message : StdStrings.String;
  Y,X : INTEGER;
  
BEGIN (*TestFonts*)
  
  Graph.SaveSettings;

  (* what to write before each font and where *)
  Message := "This is ";
  Y := 10;

  IF (Graph.AvailableColors() > 2) THEN
    Graph.SetForeground(Graph.GetColor("red"));
  END;
	
  (* first the default font *)
  Graph.DrawString(X,Y,"Hello world!");
  
  (* now a bigger one *)
  FontName := "10x20";  
  Graph.LoadFont(FontName);
  Y := Y + Graph.StringHeight(Message) + 5;
  Graph.DrawString(X,Y,Message);
  Graph.DrawString(X+Graph.StringWidth(Message),Y,FontName);

  IF (Graph.AvailableColors() > 2) THEN
    Graph.SetForeground(Graph.GetColor("green"));
  END;
	
  (* now a bigger one *)
  FontName := "12x24";  
  Graph.LoadFont(FontName);
  Y := Y + Graph.StringHeight(Message) + 5;
  Graph.DrawString(X,Y,Message);
  Graph.DrawString(X+Graph.StringWidth(Message),Y,FontName);
    
  IF (Graph.AvailableColors() > 2) THEN
    Graph.SetForeground(Graph.GetColor("magenta"));
  END;
	
  (* and another one *)
  FontName := "-adobe-times-bold-r-normal--24-240-75-75-p-132-iso8859-1";  
  Graph.LoadFont(FontName);
  Y := Y + Graph.StringHeight(Message) + 5;
  Graph.DrawString(X,Y,Message);
  Graph.DrawString(X+Graph.StringWidth(Message),Y,FontName);
  
  Graph.RestoreSettings;
  
END TestFonts;


PROCEDURE TestArcs;
(* test the display of arcs *)
VAR
  X, Y, Width, Height, StartAngle, EndAngle : INTEGER;
  MaxX, MaxY : INTEGER;
  Count : INTEGER;
    
BEGIN (*TestArcs*)

  Graph.SaveSettings;
  
  MaxX := Graph.WindowWidth();
  MaxY := Graph.WindowHeight();
        
  IF (Graph.AvailableColors() > 2) THEN
    Graph.SetForeground(Graph.GetColor("yellow"));
  END;    

  FOR Count := 1 TO 5 DO
    X := RandomCard() MOD MaxX;
    Y := RandomCard() MOD MaxY;
    Width := RandomCard() MOD MaxX;
    Height := RandomCard() MOD MaxY;
    StartAngle := RandomCard() MOD 360;
    EndAngle := RandomCard() MOD 360;
    
    Graph.DrawArc(X,Y,Width,Height,StartAngle,EndAngle);
  END;

  IF (Graph.AvailableColors() > 2) THEN
    Graph.SetForeground(Graph.GetColor("green"));
  END;    

  FOR Count := 1 TO 5 DO
    X := RandomCard() MOD MaxX;
    Y := RandomCard() MOD MaxY;
    Width := RandomCard() MOD MaxX;
    Height := RandomCard() MOD MaxY;
    StartAngle := RandomCard() MOD 360;
    EndAngle := RandomCard() MOD 360;
    
    Graph.FillArc(X,Y,Width,Height,StartAngle,EndAngle);
  END;

  Graph.RestoreSettings;
      
END TestArcs;




PROCEDURE TestEllipses;
(* test the display of Ellipses *)
VAR
  X, Y, Width, Height : INTEGER;
  MaxX, MaxY : INTEGER;
  Count : INTEGER;
  
BEGIN (*TestEllipses*)

  MaxX := Graph.WindowWidth();
  MaxY := Graph.WindowHeight();
        
  IF (Graph.AvailableColors() > 2) THEN
    Graph.SetForeground(Graph.GetColor("brown"));
  END;
	
  FOR Count := 1 TO 5 DO
    X := RandomCard() MOD MaxX;
    Y := RandomCard() MOD MaxY;
    Width := RandomCard() MOD MaxX;
    Height := RandomCard() MOD MaxY;
    
    Graph.DrawEllipse(X,Y,Width,Height);
  END;

  IF (Graph.AvailableColors() > 2) THEN
    Graph.SetForeground(Graph.GetColor("blue"));
  END;
	
  FOR Count := 1 TO 5 DO
    X := RandomCard() MOD MaxX;
    Y := RandomCard() MOD MaxY;
    Width := RandomCard() MOD MaxX;
    Height := RandomCard() MOD MaxY;
    
    Graph.FillEllipse(X,Y,Width,Height);
  END;
  
END TestEllipses;

PROCEDURE TestPoints;
(* test the display of Points *)
VAR
  X, Y : INTEGER;
  MaxX, MaxY : INTEGER;
  Count : INTEGER;
  
BEGIN (*TestPoints*)

  MaxX := Graph.WindowWidth();
  MaxY := Graph.WindowHeight();
        
  FOR Count := 1 TO 500 DO
    X := RandomCard() MOD MaxX;
    Y := RandomCard() MOD MaxY;
    
    Graph.DrawPoint(X,Y);
  END;
  
END TestPoints;


PROCEDURE TestEvents;
(* test events by doing simple drawing *)
CONST
  SquareSize = 20;

VAR
  X,Y,LastX,LastY : INTEGER;
  AEvent : EventSet;
  Drawing : BOOLEAN; (* are we currently drawing? *)
  Width : INTEGER;
    
BEGIN (*TestEvents*)

  (* initialise *)
  Drawing := FALSE;
  Width := 3;
  
  (* give instructions *)
  Graph.DrawString(50,30,"Try drawing then click in the square to finish");
  Graph.FillRectangle(0,0,SquareSize,SquareSize);
  Graph.SetLineWidth(Width);
  
  REPEAT
    (* get an event *)
    Graph.WaitForEvent(EventSet{ButtonPress,ButtonRelease,Motion});
    AEvent := Graph.LastEvent();
    
    IF ButtonPress IN AEvent THEN
      Graph.EventPosition(LastX,LastY);
      Drawing := TRUE;    
    END;  

    IF ButtonRelease IN AEvent THEN
      Graph.EventPosition(LastX,LastY);
      Drawing := FALSE;    
    END;  

    IF Motion IN AEvent THEN
      IF Drawing THEN
	Graph.MousePosition(X,Y);
	Graph.DrawLine(LastX,LastY,X,Y);
	LastX := X; LastY := Y;
      END;
    END;
  
  UNTIL ((LastX < SquareSize) AND (LastY < SquareSize) AND 
      	  (ButtonRelease IN AEvent));
END TestEvents;





PROCEDURE DrawRubberBand(X1, Y1, X2, Y2 : INTEGER);
(* draw a single rubber band element *)
VAR
  X, Y, Width, Height : INTEGER;
  
BEGIN (*DrawRubberBand*)
  IF (X1 < X2) THEN
    X := X1;
    Width := X2 - X1;
  ELSE
    X := X2;
    Width := X1 - X2;
  END;

  IF (Y1 < Y2) THEN
    Y := Y1;
    Height := Y2 - Y1;
  ELSE
    Y := Y2;
    Height := Y1 - Y2;
  END;
  
  Graph.DrawRectangle(X,Y,Width,Height);
END DrawRubberBand;



PROCEDURE TestRubber;
(* try some simple rubber-banding *)
CONST
  SquareSize = 20;

VAR
  X,Y,LastX,LastY : INTEGER;
  AEvent : EventSet;
  Drawing : BOOLEAN; (* are we currently drawing? *)
  Width : INTEGER;
    
BEGIN (*TestRubber*)

  (* initialise *)
  Drawing := FALSE;
  Width := 1;
  
  (* give instructions *)
  Graph.DrawString(50,30,"Try rubber-banding then click in the square to finish");
  Graph.FillRectangle(0,0,SquareSize,SquareSize);

  Graph.SaveSettings;
  Graph.SetLineWidth(Width);
  Graph.SetLineStyle(LineDoubleDash);
  Graph.SetDrawingFunction(Xor); 
  Graph.SetForeground(1);
    
  REPEAT
    (* get an event *)
    Graph.WaitForEvent(EventSet{ButtonPress,ButtonRelease,Motion});
    AEvent := Graph.LastEvent();
    
    IF ButtonPress IN AEvent THEN
      Graph.EventPosition(LastX,LastY);
      X := LastX; Y := LastY;
      DrawRubberBand(LastX,LastY,X,Y);
      Drawing := TRUE;    
    END;  

    IF ButtonRelease IN AEvent THEN
      DrawRubberBand(LastX,LastY,X,Y);
      Graph.EventPosition(LastX,LastY);
      Drawing := FALSE;    
    END;  

    IF Motion IN AEvent THEN
      IF Drawing THEN
	DrawRubberBand(LastX,LastY,X,Y);
	Graph.MousePosition(X,Y);
	DrawRubberBand(LastX,LastY,X,Y);
      END;
    END;
  
  UNTIL ((LastX < SquareSize) AND (LastY < SquareSize) AND 
      	  (ButtonRelease IN AEvent));

  Graph.RestoreSettings;
END TestRubber;


(* **************** *)
(* The main program *)
(* **************** *)
BEGIN (* testgraph *)

Graph.StartGraphics(700,700,FALSE);
Graph.AlwaysFlush(FALSE);
Graph.SetTitle("Modula-2 Graphics Test");

(* note the use of a 3 bytes hexadecimal color specifier here, in RGB 
  notation *)
IF (Graph.AvailableColors() > 2) THEN
  Graph.SetForeground(Graph.GetColor("#1000ff"));
END;

Graph.ClearWindow;

IF (Graph.AvailableColors() > 2) THEN
  TestColors; WaitClick;
END;

TestPoints; WaitClick;

TestLines; WaitClick;

TestRectangles; WaitClick;

TestFonts; WaitClick;

TestArcs; WaitClick;

TestEllipses; WaitClick;

TestEvents; Graph.ClearWindow;

TestRubber; Graph.ClearWindow;

Graph.EndGraphics;

END testgraph.
