fScreenSaver2D.pas 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. { Properties dialog for the GLScene screensaver sample.
  2. Here is some basic interface and animation stuff showcased in other GLScene
  3. samples. The selection/hot mechanism is a little different from the approach
  4. used in other "interface" samples.<br>
  5. Note that the Cadencer is used in "cmManual" mode. Why doing this ? well, on
  6. slower systems the PickedObject call in each OnMouseMove may overwhelm the
  7. message queue, and since the Cadencer uses the message queue, this will result
  8. in animation "stalls" when the user is constantly and swiftly moving its
  9. mouse. Using the old "AfterRender" trick sorts this out.
  10. Beginners may also be interested in the Registry access.
  11. }
  12. unit fScreenSaver2D;
  13. interface
  14. uses
  15. Winapi.OpenGL,
  16. System.SysUtils,
  17. System.Classes,
  18. Vcl.Graphics,
  19. Vcl.Controls,
  20. Vcl.Forms,
  21. Vcl.StdCtrls,
  22. GLS.Scene,
  23. GLS.Objects,
  24. GLS.Texture,
  25. GLS.Cadencer,
  26. GLS.SceneViewer,
  27. GLS.SpaceText,
  28. GLS.GeomObjects,
  29. GLS.Coordinates,
  30. GLS.BaseClasses,
  31. GLS.Behaviours;
  32. type
  33. TForm2 = class(TForm)
  34. GLSceneViewer1: TGLSceneViewer;
  35. GLScene1: TGLScene;
  36. GLCamera1: TGLCamera;
  37. GLLightSource1: TGLLightSource;
  38. SpaceText4: TGLSpaceText;
  39. Button1: TButton;
  40. DummyCube1: TGLDummyCube;
  41. Torus1: TGLTorus;
  42. Torus2: TGLTorus;
  43. GLCadencer1: TGLCadencer;
  44. Button2: TButton;
  45. procedure FormCreate(Sender: TObject);
  46. procedure GLSceneViewer1MouseMove(Sender: TObject; Shift: TShiftState;
  47. X, Y: Integer);
  48. procedure GLSceneViewer1MouseDown(Sender: TObject; Button: TMouseButton;
  49. Shift: TShiftState; X, Y: Integer);
  50. procedure Button2Click(Sender: TObject);
  51. private
  52. FLastHotNb: Integer;
  53. procedure SetSelected(nb: Integer);
  54. procedure SetHot(nb: Integer);
  55. public
  56. end;
  57. var
  58. Form2: TForm2;
  59. function GetMeshResolutions: Integer;
  60. procedure SetMeshResolutions(MeshResolutions: Integer);
  61. implementation
  62. {$R *.DFM}
  63. uses
  64. Registry,
  65. GLS.ScreenSaver;
  66. const
  67. cSaverRegistryKey = 'Software\GLScene\Demos\utilities\ScreenSaver';
  68. cSaverRegistryMeshResolutions = 'MeshResolutions';
  69. function GetMeshResolutions: Integer;
  70. var
  71. reg: TRegistry;
  72. begin
  73. reg := TRegistry.Create;
  74. reg.OpenKey(cSaverRegistryKey, True);
  75. // If the value cannot be found, we default to hi-resolution
  76. if reg.ValueExists(cSaverRegistryMeshResolutions) then
  77. Result := reg.ReadInteger(cSaverRegistryMeshResolutions)
  78. else
  79. Result := 1;
  80. reg.Free;
  81. end;
  82. procedure SetMeshResolutions(MeshResolutions: Integer);
  83. var
  84. reg: TRegistry;
  85. begin
  86. reg := TRegistry.Create;
  87. reg.OpenKey(cSaverRegistryKey, True);
  88. reg.WriteInteger(cSaverRegistryMeshResolutions, MeshResolutions);
  89. reg.Free;
  90. end;
  91. procedure TForm2.FormCreate(Sender: TObject);
  92. begin
  93. // we highlight the current resolution
  94. SetSelected(GetMeshResolutions);
  95. SetHot(-1);
  96. end;
  97. procedure TForm2.SetSelected(nb: Integer);
  98. const
  99. cSelectionColor: array [False .. True] of Integer = (clNavy, clBlue);
  100. begin
  101. Torus1.Material.FrontProperties.Emission.AsWinColor :=
  102. (cSelectionColor[nb = 0]);
  103. Torus2.Material.FrontProperties.Emission.AsWinColor :=
  104. (cSelectionColor[nb = 1]);
  105. end;
  106. procedure TForm2.SetHot(nb: Integer);
  107. const
  108. cHotColor: array [False .. True] of Integer = (clGray, clWhite);
  109. begin
  110. FLastHotNb := nb;
  111. Torus1.Material.FrontProperties.Diffuse.AsWinColor := (cHotColor[nb = 0]);
  112. Torus2.Material.FrontProperties.Diffuse.AsWinColor := (cHotColor[nb = 1]);
  113. end;
  114. procedure TForm2.GLSceneViewer1MouseMove(Sender: TObject; Shift: TShiftState;
  115. X, Y: Integer);
  116. var
  117. bso: TGLBaseSceneObject;
  118. begin
  119. // Here I used the trick of setting Torus1.Tag=1 and Torus.Tag=2
  120. // other objects have a Tag of 0
  121. bso := GLSceneViewer1.Buffer.GetPickedObject(X, Y);
  122. if Assigned(bso) and (bso.Tag > 0) then
  123. SetHot(bso.Tag - 1)
  124. else
  125. SetHot(-1);
  126. end;
  127. procedure TForm2.GLSceneViewer1MouseDown(Sender: TObject; Button: TMouseButton;
  128. Shift: TShiftState; X, Y: Integer);
  129. begin
  130. if FLastHotNb >= 0 then
  131. begin
  132. SetSelected(FLastHotNb);
  133. SetMeshResolutions(FLastHotNb);
  134. end;
  135. end;
  136. procedure TForm2.Button2Click(Sender: TObject);
  137. begin
  138. // a call to "Form1.ScreenSaver1.SetPassword;" would have done the same
  139. SetScreenSaverPassword;
  140. end;
  141. end.