Code of web version of Unit uRendering

Smart Mobile Studio code of unit uRendering

unit uRendering;
{
    Copyright (c) 2011 Christopher Winward

    Licensed under the Apache License, Version 2.0 (the "License"); you may not
    use this file except in compliance with the License, as described at
    http://www.apache.org/licenses/ and http://www.pp4s.co.uk/licenses/
 
    Converted to run in Smart Mobile Studio by PPS (2013)
}

interface

uses 
  w3system, W3Graphics, uNodeClass;
type
  tColourState = (White, Green, Yellow, Red, Grey);

procedure drawNode(canvas : TW3Canvas; nodeToDraw : tNode; colourState : tColourState = White);
procedure renderScene(canvas : TW3Canvas);

implementation

uses
  uGlobalVariables;

procedure drawNode(canvas : TW3Canvas; nodeToDraw : tNode; colourState : tColourState = White);
begin
  case colourState of //Draw a box of a given colour.
    White :  canvas.FillStyle := 'white';
    Green :  canvas.FillStyle := 'green';
    Yellow : canvas.FillStyle := 'yellow';
    Red :    canvas.FillStyle := 'red';
    Grey :   canvas.FillStyle := 'gray';
  end;
  canvas.FillRectF(nodeToDraw.x, nodeToDraw.y, 32,  32);
  //Write the node name.
  canvas.FillStyle := 'blue';
  canvas.FillTextF(nodeToDraw.name, nodeToDraw.x, nodeToDraw.y, Max_INT);
end;

procedure renderScene(canvas : TW3Canvas);
var
  count, innerCount : integer;
begin
  //I understand that some nodes are drawn multiple times - for the sake of easy coding, I have left it as this.
  //If this program was dealing with hundreds or even thousands of nodes however, I would have optimised it.

  //Draw all the nodes.
  for count := 0 to (listOfNodes.Count - 1) do
    begin
      drawNode(canvas, tNode(listOfNodes[count]));
    end;

  //Draw the explored nodes.
  for count := 0 to (exploredListTemp.Count - 1) do
    begin
      drawNode(canvas, tNode(exploredListTemp[count]), Grey);
    end;

  if pathfoundList.Count > 0 then
    begin
      //Draw the path nodes.
      drawNode(canvas, tNode(pathfoundList[0]), Red);
      for count := 1 to (pathfoundList.Count - 2) do
        begin
          drawNode(canvas, tNode(pathfoundList[count]), Yellow);
        end;
      drawNode(canvas, tNode(pathfoundList[pathfoundList.Count - 1]), Green);
    end;

  //Draw the node connections.
  for count := 0 to (listOfNodes.Count - 2) do
    begin
      for innerCount := 0 to (tNode(listOfNodes[count]).adjacentNodes.Count - 1) do
        begin
          tempNode := tNode(tNode(listOfNodes[count]).adjacentNodes[innercount]);
          canvas.FillStyle := 'blue';
          canvas.StrokeStyle := 'blue';
          canvas.BeginPath;
          canvas.LineF(tNode(listOfNodes[count]).x + 16,
                       tNode(listOfNodes[count]).y + 16,
                       tempNode.x + 16, tempNode.y + 16);
          Canvas.ClosePath;
          Canvas.Stroke;

         Canvas.FillTextF(FloatToStr(tempNode.costToOtherNode(tNode(listOfNodes[count]))),
                          (tNode(listOfNodes[count]).x + tempNode.x) / 2,
                           ((tNode(listOfNodes[count]).y + tempNode.y) / 2) + 8, MAX_INT);
        end;
    end;

  if pathfoundList.Count > 0 then
    begin
      //Draw the path taken.
       canvas.StrokeStyle := 'rgb($FF00FF)';
       canvas.BeginPath;
       for count := 0 to (pathfoundList.Count - 2) do
         begin
           tempNode := tNode(pathfoundList[count]);
           canvas.LineF(tNode(pathfoundList[count]).x + 16,
                        tNode(pathfoundList[count]).y + 16,
                        tNode(pathfoundList[count+1]).x + 16,
                        tNode(pathfoundList[count+1]).y + 16);
         end;
      canvas.ClosePath;
      canvas.Stroke;
    end;
  canvas.Font := '9pt verdana';
  canvas.FillStyle := 'white';
  canvas.FillTextF('This program always tries to find a path from node A to F.', 6, 12, MAX_INT);
  canvas.FillTextF('Press Insert to add a node, and Delete to remove one.',   6, 24, MAX_INT);
  canvas.FillTextF('Left clicking a node will move it, right clicking allows you to link two nodes.', 6, 36, MAX_INT );
  canvas.FillTextF('To link two nodes, right click and drag between the two.', 6, 48, MAX_INT);
  canvas.FillTextF('White nodes are unexplored, Grey have been checked but ultimately ignored.', 6, 60, MAX_INT );
  canvas.FillTextF('The Red node is the start, Green is the target, and Yellow are path nodes.', 6, 72, MAX_INT);
end;

end.


Programming - a skill for life!

by Christopher Winward: U6 Age 17