1
0
mirror of synced 2024-11-22 01:53:50 +00:00

Added: initial implementation for disabled drawing

This commit is contained in:
Mark van Renswoude 2004-08-31 15:02:35 +00:00
parent 86f3b26b31
commit b65af473ce

View File

@ -108,29 +108,45 @@ type
private private
FBackground: TColor; FBackground: TColor;
FContainer: TX2GraphicContainer; FContainer: TX2GraphicContainer;
FEnabled: Boolean;
FStretchMode: TX2GLStretchMode; FStretchMode: TX2GLStretchMode;
FUpdateCount: Integer;
procedure SetBackground(const Value: TColor); procedure SetBackground(const Value: TColor);
procedure SetContainer(const Value: TX2GraphicContainer); procedure SetContainer(const Value: TX2GraphicContainer);
procedure SetStretchMode(const Value: TX2GLStretchMode); procedure SetStretchMode(const Value: TX2GLStretchMode);
procedure SetEnabled(const Value: Boolean);
protected protected
procedure DefineProperties(Filer: TFiler); override; procedure DefineProperties(Filer: TFiler); override;
procedure Notification(AComponent: TComponent; Operation: TOperation); override; procedure Notification(AComponent: TComponent; Operation: TOperation); override;
function DrawGraphic(const AIndex: Integer;
const ACanvas: TCanvas;
const AX, AY: Integer;
const AEnabled: Boolean = True): Boolean;
procedure DoDraw(Index: Integer; Canvas: TCanvas; X, Y: Integer;
Style: Cardinal; Enabled: Boolean = True); override;
procedure CreateImage(const AIndex: Integer; var AImage, AMask: TBitmap); virtual; procedure CreateImage(const AIndex: Integer; var AImage, AMask: TBitmap); virtual;
procedure AddImage(const AIndex: Integer); virtual; procedure AddImage(const AIndex: Integer); virtual;
procedure UpdateImage(const AIndex: Integer); virtual; procedure UpdateImage(const AIndex: Integer); virtual;
procedure DeleteImage(const AIndex: Integer); virtual; procedure DeleteImage(const AIndex: Integer); virtual;
procedure RebuildImages(); virtual; procedure RebuildImages(); virtual;
procedure BeginUpdate();
procedure EndUpdate();
public public
constructor Create(AOwner: TComponent); override; constructor Create(AOwner: TComponent); override;
destructor Destroy(); override; destructor Destroy(); override;
procedure Loaded(); override; procedure Loaded(); override;
procedure Change(); override;
published published
property Background: TColor read FBackground write SetBackground default clBtnFace; property Background: TColor read FBackground write SetBackground default clBtnFace;
property Container: TX2GraphicContainer read FContainer write SetContainer; property Container: TX2GraphicContainer read FContainer write SetContainer;
property Enabled: Boolean read FEnabled write SetEnabled;
property StretchMode: TX2GLStretchMode read FStretchMode write SetStretchMode default smCrop; property StretchMode: TX2GLStretchMode read FStretchMode write SetStretchMode default smCrop;
end; end;
@ -345,6 +361,14 @@ begin
RebuildImages(); RebuildImages();
end; end;
procedure TX2GraphicList.Change;
begin
inherited;
if FUpdateCount = 0 then
RebuildImages();
end;
destructor TX2GraphicList.Destroy; destructor TX2GraphicList.Destroy;
begin begin
@ -358,21 +382,16 @@ end;
{========================= TX2GraphicList {========================= TX2GraphicList
Graphics Graphics
========================================} ========================================}
procedure TX2GraphicList.CreateImage; function TX2GraphicList.DrawGraphic;
function DrawGraphic(const ADest: TCanvas; const AIndex: Integer): Boolean; procedure InternalDrawGraphic(const ADest: TCanvas);
var var
rDest: TRect; rDest: TRect;
begin begin
Result := False;
if not Assigned(FContainer.Graphics[AIndex].Picture) then
exit;
with FContainer.Graphics[AIndex].Picture do with FContainer.Graphics[AIndex].Picture do
begin
if (FStretchMode = smCrop) or if (FStretchMode = smCrop) or
((Width <= Self.Width) and (Height <= Self.Height)) then ((Width <= Self.Width) and (Height <= Self.Height)) then
ADest.Draw(0, 0, Graphic) ADest.Draw(AX, AY, Graphic)
else else
begin begin
rDest := Rect(0, 0, Width, Height); rDest := Rect(0, 0, Width, Height);
@ -384,11 +403,65 @@ procedure TX2GraphicList.CreateImage;
ADest.StretchDraw(rDest, Graphic); ADest.StretchDraw(rDest, Graphic);
end; end;
end;
Result := True;
end; end;
var
bmpBackground: TBitmap;
bmpBlend: TBitmap;
begin
Result := False;
if not Assigned(FContainer) then
exit;
if FContainer.Graphics[AIndex].Picture.Graphic.Empty then
exit;
if AEnabled then
// Enabled, simply draw the graphic
InternalDrawGraphic(ACanvas)
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...
bmpBackground := TBitmap.Create();
bmpBlend := TBitmap.Create();
try
// Get background from canvas
with bmpBackground do
begin
Width := Self.Width;
Height := Self.Height;
PixelFormat := pf24bit;
Canvas.CopyRect(Rect(0, 0, Width, Height), ACanvas,
Rect(AX, AY, AX + Width, AY + Height));
end;
bmpBlend.Assign(bmpBackground);
InternalDrawGraphic(bmpBlend.Canvas);
// TODO Blend graphic with background
// TODO Copy blended graphic back
finally
FreeAndNil(bmpBlend);
FreeAndNil(bmpBackground);
end;
end;
Result := True;
end;
procedure TX2GraphicList.DoDraw;
begin
DrawGraphic(Index, Canvas, X, Y, Enabled);
end;
procedure TX2GraphicList.CreateImage;
function RGBTriple(const AColor: TColor): TRGBTriple; function RGBTriple(const AColor: TColor): TRGBTriple;
var var
cColor: Cardinal; cColor: Cardinal;
@ -442,7 +515,7 @@ begin
begin begin
Brush.Color := FBackground; Brush.Color := FBackground;
FillRect(Rect(0, 0, Width, Height)); FillRect(Rect(0, 0, Width, Height));
bOk := DrawGraphic(Canvas, AIndex); bOk := DrawGraphic(AIndex, Canvas, 0, 0, FEnabled);
end; end;
end; end;
@ -476,7 +549,7 @@ begin
begin begin
Brush.Color := clBlack; Brush.Color := clBlack;
FillRect(Rect(0, 0, Width, Height)); FillRect(Rect(0, 0, Width, Height));
DrawGraphic(Canvas, AIndex); DrawGraphic(AIndex, Canvas, 0, 0, FEnabled);
end; end;
end; end;
@ -526,19 +599,24 @@ begin
if csLoading in ComponentState then if csLoading in ComponentState then
exit; exit;
bmpImage := TBitmap.Create(); BeginUpdate();
bmpMask := TBitmap.Create();
try try
CreateImage(AIndex, bmpImage, bmpMask); bmpImage := TBitmap.Create();
Assert(AIndex <= Self.Count, 'AAAH! Images out of sync! *panics*'); bmpMask := TBitmap.Create();
try
CreateImage(AIndex, bmpImage, bmpMask);
Assert(AIndex <= Self.Count, 'AAAH! Images out of sync! *panics*');
if AIndex = Self.Count then if AIndex = Self.Count then
Add(bmpImage, bmpMask) Add(bmpImage, bmpMask)
else else
Insert(AIndex, bmpImage, bmpMask); Insert(AIndex, bmpImage, bmpMask);
finally
FreeAndNil(bmpMask);
FreeAndNil(bmpImage);
end;
finally finally
FreeAndNil(bmpMask); EndUpdate();
FreeAndNil(bmpImage);
end; end;
end; end;
@ -551,20 +629,30 @@ begin
if csLoading in ComponentState then if csLoading in ComponentState then
exit; exit;
bmpImage := TBitmap.Create(); BeginUpdate();
bmpMask := TBitmap.Create();
try try
CreateImage(AIndex, bmpImage, bmpMask); bmpImage := TBitmap.Create();
Replace(AIndex, bmpImage, bmpMask); bmpMask := TBitmap.Create();
try
CreateImage(AIndex, bmpImage, bmpMask);
Replace(AIndex, bmpImage, bmpMask);
finally
FreeAndNil(bmpMask);
FreeAndNil(bmpImage);
end;
finally finally
FreeAndNil(bmpMask); EndUpdate();
FreeAndNil(bmpImage);
end; end;
end; end;
procedure TX2GraphicList.DeleteImage; procedure TX2GraphicList.DeleteImage;
begin begin
Delete(AIndex); BeginUpdate();
try
Delete(AIndex);
finally
EndUpdate();
end;
end; end;
@ -577,13 +665,18 @@ begin
(Width = 0) or (Height = 0) then (Width = 0) or (Height = 0) then
exit; exit;
Clear(); BeginUpdate();
try
Clear();
if not Assigned(FContainer) then if not Assigned(FContainer) then
exit; exit;
for iIndex := 0 to FContainer.Graphics.Count - 1 do for iIndex := 0 to FContainer.Graphics.Count - 1 do
AddImage(iIndex); AddImage(iIndex);
finally
EndUpdate();
end;
end; end;
@ -640,10 +733,28 @@ begin
RebuildImages(); RebuildImages();
end; end;
procedure TX2GraphicList.SetEnabled;
begin
FEnabled := Value;
RebuildImages();
end;
procedure TX2GraphicList.SetStretchMode; procedure TX2GraphicList.SetStretchMode;
begin begin
FStretchMode := Value; FStretchMode := Value;
RebuildImages(); RebuildImages();
end; end;
procedure TX2GraphicList.BeginUpdate;
begin
Inc(FUpdateCount);
end;
procedure TX2GraphicList.EndUpdate;
begin
Assert(FUpdateCount > 0, 'EndUpdate without matching BeginUpdate!');
Dec(FUpdateCount);
end;
end. end.