| 1 |
{ |
| 2 |
Copyright 2018-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 |
{ Various castle-editor utilities. } |
| 17 |
unit EditorUtils; |
| 18 |
|
| 19 |
{$I editorconf.inc} |
| 20 |
|
| 21 |
interface |
| 22 |
|
| 23 |
uses Classes, Types, Controls, StdCtrls, Process, Menus, Generics.Collections, |
| 24 |
Dialogs, Contnrs, |
| 25 |
CastleStringUtils, CastleInternalTools, |
| 26 |
ToolArchitectures, ToolManifest, ToolProcess; |
| 27 |
|
| 28 |
type |
| 29 |
TMenuItemHelper = class helper for TMenuItem |
| 30 |
public |
| 31 |
procedure SetEnabledVisible(const Value: Boolean); |
| 32 |
end; |
| 33 |
|
| 34 |
TOutputKind = ( |
| 35 |
okInfo, |
| 36 |
okImportantInfo, |
| 37 |
okWarning, |
| 38 |
okError |
| 39 |
); |
| 40 |
|
| 41 |
{ Instance of this takes control of your TListBox to display messages. } |
| 42 |
TOutputList = class |
| 43 |
strict private |
| 44 |
type |
| 45 |
TOutputInfo = class |
| 46 |
Kind: TOutputKind; |
| 47 |
end; |
| 48 |
var |
| 49 |
OutputInfoPerKind: array [TOutputKind] of TOutputInfo; |
| 50 |
FList: TListBox; |
| 51 |
procedure DrawItem(Control: TWinControl; Index: Integer; |
| 52 |
ARect: TRect; State: TOwnerDrawState); |
| 53 |
public |
| 54 |
constructor Create(const AList: TListBox); |
| 55 |
destructor Destroy; override; |
| 56 |
property List: TListBox read FList; |
| 57 |
|
| 58 |
procedure AddLine(const S: String; const Kind: TOutputKind); |
| 59 |
{ Split S into multiple lines. |
| 60 |
Add each of them as separate line. |
| 61 |
Avoids adding empty lines at the very end (since they would |
| 62 |
make the look uglier; instead, |
| 63 |
we make nice separators with AddSeparator). } |
| 64 |
//procedure SplitAddLines(const S: String; const Kind: TOutputKind); |
| 65 |
procedure AddSeparator; |
| 66 |
procedure Clear; |
| 67 |
end; |
| 68 |
|
| 69 |
{ Call external process asynchronously (doesn't hang this process at any point), |
| 70 |
sending output to TOutputList. } |
| 71 |
TAsynchronousProcess = class |
| 72 |
strict private |
| 73 |
Process: TProcess; |
| 74 |
{ Text read from Process output, but not passed to OutputList yet. } |
| 75 |
PendingLines: String; |
| 76 |
FRunning: Boolean; |
| 77 |
FExitStatus: Integer; |
| 78 |
Environment: TStringList; |
| 79 |
{ Additional process id to kill, when killing Process. } |
| 80 |
ChildProcessId: TProcessId; |
| 81 |
public |
| 82 |
{ Set before @link(Start), do not change later. |
| 83 |
@groupBegin } |
| 84 |
ExeName, CurrentDirectory: String; |
| 85 |
Parameters: TCastleStringList; |
| 86 |
OutputList: TOutputList; |
| 87 |
{ @groupEnd } |
| 88 |
|
| 89 |
constructor Create; |
| 90 |
destructor Destroy; override; |
| 91 |
procedure Start; |
| 92 |
procedure Update; |
| 93 |
property Running: Boolean read FRunning; |
| 94 |
|
| 95 |
{ Defined only after callling Start, once Running turned false. } |
| 96 |
property ExitStatus: Integer read FExitStatus; |
| 97 |
|
| 98 |
{ On some platforms, if you want to make sure to kill children |
| 99 |
(like child of "castle-engine run") you should run this before |
| 100 |
freeing the process. } |
| 101 |
procedure TerminateChildrenHarder; |
| 102 |
end; |
| 103 |
|
| 104 |
{ Call asynchronous processes, one after another, until the end |
| 105 |
or until one of them exits with non-zero status. |
| 106 |
|
| 107 |
This has deliberately similar API to TAsynchronousProcess, |
| 108 |
since (from the outside) calling a sequence of processes |
| 109 |
should have the same features as calling a single process. } |
| 110 |
TAsynchronousProcessQueue = class |
| 111 |
strict private |
| 112 |
FQueuePosition: Integer; |
| 113 |
AsyncProcess: TAsynchronousProcess; |
| 114 |
procedure CreateAsyncProcess; |
| 115 |
public |
| 116 |
type |
| 117 |
TQueueItem = class |
| 118 |
ExeName, CurrentDirectory: String; |
| 119 |
Parameters: TCastleStringList; |
| 120 |
constructor Create; |
| 121 |
destructor Destroy; override; |
| 122 |
end; |
| 123 |
|
| 124 |
TQueueItemList = specialize TObjectList<TQueueItem>; |
| 125 |
|
| 126 |
var |
| 127 |
Queue: TQueueItemList; |
| 128 |
OutputList: TOutputList; |
| 129 |
OnFinished, OnSuccessfullyFinishedAll: TNotifyEvent; |
| 130 |
|
| 131 |
constructor Create; |
| 132 |
destructor Destroy; override; |
| 133 |
procedure Start; |
| 134 |
procedure Update; |
| 135 |
function Running: Boolean; |
| 136 |
|
| 137 |
{ On some platforms, if you want to make sure to kill children |
| 138 |
(like child of "castle-engine run") you should run this before |
| 139 |
freeing the process. } |
| 140 |
procedure TerminateChildrenHarder; |
| 141 |
end; |
| 142 |
|
| 143 |
TPlatformInfo = class |
| 144 |
Target: TTarget; |
| 145 |
OS: TOS; |
| 146 |
CPU: TCPU; |
| 147 |
end; |
| 148 |
TPlatformInfoList = specialize TObjectList<TPlatformInfo>; |
| 149 |
|
| 150 |
procedure ErrorBox(const Message: String); |
| 151 |
procedure InfoBox(const Message: String); |
| 152 |
procedure WarningBox(const Message: String); |
| 153 |
function YesNoBox(const Message: String): Boolean; |
| 154 |
function YesNoBox(const Caption, Message: String): Boolean; |
| 155 |
|
| 156 |
{ Set both C.Enabled and C.Exists. } |
| 157 |
procedure SetEnabledVisible(const C: TControl; const Value: Boolean); |
| 158 |
|
| 159 |
const |
| 160 |
FpcRtlApiReferenceUrl = 'https://www.freepascal.org/docs-html/rtl/'; |
| 161 |
LclApiReferenceUrl = 'https://lazarus-ccr.sourceforge.io/docs/lcl/'; |
| 162 |
|
| 163 |
function ApiReferenceUrl: String; |
| 164 |
|
| 165 |
{ Get full URL to display API reference of a given property in the given |
| 166 |
PropertyObject. |
| 167 |
|
| 168 |
PropertyName may be '', in which case the link leads to the whole class reference. |
| 169 |
In this case PropertyNameForLink must also be ''. |
| 170 |
Both PropertyName and PropertyNameForLink should be '', |
| 171 |
or both should be non-empty. |
| 172 |
} |
| 173 |
function ApiReference(const PropertyObject: TObject; |
| 174 |
const PropertyName, PropertyNameForLink: String): String; |
| 175 |
|
| 176 |
{ Add to given submenus (TMenuItem) items for each registered serializable |
| 177 |
component. Thus it allows to construct menu to add all possible |
| 178 |
TCastleTransform instances, all possible TCastleUserInterface instances etc. |
| 179 |
Any ParentXxx may be nil, then relevant components are not added. |
| 180 |
|
| 181 |
All created menu items have OnClick set to OnClickEvent. } |
| 182 |
procedure BuildComponentsMenu( |
| 183 |
const ParentUserInterface, ParentTransform, ParentBehavior, ParentNonVisual: TMenuItem; |
| 184 |
const OnClickEvent: TNotifyEvent); |
| 185 |
|
| 186 |
type |
| 187 |
TCodeEditor = ( |
| 188 |
{ Autodetect one of the below (Lazarus, Delphi, VS Code) and use hardcoded logic suitable for it. } |
| 189 |
ceAutodetect, |
| 190 |
{ Use hardcoded logic suitable for Lazarus. } |
| 191 |
ceLazarus, |
| 192 |
{ Use hardcoded logic suitable for Delphi. } |
| 193 |
ceDelphi, |
| 194 |
{ Use hardcoded logic suitable for Visual Studio Code. } |
| 195 |
ceVSCode, |
| 196 |
{ Use custom commands from CodeEditor, CodeEditorProject. } |
| 197 |
ceCustom |
| 198 |
); |
| 199 |
|
| 200 |
const |
| 201 |
DefaultCodeEditor = ceAutodetect; |
| 202 |
|
| 203 |
var |
| 204 |
{ Which code editor to use. Current user preference. } |
| 205 |
CodeEditor: TCodeEditor; |
| 206 |
{ Code editor used to open Pascal files, when CodeEditor = ceCustom. } |
| 207 |
CodeEditorCommand: String; |
| 208 |
{ Code editor used to open Pascal files with line and number, when CodeEditor = ceCustom. } |
| 209 |
CodeEditorCommandLineColumn: String; |
| 210 |
{ Code editor used to open project, when CodeEditor = ceCustom. } |
| 211 |
CodeEditorCommandProject: String; |
| 212 |
|
| 213 |
var |
| 214 |
{ Which compiler to use (passed to build tool). Current user preference. } |
| 215 |
Compiler: TCompiler; |
| 216 |
|
| 217 |
const |
| 218 |
DefaultMuteOnRun = true; |
| 219 |
DefaultEditorVolume = 1.0; |
| 220 |
|
| 221 |
var |
| 222 |
{ Mute/restore when you run the application. } |
| 223 |
MuteOnRun: Boolean; |
| 224 |
EditorVolume: Single; |
| 225 |
|
| 226 |
{ Current state on "running application" for the purpose of implementing |
| 227 |
MuteOnRun. } |
| 228 |
RunningApplication: Boolean; |
| 229 |
|
| 230 |
{ Update SoundEngine.Volume based on |
| 231 |
global MuteOnRun, EditorVolume, RunningApplication. } |
| 232 |
procedure SoundEngineSetVolume; |
| 233 |
|
| 234 |
{ Update SoundEngine.Volume based on |
| 235 |
global MuteOnRun, RunningApplication and parameter FakeVolume. } |
| 236 |
procedure SoundEngineSetVolume(const FakeVolume: Single); |
| 237 |
|
| 238 |
{ Find Visual Studio Code, or '' if cannot find. } |
| 239 |
function FindExeVSCode(const ExceptionWhenMissing: Boolean): String; |
| 240 |
|
| 241 |
{ Return auto-detected code editor, never ceAutodetect. |
| 242 |
@eaises Exception if cannot autodetect, no IDE available. } |
| 243 |
function AutodetectCodeEditor: TCodeEditor; |
| 244 |
|
| 245 |
{ Consider this path as candidate for CastleEngineOverridePath, |
| 246 |
check (without setting anything) whether it would make sense. } |
| 247 |
function CgePathStatus(const CgePath: String; out StatusText: String): Boolean; |
| 248 |
function CgePathStatus(const CgePath: String): Boolean; |
| 249 |
|
| 250 |
{ Set SaveDialog.DefaultExt / Filter, based on ComponentToSave class. } |
| 251 |
procedure PrepareSaveDesignDialog(const SaveDialog: TSaveDialog; |
| 252 |
const ComponentToSave: TComponent); |
| 253 |
|
| 254 |
{ Should we use colors and icons for dark theme. Based on luminance. } |
| 255 |
function UseIconsAndColorsForDarkTheme: Boolean; |
| 256 |
|
| 257 |
type |
| 258 |
{ Saved selection state, |
| 259 |
that can be restored even after loading a new version of design from file. |
| 260 |
We save component by a Name (String) to avoid worrying about references |
| 261 |
to freed component, and to be able to restore it even on new design file. } |
| 262 |
TSavedSelection = record |
| 263 |
SelectedComponent: String; |
| 264 |
CurrentViewport: String; |
| 265 |
{ Index of the value selected/edited in the Object Inspector. } |
| 266 |
ItemIndex: Integer; |
| 267 |
{ Index of the tab of Object Inspector. } |
| 268 |
TabIndex: Integer; |
| 269 |
class function Equals(const A, B: TSavedSelection): Boolean; static; |
| 270 |
end; |
| 271 |
|
| 272 |
{ Show last file modification time as nice string. } |
| 273 |
function FileDateTimeStr(const FileName: String): String; |
| 274 |
|
| 275 |
type |
| 276 |
TComponentListEnumerator = record |
| 277 |
strict private |
| 278 |
FList: TComponentList; |
| 279 |
FPosition: Integer; |
| 280 |
function GetCurrent: TComponent; inline; |
| 281 |
public |
| 282 |
constructor Create(const AList: TComponentList); |
| 283 |
function MoveNext: Boolean; inline; |
| 284 |
property Current: TComponent read GetCurrent; |
| 285 |
end; |
| 286 |
|
| 287 |
{ Useful "for in" iterator over TComponentList, |
| 288 |
following FPC feature of custom enumerators: |
| 289 |
https://wiki.freepascal.org/for-in_loop } |
| 290 |
operator Enumerator(const A: TComponentList): TComponentListEnumerator; |
| 291 |
|
| 292 |
var |
| 293 |
{ Path to Android SDK (for ANDROID_HOME), and Java (JAVA_HOME). } |
| 294 |
AndroidHome, JavaHome: String; |
| 295 |
|
| 296 |
implementation |
| 297 |
|
| 298 |
uses |
| 299 |
SysUtils, Graphics, TypInfo, Generics.Defaults, Math, DateUtils, |
| 300 |
CastleUtils, CastleLog, CastleSoundEngine, CastleFilesUtils, CastleLclUtils, |
| 301 |
CastleComponentSerialize, CastleUiControls, CastleCameras, CastleTransform, |
| 302 |
CastleColors, CastleTimeUtils, CastleUriUtils, |
| 303 |
ToolCompilerInfo, ToolCommonUtils; |
| 304 |
|
| 305 |
procedure TMenuItemHelper.SetEnabledVisible(const Value: Boolean); |
| 306 |
begin |
| 307 |
Visible := Value; |
| 308 |
Enabled := Value; |
| 309 |
end; |
| 310 |
|
| 311 |
{ TAsynchronousProcessQueue.TQueueItem --------------------------------------- } |
| 312 |
|
| 313 |
constructor TAsynchronousProcessQueue.TQueueItem.Create; |
| 314 |
begin |
| 315 |
inherited; |
| 316 |
Parameters := TCastleStringList.Create; |
| 317 |
end; |
| 318 |
|
| 319 |
destructor TAsynchronousProcessQueue.TQueueItem.Destroy; |
| 320 |
begin |
| 321 |
FreeAndNil(Parameters); |
| 322 |
inherited; |
| 323 |
end; |
| 324 |
|
| 325 |
{ TAsynchronousProcessQueue -------------------------------------------------- } |
| 326 |
|
| 327 |
constructor TAsynchronousProcessQueue.Create; |
| 328 |
begin |
| 329 |
inherited; |
| 330 |
Queue := TQueueItemList.Create(true); |
| 331 |
FQueuePosition := -1; |
| 332 |
end; |
| 333 |
|
| 334 |
destructor TAsynchronousProcessQueue.Destroy; |
| 335 |
begin |
| 336 |
FreeAndNil(AsyncProcess); |
| 337 |
FreeAndNil(Queue); |
| 338 |
inherited; |
| 339 |
end; |
| 340 |
|
| 341 |
procedure TAsynchronousProcessQueue.Start; |
| 342 |
begin |
| 343 |
if Queue.Count = 0 then |
| 344 |
Exit; // nothing to do |
| 345 |
|
| 346 |
FQueuePosition := 0; |
| 347 |
CreateAsyncProcess; |
| 348 |
end; |
| 349 |
|
| 350 |
procedure TAsynchronousProcessQueue.CreateAsyncProcess; |
| 351 |
begin |
| 352 |
AsyncProcess := TAsynchronousProcess.Create; |
| 353 |
AsyncProcess.ExeName := Queue[FQueuePosition].ExeName; |
| 354 |
AsyncProcess.CurrentDirectory := Queue[FQueuePosition].CurrentDirectory; |
| 355 |
AsyncProcess.Parameters.Assign(Queue[FQueuePosition].Parameters); |
| 356 |
AsyncProcess.OutputList := OutputList; |
| 357 |
AsyncProcess.Start; |
| 358 |
end; |
| 359 |
|
| 360 |
procedure TAsynchronousProcessQueue.Update; |
| 361 |
var |
| 362 |
LastLineKind: TOutputKind; |
| 363 |
SuccessfullyFinishedAll: Boolean; |
| 364 |
begin |
| 365 |
if AsyncProcess <> nil then |
| 366 |
begin |
| 367 |
AsyncProcess.Update; |
| 368 |
if not AsyncProcess.Running then |
| 369 |
begin |
| 370 |
SuccessfullyFinishedAll := false; |
| 371 |
|
| 372 |
if AsyncProcess.ExitStatus <> 0 then |
| 373 |
begin |
| 374 |
LastLineKind := okError; |
| 375 |
FQueuePosition := Queue.Count; // abort executing rest |
| 376 |
end else |
| 377 |
begin |
| 378 |
Inc(FQueuePosition); |
| 379 |
LastLineKind := okImportantInfo; |
| 380 |
// if we just finished the last queue item, then success |
| 381 |
SuccessfullyFinishedAll := FQueuePosition = Queue.Count; |
| 382 |
end; |
| 383 |
OutputList.AddSeparator; |
| 384 |
OutputList.AddLine('Command finished with status ' + IntToStr(AsyncProcess.ExitStatus) + '.', |
| 385 |
LastLineKind); |
| 386 |
FreeAndNil(AsyncProcess); |
| 387 |
|
| 388 |
// create next process in queue |
| 389 |
if FQueuePosition < Queue.Count then |
| 390 |
CreateAsyncProcess |
| 391 |
else |
| 392 |
begin |
| 393 |
// no more processes (regardless if we done all, or interrupted because of some error) |
| 394 |
if Assigned(OnFinished) then |
| 395 |
OnFinished(Self); |
| 396 |
end; |
| 397 |
|
| 398 |
if SuccessfullyFinishedAll and Assigned(OnSuccessfullyFinishedAll) then |
| 399 |
OnSuccessfullyFinishedAll(Self); |
| 400 |
end; |
| 401 |
end; |
| 402 |
end; |
| 403 |
|
| 404 |
function TAsynchronousProcessQueue.Running: Boolean; |
| 405 |
begin |
| 406 |
Result := (FQueuePosition >= 0) and (FQueuePosition < Queue.Count); |
| 407 |
end; |
| 408 |
|
| 409 |
procedure TAsynchronousProcessQueue.TerminateChildrenHarder; |
| 410 |
begin |
| 411 |
if (AsyncProcess <> nil) and AsyncProcess.Running then |
| 412 |
AsyncProcess.TerminateChildrenHarder; |
| 413 |
end; |
| 414 |
|
| 415 |
{ TAsynchronousProcess ------------------------------------------------------- } |
| 416 |
|
| 417 |
constructor TAsynchronousProcess.Create; |
| 418 |
begin |
| 419 |
inherited; |
| 420 |
Parameters := TCastleStringList.Create; |
| 421 |
end; |
| 422 |
|
| 423 |
destructor TAsynchronousProcess.Destroy; |
| 424 |
begin |
| 425 |
FreeAndNil(Parameters); |
| 426 |
if (Process <> nil) and Process.Running then |
| 427 |
{ stop the process, TProcess destructor doesn't do it |
| 428 |
(https://www.freepascal.org/docs-html/fcl/process/tprocess.destroy.html) } |
| 429 |
Process.Terminate(0); |
| 430 |
FreeAndNil(Process); |
| 431 |
FreeAndNil(Environment); |
| 432 |
inherited; |
| 433 |
end; |
| 434 |
|
| 435 |
procedure TAsynchronousProcess.Start; |
| 436 |
var |
| 437 |
S, LogLine: String; |
| 438 |
begin |
| 439 |
{ copy environment } |
| 440 |
Environment := EnvironmentStrings; |
| 441 |
|
| 442 |
{ Extend PATH, to effectively use FpcCustomPath and LazarusCustomPath |
| 443 |
in the build tool. |
| 444 |
Initially we used to just pass them in special environment variables, |
| 445 |
and restore in ToolCompilerInfo initialization, but this is not enough: |
| 446 |
on Windows, calling "windres" requires that "cpp" is also on PATH. |
| 447 |
It seems more reliable to just add them to PATH. } |
| 448 |
Environment.Values['PATH'] := PathExtendForFpcLazarus(Environment.Values['PATH']); |
| 449 |
WritelnLog('Calling process with extended PATH: ' + Environment.Values['PATH']); |
| 450 |
|
| 451 |
{ Pass CASTLE_ENGINE_PATH to build tool, to use the same CGE as detected by editor. |
| 452 |
This means that e.g. editor that autodetects CGE (based on GetCastleEnginePathFromExeName, |
| 453 |
because editor exe is in <cge>/tools/castle-editor/castle-editor) |
| 454 |
invokes build tool in local bin (like ~/bin) |
| 455 |
and the build tool uses the same <cge> as detected by editor. } |
| 456 |
if CastleEnginePath <> '' then |
| 457 |
begin |
| 458 |
Environment.Values['CASTLE_ENGINE_PATH'] := CastleEnginePath; |
| 459 |
WritelnLog('Calling process with extended CASTLE_ENGINE_PATH: ' + Environment.Values['CASTLE_ENGINE_PATH']); |
| 460 |
end; |
| 461 |
|
| 462 |
if AndroidHome <> '' then |
| 463 |
Environment.Values['ANDROID_HOME'] := AndroidHome; |
| 464 |
if JavaHome <> '' then |
| 465 |
Environment.Values['JAVA_HOME'] := JavaHome; |
| 466 |
|
| 467 |
{ create Process and call Process.Execute } |
| 468 |
Process := TProcess.Create(nil); |
| 469 |
Process.Executable := ExeName; |
| 470 |
if CurrentDirectory <> '' then |
| 471 |
Process.CurrentDirectory := CurrentDirectory; |
| 472 |
for S in Parameters do |
| 473 |
Process.Parameters.Add(S); |
| 474 |
{ on Windows, we need swoHide, otherwise console appears for build tool. |
| 475 |
Note that poNoConsole is not a solution, as it prevents castle-engine |
| 476 |
console, but FPC (called by castle-engine) console is still visible. } |
| 477 |
Process.ShowWindow := swoHide; |
| 478 |
Process.Options := [poUsePipes, poStderrToOutput]; |
| 479 |
Process.Environment := Environment; |
| 480 |
Process.Execute; |
| 481 |
|
| 482 |
{ since the process is executed independently, in theory it *could* |
| 483 |
finish before we get to next line, possibly? } |
| 484 |
//if not Process.Running then |
| 485 |
// raise EInternalError.Create('Process not running right after Execute?'); |
| 486 |
|
| 487 |
{ log in OutputList } |
| 488 |
LogLine := 'Running "' + ExeName; |
| 489 |
for S in Parameters do |
| 490 |
LogLine += ' ' + S; |
| 491 |
LogLine += '"'; |
| 492 |
OutputList.AddLine(LogLine, okImportantInfo); |
| 493 |
OutputList.AddSeparator; |
| 494 |
|
| 495 |
FRunning := true; |
| 496 |
end; |
| 497 |
|
| 498 |
procedure TAsynchronousProcess.Update; |
| 499 |
|
| 500 |
function LineProcessInternalInfo(const Line: String): Boolean; |
| 501 |
const |
| 502 |
Prefix = 'Castle Game Engine Internal: ProcessID: '; |
| 503 |
var |
| 504 |
ParsedProcessId: TProcessId; |
| 505 |
begin |
| 506 |
Result := false; |
| 507 |
if IsPrefix(Prefix, Line, false) and |
| 508 |
TryStrToInt64(PrefixRemove(Prefix, Line, false), ParsedProcessId) then |
| 509 |
begin |
| 510 |
ChildProcessId := ParsedProcessId; |
| 511 |
Result := true; |
| 512 |
end; |
| 513 |
end; |
| 514 |
|
| 515 |
function LineOutputKind(const Line: String): TOutputKind; |
| 516 |
begin |
| 517 |
if (Pos(') Error', Line) <> 0) or |
| 518 |
(Pos(') Fatal', Line) <> 0) then |
| 519 |
Result := okError |
| 520 |
else |
| 521 |
if (Pos(') Warning', Line) <> 0) then |
| 522 |
Result := okWarning |
| 523 |
else |
| 524 |
Result := okInfo; |
| 525 |
end; |
| 526 |
|
| 527 |
const |
| 528 |
ReadMaxSize = 65536; |
| 529 |
var |
| 530 |
Buffer: array [0..ReadMaxSize - 1] of Byte; |
| 531 |
ReadBytes: LongInt; |
| 532 |
NewLinePos, OldPendingLinesLen, ProcessedLength: Integer; |
| 533 |
Line: String; |
| 534 |
begin |
| 535 |
{ In case Process.Running=false, set our own FRunning and FExitStatus. |
| 536 |
Note that we don't make a method like TAsynchronousProcess.Running that |
| 537 |
simply returnsProcess.Running, as then we could report as Running=false |
| 538 |
a process that didn't yet dump all it's output to OutputList. } |
| 539 |
if not Process.Running then |
| 540 |
begin |
| 541 |
FRunning := false; |
| 542 |
FExitStatus := Process.ExitStatus; |
| 543 |
end; |
| 544 |
|
| 545 |
{ Read Process.Output. |
| 546 |
This must never hang waiting for process, so we check NumBytesAvailable. } |
| 547 |
if Process.Output.NumBytesAvailable <> 0 then |
| 548 |
ReadBytes := Process.Output.Read(Buffer[0], ReadMaxSize) |
| 549 |
else |
| 550 |
ReadBytes := 0; |
| 551 |
|
| 552 |
// early exit for most usual case when ReadBytes = 0 |
| 553 |
if ReadBytes = 0 then |
| 554 |
begin |
| 555 |
{ Once the process is not running, and we read everything (ReadBytes = 0), |
| 556 |
dump the remaining PendingLines as last line. } |
| 557 |
if (not Process.Running) and (Length(PendingLines) <> 0) then |
| 558 |
begin |
| 559 |
if not LineProcessInternalInfo(Line) then |
| 560 |
OutputList.AddLine(PendingLines, LineOutputKind(PendingLines)); |
| 561 |
PendingLines := ''; |
| 562 |
end; |
| 563 |
Exit; |
| 564 |
end; |
| 565 |
|
| 566 |
// add Buffer to PendingLines |
| 567 |
OldPendingLinesLen := Length(PendingLines); |
| 568 |
SetLength(PendingLines, OldPendingLinesLen + ReadBytes); |
| 569 |
Move(Buffer[0], PendingLines[OldPendingLinesLen + 1], ReadBytes); |
| 570 |
|
| 571 |
// extract from PendingLines complete lines (as many as possible) |
| 572 |
repeat |
| 573 |
{ Note: it's tempting to try to detect #13#10 (Windows), |
| 574 |
or #10 (Unix), |
| 575 |
or #13 (nothing supported), |
| 576 |
or #10#13 (nothing supported) |
| 577 |
sequence, |
| 578 |
by detecting first char (#13 or #10) and then remove the following |
| 579 |
(if #10 is followed or #13, or #13 is followed by #10). |
| 580 |
But that's risky: if the read chunks will be split right in the middle |
| 581 |
of #13#10, we would introduce an extra newline in the intepreted output. |
| 582 |
|
| 583 |
It's easier *and* more reliable to just detect newlines as #10, |
| 584 |
and simply ignore any #13 character if it is followed by #10. |
| 585 |
This works for Windows and Unix, which is OK for us. } |
| 586 |
|
| 587 |
NewLinePos := Pos(#10, PendingLines); |
| 588 |
if NewLinePos <> 0 then |
| 589 |
begin |
| 590 |
ProcessedLength := NewLinePos; |
| 591 |
|
| 592 |
if SCharIs(PendingLines, NewLinePos - 1, #13) then |
| 593 |
Dec(NewLinePos); |
| 594 |
Line := Copy(PendingLines, 1, NewLinePos - 1); |
| 595 |
if not LineProcessInternalInfo(Line) then |
| 596 |
OutputList.AddLine(Line, LineOutputKind(Line)); |
| 597 |
|
| 598 |
PendingLines := SEnding(PendingLines, ProcessedLength + 1); |
| 599 |
end else |
| 600 |
Break; |
| 601 |
until false; |
| 602 |
|
| 603 |
// if we get a lot of output, maybe more than ReadMaxSize, then keep reading! |
| 604 |
if ReadMaxSize = ReadBytes then |
| 605 |
Update; |
| 606 |
end; |
| 607 |
|
| 608 |
procedure TAsynchronousProcess.TerminateChildrenHarder; |
| 609 |
begin |
| 610 |
if ChildProcessId <> 0 then |
| 611 |
begin |
| 612 |
OutputList.AddLine(Format('Stopping child process (id: %d)', [ChildProcessId]), okInfo); |
| 613 |
StopProcess(ChildProcessId); |
| 614 |
end; |
| 615 |
end; |
| 616 |
|
| 617 |
{ TOutputList --------------------------------------------------------------- } |
| 618 |
|
| 619 |
procedure TOutputList.DrawItem(Control: TWinControl; Index: Integer; |
| 620 |
ARect: TRect; State: TOwnerDrawState); |
| 621 |
|
| 622 |
{ Copied from LCL stdctrls.pp } |
| 623 |
procedure InternalDrawItem(Control: TControl; |
| 624 |
Canvas: TCanvas; ARect: TRect; const Text: string); |
| 625 |
var |
| 626 |
OldBrushStyle: TBrushStyle; |
| 627 |
OldTextStyle: TTextStyle; |
| 628 |
NewTextStyle: TTextStyle; |
| 629 |
begin |
| 630 |
OldBrushStyle := Canvas.Brush.Style; |
| 631 |
Canvas.Brush.Style := bsClear; |
| 632 |
|
| 633 |
OldTextStyle := Canvas.TextStyle; |
| 634 |
NewTextStyle := OldTextStyle; |
| 635 |
NewTextStyle.Layout := tlCenter; |
| 636 |
NewTextStyle.RightToLeft := Control.UseRightToLeftReading; |
| 637 |
if Control.UseRightToLeftAlignment then |
| 638 |
begin |
| 639 |
NewTextStyle.Alignment := taRightJustify; |
| 640 |
ARect.Right := ARect.Right - 2; |
| 641 |
end |
| 642 |
else |
| 643 |
begin |
| 644 |
NewTextStyle.Alignment := taLeftJustify; |
| 645 |
ARect.Left := ARect.Left + 2; |
| 646 |
end; |
| 647 |
|
| 648 |
Canvas.TextStyle := NewTextStyle; |
| 649 |
|
| 650 |
Canvas.TextRect(ARect, ARect.Left, ARect.Top, Text); |
| 651 |
Canvas.Brush.Style := OldBrushStyle; |
| 652 |
Canvas.TextStyle := OldTextStyle; |
| 653 |
end; |
| 654 |
|
| 655 |
var |
| 656 |
C: TCanvas; |
| 657 |
OutputInfo: TOutputInfo; |
| 658 |
begin |
| 659 |
C := List.Canvas; |
| 660 |
OutputInfo := List.Items.Objects[Index] as TOutputInfo; |
| 661 |
|
| 662 |
{ (On Windows only) |
| 663 |
For unknown reason (LCL bug? our bug?), sometimes OutputInfo is nil, |
| 664 |
even though we clear the List.Items and we always add to it by |
| 665 |
AddLine (List.Items.AddObject(S, OutputInfoPerKind[Kind])). |
| 666 |
|
| 667 |
This can be reproduced in various situations if you hit F9: |
| 668 |
1. after switching from Lazarus -> CGE editor |
| 669 |
2. open CGE editor, some project, and use "Restart Editor (with custom components)" |
| 670 |
3. open CGE editor, some project, open some design, hit F9 |
| 671 |
|
| 672 |
It is *not* 100% reproducible always. Case 1. and 2. was definitely |
| 673 |
observed (by Michalis and others) but the crash can just disappear by itself |
| 674 |
after you reproduce it a few times. |
| 675 |
Case 3. proved to be most reliably reproducible. |
| 676 |
|
| 677 |
Seen with |
| 678 |
- Lazarus 2.0.10 + FPC 3.2.0 |
| 679 |
- Lazarus 2.0.12 + FPC 3.2.2 |
| 680 |
Only seen on Windows/x86_64 (though we make a workaround general). |
| 681 |
|
| 682 |
The log confirms the problem: |
| 683 |
- AddLine added |
| 684 |
'Running "...\castle-engine.exe --mode=debug compile"' |
| 685 |
with OutputInfo <> nil |
| 686 |
- But then TOutputList.DrawItem is executed with |
| 687 |
Index = 0 |
| 688 |
List.Items[Index] = 'Running "...\castle-engine.exe --mode=debug compile"' |
| 689 |
OutputInfo = nil |
| 690 |
} |
| 691 |
|
| 692 |
if OutputInfo <> nil then |
| 693 |
begin |
| 694 |
case OutputInfo.Kind of |
| 695 |
okImportantInfo: C.Font.Bold := true; |
| 696 |
okWarning: |
| 697 |
begin |
| 698 |
C.Brush.Color := clYellow; |
| 699 |
{ If the font color is too light on yellow, change it } |
| 700 |
if GrayscaleValue(ColorToVector3(C.Font.Color)) > 0.75 then |
| 701 |
begin |
| 702 |
if List.ItemIndex = Index then |
| 703 |
C.Font.Color := clBlue |
| 704 |
else |
| 705 |
C.Font.Color := clBlack; |
| 706 |
end; |
| 707 |
end; |
| 708 |
okError: C.Brush.Color := clRed; |
| 709 |
else ; // no need to customize font/brush otherwise |
| 710 |
end; |
| 711 |
end else |
| 712 |
begin |
| 713 |
WritelnLog('Missing OutputInfo for the line "%s", known bug (probably in LCL) on Windows', [ |
| 714 |
List.Items[Index] |
| 715 |
]); |
| 716 |
// assuming okImportantInfo, as the problem occurs with 1st line in list |
| 717 |
C.Font.Bold := true; |
| 718 |
end; |
| 719 |
|
| 720 |
C.FillRect(ARect); |
| 721 |
InternalDrawItem(List, C, ARect, List.Items[Index]); |
| 722 |
end; |
| 723 |
|
| 724 |
constructor TOutputList.Create(const AList: TListBox); |
| 725 |
|
| 726 |
{ Measure font height in pixels. } |
| 727 |
function GetRowHeight: Integer; |
| 728 |
begin |
| 729 |
FList.Canvas.Font := FList.Font; |
| 730 |
Result := FList.Canvas.TextHeight('Wg'); |
| 731 |
end; |
| 732 |
|
| 733 |
var |
| 734 |
Kind: TOutputKind; |
| 735 |
begin |
| 736 |
inherited Create; |
| 737 |
|
| 738 |
FList := AList; |
| 739 |
FList.Style := lbOwnerDrawFixed; |
| 740 |
FList.OnDrawItem := @DrawItem; |
| 741 |
FList.ItemHeight := GetRowHeight + 4; |
| 742 |
|
| 743 |
{ Create OutputInfoPerKind instances. |
| 744 |
The idea is that every AddLine call should not construct new |
| 745 |
TOutputInfo, this could eat quite some memory for a lot of lines. |
| 746 |
Instead most AddLine calls can just take OutputInfoPerKind[Kind]. } |
| 747 |
for Kind in TOutputKind do |
| 748 |
begin |
| 749 |
OutputInfoPerKind[Kind] := TOutputInfo.Create; |
| 750 |
OutputInfoPerKind[Kind].Kind := Kind; |
| 751 |
end; |
| 752 |
end; |
| 753 |
|
| 754 |
destructor TOutputList.Destroy; |
| 755 |
var |
| 756 |
Kind: TOutputKind; |
| 757 |
begin |
| 758 |
for Kind in TOutputKind do |
| 759 |
FreeAndNil(OutputInfoPerKind[Kind]); |
| 760 |
inherited; |
| 761 |
end; |
| 762 |
|
| 763 |
procedure TOutputList.AddLine(const S: String; const Kind: TOutputKind); |
| 764 |
var |
| 765 |
IsBottom: Boolean; |
| 766 |
begin |
| 767 |
{ |
| 768 |
IsBottom := |
| 769 |
(List.Items.Count = 0) or |
| 770 |
|
| 771 |
// TODO: doesn't work reliably |
| 772 |
// List.ItemFullyVisible(List.Items.Count - 1) |
| 773 |
|
| 774 |
// TODO: doesn't work reliably |
| 775 |
// checking is visible "List.Items.Count - 1" is not good enough |
| 776 |
(List.Items.Count = 1) or |
| 777 |
List.ItemVisible(List.Items.Count - 2); |
| 778 |
} |
| 779 |
IsBottom := true; |
| 780 |
|
| 781 |
List.Items.AddObject(S, OutputInfoPerKind[Kind]); |
| 782 |
|
| 783 |
{ scroll to bottom once new message received, |
| 784 |
but only if already looking at bottom (otherwise we would prevent |
| 785 |
user from easily browsing past messages). } |
| 786 |
if IsBottom then |
| 787 |
List.TopIndex := List.Items.Count - 1; |
| 788 |
end; |
| 789 |
|
| 790 |
(* Unused: |
| 791 |
|
| 792 |
procedure TOutputList.SplitAddLines(const S: String; const Kind: TOutputKind); |
| 793 |
var |
| 794 |
SL: TStringList; |
| 795 |
Line: String; |
| 796 |
begin |
| 797 |
SL := TStringList.Create; |
| 798 |
try |
| 799 |
SL.Text := S; |
| 800 |
{ remove empty lines at end } |
| 801 |
while (SL.Count <> 0) and (SL[SL.Count - 1] = '') do |
| 802 |
SL.Delete(SL.Count - 1); |
| 803 |
for Line in SL do |
| 804 |
AddLine(Line, Kind); |
| 805 |
finally FreeAndNil(SL) end; |
| 806 |
end; |
| 807 |
*) |
| 808 |
|
| 809 |
procedure TOutputList.AddSeparator; |
| 810 |
begin |
| 811 |
AddLine('', okInfo); |
| 812 |
end; |
| 813 |
|
| 814 |
procedure TOutputList.Clear; |
| 815 |
begin |
| 816 |
List.Items.Clear; |
| 817 |
end; |
| 818 |
|
| 819 |
{ global routines ------------------------------------------------------------ } |
| 820 |
|
| 821 |
procedure ErrorBox(const Message: String); |
| 822 |
begin |
| 823 |
MessageDlg('Error', Message, mtError, [mbOK], 0); |
| 824 |
end; |
| 825 |
|
| 826 |
procedure InfoBox(const Message: String); |
| 827 |
begin |
| 828 |
MessageDlg('Information', Message, mtInformation, [mbOK], 0); |
| 829 |
end; |
| 830 |
|
| 831 |
procedure WarningBox(const Message: String); |
| 832 |
begin |
| 833 |
MessageDlg('Warning', Message, mtWarning, [mbOK], 0); |
| 834 |
end; |
| 835 |
|
| 836 |
function YesNoBox(const Message: String): Boolean; |
| 837 |
begin |
| 838 |
Result := MessageDlg('Question', Message, mtConfirmation, [mbYes, mbNo], 0) = mrYes; |
| 839 |
end; |
| 840 |
|
| 841 |
function YesNoBox(const Caption, Message: String): Boolean; |
| 842 |
begin |
| 843 |
Result := MessageDlg(Caption, Message, mtConfirmation, [mbYes, mbNo], 0) = mrYes; |
| 844 |
end; |
| 845 |
|
| 846 |
procedure SetEnabledVisible(const C: TControl; const Value: Boolean); |
| 847 |
begin |
| 848 |
C.Enabled := Value; |
| 849 |
C.Visible := Value; |
| 850 |
end; |
| 851 |
|
| 852 |
function ApiReferenceUrl: String; |
| 853 |
begin |
| 854 |
Result := ApiReferenceUrlCore(CastleEnginePath); |
| 855 |
end; |
| 856 |
|
| 857 |
function ApiReference(const PropertyObject: TObject; |
| 858 |
const PropertyName, PropertyNameForLink: String): String; |
| 859 |
|
| 860 |
{ Knowing that property PropInfo is part of class C, |
| 861 |
determine the class where it's actually declared (C or ancestor of C). |
| 862 |
|
| 863 |
Note that we search for property using PropInfo, not a string PropertyName, |
| 864 |
this means that we don't mix a property with the same name that |
| 865 |
obscures ancestor property (same name, but actually different property). } |
| 866 |
function ClassOfPropertyDeclaration(const C: TClass; const PropInfo: PPropInfo): TClass; |
| 867 |
var |
| 868 |
ParentC: TClass; |
| 869 |
PropList: PPropList; |
| 870 |
PropCount, I: Integer; |
| 871 |
begin |
| 872 |
ParentC := C.ClassParent; |
| 873 |
if ParentC = nil then |
| 874 |
Exit(C); // no ancestor |
| 875 |
|
| 876 |
PropCount := GetPropList(ParentC, PropList); |
| 877 |
for I := 0 to PropCount - 1 do |
| 878 |
if PropList^[I] = PropInfo then |
| 879 |
// property found in ancestor |
| 880 |
Exit(ClassOfPropertyDeclaration(ParentC, PropInfo)); |
| 881 |
|
| 882 |
// property not found in ancestor |
| 883 |
Exit(C); |
| 884 |
end; |
| 885 |
|
| 886 |
function FpDocSuffix(const LinkUnitName, LinkClassName, LinkPropertyName: String): String; |
| 887 |
begin |
| 888 |
Result := LowerCase(LinkUnitName) + '/' + LowerCase(LinkClassName) + '.'; |
| 889 |
if LinkPropertyName <> '' then |
| 890 |
Result := Result + LowerCase(LinkPropertyName) + '.'; |
| 891 |
Result := Result + 'html'; |
| 892 |
end; |
| 893 |
|
| 894 |
function PasDocSuffix(const LinkUnitName, LinkClassName, LinkPropertyName: String): String; |
| 895 |
begin |
| 896 |
Result := LinkUnitName + '.' + LinkClassName + '.html'; |
| 897 |
if LinkPropertyName <> '' then |
| 898 |
Result := Result + '#' + LinkPropertyName; |
| 899 |
end; |
| 900 |
|
| 901 |
var |
| 902 |
LinkUnitName, LinkClassName, LowerLinkUnitName, LinkPropertyName: String; |
| 903 |
PropInfo: PPropInfo; |
| 904 |
ClassOfProperty: TClass; |
| 905 |
begin |
| 906 |
LinkUnitName := PropertyObject.UnitName; |
| 907 |
LinkClassName := PropertyObject.ClassName; |
| 908 |
LinkPropertyName := ''; |
| 909 |
|
| 910 |
if PropertyName <> '' then |
| 911 |
begin |
| 912 |
{ PropertyName doesn't necessarily belong to the exact PropertyObject class, |
| 913 |
it may belong to ancestor. E.g. TCastleScene.Url is actually from |
| 914 |
TCastleSceneCore. |
| 915 |
This is important to construct API links. |
| 916 |
Unfortunately GetPropInfo doesn't have this info directly. } |
| 917 |
|
| 918 |
PropInfo := GetPropInfo(PropertyObject, PropertyName); |
| 919 |
if PropInfo <> nil then |
| 920 |
begin |
| 921 |
ClassOfProperty := ClassOfPropertyDeclaration(PropertyObject.ClassType, PropInfo); |
| 922 |
LinkClassName := ClassOfProperty.ClassName; |
| 923 |
LinkUnitName := ClassOfProperty.UnitName; |
| 924 |
LinkPropertyName := PropertyNameForLink; |
| 925 |
end else |
| 926 |
WritelnWarning('Cannot get property info "%s"', [PropertyName]); |
| 927 |
end; |
| 928 |
|
| 929 |
{ construct UrlSuffix, knowing LinkUnit/Class/MemberName } |
| 930 |
LowerLinkUnitName := LowerCase(LinkUnitName); |
| 931 |
if (LowerLinkUnitName = 'sysutils') or |
| 932 |
(LowerLinkUnitName = 'math') or |
| 933 |
(LowerLinkUnitName = 'system') or |
| 934 |
(LowerLinkUnitName = 'classes') then |
| 935 |
begin |
| 936 |
// adjust for fpdoc links in FPC |
| 937 |
Result := FpcRtlApiReferenceUrl + FpDocSuffix(LinkUnitName, LinkClassName, LinkPropertyName); |
| 938 |
end else |
| 939 |
if (LowerLinkUnitName = 'controls') then |
| 940 |
begin |
| 941 |
// adjust for fpdoc links in LCL |
| 942 |
Result := LclApiReferenceUrl + FpDocSuffix(LinkUnitName, LinkClassName, LinkPropertyName); |
| 943 |
end else |
| 944 |
begin |
| 945 |
// adjust for PasDoc links in CGE |
| 946 |
Result := ApiReferenceUrl + PasDocSuffix(LinkUnitName, LinkClassName, LinkPropertyName); |
| 947 |
end; |
| 948 |
end; |
| 949 |
|
| 950 |
function CompareRegisteredComponent({$ifdef GENERICS_CONSTREF}constref{$else}const{$endif} |
| 951 |
Left, Right: TRegisteredComponent): Integer; |
| 952 |
var |
| 953 |
I: Integer; |
| 954 |
begin |
| 955 |
for I := 0 to Min(Length(Left.Caption), Length(Right.Caption)) - 1 do |
| 956 |
begin |
| 957 |
Result := AnsiCompareStr(Left.Caption[I], Right.Caption[I]); |
| 958 |
if Result <> 0 then Exit; |
| 959 |
end; |
| 960 |
|
| 961 |
{ When all common parts are the same, let the shorter one be considered smaller. |
| 962 |
So < 0 when Length(Left.Caption) < Length(Right.Caption). } |
| 963 |
Result := Length(Left.Caption) - Length(Right.Caption); |
| 964 |
end; |
| 965 |
|
| 966 |
procedure BuildComponentsMenu( |
| 967 |
const ParentUserInterface, ParentTransform, ParentBehavior, ParentNonVisual: TMenuItem; |
| 968 |
const OnClickEvent: TNotifyEvent); |
| 969 |
|
| 970 |
function CreateMenuItemForComponent(const OwnerAndParent: TMenuItem; |
| 971 |
const R: TRegisteredComponent; const CaptionPart: Integer = 0): TMenuItem; |
| 972 |
var |
| 973 |
S: String; |
| 974 |
begin |
| 975 |
if OwnerAndParent = nil then |
| 976 |
Exit; // exit if relevant ParentXxx is nil |
| 977 |
|
| 978 |
if CaptionPart = Length(R.Caption) - 1 then |
| 979 |
begin |
| 980 |
{ create last part, to actually invoke OnClickEvent } |
| 981 |
Result := TMenuItem.Create(OwnerAndParent); |
| 982 |
S := R.Caption[CaptionPart] + ' (' + R.ComponentClass.ClassName + ')'; |
| 983 |
if R.IsDeprecated then |
| 984 |
S := '(Deprecated) ' + S; |
| 985 |
Result.Caption := S; |
| 986 |
Result.Tag := PtrInt(Pointer(R)); |
| 987 |
Result.OnClick := OnClickEvent; |
| 988 |
OwnerAndParent.Add(Result); |
| 989 |
end else |
| 990 |
begin |
| 991 |
{ create intermediate submenu part } |
| 992 |
Result := OwnerAndParent.Find(R.Caption[CaptionPart]); |
| 993 |
if Result = nil then |
| 994 |
begin |
| 995 |
Result := TMenuItem.Create(OwnerAndParent); |
| 996 |
Result.Caption := R.Caption[CaptionPart]; |
| 997 |
OwnerAndParent.Add(Result); |
| 998 |
end; |
| 999 |
{ recursive call to create deeper menu level } |
| 1000 |
CreateMenuItemForComponent(Result, R, CaptionPart + 1); |
| 1001 |
end; |
| 1002 |
end; |
| 1003 |
|
| 1004 |
type |
| 1005 |
TRegisteredComponentComparer = specialize TComparer<TRegisteredComponent>; |
| 1006 |
var |
| 1007 |
R: TRegisteredComponent; |
| 1008 |
begin |
| 1009 |
{ While RegisteredComponents is documented as "read-only", |
| 1010 |
we knowingly break it here for internal CGE purposes. |
| 1011 |
We need some reliable order of this list (as "RegisterSerializableComponent" may be called in any order), |
| 1012 |
for now alphabetic order seems good enough. } |
| 1013 |
RegisteredComponents.Sort(TRegisteredComponentComparer.Construct(@CompareRegisteredComponent)); |
| 1014 |
|
| 1015 |
{ add non-deprecated components } |
| 1016 |
for R in RegisteredComponents do |
| 1017 |
if not R.IsDeprecated then |
| 1018 |
begin |
| 1019 |
if R.ComponentClass.InheritsFrom(TCastleUserInterface) then |
| 1020 |
CreateMenuItemForComponent(ParentUserInterface, R) |
| 1021 |
else |
| 1022 |
if R.ComponentClass.InheritsFrom(TCastleTransform) then |
| 1023 |
CreateMenuItemForComponent(ParentTransform, R) |
| 1024 |
else |
| 1025 |
if R.ComponentClass.InheritsFrom(TCastleBehavior) then |
| 1026 |
CreateMenuItemForComponent(ParentBehavior, R) |
| 1027 |
else |
| 1028 |
CreateMenuItemForComponent(ParentNonVisual, R); |
| 1029 |
end; |
| 1030 |
|
| 1031 |
(* |
| 1032 |
Don't show deprecated for now, keep the menu clean. |
| 1033 |
|
| 1034 |
{ add separators from deprecated } |
| 1035 |
MenuItem := TMenuItem.Create(ParentUserInterface); |
| 1036 |
MenuItem.Caption := '-'; |
| 1037 |
ParentUserInterface.Add(MenuItem); |
| 1038 |
|
| 1039 |
MenuItem := TMenuItem.Create(ParentTransform); |
| 1040 |
MenuItem.Caption := '-'; |
| 1041 |
ParentTransform.Add(MenuItem); |
| 1042 |
|
| 1043 |
{ add deprecated components } |
| 1044 |
for R in RegisteredComponents do |
| 1045 |
if R.IsDeprecated then |
| 1046 |
begin |
| 1047 |
... same code as above |
| 1048 |
end; |
| 1049 |
*) |
| 1050 |
end; |
| 1051 |
|
| 1052 |
procedure SoundEngineSetVolume; |
| 1053 |
begin |
| 1054 |
SoundEngineSetVolume(EditorVolume); |
| 1055 |
end; |
| 1056 |
|
| 1057 |
procedure SoundEngineSetVolume(const FakeVolume: Single); |
| 1058 |
begin |
| 1059 |
if MuteOnRun and RunningApplication then |
| 1060 |
SoundEngine.Volume := 0 |
| 1061 |
else |
| 1062 |
SoundEngine.Volume := FakeVolume; |
| 1063 |
end; |
| 1064 |
|
| 1065 |
function FindExeVSCode(const ExceptionWhenMissing: Boolean): String; |
| 1066 |
{$ifdef DARWIN} |
| 1067 |
const |
| 1068 |
MacVSCodePath = '/Applications/Visual Studio Code.app/Contents/MacOS/Electron'; |
| 1069 |
{$endif} |
| 1070 |
begin |
| 1071 |
Result := FindExe('code'); |
| 1072 |
|
| 1073 |
(*Failed workaround to fix handling filenames/dirnames with spaces inside |
| 1074 |
as "code" arguments. |
| 1075 |
I hoped that calling code.exe directly, instead of code.cmd, will help |
| 1076 |
-- it doesn't. |
| 1077 |
|
| 1078 |
{$ifdef MSWINDOWS} |
| 1079 |
if (Result <> '') and (SameText(ExtractFileName(Result), 'code.cmd')) then |
| 1080 |
Result := ParentPath(ExtractFileDir(Result), false) + 'code.exe'; |
| 1081 |
{$endif} |
| 1082 |
*) |
| 1083 |
|
| 1084 |
{$ifdef DARWIN} |
| 1085 |
if (Result = '') and (FileExists(MacVSCodePath)) then |
| 1086 |
Result := MacVSCodePath; |
| 1087 |
{$endif} |
| 1088 |
|
| 1089 |
if (Result = '') and ExceptionWhenMissing then |
| 1090 |
raise EExecutableNotFound.Create('Cannot find Visual Studio Code. Make sure it is installed, and available on environment variable $PATH (there should be an option to set this up during VS Code installlation).'); |
| 1091 |
end; |
| 1092 |
|
| 1093 |
(*Another failed workaround to fix handling filenames/dirnames with spaces inside |
| 1094 |
as "code" arguments. |
| 1095 |
I hoped that using URL, with spaces encoded as %20, will solve the problem. |
| 1096 |
Unfortunately VS code doesn't seem to understand %20 in URLs at all. |
| 1097 |
|
| 1098 |
function PathToVSCode(const Path: String): String; |
| 1099 |
var |
| 1100 |
URI: TURI; |
| 1101 |
begin |
| 1102 |
Result := Path; |
| 1103 |
Assert(IsPathAbsolute(Path)); |
| 1104 |
|
| 1105 |
FillByte(URI, SizeOf(URI), 0); |
| 1106 |
URI.Protocol := 'vscode'; |
| 1107 |
{ We want // between vscode and "file", see |
| 1108 |
https://code.visualstudio.com/docs/editor/command-line } |
| 1109 |
URI.HasAuthority := true; |
| 1110 |
URI.Document := 'file' + |
| 1111 |
{$ifdef MSWINDOWS} '/' + SReplaceChars(Path, '\', '/') |
| 1112 |
{$else} Path |
| 1113 |
{$endif}; |
| 1114 |
Result := EncodeURI(URI); |
| 1115 |
end; |
| 1116 |
*) |
| 1117 |
|
| 1118 |
function AutodetectCodeEditor: TCodeEditor; |
| 1119 |
begin |
| 1120 |
if FindExeLazarusIDE(false) <> '' then |
| 1121 |
Result := ceLazarus |
| 1122 |
else |
| 1123 |
if FindDelphiPath(false) <> '' then |
| 1124 |
Result := ceDelphi |
| 1125 |
else |
| 1126 |
if FindExeVSCode(false) <> '' then |
| 1127 |
Result := ceVSCode |
| 1128 |
else |
| 1129 |
raise Exception.Create('Cannot auto-detect IDE. Install one of the supported IDEs: Lazarus, Delphi or Visual Studio Code.'); |
| 1130 |
end; |
| 1131 |
|
| 1132 |
function CgePathStatus(const CgePath: String; out StatusText: String): Boolean; |
| 1133 |
|
| 1134 |
function RemoveCommitHash(const Ver: String): String; |
| 1135 |
var |
| 1136 |
I: Integer; |
| 1137 |
begin |
| 1138 |
I := Pos(' (commit ', Ver); |
| 1139 |
if I <> 0 then |
| 1140 |
Result := Copy(Ver, 1, I - 1) |
| 1141 |
else |
| 1142 |
Result := Ver; |
| 1143 |
end; |
| 1144 |
|
| 1145 |
var |
| 1146 |
VersionFile, VersionLine, Version, EditorVersion: String; |
| 1147 |
VersionContentsList: TStringList; |
| 1148 |
begin |
| 1149 |
if CgePath = '' then |
| 1150 |
begin |
| 1151 |
StatusText := 'Status: Cannot auto-detect engine location, set it manually above'; |
| 1152 |
Exit(false); |
| 1153 |
end; |
| 1154 |
|
| 1155 |
VersionFile := InclPathDelim(CgePath) + 'src' + PathDelim + |
| 1156 |
'base' + PathDelim + 'castleversion.inc'; |
| 1157 |
if not FileExists(VersionFile) then |
| 1158 |
begin |
| 1159 |
StatusText := Format('Status: Invalid, cannot find file "%s"', [VersionFile]); |
| 1160 |
Exit(false); |
| 1161 |
end; |
| 1162 |
|
| 1163 |
VersionContentsList := TStringList.Create; |
| 1164 |
try |
| 1165 |
try |
| 1166 |
VersionContentsList.LoadFromFile(VersionFile); |
| 1167 |
except |
| 1168 |
StatusText := Format('Status: Invalid, cannot read file "%s"', [VersionFile]); |
| 1169 |
Result := true; |
| 1170 |
end; |
| 1171 |
|
| 1172 |
if VersionContentsList.Count = 0 then |
| 1173 |
begin |
| 1174 |
StatusText := Format('Status: Invalid, empty file "%s"', [VersionFile]); |
| 1175 |
Exit(false); |
| 1176 |
end; |
| 1177 |
|
| 1178 |
VersionLine := VersionContentsList[0]; |
| 1179 |
if (not SCharIs(VersionLine, 1, '''')) or |
| 1180 |
(not SCharIs(VersionLine, Length(VersionLine), '''')) then |
| 1181 |
begin |
| 1182 |
StatusText := Format('Status: Invalid, first line is not a String: "%s"', [VersionFile]); |
| 1183 |
Exit(false); |
| 1184 |
end; |
| 1185 |
finally FreeAndNil(VersionContentsList) end; |
| 1186 |
|
| 1187 |
Version := Copy(VersionLine, 2, Length(VersionLine) - 2); |
| 1188 |
|
| 1189 |
{ Due to the way pack_release.sh works, actually Version will never have a hash. |
| 1190 |
Only CastleEngineVersion can have it, |
| 1191 |
and it would cause mismatches, to remove it. } |
| 1192 |
Version := RemoveCommitHash(Version); |
| 1193 |
EditorVersion := RemoveCommitHash(CastleEngineVersion); |
| 1194 |
|
| 1195 |
if Version <> EditorVersion then |
| 1196 |
begin |
| 1197 |
StatusText := Format('Status: Valid engine, but version mismatch with editor: "%s" vs editor "%s"', [ |
| 1198 |
Version, |
| 1199 |
EditorVersion |
| 1200 |
]); |
| 1201 |
Exit(False); |
| 1202 |
end; |
| 1203 |
|
| 1204 |
StatusText := 'Status: OK (engine found, version matches editor)'; |
| 1205 |
Result := true; |
| 1206 |
end; |
| 1207 |
|
| 1208 |
function CgePathStatus(const CgePath: String): Boolean; |
| 1209 |
var |
| 1210 |
IgnoreStatusText: String; |
| 1211 |
begin |
| 1212 |
Result := CgePathStatus(CgePath, IgnoreStatusText); |
| 1213 |
end; |
| 1214 |
|
| 1215 |
procedure PrepareSaveDesignDialog(const SaveDialog: TSaveDialog; const ComponentToSave: TComponent); |
| 1216 |
begin |
| 1217 |
if ComponentToSave is TCastleUserInterface then |
| 1218 |
begin |
| 1219 |
SaveDialog.DefaultExt := 'castle-user-interface'; |
| 1220 |
SaveDialog.Filter := 'CGE User Interface Design (*.castle-user-interface)|*.castle-user-interface|All Files|*'; |
| 1221 |
end else |
| 1222 |
if ComponentToSave is TCastleTransform then |
| 1223 |
begin |
| 1224 |
{ We modify both Filter and DefaultExt, otherwise (at least on GTK2) |
| 1225 |
the default extension (for filter like '*.castle-user-interface;*.castle-transform') |
| 1226 |
would still be castle-user-interface. I.e. DefaultExt seems to be ignored, |
| 1227 |
and instead GTK applies first filter. } |
| 1228 |
SaveDialog.DefaultExt := 'castle-transform'; |
| 1229 |
SaveDialog.Filter := 'CGE Transform Design (*.castle-transform)|*.castle-transform|All Files|*'; |
| 1230 |
end else |
| 1231 |
begin |
| 1232 |
SaveDialog.DefaultExt := 'castle-component'; |
| 1233 |
SaveDialog.Filter := 'CGE Component Design (*.castle-component)|*.castle-component|All Files|*'; |
| 1234 |
end; |
| 1235 |
end; |
| 1236 |
|
| 1237 |
function UseIconsAndColorsForDarkTheme: Boolean; |
| 1238 |
var |
| 1239 |
Luminance: Single; |
| 1240 |
begin |
| 1241 |
Luminance := GrayscaleValue(ColorToVector3(clForm)); |
| 1242 |
Result := Luminance < 180 / 255; |
| 1243 |
end; |
| 1244 |
|
| 1245 |
class function TSavedSelection.Equals(const A, B: TSavedSelection): Boolean; static; |
| 1246 |
begin |
| 1247 |
Result := |
| 1248 |
(A.SelectedComponent = B.SelectedComponent) and |
| 1249 |
(A.CurrentViewport = B.CurrentViewport) and |
| 1250 |
(A.ItemIndex = B.ItemIndex) and |
| 1251 |
(A.TabIndex = B.TabIndex); |
| 1252 |
end; |
| 1253 |
|
| 1254 |
function FileDateTimeStr(const FileName: String): String; |
| 1255 |
|
| 1256 |
function RoundUp(const Val: Double): Int64; |
| 1257 |
begin |
| 1258 |
Result := Trunc(Val); |
| 1259 |
if Frac(Val) > 0 then |
| 1260 |
Inc(Result); |
| 1261 |
end; |
| 1262 |
|
| 1263 |
var |
| 1264 |
FileDateTime: TDateTime; |
| 1265 |
Secs, Mins: Int64; |
| 1266 |
begin |
| 1267 |
if FileAge(FileName, FileDateTime) then |
| 1268 |
begin |
| 1269 |
Secs := RoundUp(SecondSpan(Now, FileDateTime)); |
| 1270 |
if Secs < 60 then |
| 1271 |
FileDateTimeStr := Format('%d second%s ago', [ |
| 1272 |
Secs, |
| 1273 |
Iff(Secs > 0, 's', '') |
| 1274 |
]) |
| 1275 |
else |
| 1276 |
begin |
| 1277 |
Mins := RoundUp(MinuteSpan(Now, FileDateTime)); |
| 1278 |
if Mins < 60 then |
| 1279 |
FileDateTimeStr := Format('%d minute%s ago', [ |
| 1280 |
Mins, |
| 1281 |
Iff(Mins > 0, 's', '') |
| 1282 |
]) |
| 1283 |
else |
| 1284 |
FileDateTimeStr := DateTimeToAtStr(FileDateTime); |
| 1285 |
end; |
| 1286 |
end else |
| 1287 |
Result := 'Unknown'; |
| 1288 |
end; |
| 1289 |
|
| 1290 |
{ TComponentListEnumerator ------------------------------------------------- } |
| 1291 |
|
| 1292 |
function TComponentListEnumerator.GetCurrent: TComponent; |
| 1293 |
begin |
| 1294 |
Result := FList[FPosition]; |
| 1295 |
end; |
| 1296 |
|
| 1297 |
constructor TComponentListEnumerator.Create(const AList: TComponentList); |
| 1298 |
begin |
| 1299 |
FList := AList; |
| 1300 |
FPosition := -1; |
| 1301 |
end; |
| 1302 |
|
| 1303 |
function TComponentListEnumerator.MoveNext: Boolean; |
| 1304 |
begin |
| 1305 |
Inc(FPosition); |
| 1306 |
Result := FPosition < FList.Count; |
| 1307 |
end; |
| 1308 |
|
| 1309 |
operator Enumerator(const A: TComponentList): TComponentListEnumerator; |
| 1310 |
begin |
| 1311 |
Result := TComponentListEnumerator.Create(A); |
| 1312 |
end; |
| 1313 |
|
| 1314 |
end. |