Class TLife (unit ULife)

Inherits from

TCustomControl

Constructors


constructor Create(aOwner : TComponent);



Functions

procedure ClearCells;

Mark all the cells as "dead".

destructor Destroy;


procedure LoadFromFile(const FileName : String);

Load a previous simulation from the named file.

procedure LoadFromStream(const Stream : TStream);

Load a previous simulation from the specified stream.

function NextGeneration : Cardinal;

Save the current state of FGrid to FHistory, then determine the new state of FGrid by applying the Rules of Life.

procedure ResetGeneration;

Reset the current Generation to zero

procedure SaveToFile(const FileName : String);

Save this simulation to the named file.

procedure SaveToStream(const Stream : TStream);

Save this simulation to the specified stream.

procedure SetGridSize(const NewGridWidth : TGridWidthRange; const NewGridHeight : TGridHeightRange);


function CellAtPos(const X, Y : Longint) : TPoint;

Returns the cell containing the specified pixel.

function CellCoords(const X : TGridWidthRange; const Y : TGridHeightRange) : TRect;

Returns the bounds of the specified cell.

procedure Change;

Call the OnChange event.

function DoesCellLive(const X : TGridWidthRange; const Y : TGridHeightRange; const Grid : TLifeGrid) : Boolean;

Applies the Rules of Life to determine if the specified cell should live or die.

procedure InvalidateCell(const X : TGridWidthRange; const Y : TGridHeightRange);

Invalidate the specified cell so that it is redrawn.

procedure MouseUp(Button : TMouseButton; Shift : TShiftState; X, Y : Integer);


procedure Paint;


procedure SetCell(const X : TGridWidthRange; const Y : TGridHeightRange; const Value : Boolean);

Set the value of a single cell.

function CellEdge(const Coordinate, Size, Divisions : Integer) : Integer;

This function calculates the left or top edge of a cell when supplied with the correct information.

function GetCell(const X : TGridWidthRange; const Y : TGridHeightRange) : Boolean;

Is the cell at the specified position alive?

function GetGridHeight : TPermissableGridHeight;


function GetGridWidth : TPermissableGridWidth;


function GetLiveCellCount : Integer;

Returns the number of "live" cells.

function GetMaxNumberOfHistoryLevels : Cardinal;


function GetVersion : String;


procedure SetAcceptMouseClicks(const Value : Boolean);


procedure SetCellColor(Const Value : TColor);


procedure SetCells(const X : TGridWidthRange; const Y : TGridHeightRange; const Value : Boolean);

Set the value of a single cell by calling SetCell.

procedure SetGridHeight(const NewGridHeight : TPermissableGridHeight);


procedure SetGridLineColor(Const Value : TColor);


procedure SetGridLineStyle(Const Value : TPenStyle);


procedure SetGridWidth(const NewGridWidth : TPermissableGridWidth);


procedure SetMaxNumberOfHistoryLevels(const Value : Cardinal);


procedure SetShowGridLines(const Value : Boolean);


Properties

property AcceptMouseClicks : Boolean


property Align :


property CellColor : TColor


property Color :


property DragCursor :


property DragMode :


property GridHeight : TPermissableGridHeight


property GridLineColor : TColor


property GridLineStyle : TPenStyle


property GridWidth : TPermissableGridWidth


property MaxNumberOfHistoryLevels : Cardinal


property ParentColor :


property PopupMenu :


property ShowGridLines : Boolean


property ShowHint :


property Visible :


property Cells : Boolean


property Generation : Cardinal


property History : TLifeHistory


property LiveCellCount : Integer


property Version : String


Events

event OnChange : TNotifyEvent


event OnClick :


event OnDblClick :


event OnDoesCellLive : TDoesCellLiveEvent


event OnDragDrop :


event OnDragOver :


event OnEndDrag :


event OnMouseDown :


event OnMouseMove :


event OnMouseUp :


event OnStartDrag :


Variables

FAcceptMouseClicks : Boolean;


FCellColor : TColor;


FGeneration : Cardinal;


FGrid : TLifeGrid;


FGridLineColor : TColor;


FGridLineStyle : TPenStyle;


FHistory : TLifeHistory;


FOnChange : TNotifyEvent;


FOnDoesCellLive : TDoesCellLiveEvent;


FShowGridLines : Boolean;



Constructors


constructor Create(aOwner : TComponent);


Functions


procedure ClearCells;

Mark all the cells as "dead".


destructor Destroy;


procedure LoadFromFile(const FileName : String);

Load a previous simulation from the named file.


procedure LoadFromStream(const Stream : TStream);

