-
Notifications
You must be signed in to change notification settings - Fork 5
/
fxgraphiccontrol.pas
199 lines (171 loc) Β· 4.54 KB
/
fxgraphiccontrol.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
unit FXGraphicControl;
{$mode objfpc}{$H+}{$MODESWITCH ADVANCEDRECORDS}
interface
uses
Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs,
BGRABitmap, BGRABitmapTypes, BGRAOpenGL, FXContainer, FGL;
type
{ TFXLayer }
TFXLayer = class
private
FTexture: IBGLTexture;
FBGRA: TBGRABitmap;
FColor: TBGRAPixel;
procedure SetFBGRA(AValue: TBGRABitmap);
procedure SetFColor(AValue: TBGRAPixel);
procedure SetFTexture(AValue: IBGLTexture);
public
constructor Create;
destructor Destroy; override;
property Texture: IBGLTexture read FTexture write SetFTexture;
property BGRA: TBGRABitmap read FBGRA write SetFBGRA;
property Color: TBGRAPixel read FColor write SetFColor;
end;
TFXLayers = specialize TFPGObjectList<TFXLayer>;
{ TFXGraphicControl }
TFXGraphicControl = class(TGraphicControl, IFXDrawable)
protected
FXLayers: TFXLayers;
protected
procedure FXDraw; virtual;
procedure FXPreview(aCanvas: TCanvas);
procedure Draw; virtual;
procedure Paint; override;
procedure DrawWithColor(Source: TBGRABitmap; c: TBGRAPixel;
Dest: TCanvas; x, y: integer; Opaque: boolean = True);
procedure Colorize(Source, Dest: TBGRABitmap; c: TBGRAPixel);
public
constructor Create(TheOwner: TComponent); override;
destructor Destroy; override;
end;
implementation
{ TFXLayer }
procedure TFXLayer.SetFTexture(AValue: IBGLTexture);
begin
if FTexture = AValue then
Exit;
FTexture := AValue;
end;
constructor TFXLayer.Create;
begin
inherited Create;
FBGRA := TBGRABitmap.Create;
FColor := BGRAWhite;
end;
destructor TFXLayer.Destroy;
begin
if Assigned(FBGRA) then
FBGRA.Free;
FTexture := nil;
inherited Destroy;
end;
procedure TFXLayer.SetFBGRA(AValue: TBGRABitmap);
begin
if FBGRA = AValue then
Exit;
FBGRA := AValue;
end;
procedure TFXLayer.SetFColor(AValue: TBGRAPixel);
begin
if FColor = AValue then
Exit;
FColor := AValue;
end;
{ TFXGraphicControl }
procedure TFXGraphicControl.FXDraw;
var
i: integer;
begin
Draw;
for i := 0 to FXLayers.Count - 1 do
begin
if (FXLayers[i].Texture = nil) then
FXLayers[i].Texture := BGLTexture(FXLayers[i].BGRA);
BGLCanvas.PutImage(Left, Top, FXLayers[i].Texture, FXLayers[i].Color);
end;
end;
procedure TFXGraphicControl.FXPreview(aCanvas: TCanvas);
var
i: integer;
begin
Draw;
for i := 0 to FXLayers.Count - 1 do
begin
if FXLayers[i].Color <> BGRAWhite then
DrawWithColor(FXLayers[i].BGRA, FXLayers[i].Color, aCanvas, Left, Top, False)
else
FXLayers[i].BGRA.Draw(aCanvas, Left, Top, False);
end;
end;
procedure TFXGraphicControl.Draw;
begin
if (FXLayers[0].BGRA.Width <> Width) or (FXLayers[0].BGRA.Height <> Height) then
FXLayers[0].BGRA.SetSize(Width, Height);
FXLayers[0].BGRA.FillTransparent;
FXLayers[0].Texture := nil;
end;
procedure TFXGraphicControl.Paint;
var
i: integer;
begin
if csLoading in ComponentState then
exit;
if Parent is TFXContainer then
Parent.Invalidate
else
begin
Draw;
for i := 0 to FXLayers.Count - 1 do
begin
if FXLayers[i].Color <> BGRAWhite then
DrawWithColor(FXLayers[i].BGRA, FXLayers[i].Color, Canvas, 0, 0, False)
else
FXLayers[i].BGRA.Draw(Canvas, 0, 0, False);
end;
end;
end;
procedure TFXGraphicControl.DrawWithColor(Source: TBGRABitmap;
c: TBGRAPixel; Dest: TCanvas; x, y: integer; Opaque: boolean = True);
var
temp: TBGRABitmap;
begin
temp := TBGRABitmap.Create(Source.Width, Source.Height);
Colorize(Source, temp, c);
temp.Draw(Dest, x, y, Opaque);
temp.Free;
end;
procedure TFXGraphicControl.Colorize(Source, Dest: TBGRABitmap; c: TBGRAPixel);
var
psource: PBGRAPixel;
pdest: PBGRAPixel;
ec: TExpandedPixel;
n: integer;
begin
psource := Source.Data;
pdest := Dest.Data;
ec := GammaExpansion(c);
for n := Source.NbPixels - 1 downto 0 do
begin
pdest^.red := GammaCompressionTab[
((GammaExpansionTab[psource^.red] * ec.red + 65535) shr 16)];
pdest^.green := GammaCompressionTab[
((GammaExpansionTab[psource^.green] * ec.green + 65535) shr 16)];
pdest^.blue := GammaCompressionTab[
((GammaExpansionTab[psource^.blue] * ec.blue + 65535) shr 16)];
pdest^.alpha := (psource^.alpha * ec.alpha + 255) shr 16;
Inc(pdest);
Inc(psource);
end;
end;
constructor TFXGraphicControl.Create(TheOwner: TComponent);
begin
inherited Create(TheOwner);
FXLayers := TFXLayers.Create;
FXLayers.Add(TFXLayer.Create);
end;
destructor TFXGraphicControl.Destroy;
begin
FreeAndNil(FXLayers);
inherited Destroy;
end;
end.