GLBaseClasses.pas 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //
  2. // This unit is part of the GLScene Engine, http://glscene.org
  3. //
  4. (*
  5. Base classes
  6. *)
  7. unit GLBaseClasses;
  8. interface
  9. uses
  10. System.Classes,
  11. System.SysUtils,
  12. GLStrings,
  13. GLPersistentClasses,
  14. GLCrossPlatform;
  15. type
  16. TGLProgressTimes = record
  17. deltaTime, newTime: Double
  18. end;
  19. (* Progression event for time-base animations/simulations.
  20. deltaTime is the time delta since last progress and newTime is the new
  21. time after the progress event is completed. *)
  22. TGLProgressEvent = procedure(Sender: TObject; const deltaTime, newTime: Double) of object;
  23. IGLNotifyAble = interface(IInterface)
  24. ['{00079A6C-D46E-4126-86EE-F9E2951B4593}']
  25. procedure NotifyChange(Sender: TObject);
  26. end;
  27. IGLProgessAble = interface(IInterface)
  28. ['{95E44548-B0FE-4607-98D0-CA51169AF8B5}']
  29. procedure DoProgress(const progressTime: TGLProgressTimes);
  30. end;
  31. // An abstract class describing the "update" interface.
  32. TGLUpdateAbleObject = class(TGLInterfacedPersistent, IGLNotifyAble)
  33. private
  34. FOwner: TPersistent;
  35. FUpdating: Integer;
  36. FOnNotifyChange: TNotifyEvent;
  37. protected
  38. function GetOwner: TPersistent; override; final;
  39. public
  40. constructor Create(AOwner: TPersistent); virtual;
  41. procedure NotifyChange(Sender: TObject); virtual;
  42. procedure Notification(Sender: TObject; Operation: TOperation); virtual;
  43. property Updating: Integer read FUpdating;
  44. procedure BeginUpdate; inline;
  45. procedure EndUpdate; inline;
  46. property Owner: TPersistent read FOwner;
  47. property OnNotifyChange: TNotifyEvent read FOnNotifyChange write FOnNotifyChange;
  48. end;
  49. // A base class describing the "cadenceing" interface.
  50. TGLCadenceAbleComponent = class(TGLComponent, IGLProgessAble)
  51. public
  52. procedure DoProgress(const progressTime: TGLProgressTimes); virtual;
  53. end;
  54. { A base class describing the "update" interface. }
  55. TGLUpdateAbleComponent = class(TGLCadenceAbleComponent, IGLNotifyAble)
  56. public
  57. procedure NotifyChange(Sender: TObject); virtual;
  58. end;
  59. TGLNotifyCollection = class(TOwnedCollection)
  60. strict private
  61. FOnNotifyChange: TNotifyEvent;
  62. strict protected
  63. procedure Update(item: TCollectionItem); override;
  64. public
  65. constructor Create(AOwner: TPersistent; AItemClass: TCollectionItemClass);
  66. property OnNotifyChange: TNotifyEvent read FOnNotifyChange write FOnNotifyChange;
  67. end;
  68. //-------------------------------------------------------------------------
  69. implementation
  70. //-------------------------------------------------------------------------
  71. //---------------------- TGLUpdateAbleObject -----------------------------------
  72. constructor TGLUpdateAbleObject.Create(AOwner: TPersistent);
  73. begin
  74. inherited Create;
  75. FOwner := AOwner;
  76. end;
  77. procedure TGLUpdateAbleObject.NotifyChange(Sender: TObject);
  78. begin
  79. if FUpdating = 0 then
  80. begin
  81. if Assigned(FOwner) then
  82. begin
  83. if FOwner is TGLUpdateAbleObject then
  84. TGLUpdateAbleObject(FOwner).NotifyChange(Self)
  85. else if FOwner is TGLUpdateAbleComponent then
  86. TGLUpdateAbleComponent(FOwner).NotifyChange(Self);
  87. end;
  88. if Assigned(FOnNotifyChange) then
  89. FOnNotifyChange(Self);
  90. end;
  91. end;
  92. procedure TGLUpdateAbleObject.Notification(Sender: TObject; Operation: TOperation);
  93. begin
  94. end;
  95. function TGLUpdateAbleObject.GetOwner: TPersistent;
  96. begin
  97. Result := FOwner;
  98. end;
  99. procedure TGLUpdateAbleObject.BeginUpdate;
  100. begin
  101. Inc(FUpdating);
  102. end;
  103. procedure TGLUpdateAbleObject.EndUpdate;
  104. begin
  105. Dec(FUpdating);
  106. if FUpdating <= 0 then
  107. begin
  108. Assert(FUpdating = 0);
  109. NotifyChange(Self);
  110. end;
  111. end;
  112. // ------------------
  113. // ------------------ TGLCadenceAbleComponent ------------------
  114. // ------------------
  115. procedure TGLCadenceAbleComponent.DoProgress(const progressTime: TGLProgressTimes);
  116. begin
  117. // nothing
  118. end;
  119. // ------------------
  120. // ------------------ TGLUpdateAbleObject ------------------
  121. // ------------------
  122. procedure TGLUpdateAbleComponent.NotifyChange(Sender: TObject);
  123. begin
  124. if Assigned(Owner) then
  125. if (Owner is TGLUpdateAbleComponent) then
  126. (Owner as TGLUpdateAbleComponent).NotifyChange(Self);
  127. end;
  128. // ------------------
  129. // ------------------ TGLNotifyCollection ------------------
  130. // ------------------
  131. constructor TGLNotifyCollection.Create(AOwner: TPersistent; AItemClass: TCollectionItemClass);
  132. begin
  133. inherited Create(AOwner, AItemClass);
  134. if Assigned(AOwner) and (AOwner is TGLUpdateAbleComponent) then
  135. FOnNotifyChange := TGLUpdateAbleComponent(AOwner).NotifyChange;
  136. end;
  137. procedure TGLNotifyCollection.Update(Item: TCollectionItem);
  138. begin
  139. inherited;
  140. if Assigned(FOnNotifyChange) then
  141. FOnNotifyChange(Self);
  142. end;
  143. end.