Debian Codegraph @

tools/castle-editor/code/formpreferences.pas

raw
1 {
2 Copyright 2019-2024 Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14 }
15
16 { Form to configure CGE editor (@link(TPreferencesForm)). }
17 unit FormPreferences;
18
19 interface
20
21 uses
22 Classes, SysUtils, Forms, Controls, Graphics, Dialogs, EditBtn, StdCtrls,
23 ExtCtrls, ButtonPanel, ComCtrls;
24
25 type
26 TPreferencesForm = class(TForm)
27 ButtonRegisterLazarusPackages: TButton;
28 ButtonPanel1: TButtonPanel;
29 CheckBoxMuteOnRun: TCheckBox;
30 DirectoryEditAndroidHome: TDirectoryEdit;
31 DirectoryEditJavaHome: TDirectoryEdit;
32 DirectoryEditFpc: TDirectoryEdit;
33 DirectoryEditCgePath: TDirectoryEdit;
34 DirectoryEditLazarus: TDirectoryEdit;
35 EditCodeEditorCommand: TFileNameEdit;
36 EditCodeEditorCommandLineColumn: TFileNameEdit;
37 EditCodeEditorCommandProject: TFileNameEdit;
38 LabelAndroidDocsWww: TLabel;
39 LabelAndroidHome: TLabel;
40 LabelJavaHome: TLabel;
41 LabelAndroidHomeHint: TLabel;
42 LabelJavaHomeHint: TLabel;
43 LabelCodeEditorAutodetect: TLabel;
44 LabelCodeEditorCommandLineColumn: TLabel;
45 LabelCompilerAutodetect: TLabel;
46 LabelCompilerDelphi: TLabel;
47 LabelCompilationHeader: TLabel;
48 LabelCodeEditorLazarus: TLabel;
49 LabelCodeEditorDelphi: TLabel;
50 LabelCompilerFpc: TLabel;
51 LabelCodeEditorVSCode: TLabel;
52 LabelCgePath: TLabel;
53 LabelCgePathAutoDetected: TLabel;
54 LabelCgePathAutoDetectedCaption: TLabel;
55 LabelInstructions0: TLabel;
56 LabelInstructions1: TLabel;
57 LabelInstructions2: TLabel;
58 LabelAndroidDocsWwwCaption: TLabel;
59 LabelLazarusWebsite: TLabel;
60 LabelVolume: TLabel;
61 LabelCodeEditorCommandInstructions: TLabel;
62 LabelCodeEditorCommand: TLabel;
63 LabelCodeEditorCommandProjectInstructions: TLabel;
64 LabelCodeEditorHeader: TLabel;
65 LabelSound: TLabel;
66 LabelFpc: TLabel;
67 LabelFpcAutoDetectedCaption: TLabel;
68 LabelLazarus: TLabel;
69 LabelLazarusAutoDetectedCaption: TLabel;
70 LabelTitle: TLabel;
71 LabelFpcAutoDetected: TLabel;
72 LabelLazarusAutoDetected: TLabel;
73 ListPages: TListBox;
74 PanelGeneral: TPanel;
75 PanelCompilation: TPanel;
76 PanelAndroid: TPanel;
77 PanelInstructions: TPanel;
78 PanelCodeEditor: TPanel;
79 PanelSound: TPanel;
80 PanelFpcLazarusConfig: TPanel;
81 RadioCodeEditorAutodetect: TRadioButton;
82 RadioCompilerAutodetect: TRadioButton;
83 RadioCodeEditorCustom: TRadioButton;
84 RadioCompilerDelphi: TRadioButton;
85 RadioCodeEditorLazarus: TRadioButton;
86 RadioCodeEditorDelphi: TRadioButton;
87 RadioCompilerFpc: TRadioButton;
88 RadioCodeEditorVSCode: TRadioButton;
89 TrackVolume: TTrackBar;
90 procedure ButtonRegisterLazarusPackagesClick(Sender: TObject);
91 procedure DirectoryEditAndroidHomeAcceptDirectory(Sender: TObject;
92 var Value: String);
93 procedure DirectoryEditCgePathChange(Sender: TObject);
94 procedure DirectoryEditFpcChange(Sender: TObject);
95 procedure DirectoryEditLazarusChange(Sender: TObject);
96 procedure EditCodeEditorCommandAcceptFileName(Sender: TObject;
97 var Value: String);
98 procedure EditCodeEditorCommandLineColumnAcceptFileName(Sender: TObject;
99 var Value: String);
100 procedure EditCodeEditorCommandProjectAcceptFileName(Sender: TObject;
101 var Value: String);
102 procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
103 procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);
104 procedure FormShow(Sender: TObject);
105 procedure LabelAndroidDocsWwwClick(Sender: TObject);
106 procedure LabelLazarusWebsiteClick(Sender: TObject);
107 procedure ListPagesClick(Sender: TObject);
108 procedure RadioCodeEditorAnyChange(Sender: TObject);
109 procedure TrackVolumeChange(Sender: TObject);
110 private
111 OriginalFpcCustomPath, OriginalLazarusCustomPath, OriginalCastleEngineOverridePath: String;
112 procedure UpdateAutoDetectedLabels;
113 procedure UpdatePageVisible;
114 public
115
116 end;
117
118 var
119 PreferencesForm: TPreferencesForm;
120
121 implementation
122
123 uses CastleOpenDocument, CastleUtils, CastleLog, CastleSoundEngine,
124 CastleStringUtils, CastleFilesUtils, CastleUriUtils,
125 ToolCompilerInfo, ToolFpcVersion, ToolCommonUtils, ToolManifest,
126 EditorUtils, ProjectUtils;
127
128 {$R *.lfm}
129
130 { TPreferencesForm }
131
132 procedure TPreferencesForm.LabelLazarusWebsiteClick(Sender: TObject);
133 begin
134 OpenUrl('https://www.lazarus-ide.org/');
135 end;
136
137 procedure TPreferencesForm.ListPagesClick(Sender: TObject);
138 begin
139 UpdatePageVisible;
140 end;
141
142 procedure TPreferencesForm.RadioCodeEditorAnyChange(Sender: TObject);
143 begin
144 EditCodeEditorCommand.Enabled := RadioCodeEditorCustom.Checked;
145 EditCodeEditorCommandLineColumn.Enabled := RadioCodeEditorCustom.Checked;
146 EditCodeEditorCommandProject.Enabled := RadioCodeEditorCustom.Checked;
147 end;
148
149 procedure TPreferencesForm.TrackVolumeChange(Sender: TObject);
150 begin
151 { While the volume will be adjusted in TPreferencesForm.FormClose too,
152 along with EditorVolume, but it is natural to also modify the audible
153 volume immediately. }
154 SoundEngineSetVolume(TrackVolume.Position / TrackVolume.Max);
155 end;
156
157 procedure TPreferencesForm.UpdateAutoDetectedLabels;
158
159 function CgePathAutodetectedLine: String;
160 begin
161 if (CastleEngineOverridePath = '') and
162 (CastleEnginePath <> '') then
163 Result := 'Auto-detected in: ' + CastleEnginePath + NL
164 else
165 Result := '';
166 end;
167
168 var
169 FpcExe, FpcVer, LazarusExe, LazarusVer, DelphiPath, VSCodeExe,
170 CgePathStatusText: String;
171 begin
172 CgePathStatus(CastleEnginePath, CgePathStatusText);
173 LabelCgePathAutoDetected.Caption := CgePathAutodetectedLine + CgePathStatusText;
174
175 FpcExe := '';
176 try
177 FpcExe := FindExeFpcCompiler;
178 FpcVer := FpcVersion.ToString;
179 LabelFpcAutoDetected.Caption :=
180 'FPC executable: ' + FpcExe + NL +
181 'FPC version: ' + FpcVer;
182 except
183 on E: Exception do
184 begin
185 WritelnLog('Cannot determine FPC executable or version, error: ' + ExceptMessage(E));
186 LabelFpcAutoDetected.Caption :=
187 'Cannot determine FPC executable.' + NL +
188 'Make sure FPC is installed, and available on $PATH or configured above.';
189 end;
190 end;
191
192 LazarusExe := '';
193 try
194 LazarusExe := FindExeLazarusIDE;
195 LazarusVer := LazarusVersion.ToString;
196 LabelLazarusAutoDetected.Caption :=
197 'Lazarus executable: ' + LazarusExe + NL +
198 'Lazarus version: ' + LazarusVer;
199 except
200 on E: EExecutableNotFound do
201 begin
202 WritelnLog('Cannot determine Lazarus executable, error: ' + ExceptMessage(E));
203 LabelLazarusAutoDetected.Caption :=
204 'Cannot determine Lazarus executable.' + NL +
205 'Make sure Lazarus is installed, and available on $PATH or configured above.';
206 end;
207 end;
208
209 if FpcExe <> '' then
210 LabelCompilerFpc.Caption := 'Detected: ' + FpcExe
211 else
212 LabelCompilerFpc.Caption := 'Not found.';
213
214 if LazarusExe <> '' then
215 LabelCodeEditorLazarus.Caption := 'Detected: ' + LazarusExe
216 else
217 LabelCodeEditorLazarus.Caption := 'Not found.';
218
219 DelphiPath := FindDelphiPath(false);
220 if DelphiPath <> '' then
221 begin
222 LabelCodeEditorDelphi.Caption := 'Detected path: ' + DelphiPath;
223 LabelCompilerDelphi.Caption := 'Detected path: ' + DelphiPath;
224 end else
225 begin
226 LabelCodeEditorDelphi.Caption := 'Not found.';
227 LabelCompilerDelphi.Caption := 'Not found.';
228 end;
229
230 VSCodeExe := FindExeVSCode(false);
231 if VSCodeExe <> '' then
232 LabelCodeEditorVSCode.Caption := 'Detected: ' + VSCodeExe
233 else
234 LabelCodeEditorVSCode.Caption := 'Not found.';
235
236 if LazarusExe <> '' then
237 LabelCodeEditorAutodetect.Caption := 'First auto-detected IDE: Lazarus'
238 else
239 if DelphiPath <> '' then
240 LabelCodeEditorAutodetect.Caption := 'First auto-detected IDE: Delphi'
241 else
242 if VSCodeExe <> '' then
243 LabelCodeEditorAutodetect.Caption := 'First auto-detected IDE: Visual Studio Code'
244 else
245 LabelCodeEditorAutodetect.Caption := 'First auto-detected IDE: None';
246
247 if FpcExe <> '' then
248 LabelCompilerAutodetect.Caption := 'First auto-detected compiler: FPC'
249 else
250 if DelphiPath <> '' then
251 LabelCompilerAutodetect.Caption := 'First auto-detected compiler: Delphi'
252 else
253 LabelCompilerAutodetect.Caption := 'First auto-detected compiler: None';
254 end;
255
256 procedure TPreferencesForm.FormShow(Sender: TObject);
257 begin
258 // ListPages.ItemIndex := ; // leave previous
259 UpdatePageVisible;
260
261 UpdateAutoDetectedLabels;
262
263 { Update Enabled of edit fields }
264 RadioCodeEditorAnyChange(nil);
265
266 DirectoryEditFpc.Directory := FpcCustomPath;
267 DirectoryEditLazarus.Directory := LazarusCustomPath;
268 DirectoryEditCgePath.Directory := CastleEngineOverridePath;
269 DirectoryEditAndroidHome.Directory := AndroidHome;
270 DirectoryEditJavaHome.Directory := JavaHome;
271 { We will change the global Fpc/LazarusCustomPath during this dialog,
272 so allow to revert them on "Cancel". }
273 OriginalFpcCustomPath := FpcCustomPath;
274 OriginalLazarusCustomPath := LazarusCustomPath;
275 OriginalCastleEngineOverridePath := CastleEngineOverridePath;
276
277 // Note that making any RadioCodeEditorXxx checked will uncheck the others
278 case CodeEditor of
279 ceAutodetect: RadioCodeEditorAutodetect.Checked := true;
280 ceLazarus : RadioCodeEditorLazarus.Checked := true;
281 ceDelphi : RadioCodeEditorDelphi.Checked := true;
282 ceVSCode : RadioCodeEditorVSCode.Checked := true;
283 ceCustom : RadioCodeEditorCustom.Checked := true;
284 else raise EInternalError.Create('CodeEditor?');
285 end;
286 EditCodeEditorCommand.Text := CodeEditorCommand;
287 EditCodeEditorCommandLineColumn.Text := CodeEditorCommandLineColumn;
288 EditCodeEditorCommandProject.Text := CodeEditorCommandProject;
289
290 // compilation tab
291 case Compiler of
292 coAutodetect: RadioCompilerAutodetect.Checked := true;
293 coFpc : RadioCompilerFpc.Checked := true;
294 coDelphi : RadioCompilerDelphi.Checked := true;
295 else raise EInternalError.Create('Compiler?');
296 end;
297
298 // sound tab
299 TrackVolume.Position := Round(EditorVolume * TrackVolume.Max);
300 CheckBoxMuteOnRun.Checked := MuteOnRun;
301 end;
302
303 procedure TPreferencesForm.LabelAndroidDocsWwwClick(Sender: TObject);
304 begin
305 OpenUrl('https://castle-engine.io/android');
306 end;
307
308 procedure TPreferencesForm.FormClose(Sender: TObject;
309 var CloseAction: TCloseAction);
310
311 { When CastleEngineOverridePath changed, we need to recalculate stuff based on it. }
312 procedure CastleEngineOverridePathChanged;
313 var
314 OldApplicationDataOverride: String;
315 begin
316 { Recalculate InternalCastleDesignData that depends on detected CGE path.
317 This way changes to CGE path in "Preferences" update also
318 InternalCastleDesignData used to read e.g. 3D models of gizmos.
319
320 Testcase when this is needed:
321 - test with castle-editor in bin/ subdirectory of CGE
322 (like in binary distribution; this means that ExeName will not be enough
323 to guess editor data location)
324 - set CastleEngineOverridePath to something invalid but non-empty (i.e. to non-existing dir)
325 - restart editor (TChooseProjectForm.FormCreate will set InternalCastleDesignData to '',
326 because CastleEnginePath is invalid and CastleEnginePath + tools/castle-editor/data doesn't exist)
327 - open some project, open some design with viewport.
328 It will fail, and it's kind of OK -- engine path was invalid.
329 - go to Preferences and change CastleEngineOverridePath to empty
330 - reopen the project (or reopen design within it)
331 - now it should open OK.
332 }
333 OldApplicationDataOverride := ApplicationDataOverride;
334 UseEditorApplicationData;
335 InternalCastleDesignData := ResolveCastleDataUrl('castle-data:/');
336 ApplicationDataOverride := OldApplicationDataOverride;
337 end;
338
339 begin
340 if ModalResult = mrOK then
341 begin
342 { copy UI -> global variables }
343
344 // code editor tab
345 if RadioCodeEditorCustom.Checked then
346 CodeEditor := ceCustom
347 else
348 if RadioCodeEditorAutodetect.Checked then
349 CodeEditor := ceAutodetect
350 else
351 if RadioCodeEditorLazarus.Checked then
352 CodeEditor := ceLazarus
353 else
354 if RadioCodeEditorDelphi.Checked then
355 CodeEditor := ceDelphi
356 else
357 if RadioCodeEditorVSCode.Checked then
358 CodeEditor := ceVSCode
359 else
360 raise EInternalError.Create('Cannot determine CodeEditor choice, no radio selected');
361
362 CodeEditorCommand := EditCodeEditorCommand.Text;
363 CodeEditorCommandLineColumn := EditCodeEditorCommandLineColumn.Text;
364 CodeEditorCommandProject := EditCodeEditorCommandProject.Text;
365
366 // compilation tab
367 if RadioCompilerAutodetect.Checked then
368 Compiler := coAutodetect
369 else
370 if RadioCompilerFpc.Checked then
371 Compiler := coFpc
372 else
373 if RadioCompilerDelphi.Checked then
374 Compiler := coDelphi
375 else
376 raise EInternalError.Create('Cannot determine Compiler choice, no radio selected');
377
378 // sound tab
379 EditorVolume := TrackVolume.Position / TrackVolume.Max;
380 MuteOnRun := CheckBoxMuteOnRun.Checked;
381
382 // Android tab
383 AndroidHome := DirectoryEditAndroidHome.Directory;
384 JavaHome := DirectoryEditJavaHome.Directory;
385 end else
386 begin
387 { XxxCustomPath are special.
388 They are updated all the time when you change them in UI,
389 this makes functions like FindExeLazarusIDE work OK,
390 and at the end we restore them to orignal values if user *did not*
391 accept the changes by clicking "OK". }
392 FpcCustomPath := OriginalFpcCustomPath;
393 LazarusCustomPath := OriginalLazarusCustomPath;
394 CastleEngineOverridePath := OriginalCastleEngineOverridePath;
395 end;
396
397 CastleEngineOverridePathChanged;
398
399 { Set SoundEngine.Volume regardless if we accepted
400 (so MuteOnRun, EditorVolume changed) or not (so they are unchanged)
401 to revert any immediate changes to volume in TPreferencesForm.TrackVolumeChange. }
402 SoundEngineSetVolume;
403 end;
404
405 procedure TPreferencesForm.FormCloseQuery(Sender: TObject; var CanClose: boolean);
406 begin
407 if ModalResult = mrOK then
408 begin
409 if RadioCodeEditorCustom.Checked and
410 (Trim(EditCodeEditorCommand.Text) = '') then
411 begin
412 ErrorBox('You must specify some custom editor command, or switch to use other (e.g. autodetected) code editor');
413 CanClose := false;
414 Exit;
415 end;
416 end;
417 end;
418
419 procedure TPreferencesForm.DirectoryEditFpcChange(Sender: TObject);
420 begin
421 FpcCustomPath := DirectoryEditFpc.Directory;
422 UpdateAutoDetectedLabels;
423 end;
424
425 procedure TPreferencesForm.ButtonRegisterLazarusPackagesClick(Sender: TObject);
426
427 procedure RegisterPackage(const LpkFileName: String);
428 var
429 LazbuildExe, LazbuildOutput, PackageFileName: String;
430 LazbuildExitStatus: integer;
431 begin
432 LazbuildExe := FindExeLazbuild;
433
434 PackageFileName := CastleEnginePath + LpkFileName;
435
436 WritelnLog('Executing: lazbuild --add-package-link "' + PackageFileName + '"');
437 MyRunCommandIndir(
438 GetCurrentDir { no better directory, but also should not matter },
439 LazbuildExe, [
440 '--add-package-link',
441 PackageFileName
442 ], LazbuildOutput, LazbuildExitStatus);
443
444 WritelnLog('Execution finished: lazbuild status %d, output:' + NL + '%s', [LazbuildExitStatus, LazbuildOutput]);
445
446 if (LazbuildExitStatus <> 0) or
447 (Pos('Invalid option', LazbuildOutput) <> 0) { lazbuild has exit status 0 in this case } then
448 raise Exception.Create('Executing lazbuild failed:' + NL + NL + LazbuildOutput);
449 end;
450
451 begin
452 try
453 RegisterPackage('packages/castle_base.lpk');
454 RegisterPackage('packages/castle_window.lpk');
455 RegisterPackage('packages/castle_components.lpk');
456 RegisterPackage('packages/castle_editor_components.lpk');
457 RegisterPackage('packages/alternative_castle_window_based_on_lcl.lpk');
458 RegisterPackage('packages/castle_indy.lpk');
459
460 ShowMessage('Lazarus packages registered successfully.');
461 except
462 on E: Exception do
463 ErrorBox(E.Message);
464 end;
465 end;
466
467 procedure TPreferencesForm.DirectoryEditAndroidHomeAcceptDirectory(
468 Sender: TObject; var Value: String);
469 begin
470 if Value <> '' then
471 begin
472 if not DirectoryExists(Value) then
473 begin
474 WarningBox(Format('Directory "%s" does not exist', [Value]));
475 end else
476 if not (
477 // Android SDK 30 doesn't contain 'tools', only 'platform-tools', so don't check it
478 // DirectoryExists(InclPathDelim(Value) + 'tools') and
479 DirectoryExists(InclPathDelim(Value) + 'platform-tools')) then
480 begin
481 WarningBox(Format('Directory "%s" does not contain typical Android SDK subdirectories "tools", "platform-tools". Make sure it is correct.', [Value]));
482 end;
483 end;
484 end;
485
486 procedure TPreferencesForm.DirectoryEditCgePathChange(Sender: TObject);
487 begin
488 CastleEngineOverridePath := DirectoryEditCgePath.Directory;
489 UpdateAutoDetectedLabels;
490 end;
491
492 procedure TPreferencesForm.DirectoryEditLazarusChange(Sender: TObject);
493 begin
494 LazarusCustomPath := DirectoryEditLazarus.Directory;
495 UpdateAutoDetectedLabels;
496 end;
497
498 procedure TPreferencesForm.EditCodeEditorCommandAcceptFileName(Sender: TObject;
499 var Value: String);
500 begin
501 // auto-add ${PAS} macro and propose quoting
502 Value := '"' + Value + '" ${PAS}';
503 end;
504
505 procedure TPreferencesForm.EditCodeEditorCommandLineColumnAcceptFileName(
506 Sender: TObject; var Value: String);
507 begin
508 // auto-add macros and propose quoting
509 Value := '"' + Value + '" ${PAS}:${LINE}:${COLUMN}';
510 end;
511
512 procedure TPreferencesForm.EditCodeEditorCommandProjectAcceptFileName(
513 Sender: TObject; var Value: String);
514 begin
515 // auto-add ${PROJECT_DIR} macro and propose quoting
516 Value := '"' + Value + '" ${PROJECT_DIR}';
517 end;
518
519 procedure TPreferencesForm.UpdatePageVisible;
520 var
521 PageIndex: Integer;
522 SelectedPage: TPanel;
523 begin
524 PageIndex := ListPages.ItemIndex;
525 if PageIndex = -1 then
526 // on macOS (Cocoa) user can click anywhere in list to deselect it, tolerate it
527 PageIndex := 0;
528
529 case PageIndex of
530 0: SelectedPage := PanelGeneral;
531 1: SelectedPage := PanelCodeEditor;
532 2: SelectedPage := PanelCompilation;
533 3: SelectedPage := PanelFpcLazarusConfig;
534 4: SelectedPage := PanelSound;
535 5: SelectedPage := PanelAndroid;
536 else raise Exception.CreateFmt('Unexpected ListPages.ItemIndex %d', [ListPages.ItemIndex]);
537 end;
538 SetEnabledVisible(PanelGeneral , PanelGeneral = SelectedPage);
539 SetEnabledVisible(PanelCodeEditor , PanelCodeEditor = SelectedPage);
540 SetEnabledVisible(PanelCompilation , PanelCompilation = SelectedPage);
541 SetEnabledVisible(PanelFpcLazarusConfig, PanelFpcLazarusConfig = SelectedPage);
542 SetEnabledVisible(PanelSound , PanelSound = SelectedPage);
543 SetEnabledVisible(PanelAndroid , PanelAndroid = SelectedPage);
544 end;
545
546 end.