Added: support for custom drawing procedures
This commit is contained in:
parent
18b17e3487
commit
2ac4754fa6
@ -24,6 +24,7 @@ uses
|
|||||||
{$IFDEF VER150}
|
{$IFDEF VER150}
|
||||||
{$WARN UNSAFE_CODE OFF}
|
{$WARN UNSAFE_CODE OFF}
|
||||||
{$WARN UNSAFE_CAST OFF}
|
{$WARN UNSAFE_CAST OFF}
|
||||||
|
{$WARN UNSAFE_TYPE OFF}
|
||||||
{$ENDIF}
|
{$ENDIF}
|
||||||
|
|
||||||
|
|
||||||
@ -32,6 +33,13 @@ type
|
|||||||
TX2GraphicList = class;
|
TX2GraphicList = class;
|
||||||
TX2GraphicContainer = class;
|
TX2GraphicContainer = class;
|
||||||
|
|
||||||
|
|
||||||
|
TX2GLCustomDrawImageProc = function(ACanvas: TCanvas;
|
||||||
|
AGraphicList: TX2GraphicList;
|
||||||
|
AIndex: Integer;
|
||||||
|
AX, AY: Integer;
|
||||||
|
AEnabled: Boolean): Boolean;
|
||||||
|
|
||||||
{
|
{
|
||||||
:$ Holds a single graphic.
|
:$ Holds a single graphic.
|
||||||
}
|
}
|
||||||
@ -190,6 +198,11 @@ type
|
|||||||
property StretchMode: TX2GLStretchMode read FStretchMode write SetStretchMode default smCrop;
|
property StretchMode: TX2GLStretchMode read FStretchMode write SetStretchMode default smCrop;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
||||||
|
procedure X2GLRegisterCustomDrawImageProc(ACustomDrawImageProc: TX2GLCustomDrawImageProc);
|
||||||
|
procedure X2GLUnregisterCustomDrawImageProc(ACustomDrawImageProc: TX2GLCustomDrawImageProc);
|
||||||
|
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
uses
|
uses
|
||||||
Forms,
|
Forms,
|
||||||
@ -197,6 +210,10 @@ uses
|
|||||||
SysUtils;
|
SysUtils;
|
||||||
|
|
||||||
|
|
||||||
|
var
|
||||||
|
CustomDrawImageProcs: TList;
|
||||||
|
|
||||||
|
|
||||||
type
|
type
|
||||||
PClass = ^TClass;
|
PClass = ^TClass;
|
||||||
|
|
||||||
@ -223,6 +240,47 @@ type
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
procedure X2GLRegisterCustomDrawImageProc(ACustomDrawImageProc: TX2GLCustomDrawImageProc);
|
||||||
|
var
|
||||||
|
procPointer: Pointer absolute ACustomDrawImageProc;
|
||||||
|
|
||||||
|
begin
|
||||||
|
if CustomDrawImageProcs.IndexOf(procPointer) = -1 then
|
||||||
|
CustomDrawImageProcs.Add(procPointer);
|
||||||
|
end;
|
||||||
|
|
||||||
|
|
||||||
|
procedure X2GLUnregisterCustomDrawImageProc(ACustomDrawImageProc: TX2GLCustomDrawImageProc);
|
||||||
|
var
|
||||||
|
procPointer: Pointer absolute ACustomDrawImageProc;
|
||||||
|
|
||||||
|
begin
|
||||||
|
CustomDrawImageProcs.Remove(procPointer);
|
||||||
|
end;
|
||||||
|
|
||||||
|
|
||||||
|
function CustomDrawImage(ACanvas: TCanvas; AGraphicList: TX2GraphicList;
|
||||||
|
AIndex: Integer; AX, AY: Integer; AEnabled: Boolean): Boolean;
|
||||||
|
var
|
||||||
|
customProcIndex: Integer;
|
||||||
|
customProc: TX2GLCustomDrawImageProc;
|
||||||
|
|
||||||
|
begin
|
||||||
|
Result := False;
|
||||||
|
|
||||||
|
for customProcIndex := Pred(CustomDrawImageProcs.Count) downto 0 do
|
||||||
|
begin
|
||||||
|
customProc := TX2GLCustomDrawImageProc(CustomDrawImageProcs[customProcIndex]);
|
||||||
|
|
||||||
|
if customProc(ACanvas, AGraphicList, AIndex, AX, AY, AEnabled) then
|
||||||
|
begin
|
||||||
|
Result := True;
|
||||||
|
Break;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
|
||||||
{================ TX2GraphicContainerItem
|
{================ TX2GraphicContainerItem
|
||||||
Initialization
|
Initialization
|
||||||
========================================}
|
========================================}
|
||||||
@ -900,20 +958,23 @@ begin
|
|||||||
(FContainer.Graphics[AIndex].Picture.Graphic.Empty) then
|
(FContainer.Graphics[AIndex].Picture.Graphic.Empty) then
|
||||||
exit;
|
exit;
|
||||||
|
|
||||||
|
{ First see if any custom draw handlers want to draw the image }
|
||||||
|
if not CustomDrawImage(ACanvas, Self, AIndex, AX, AY, AEnabled) then
|
||||||
|
begin
|
||||||
if AEnabled then
|
if AEnabled then
|
||||||
// Enabled, simply draw the graphic
|
{ Enabled, simply draw the graphic }
|
||||||
InternalDrawGraphic(ACanvas, AX, AY)
|
InternalDrawGraphic(ACanvas, AX, AY)
|
||||||
else
|
else
|
||||||
begin
|
begin
|
||||||
// Disabled, need to draw the image using 50% transparency. There's only
|
{ Disabled, need to draw the image using 50% transparency. There's only
|
||||||
// one problem; not all TGraphic's support that, and neither is there a
|
one problem; not all TGraphic's support that, and neither is there a
|
||||||
// generic way of determining a pixel's transparency. So instead, we
|
generic way of determining a pixel's transparency. So instead, we
|
||||||
// blend the background with a copy of the background with the graphic
|
blend the background with a copy of the background with the graphic
|
||||||
// painted on it...
|
painted on it... }
|
||||||
bmpBackground := TBitmap.Create();
|
bmpBackground := TBitmap.Create();
|
||||||
bmpBlend := TBitmap.Create();
|
bmpBlend := TBitmap.Create();
|
||||||
try
|
try
|
||||||
// Get background from canvas
|
{ Get background from canvas }
|
||||||
with bmpBackground do
|
with bmpBackground do
|
||||||
begin
|
begin
|
||||||
Width := Self.Width;
|
Width := Self.Width;
|
||||||
@ -926,7 +987,7 @@ begin
|
|||||||
bmpBlend.Assign(bmpBackground);
|
bmpBlend.Assign(bmpBackground);
|
||||||
InternalDrawGraphic(bmpBlend.Canvas, 0, 0);
|
InternalDrawGraphic(bmpBlend.Canvas, 0, 0);
|
||||||
|
|
||||||
// Blend graphic with background at 50%
|
{ Blend graphic with background at 50% }
|
||||||
for iY := 0 to bmpBackground.Height - 1 do
|
for iY := 0 to bmpBackground.Height - 1 do
|
||||||
begin
|
begin
|
||||||
pBackground := bmpBackground.ScanLine[iY];
|
pBackground := bmpBackground.ScanLine[iY];
|
||||||
@ -944,13 +1005,14 @@ begin
|
|||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
// Copy blended graphic back
|
{ Copy blended graphic back }
|
||||||
ACanvas.Draw(AX, AY, bmpBlend);
|
ACanvas.Draw(AX, AY, bmpBlend);
|
||||||
finally
|
finally
|
||||||
FreeAndNil(bmpBlend);
|
FreeAndNil(bmpBlend);
|
||||||
FreeAndNil(bmpBackground);
|
FreeAndNil(bmpBackground);
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
Result := True;
|
Result := True;
|
||||||
end;
|
end;
|
||||||
@ -1338,5 +1400,9 @@ end;
|
|||||||
|
|
||||||
initialization
|
initialization
|
||||||
RegisterClass(TX2GraphicContainerItem);
|
RegisterClass(TX2GraphicContainerItem);
|
||||||
|
CustomDrawImageProcs := TList.Create;
|
||||||
|
|
||||||
|
finalization
|
||||||
|
FreeAndNil(CustomDrawImageProcs);
|
||||||
|
|
||||||
end.
|
end.
|
||||||
|
Loading…
Reference in New Issue
Block a user