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
| unit CThermometre;
interface
uses
System.SysUtils, System.Classes, System.UITypes,
FMX.Types, FMX.Controls, FMX.Objects,FMX.Graphics,
Data.Bind.Components;
type
[ObservableMembers('Progress',false)]
TThermometre = class(TRectangle)
private
FProgress: integer;
procedure SetProgress(const Value: integer);
procedure ObserverToggle(const AObserver: IObserver; const Value: Boolean);
{ Déclarations privées }
protected
{ Déclarations protégées }
function CanObserve(const ID: Integer): Boolean; override; { declaration is in System.Classes }
procedure ObserverAdded(const ID: Integer; const Observer: IObserver); override; { declaration is in System.Classes }
procedure OnResize;
public
constructor Create(AOwner: TComponent); override;
function Paint: Boolean; reintroduce;
{ Déclarations publiques }
published
{ Déclarations publiées }
property Progress : integer {read FProgress} write SetProgress;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Samples', [TThermometre]);
end;
{ TRectangle1 }
function TThermometre.CanObserve(const ID: Integer): Boolean;
begin
case ID of
TObserverMapping.EditLinkID, TObserverMapping.ControlValueID:
Result := True;
else
Result := False;
end;
end;
constructor TThermometre.create(AOwner: TComponent);
var pos : single;
aBrush : TBrush;
begin
if not(csloading in ComponentState) then
begin
inherited;
width:=30;
height:=110;
Xradius:=10;
Yradius:=10;
FProgress:=50;
pos:=0.5;
if pos=1 then pos:=1-0.000001;
aBrush:=TBrush.Create(TBrushKind.Gradient,Talphacolors.null);
try
aBrush.Gradient.StartPosition.X:=0.5;
aBrush.Gradient.StartPosition.Y:=1;
aBrush.Gradient.StopPosition.X:=0.5;
aBrush.Gradient.StopPosition.Y:=0;
aBrush.Gradient.Points.Clear;
aBrush.Gradient.Points.Add;
aBrush.Gradient.Points[0].Color:=Talphacolors.red;
aBrush.Gradient.Points[0].Offset:=0;
aBrush.Gradient.Points.Add;
aBrush.Gradient.Points[1].Color:=Talphacolors.red;
aBrush.Gradient.Points[1].Offset:=pos;
aBrush.Gradient.Points.Add;
aBrush.Gradient.Points[2].Color:=Talphacolors.null;
aBrush.Gradient.Points[2].Offset:=pos+0.000001;
aBrush.Gradient.Points.Add;
aBrush.Gradient.Points[3].Color:=Talphacolors.null;
aBrush.Gradient.Points[3].Offset:=1;
Fill:=aBrush;
finally
aBrush.Free;
end;
end;
end;
procedure TThermometre.ObserverAdded(const ID: Integer;
const Observer: IObserver);
begin
if ID = TObserverMapping.EditLinkID then
Observer.OnObserverToggle := ObserverToggle;
end;
procedure TThermometre.ObserverToggle(const AObserver: IObserver;
const Value: Boolean);
var
LEditLinkObserver: IEditLinkObserver;
begin
if Value then
begin
if Supports(AObserver, IEditLinkObserver, LEditLinkObserver) then
Enabled := not LEditLinkObserver.IsReadOnly;
end
else
Enabled := True;
end;
procedure TThermometre.OnResize;
begin
Paint;
end;
function TThermometre.Paint: Boolean;
var pos : single;
begin
BeginUpDate;
pos:=(FProgress/100);
if pos=1 then pos:=1-0.000001;
Fill.Gradient.Points.Clear;
Fill.Gradient.Points.Add;
Fill.Gradient.Points[0].Color:=Talphacolors.red;
Fill.Gradient.Points[0].Offset:=0;
Fill.Gradient.Points.Add;
Fill.Gradient.Points[1].Color:=Talphacolors.red;
Fill.Gradient.Points[1].Offset:=pos;
Fill.Gradient.Points.Add;
Fill.Gradient.Points[2].Color:=Talphacolors.null;
Fill.Gradient.Points[2].Offset:=pos+0.000001;
Fill.Gradient.Points.Add;
Fill.Gradient.Points[3].Color:=Talphacolors.null;
Fill.Gradient.Points[3].Offset:=1;
EndUpdate;
result:=true;
end;
procedure TThermometre.SetProgress(const Value: integer);
begin
if FProgress<>Value then
begin
FProgress := Value;
Paint;
end;
end;
initialization
Data.Bind.Components.RegisterValuePropertyName
(TArray<TClass>.create(TThermometre), 'Progress', 'FMX');
finalization
Data.Bind.Components.UnregisterValuePropertyName
(TArray<TClass>.create(TThermometre));
end. |
Partager