unit BGRATileEditor;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Graphics, BGRABitmap, BGRABitMapTypes, BGRALayers;
type
{ BGRATileEditor }
{ tBGRATileEditor }
tBGRATileEditor = class
//Variables, Procs, Funcs ONLY visible internally to this class
Private
//Is the Bitmap initialised?
bInit: boolean;
//The exploded view of the Tile where Editing Will Take Place
//will have layers so we can show/hide a grid
//tExplodedImg: TBGRALayeredBitmap;
tExplodedImg: TBGRABitMap;
tExpLayers: TBGRALayeredBitMap;
//Structure
iSize: integer; //Size of the window
iGridSize: integer; //Size of the grid
tGridCol: TBGRAPixel; //Colour for the grid
iCellSize: integer; //How Big is Each Cell to Paint/Display
iDrawX, iDrawY: integer;//Where to Draw
aDrawToCanvas: tCanvas; //Were to Draw To
//Editing Options
tPaintCol: TBGRAPixel; //Which colour are we painting with?
tEraseCol: TBGRAPixel; //Which colour is the Erase Color?
//Switches
bGridVisible: boolean;
//Vars, Procs, Funcs visible Outside of this class
Public
constructor Create(iSizeOfEditor, iNumBigPixels, iDrawAtX, iDrawAtY: integer; tCanvasToDrawTo: TCanvas);
//Draw the TileEditor
procedure Draw();
//Painting
procedure PaintCell(ixCell, iyCell: integer; tCol: TColor);
procedure EraseCell(ixCell, iyCell: integer);
//Grid Procs
procedure DrawGrid(); //CellSize = Big Pixel Size
//Is the Grid to be Visible?
Property Grid_Visible: boolean read bGridVisible write bGridVisible;
//Change Grid Color
procedure SetGridColor(iRed, iGreen, iBlue, iAlpha: integer);
Property Size: integer read iSize;
Property xPos: integer read iDrawX write iDrawX;
property yPos: integer read iDrawY write iDrawY;
property CellSize: integer read iCellSize;
end;
implementation
const
LYRBACK = 0;
LYRPAINT= 1;
LYRGRID = 2;
{ BGRATileEditor }
constructor tBGRATileEditor.Create(iSizeOfEditor, iNumBigPixels, iDrawAtX, iDrawAtY: integer; tCanvasToDrawTo: TCanvas);
begin
//Set Init to false to start
bInit:=false;
//Hold Draw Position
iDrawX:=iDrawAtX;
iDrawY:=iDrawAtY;
aDrawToCanvas:=tCanvasToDrawTo;
//Set an initial Grid Colour
tGridCol:=BGRA(0,255,0,255);
tEraseCol:=BGRA(255,255,255,0);
//Create the Bitmap that we will draw our exploded view to
if (iSizeOfEditor < 64) or (iSizeOfEditor > 512) then
exit;
iSize:=iSizeOfEditor;
tExplodedImg:=tBGRABitMap.Create(iSize, iSize, BGRAPixelTransparent );
//Draw Transparent BackGround
tExplodedImg.DrawCheckers(rect(0,0,iSize, iSize), BGRA(100,100,100,255), BGRA(200,200,200,255));
bInit:=true;
bGridVisible:=false;
iCellSize:=iSize div iNumBigPixels;
//DEBUG - Trying Layers
tExpLayers:=TBGRALayeredBitMap.Create(20,20);
//a Checkered Background
tExpLayers.AddLayer('BackGrnd',TBGRABitMap.Create(iSize, iSize, BGRAPixelTransparent), 0);
tExpLayers.LayerBitmap[LYRBACK].DrawCheckers(rect(0,0,iSize, iSize), BGRA(225,228,234,255), BGRA(183,186,191,255));
//The Layer we paint To
tExpLayers.AddLayer('Paint',TBGRABitmap.Create(iSize, iSize, BGRAPixelTransparent), 0);
//The Grid Layer
tExpLayers.AddLayer('Grid',TBGRABitmap.Create(iSize, iSize, BGRAPixelTransparent), 0);
//DEBUG - Test Layers
DrawGrid();
Draw();
end;
procedure tBGRATileEditor.Draw();
begin
//Are we initialised?
if bInit = false then
//No Jump Out
exit;
//tExplodedImg.Draw(CanvasToDrawTo, ixPos, iyPos, false);
if bGridVisible = true then
begin
tExpLayers.LayerBitmap[LYRBACK].Draw(aDrawToCanvas, iDrawX, iDrawY, false);
tExpLayers.LayerBitmap[LYRPAINT].Draw(aDrawToCanvas, iDrawX, iDrawY, false);
tExplayers.LayerBitmap[LYRGRID].Draw(aDrawToCanvas, iDrawX, iDrawY, false);
end
else
begin
tExpLayers.LayerBitmap[LYRBACK].Draw(aDrawToCanvas, iDrawX, iDrawY, false);
tExpLayers.LayerBitmap[LYRPAINT].Draw(aDrawToCanvas, iDrawX, iDrawY, false);
end;
end;
procedure tBGRATileEditor.PaintCell(ixCell, iyCell: integer; tCol: TColor); overload;
var
pRect: TRect;
aPixel: TBGRAPixel;
begin
//Are we intialised?
if (bInit = false) then
//No - Jump Out
exit;
aPixel.red:=Red(tCol);
aPixel.green:=Green(tCol);
aPixel.blue:=Blue(tCol);
//DEBUG Set Alpha Here - Ideally Allow User to Change
aPixel.alpha:=255;
pRect.Left:=ixCell * iCellSize;
pRect.Top:=iyCell * iCellSize;
pRect.Width:=iCellSize;
pREct.Height:=iCellSize;
//Bounds Checking
if (pRect.Left >=0) and (pRect.Left+ pRect.Width <= iSize) and (pRect.Top >=0) and (pRect.Top+pRect.Height <= iSize) then
begin
tExpLayers.LayerBitmap[LYRPAINT].FillRect(pRect, aPixel, tDrawMode.dmDrawWithTransparency);
//tExpLayers.LayerBitmap[LYRPAINT].Canvas2d.fillStyle(aPixel);
//tExpLayers.LayerBitmap[LYRPAINT].Canvas2d.fillRect(pRect.Left, pRect.Top, iCellSize, iCellSize);
Draw();
end;
end;
procedure tBGRATileEditor.EraseCell(ixCell, iyCell: integer); overload;
var
pRect: TRect;
aPixel: TBGRAPixel;
begin
pRect.Left:=ixCell * iCellSize;
pRect.Top:=iyCell * iCellSize;
pRect.Width:=iCellSize;
pREct.Height:=iCellSize;
//Bounds Checking
if (pRect.Left >=0) and (pRect.Left+ pRect.Width <= iSize) and (pRect.Top >=0) and (pRect.Top+pRect.Height <= iSize) then
begin
tExpLayers.LayerBitmap[LYRPAINT].EraseRectAntialias(pRect.Left, pRect.Top, pRect.Right, pRect.Bottom,255);
Draw();
end;
end;
procedure tBGRATileEditor.DrawGrid();
var
iCount: integer;
ixPos, iyPos: integer;
iGridLayer: integer;
begin
//Are we intialised?
if (bInit = false) then
//No - Jump Out
exit;
ixPos:=0;
for iCount:=1 to tExpLayers.LayerBitmap[1].Width div iCellSize do
begin;
ixPos:=iCount * iCellSize;
tExpLayers.LayerBitmap[LYRGRID].DrawLine(ixPos,0,ixPos, tExplodedImg.Height, tGridCol, true, TDrawMode.dmDrawWithTransparency );
end;
//Draw Horizontals
iyPos:=0;
for iCount:=1 to tExplodedImg.Height div iCellSize do
begin
iyPos:=iCount*iCellSize;
tExpLayers.LayerBitmap[LYRGRID].DrawLine(0, iyPos, tExplodedImg.Width, iyPos, tGridCol, true, TDrawMode.dmDrawWithTransparency );
end;
end;
procedure tBGRATileEditor.SetGridColor(iRed, iGreen, iBlue, iAlpha: integer);
begin
tGridCol.red:=iRed;
tGridCol.green:=iGreen;
tGridCol.blue:=iBlue;
tGridCol.alpha:=iAlpha;
DrawGrid();
end;
end.