-
Notifications
You must be signed in to change notification settings - Fork 3
/
principale.pas
364 lines (324 loc) · 11.3 KB
/
principale.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
{ Developed by Grégory Bersegeay for "Delphi for fun competition" : https://community.embarcadero.com/competitions/14-fun-with-delphi-nasa-api-mash-up
This application was inspired by the demo of Marco Cantu : https://github.com/EmbarcaderoPublic/FunWithRADStudio/
My site : http://www.gbesoft.fr
}
unit principale;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, IPPeerClient,
FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Param,
FireDAC.Stan.Error, FireDAC.DatS, FireDAC.Phys.Intf, FireDAC.DApt.Intf,
System.Rtti, System.Bindings.Outputs, Fmx.Bind.Editors, Data.Bind.EngExt,
Fmx.Bind.DBEngExt, Data.Bind.Components, Data.Bind.DBScope, FMX.Edit,
FMX.Controls.Presentation, FMX.StdCtrls, FMX.Layouts, FMX.ExtCtrls, Data.DB,
FireDAC.Comp.DataSet, FireDAC.Comp.Client, REST.Response.Adapter, REST.Client,
Data.Bind.ObjectScope, System.Net.URLClient, System.Net.HttpClient,
System.Net.HttpClientComponent, FMX.Calendar, DateUtils, System.Actions,
FMX.ActnList, FMX.Objects, FMX.MultiView, System.Threading, System.JSON.Readers, System.JSON.Types, uCuriosityImg,
System.Generics.Collections, FMX.Ani, FMX.ListBox, FMX.Effects, FMX.Filter.Effects, System.ImageList, FMX.ImgList,
System.Math.Vectors, FMX.MaterialSources, FMX.Objects3D, FMX.Controls3D, FMX.Viewport3D;
type
TfPrincipale = class(TForm)
RESTClient1: TRESTClient;
RESTRequest1: TRESTRequest;
RESTResponse1: TRESTResponse;
RESTResponseDataSetAdapter1: TRESTResponseDataSetAdapter;
VisualiseurPrincipal: TImageViewer;
NetHTTPClient1: TNetHTTPClient;
ActionList1: TActionList;
GetImageAction: TAction;
StyleBook1: TStyleBook;
MultiView1: TMultiView;
Calendar1: TCalendar;
ToolBar1: TToolBar;
WorkingAnimation: TAction;
BindSourceDB1: TBindSourceDB;
Layout1: TLayout;
LayoutInfo: TLayout;
lInfo: TLabel;
lNoPhoto: TLabel;
LayoutHome: TLayout;
FloatAnimation1: TFloatAnimation;
bStart: TButton;
FloatAnimation2: TFloatAnimation;
GroupBox1: TGroupBox;
GroupBox2: TGroupBox;
LayoutZoom: TLayout;
TrackBar1: TTrackBar;
PixelateEffect: TPixelateEffect;
lUrl: TLabel;
Pie2: TPie;
Pie1: TPie;
RoundRect1: TRoundRect;
RoundRect2: TRoundRect;
Image1: TImage;
Image2: TImage;
ShadowEffect1: TShadowEffect;
layoutMars3D: TLayout;
Viewport3D1: TViewport3D;
Dummy1: TDummy;
Mars: TSphere;
TextureMaterialSource1: TTextureMaterialSource;
FloatAnimation3: TFloatAnimation;
layoutMain: TLayout;
PathAnimation1: TPathAnimation;
PaperSketchEffect1: TPaperSketchEffect;
ShadowEffect2: TShadowEffect;
GroupBox3: TGroupBox;
CheckBox1: TCheckBox;
SepiaEffect1: TSepiaEffect;
CheckBox2: TCheckBox;
InvertEffect1: TInvertEffect;
CheckBox3: TCheckBox;
CheckBox4: TCheckBox;
PencilStrokeEffect1: TPencilStrokeEffect;
IntroImage: TImage;
lDelphiFun: TLabel;
GlowEffect1: TGlowEffect;
Image3: TImage;
Layout2: TLayout;
ComboBox1: TComboBox;
lCamera: TLabel;
procedure FormCreate(Sender: TObject);
procedure GetImageActionExecute(Sender: TObject);
procedure Calendar1Change(Sender: TObject);
procedure Pie1Click(Sender: TObject);
procedure Pie2Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure bStartClick(Sender: TObject);
procedure FloatAnimation1Finish(Sender: TObject);
procedure ComboBox1Change(Sender: TObject);
procedure FloatAnimation2Finish(Sender: TObject);
procedure FloatAnimation2Process(Sender: TObject);
procedure TrackBar1Change(Sender: TObject);
procedure VisualiseurPrincipalMouseWheel(Sender: TObject; Shift: TShiftState; WheelDelta: Integer; var Handled: Boolean);
procedure Viewport3D1Click(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure CheckBox1Change(Sender: TObject);
procedure CheckBox2Change(Sender: TObject);
procedure CheckBox3Change(Sender: TObject);
procedure CheckBox4Change(Sender: TObject);
private
{ Private declarations }
Sr: TStringReader;
Reader: TJsonTextReader;
procedure CreateReader(Str: string);
function ParseObject(jsonText : string): string;
public
{ Public declarations }
BaseURL: String;
BaseDate: String;
lstImage: TObjectList<TCuriosityImg>;
end;
const
// get your key from https://api.nasa.gov/index.html#apply-for-an-api-key
APIKey = 'DEMO_KEY';
var
fPrincipale: TfPrincipale;
implementation
{$R *.fmx}
{ methods to parser JSON }
procedure TfPrincipale.CreateReader(Str: string);
begin
if Reader <> nil then
Reader.Free;
if Sr <> nil then
Sr.Free;
Sr := TStringReader.Create(Str);
Reader := TJsonTextReader.Create(Sr);
end;
function TfPrincipale.ParseObject(jsonText : string): string;
var
id, sol, camera, cameraFull, ImgSrc, earthDate, propertyName, resultat : string;
begin
resultat := '';
lstImage.Clear;
CreateReader(jsonText);
while Reader.read do
case Reader.TokenType of
TJsonToken.PropertyName: propertyName := lowercase(Reader.Value.ToString);
TJsonToken.String:
begin
if propertyName = 'name' then camera := Reader.Value.ToString;
if propertyName = 'full_name' then cameraFull := Reader.Value.ToString;
if propertyName = 'img_src' then ImgSrc := Reader.Value.ToString;
if propertyName = 'earth_date' then
begin
earthDate := Reader.Value.ToString;
lstImage.add(TCuriosityImg.create(StrToInt(id), StrToInt(sol), camera, cameraFull, ImgSrc, earthDate));
// Just th first image returned by request for the moment...
break;
end;
if propertyName = 'errors' then resultat := Reader.Value.ToString;
end;
TJsonToken.Integer:
begin
if propertyName = 'id' then id := Reader.Value.ToString;
if propertyName = 'sol' then sol := Reader.Value.ToString;
end;
end;
result := resultat;
end;
{ end methods to parser JSON }
procedure TfPrincipale.bStartClick(Sender: TObject);
begin
PathAnimation1.Stop;
FloatAnimation1.StartValue := 0;
FloatAnimation1.StopValue := fPrincipale.Height + 1;
FloatAnimation1.Start;
Calendar1.Date := now;
layoutMars3D.Visible := true;
LayoutInfo.Visible := true;
LayoutZoom.Visible := true;
end;
procedure TfPrincipale.Calendar1Change(Sender: TObject);
begin
GetImageAction.Execute;
end;
procedure TfPrincipale.CheckBox1Change(Sender: TObject);
begin
PaperSketchEffect1.Enabled := checkBox1.IsChecked;
end;
procedure TfPrincipale.CheckBox2Change(Sender: TObject);
begin
SepiaEffect1.Enabled := checkBox2.IsChecked;
end;
procedure TfPrincipale.CheckBox3Change(Sender: TObject);
begin
InvertEffect1.Enabled := checkBox3.IsChecked;
end;
procedure TfPrincipale.CheckBox4Change(Sender: TObject);
begin
PencilStrokeEffect1.Enabled := checkBox4.IsChecked;
end;
procedure TfPrincipale.ComboBox1Change(Sender: TObject);
begin
GetImageAction.Execute;
end;
procedure TfPrincipale.FloatAnimation1Finish(Sender: TObject);
begin
LayoutHome.Visible := false;
end;
procedure TfPrincipale.FloatAnimation2Finish(Sender: TObject);
begin
PixelateEffect.Enabled := false;
end;
procedure TfPrincipale.FloatAnimation2Process(Sender: TObject);
begin
PixelateEffect.BlockCount := PixelateEffect.blockCount +10;
end;
procedure TfPrincipale.FormCreate(Sender: TObject);
begin
LayoutHome.Visible := true;
PixelateEffect.Enabled := false;
BaseURL := 'https://api.nasa.gov/mars-photos/api/v1/rovers/curiosity/photos?api_key=' + APIKey;
lstImage := TObjectList<TCuriosityImg>.Create(True);
end;
procedure TfPrincipale.FormDestroy(Sender: TObject);
begin
lstImage.free;
end;
procedure TfPrincipale.FormShow(Sender: TObject);
var
centreX, centreY, limiteHaut, limiteBas, limiteGauche, limiteDroite : Extended;
begin
centreX := 150;
centreY := 120;
limiteHaut := 150;
limiteBas := 250;
limiteDroite := 320;
limiteGauche := -150;
PathAnimation1.Parent := lDelphiFun;
PathAnimation1.Path.MoveTo(PointF(limiteGauche, limiteHaut));
PathAnimation1.Path.LineTo(PointF(centreX, centreY));
PathAnimation1.Path.LineTo(PointF(limiteDroite,limiteHaut));
PathAnimation1.Path.LineTo(PointF(centreX,limiteHaut));
PathAnimation1.Path.LineTo(PointF(limiteGauche,limiteBas));
PathAnimation1.Path.LineTo(PointF(centreX, centreY));
PathAnimation1.Path.LineTo(PointF(limiteDroite, limiteBas));
PathAnimation1.Path.LineTo(PointF(limiteGauche, limiteHaut));
PathAnimation1.Path.ClosePath;
PathAnimation1.Loop := True;
PathAnimation1.Duration := 10;
PathAnimation1.Start;
end;
procedure TfPrincipale.GetImageActionExecute(Sender: TObject);
var
AResponseStream: TMemoryStream;
resultat : string;
begin
lNoPhoto.Visible := false;
VisualiseurPrincipal.Visible := false;
layoutzoom.Visible := false;
lUrl.Text := '';
BaseDate := YearOf(Calendar1.Date).ToString + '-' + MonthOfTheYear(Calendar1.Date).ToString + '-' + DayOfTheMonth(Calendar1.Date).ToString;
RESTClient1.BaseURL := baseurl+ '&earth_date='+BaseDate+'&camera='+lowercase(combobox1.Items[combobox1.ItemIndex]);
ITask(TTask.Create(procedure
begin
TThread.Queue(nil,procedure
begin
RESTRequest1.Execute;
resultat := ParseObject(RESTResponse1.content);
if trim(resultat) = '' then
begin
AResponseStream := TMemoryStream.Create;
if lstImage.count > 0 then
begin
NetHTTPClient1.Get(lstImage[0].ImgSrc,AResponseStream);
try
lUrl.Text := lstImage[0].ImgSrc;
VisualiseurPrincipal.Bitmap.LoadFromStream(AResponseStream);
VisualiseurPrincipal.Visible := true;
layoutzoom.Visible := true;
PixelateEffect.BlockCount := 25;
PixelateEffect.Enabled := true;
FloatAnimation2.Start;
lInfo.text := 'Sol '+inttostr(lstImage[0].Sol)+' Date : ' +lstImage[0].EarthDate+' Camera : ' +lstImage[0].Camera;
except
end;
end;
AResponseStream.Free;
end
else
begin
lNoPhoto.text := resultat+sLineBreak+'(selected date : '+FormatDateTime('dd/mm/yyyy',Calendar1.Date)+sLineBreak+'camera : '+combobox1.Items[Combobox1.ItemIndex]+')';
lNoPhoto.Visible := true;
end;
end);
end)).Start;
end;
procedure TfPrincipale.VisualiseurPrincipalMouseWheel(Sender: TObject; Shift: TShiftState; WheelDelta: Integer;
var Handled: Boolean);
begin
TrackBar1.Value := VisualiseurPrincipal.BitmapScale *100;
end;
procedure TfPrincipale.Pie1Click(Sender: TObject);
begin
Calendar1.Date := Calendar1.Date-1;
end;
procedure TfPrincipale.Pie2Click(Sender: TObject);
begin
if (Calendar1.Date+1)>Now then Exit;
Calendar1.Date := Calendar1.Date+1;
end;
procedure TfPrincipale.TrackBar1Change(Sender: TObject);
begin
VisualiseurPrincipal.BitmapScale := TrackBar1.Value / 100;
end;
procedure TfPrincipale.Viewport3D1Click(Sender: TObject);
begin
if layoutMars3D.Height = 171 then
begin
layoutMars3D.Height := 450;
Viewport3D1.width := 450;
Viewport3D1.Align := TAlignLayout.Center;
end
else
begin
layoutMars3D.Height := 171;
Viewport3D1.width := 171;
Viewport3D1.Align := TAlignLayout.Right;
end;
end;
end.