| 1 | unit BGRAFlashProgressBar;
|
|---|
| 2 |
|
|---|
| 3 | {$mode objfpc}{$H+}
|
|---|
| 4 |
|
|---|
| 5 | interface
|
|---|
| 6 |
|
|---|
| 7 | uses
|
|---|
| 8 | Classes, SysUtils, LResources, LMessages, Forms, Controls, Graphics,
|
|---|
| 9 | Dialogs, BGRABitmap;
|
|---|
| 10 |
|
|---|
| 11 | type
|
|---|
| 12 |
|
|---|
| 13 | { TBGRAFlashProgressBar }
|
|---|
| 14 |
|
|---|
| 15 | TBGRAFlashProgressBar = class(TGraphicControl)
|
|---|
| 16 | private
|
|---|
| 17 | FMaxValue: integer;
|
|---|
| 18 | FMinValue: integer;
|
|---|
| 19 | FValue: integer;
|
|---|
| 20 | FBmp: TBGRABitmap;
|
|---|
| 21 | FRandSeed: integer;
|
|---|
| 22 | procedure SetMaxValue(const AValue: integer);
|
|---|
| 23 | procedure SetMinValue(const AValue: integer);
|
|---|
| 24 | procedure SetValue(const AValue: integer);
|
|---|
| 25 | { Private declarations }
|
|---|
| 26 | protected
|
|---|
| 27 | { Protected declarations }
|
|---|
| 28 | procedure CalculatePreferredSize(var PreferredWidth, PreferredHeight: integer;
|
|---|
| 29 | WithThemeSpace: boolean); override;
|
|---|
| 30 | procedure Paint; override;
|
|---|
| 31 | procedure WMEraseBkgnd(var Message: TLMEraseBkgnd); message LM_ERASEBKGND;
|
|---|
| 32 | public
|
|---|
| 33 | { Public declarations }
|
|---|
| 34 | constructor Create(AOwner: TComponent); override;
|
|---|
| 35 | destructor Destroy; override;
|
|---|
| 36 | published
|
|---|
| 37 | { Published declarations }
|
|---|
| 38 | property Align;
|
|---|
| 39 | property Anchors;
|
|---|
| 40 | property MinValue: integer Read FMinValue Write SetMinValue;
|
|---|
| 41 | property MaxValue: integer Read FMaxValue Write SetMaxValue;
|
|---|
| 42 | property Value: integer Read FValue Write SetValue;
|
|---|
| 43 | property OnClick;
|
|---|
| 44 | property OnMouseDown;
|
|---|
| 45 | property OnMouseEnter;
|
|---|
| 46 | property OnMouseLeave;
|
|---|
| 47 | property OnMouseMove;
|
|---|
| 48 | property OnMouseUp;
|
|---|
| 49 | property Color;
|
|---|
| 50 | end;
|
|---|
| 51 |
|
|---|
| 52 | procedure Register;
|
|---|
| 53 |
|
|---|
| 54 | implementation
|
|---|
| 55 |
|
|---|
| 56 | uses BGRABitmapTypes, BGRAGradients, Types;
|
|---|
| 57 |
|
|---|
| 58 | procedure Register;
|
|---|
| 59 | begin
|
|---|
| 60 | {$I bgraflashprogressbar_icon.lrs}
|
|---|
| 61 | RegisterComponents('BGRA Controls', [TBGRAFlashProgressBar]);
|
|---|
| 62 | end;
|
|---|
| 63 |
|
|---|
| 64 | { TBGRAFlashProgressBar }
|
|---|
| 65 |
|
|---|
| 66 | procedure TBGRAFlashProgressBar.SetMinValue(const AValue: integer);
|
|---|
| 67 | begin
|
|---|
| 68 | if FMinValue = AValue then
|
|---|
| 69 | exit;
|
|---|
| 70 | FMinValue := AValue;
|
|---|
| 71 | if FValue < FMinValue then
|
|---|
| 72 | FValue := FMinValue;
|
|---|
| 73 | if FMaxValue < FMinValue then
|
|---|
| 74 | FMaxValue := FMinValue;
|
|---|
| 75 | Invalidate;
|
|---|
| 76 | end;
|
|---|
| 77 |
|
|---|
| 78 | procedure TBGRAFlashProgressBar.SetValue(const AValue: integer);
|
|---|
| 79 | begin
|
|---|
| 80 | if FValue = AValue then
|
|---|
| 81 | exit;
|
|---|
| 82 | FValue := AValue;
|
|---|
| 83 | if FValue < FMinValue then
|
|---|
| 84 | FValue := FMinValue;
|
|---|
| 85 | if FValue > FMaxValue then
|
|---|
| 86 | FValue := FMaxValue;
|
|---|
| 87 | Invalidate;
|
|---|
| 88 | end;
|
|---|
| 89 |
|
|---|
| 90 | {$hints off}
|
|---|
| 91 | procedure TBGRAFlashProgressBar.CalculatePreferredSize(
|
|---|
| 92 | var PreferredWidth, PreferredHeight: integer; WithThemeSpace: boolean);
|
|---|
| 93 | begin
|
|---|
| 94 | PreferredWidth := 379;
|
|---|
| 95 | PreferredHeight := 33;
|
|---|
| 96 | end;
|
|---|
| 97 |
|
|---|
| 98 | {$hints on}
|
|---|
| 99 |
|
|---|
| 100 | procedure TBGRAFlashProgressBar.Paint;
|
|---|
| 101 | var
|
|---|
| 102 | content: TRect;
|
|---|
| 103 | xpos, y, tx, ty: integer;
|
|---|
| 104 | grayValue: integer;
|
|---|
| 105 |
|
|---|
| 106 | function ApplyLightness(c: TBGRAPixel; lightness: word): TBGRAPixel;
|
|---|
| 107 | begin
|
|---|
| 108 | Result := GammaCompression(SetLightness(GammaExpansion(c), lightness));
|
|---|
| 109 | end;
|
|---|
| 110 |
|
|---|
| 111 | procedure DrawBar(bounds: TRect);
|
|---|
| 112 | var
|
|---|
| 113 | lCol: TBGRAPixel;
|
|---|
| 114 | begin
|
|---|
| 115 | lCol := ColorToBGRA(ColorToRGB(Color));
|
|---|
| 116 |
|
|---|
| 117 | DoubleGradientAlphaFill(FBmp, bounds,
|
|---|
| 118 | ApplyLightness(lCol, 37000), ApplyLightness(lCol, 29000),
|
|---|
| 119 | ApplyLightness(lCol, 26000), ApplyLightness(lCol, 18000),
|
|---|
| 120 | gdVertical, gdVertical, gdVertical, 0.53);
|
|---|
| 121 |
|
|---|
| 122 | InflateRect(bounds, -1, -1);
|
|---|
| 123 |
|
|---|
| 124 | DoubleGradientAlphaFill(FBmp, bounds,
|
|---|
| 125 | ApplyLightness(lCol, 28000), ApplyLightness(lCol, 22000),
|
|---|
| 126 | ApplyLightness(lCol, 19000), ApplyLightness(lCol, 11000),
|
|---|
| 127 | gdVertical, gdVertical, gdVertical, 0.53);
|
|---|
| 128 | end;
|
|---|
| 129 |
|
|---|
| 130 | begin
|
|---|
| 131 | tx := ClientWidth;
|
|---|
| 132 | ty := ClientHeight;
|
|---|
| 133 | if Assigned(FBmp) and ((FBmp.Width <> tx) or (FBmp.Height <> ty)) then
|
|---|
| 134 | FreeAndNil(FBmp);
|
|---|
| 135 |
|
|---|
| 136 | if not Assigned(FBmp) then
|
|---|
| 137 | FBmp := TBGRABitmap.Create(tx, ty)
|
|---|
| 138 | else
|
|---|
| 139 | FBmp.FillTransparent;
|
|---|
| 140 |
|
|---|
| 141 | FBmp.Rectangle(0, 0, tx, ty, BGRA(255, 255, 255, 6), dmSet);
|
|---|
| 142 | if (tx > 2) and (ty > 2) then
|
|---|
| 143 | FBmp.Rectangle(1, 1, tx - 1, ty - 1, BGRA(29, 29, 29), dmSet);
|
|---|
| 144 |
|
|---|
| 145 | if (tx > 4) and (ty > 4) then
|
|---|
| 146 | begin
|
|---|
| 147 | content := Rect(2, 2, tx - 2, ty - 2);
|
|---|
| 148 | randseed := FRandSeed;
|
|---|
| 149 | for y := content.Top to content.Bottom - 1 do
|
|---|
| 150 | begin
|
|---|
| 151 | if y = content.Top then
|
|---|
| 152 | grayValue := 33
|
|---|
| 153 | else
|
|---|
| 154 | if y = content.Top + 1 then
|
|---|
| 155 | grayValue := 43
|
|---|
| 156 | else
|
|---|
| 157 | grayValue := 47 + random(50 - 47 + 1);
|
|---|
| 158 | FBmp.SetHorizLine(content.Left, y, content.Right - 1, BGRA(
|
|---|
| 159 | grayValue, grayValue, grayValue));
|
|---|
| 160 | end;
|
|---|
| 161 | if tx >= 6 then
|
|---|
| 162 | FBmp.DrawVertLine(content.Right - 1, content.Top, content.Bottom - 1,
|
|---|
| 163 | BGRA(0, 0, 0, 32));
|
|---|
| 164 | if FMaxValue > FMinValue then
|
|---|
| 165 | begin
|
|---|
| 166 | xpos := round((FValue - FMinValue) / (FMaxValue - FMinValue) *
|
|---|
| 167 | (content.right - content.left)) + content.left;
|
|---|
| 168 | if xpos > content.left then
|
|---|
| 169 | begin
|
|---|
| 170 | DrawBar(rect(content.left, content.top, xpos, content.bottom));
|
|---|
| 171 | if xpos < content.right then
|
|---|
| 172 | begin
|
|---|
| 173 | FBmp.SetPixel(xpos, content.top, BGRA(62, 62, 62));
|
|---|
| 174 | FBmp.SetVertLine(xpos, content.top + 1, content.bottom - 1, BGRA(40, 40, 40));
|
|---|
| 175 | end;
|
|---|
| 176 | end;
|
|---|
| 177 | end;
|
|---|
| 178 | end;
|
|---|
| 179 | FBmp.Draw(Canvas, 0, 0, False);
|
|---|
| 180 | end;
|
|---|
| 181 |
|
|---|
| 182 | {$hints off}
|
|---|
| 183 | procedure TBGRAFlashProgressBar.WMEraseBkgnd(var Message: TLMEraseBkgnd);
|
|---|
| 184 | begin
|
|---|
| 185 | //do nothing
|
|---|
| 186 | end;
|
|---|
| 187 |
|
|---|
| 188 | {$hints on}
|
|---|
| 189 |
|
|---|
| 190 | constructor TBGRAFlashProgressBar.Create(AOwner: TComponent);
|
|---|
| 191 | begin
|
|---|
| 192 | inherited Create(AOwner);
|
|---|
| 193 | with GetControlClassDefaultSize do
|
|---|
| 194 | SetInitialBounds(0, 0, CX, 33);
|
|---|
| 195 | FMinValue := 0;
|
|---|
| 196 | FMaxValue := 100;
|
|---|
| 197 | FValue := 30;
|
|---|
| 198 | FBmp := nil;
|
|---|
| 199 | randomize;
|
|---|
| 200 | FRandSeed := randseed;
|
|---|
| 201 | Color := BGRAToColor(BGRA(102, 163, 226));
|
|---|
| 202 | end;
|
|---|
| 203 |
|
|---|
| 204 | destructor TBGRAFlashProgressBar.Destroy;
|
|---|
| 205 | begin
|
|---|
| 206 | FreeAndNil(FBmp);
|
|---|
| 207 | inherited Destroy;
|
|---|
| 208 | end;
|
|---|
| 209 |
|
|---|
| 210 | procedure TBGRAFlashProgressBar.SetMaxValue(const AValue: integer);
|
|---|
| 211 | begin
|
|---|
| 212 | if FMaxValue = AValue then
|
|---|
| 213 | exit;
|
|---|
| 214 | FMaxValue := AValue;
|
|---|
| 215 | if FValue > FMaxValue then
|
|---|
| 216 | FValue := FMaxValue;
|
|---|
| 217 | if FMinValue > FMaxValue then
|
|---|
| 218 | FMinValue := FMaxValue;
|
|---|
| 219 | Invalidate;
|
|---|
| 220 | end;
|
|---|
| 221 |
|
|---|
| 222 | end.
|
|---|