Unit1.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "Unit1.h"
  5. //---------------------------------------------------------------------------
  6. #pragma package(smart_init)
  7. #pragma link "GLS.BaseClasses"
  8. #pragma link "GLS.Cadencer"
  9. #pragma link "GLS.Coordinates"
  10. #pragma link "GLS.GeomObjects"
  11. #pragma link "GLS.Graph"
  12. #pragma link "GLS.Objects"
  13. #pragma link "GLS.Scene"
  14. #pragma link "GLS.SceneViewer"
  15. #pragma resource "*.dfm"
  16. TForm1 *Form1;
  17. //---------------------------------------------------------------------------
  18. __fastcall TForm1::TForm1(TComponent* Owner)
  19. : TForm(Owner)
  20. {
  21. }
  22. //---------------------------------------------------------------------------
  23. void __fastcall TForm1::FormCreate(TObject *Sender)
  24. {
  25. Navigator = new TGLSmoothNavigator (this);
  26. Navigator->AngleLock = false;
  27. Navigator->AutoUpdateObject = false;
  28. Navigator->InvertHorizontalSteeringWhenUpsideDown = true;
  29. Navigator->MoveUpWhenMovingForward = true;
  30. Navigator->UseVirtualUp = true;
  31. Navigator->VirtualUp->AsAffineVector = YVector;
  32. Navigator->MovingObject = GLCamera1;
  33. Navigator->InertiaParams->MovementAcceleration = 7;
  34. Navigator->InertiaParams->MovementInertia = 200;
  35. Navigator->InertiaParams->MovementSpeed = 200;
  36. Navigator->InertiaParams->TurnInertia = 150;
  37. Navigator->InertiaParams->TurnSpeed = 40;
  38. Navigator->InertiaParams->TurnMaxAngle = 0.5;
  39. Navigator->MoveAroundParams->TargetObject = GLArrowLine1;
  40. UI = new TGLSmoothUserInterface(this);
  41. // UI.AutoUpdateMouse = false;
  42. UI->SmoothNavigator = Navigator;
  43. }
  44. //---------------------------------------------------------------------------
  45. void __fastcall TForm1::CheckControls(double DeltaTime, double newTime)
  46. {
  47. bool NeedToAccelerate;
  48. NeedToAccelerate = IsKeyDown(VK_SHIFT);
  49. Navigator->StrafeVertical(IsKeyDown('F'), IsKeyDown('R'), DeltaTime, NeedToAccelerate);
  50. Navigator->MoveForward(IsKeyDown('W'), IsKeyDown('S'), DeltaTime, NeedToAccelerate);
  51. Navigator->StrafeHorizontal(IsKeyDown('D'), IsKeyDown('A'), DeltaTime, NeedToAccelerate);
  52. // GetCursorPos(RealPos);
  53. UI->MouseLook(/*RealPos, */DeltaTime);
  54. // if UI.MouseLookActive then
  55. // SetCursorPos(Round(UI.OriginalMousePos.X), Round(UI.OriginalMousePos.Y));
  56. }
  57. void __fastcall TForm1::GLCadencer1Progress(TObject *Sender, const double deltaTime,
  58. const double newTime)
  59. {
  60. GLSceneViewer1->Invalidate();
  61. if (UI->MouseLookActive)
  62. CheckControls(deltaTime, newTime);
  63. else
  64. {
  65. if (ShiftState.Contains(ssRight) && ShiftState.Contains(ssLeft))
  66. {
  67. Navigator->MoveAroundTarget(0, 0, deltaTime);
  68. Navigator->AdjustDistanceToTarget(yy - NewYY, deltaTime);
  69. }
  70. else
  71. if (ShiftState.Contains(ssRight) || ShiftState.Contains(ssLeft))
  72. {
  73. Navigator->MoveAroundTarget(yy - NewYY, xx - NewXX, deltaTime);
  74. Navigator->AdjustDistanceToTarget(0, deltaTime);
  75. }
  76. else
  77. {
  78. Navigator->MoveAroundTarget(0, 0, deltaTime);
  79. Navigator->AdjustDistanceToTarget(0, deltaTime);
  80. }
  81. xx = NewXX;
  82. yy = NewYY;
  83. }
  84. }
  85. //---------------------------------------------------------------------------
  86. void __fastcall TForm1::FPSTimerTimer(TObject *Sender)
  87. {
  88. Caption = "Smooth Navigator - " + GLSceneViewer1->FramesPerSecondText();
  89. Navigator->AutoScaleParameters(GLSceneViewer1->FramesPerSecond());
  90. GLSceneViewer1->ResetPerformanceMonitor();
  91. }
  92. //---------------------------------------------------------------------------
  93. void __fastcall TForm1::MouseLookCheckBoxClick(TObject *Sender)
  94. {
  95. if (MouseLookCheckBox->Checked)
  96. {
  97. GLCamera1->TargetObject = NULL;
  98. GLCamera1->PointTo(GLArrowLine1, YHmgVector);
  99. UI->MouseLookActive = true;
  100. // GetCursorPos(RealPos);
  101. // UI->OriginalMousePos->SetPoint2D(RealPos->X, RealPos->Y);
  102. // ShowCursor(false);
  103. }
  104. else
  105. {
  106. UI->MouseLookActive = false;
  107. // ShowCursor(true);
  108. GLCamera1->Up->SetVector(0, 1, 0);
  109. GLCamera1->TargetObject = GLArrowLine1;
  110. }
  111. }
  112. //---------------------------------------------------------------------------
  113. void __fastcall TForm1::FormKeyPress(TObject *Sender, System::WideChar &Key)
  114. {
  115. if (Key == Char(VK_SPACE))
  116. MouseLookCheckBoxClick(Sender);
  117. if (Key == Char(VK_ESCAPE))
  118. Close();
  119. }
  120. //---------------------------------------------------------------------------
  121. void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
  122. {
  123. GLSceneViewer1->Enabled = false;
  124. GLCadencer1->Enabled = false;
  125. FPSTimer->Enabled = false;
  126. delete UI;
  127. delete Navigator;
  128. GLShowCursor(true);
  129. }
  130. //---------------------------------------------------------------------------
  131. void __fastcall TForm1::RadioButton6Click(TObject *Sender)
  132. {
  133. GLCadencer1->FixedDeltaTime = 0;
  134. }
  135. //---------------------------------------------------------------------------
  136. void __fastcall TForm1::RadioButton7Click(TObject *Sender)
  137. {
  138. GLCadencer1->FixedDeltaTime = 0.01;
  139. }
  140. //---------------------------------------------------------------------------
  141. void __fastcall TForm1::RadioButton8Click(TObject *Sender)
  142. {
  143. GLCadencer1->FixedDeltaTime = 0.1;
  144. }
  145. //---------------------------------------------------------------------------
  146. void __fastcall TForm1::GLSceneViewer1MouseMove(TObject *Sender, TShiftState Shift,
  147. int X, int Y)
  148. {
  149. ShiftState = Shift;
  150. NewXX = X;
  151. NewYY = Y;
  152. }
  153. //---------------------------------------------------------------------------
  154. void __fastcall TForm1::GLSceneViewer1MouseDown(TObject *Sender, TMouseButton Button,
  155. TShiftState Shift, int X, int Y)
  156. {
  157. xx = X;
  158. yy = Y;
  159. }
  160. //---------------------------------------------------------------------------
  161. void __fastcall TForm1::FormMouseWheel(TObject *Sender, TShiftState Shift, int WheelDelta,
  162. TPoint &MousePos, bool &Handled)
  163. {
  164. // (WheelDelta / Abs(WheelDelta) is used to deternime the sign.
  165. Navigator->AdjustDistanceParams->AddImpulse((WheelDelta / abs(WheelDelta)));
  166. }
  167. //---------------------------------------------------------------------------