Added: support for custom drawing procedures
This commit is contained in:
parent
18b17e3487
commit
2ac4754fa6
@ -24,6 +24,7 @@ uses
|
||||
{$IFDEF VER150}
|
||||
{$WARN UNSAFE_CODE OFF}
|
||||
{$WARN UNSAFE_CAST OFF}
|
||||
{$WARN UNSAFE_TYPE OFF}
|
||||
{$ENDIF}
|
||||
|
||||
|
||||
@ -32,6 +33,13 @@ type
|
||||
TX2GraphicList = class;
|
||||
TX2GraphicContainer = class;
|
||||
|
||||
|
||||
TX2GLCustomDrawImageProc = function(ACanvas: TCanvas;
|
||||
AGraphicList: TX2GraphicList;
|
||||
AIndex: Integer;
|
||||
AX, AY: Integer;
|
||||
AEnabled: Boolean): Boolean;
|
||||
|
||||
{
|
||||
:$ Holds a single graphic.
|
||||
}
|
||||
@ -190,6 +198,11 @@ type
|
||||
property StretchMode: TX2GLStretchMode read FStretchMode write SetStretchMode default smCrop;
|
||||
end;
|
||||
|
||||
|
||||
procedure X2GLRegisterCustomDrawImageProc(ACustomDrawImageProc: TX2GLCustomDrawImageProc);
|
||||
procedure X2GLUnregisterCustomDrawImageProc(ACustomDrawImageProc: TX2GLCustomDrawImageProc);
|
||||
|
||||
|
||||
implementation
|
||||
uses
|
||||
Forms,
|
||||
@ -197,6 +210,10 @@ uses
|
||||
SysUtils;
|
||||
|
||||
|
||||
var
|
||||
CustomDrawImageProcs: TList;
|
||||
|
||||
|
||||
type
|
||||
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
|
||||
Initialization
|
||||
========================================}
|
||||
@ -900,20 +958,23 @@ begin
|
||||
(FContainer.Graphics[AIndex].Picture.Graphic.Empty) then
|
||||
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
|
||||
// Enabled, simply draw the graphic
|
||||
{ Enabled, simply draw the graphic }
|
||||
InternalDrawGraphic(ACanvas, AX, AY)
|
||||
else
|
||||
begin
|
||||
// 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
|
||||
// generic way of determining a pixel's transparency. So instead, we
|
||||
// blend the background with a copy of the background with the graphic
|
||||
// painted on it...
|
||||
{ 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
|
||||
generic way of determining a pixel's transparency. So instead, we
|
||||
blend the background with a copy of the background with the graphic
|
||||
painted on it... }
|
||||
bmpBackground := TBitmap.Create();
|
||||
bmpBlend := TBitmap.Create();
|
||||
try
|
||||
// Get background from canvas
|
||||
{ Get background from canvas }
|
||||
with bmpBackground do
|
||||
begin
|
||||
Width := Self.Width;
|
||||
@ -926,7 +987,7 @@ begin
|
||||
bmpBlend.Assign(bmpBackground);
|
||||
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
|
||||
begin
|
||||
pBackground := bmpBackground.ScanLine[iY];
|
||||
@ -944,13 +1005,14 @@ begin
|
||||
end;
|
||||
end;
|
||||
|
||||
// Copy blended graphic back
|
||||
{ Copy blended graphic back }
|
||||
ACanvas.Draw(AX, AY, bmpBlend);
|
||||
finally
|
||||
FreeAndNil(bmpBlend);
|
||||
FreeAndNil(bmpBackground);
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
Result := True;
|
||||
end;
|
||||
@ -1338,5 +1400,9 @@ end;
|
||||
|
||||
initialization
|
||||
RegisterClass(TX2GraphicContainerItem);
|
||||
CustomDrawImageProcs := TList.Create;
|
||||
|
||||
finalization
|
||||
FreeAndNil(CustomDrawImageProcs);
|
||||
|
||||
end.
|
||||
|
Loading…
Reference in New Issue
Block a user