Load a previous simulation from the specified stream.


function NextGeneration : Cardinal;

Save the current state of FGrid to FHistory, then determine the new state of FGrid by applying the Rules of Life.


procedure ResetGeneration;

Reset the current Generation to zero


procedure SaveToFile(const FileName : String);

Save this simulation to the named file.


procedure SaveToStream(const Stream : TStream);

Save this simulation to the specified stream.


procedure SetGridSize(const NewGridWidth : TGridWidthRange; const NewGridHeight : TGridHeightRange);


function CellAtPos(const X, Y : Longint) : TPoint;

Returns the cell containing the specified pixel. See the CellCoords method for the reason why the mathematics are so complicated.


function CellCoords(const X : TGridWidthRange; const Y : TGridHeightRange) : TRect;

Returns the bounds of the specified cell. The mathematics for doing this are complicated by the fact that (a) we have to allow for the fact that the component dimensions will often not be an even multiple of the grid dimensions; and (b) we have to be able to reverse these calculations in the CellAtPos method.


procedure Change;

Call the OnChange event.


function DoesCellLive(const X : TGridWidthRange; const Y : TGridHeightRange; const Grid : TLifeGrid) : Boolean;

Applies the Rules of Life to determine if the specified cell should live or die.


procedure InvalidateCell(const X : TGridWidthRange; const Y : TGridHeightRange);

Invalidate the specified cell so that it is redrawn.


procedure MouseUp(Button : TMouseButton; Shift : TShiftState; X, Y : Integer);


procedure Paint;


procedure SetCell(const X : TGridWidthRange; const Y : TGridHeightRange; const Value : Boolean);

Set the value of a single cell. The area of the component relating to the cell is then invalidated so that the visible image correctly reflects the state of the cell.


function CellEdge(const Coordinate, Size, Divisions : Integer) : Integer;

This function calculates the left or top edge of a cell when supplied with the correct information. Coordinate is the cell position (x or y) that you wish to calculate; Size is the size of the component in the appropriate direction (ClientWidth or ClientHeight); and Divisions is the number of divisions in the appropriate direction (GridWidth or GridHeight).


function GetCell(const X : TGridWidthRange; const Y : TGridHeightRange) : Boolean;

Is the cell at the specified position alive?


function GetGridHeight : TPermissableGridHeight;


function GetGridWidth : TPermissableGridWidth;


function GetLiveCellCount : Integer;

Returns the number of "live" cells.


function GetMaxNumberOfHistoryLevels : Cardinal;


function GetVersion : String;


procedure SetAcceptMouseClicks(const Value : Boolean);


procedure SetCellColor(Const Value : TColor);


procedure SetCells(const X : TGridWidthRange; const Y : TGridHeightRange; const Value : Boolean);

Set the value of a single cell by calling SetCell. The Generation is then reset to 0, the history cleared and the Change method called. This is the method called by the Cells property.


procedure SetGridHeight(const NewGridHeight : TPermissableGridHeight);


procedure SetGridLineColor(Const Value : TColor);


procedure SetGridLineStyle(Const Value : TPenStyle);


procedure SetGridWidth(const NewGridWidth : TPermissableGridWidth);


procedure SetMaxNumberOfHistoryLevels(const Value : Cardinal);


procedure SetShowGridLines(const Value : Boolean);


Properties


property AcceptMouseClicks : Boolean


property Align :


property CellColor : TColor


property Color :


property DragCursor :


property DragMode :


property GridHeight : TPermissableGridHeight


property GridLineColor : TColor


property GridLineStyle : TPenStyle


property GridWidth : TPermissableGridWidth


property MaxNumberOfHistoryLevels : Cardinal


property ParentColor :


property PopupMenu :


property ShowGridLines : Boolean


property ShowHint :


property Visible :


property Cells : Boolean


property Generation : Cardinal


property History : TLifeHistory


property LiveCellCount : Integer


property Version : String


Events


event OnChange : TNotifyEvent


event OnClick :


event OnDblClick :


event OnDoesCellLive : TDoesCellLiveEvent


event OnDragDrop :


event OnDragOver :


event OnEndDrag :


event OnMouseDown :


event OnMouseMove :


event OnMouseUp :


event OnStartDrag :


Variables


FAcceptMouseClicks : Boolean;


FCellColor : TColor;


FGeneration : Cardinal;


FGrid : TLifeGrid;


FGridLineColor : TColor;


FGridLineStyle : TPenStyle;


FHistory : TLifeHistory;


FOnChange : TNotifyEvent;


FOnDoesCellLive : TDoesCellLiveEvent;


FShowGridLines : Boolean;