fObjmoveC.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "fObjmoveC.h"
  5. //---------------------------------------------------------------------------
  6. #pragma package(smart_init)
  7. #pragma link "GLScene.BaseClasses"
  8. #pragma link "GLS.BitmapFont"
  9. #pragma link "GLScene.Coordinates"
  10. #pragma link "GLS.GeomObjects"
  11. #pragma link "GLS.HUDObjects"
  12. #pragma link "GLS.Objects"
  13. #pragma link "GLS.Scene"
  14. #pragma link "GLS.SpaceText"
  15. #pragma link "GLS.SceneViewer"
  16. #pragma link "GLS.WindowsFont"
  17. #pragma link "GLS.Navigator"
  18. #pragma link "GLS.SmoothNavigator"
  19. #pragma resource "*.dfm"
  20. TFormObjmove *FormObjmove;
  21. const TGColorVector
  22. SelectionColor[] = {0.243, 0.243, 0.243, 1.000};
  23. //---------------------------------------------------------------------------
  24. void __fastcall TFormObjmove::FormCreate(TObject *Sender)
  25. {
  26. UpdateHUDText();
  27. }
  28. //---------------------------------------------------------------------------
  29. TGLVector __fastcall TFormObjmove::MouseWorldPos(int X, int Y)
  30. {
  31. TGLVector v;
  32. TGLVector Result;
  33. Y = Scene->Height - Y;
  34. if (CurrentPick)
  35. {
  36. SetVector(v, X, Y, 0);
  37. if (movingOnZ)
  38. Scene->Buffer->ScreenVectorIntersectWithPlaneXZ(
  39. v, CurrentPick->Position->Y, Result);
  40. else
  41. Scene->Buffer->ScreenVectorIntersectWithPlaneXY(
  42. v, CurrentPick->Position->Z, Result);
  43. }
  44. else
  45. SetVector(Result, NullVector);
  46. return Result;
  47. }
  48. //---------------------------------------------------------------------------
  49. void __fastcall TFormObjmove::UpdateHUDText()
  50. {
  51. TAffineVector objPos, winPos;
  52. if (CurrentPick)
  53. {
  54. SetVector(objPos, CurrentPick->AbsolutePosition);
  55. /*
  56. HUDText->Text = Format("New Object Position: Xn: %4.4f, Yn: %4.4f, Zn: %4.4f",
  57. ARRAYOFCONST ((objPos.X, objPos.Y, objPos.Z)));
  58. */
  59. HUDText->Text = "New Object Position:";
  60. HUDText->Text = " Xn: " + FloatToStrF(objPos.X,ffFixed, 4, 4) +
  61. " Yn: " + FloatToStrF(objPos.Y,ffFixed, 4, 4) +
  62. " Zn: " + FloatToStrF(objPos.Z,ffFixed, 4, 4);
  63. winPos = Scene->Buffer->WorldToScreen(objPos);
  64. HUDTextObj->Visible = true;
  65. HUDTextObj->Text = CurrentPick->Name; // outtext for Cube1 or Cube2
  66. HUDTextObj->Position->X = winPos.X + 20;
  67. HUDTextObj->Position->Y = Scene->Height - winPos.Y + 20;
  68. }
  69. else
  70. {
  71. HUDText->Text = "No selected object";
  72. HUDTextObj->Visible = false;
  73. }
  74. }
  75. //---------------------------------------------------------------------------
  76. void __fastcall TFormObjmove::ProcessPick(TGLBaseSceneObject* pick)
  77. {
  78. if (pick)
  79. {
  80. // Only Cube1 and Cube2 can be selected
  81. if ((pick->Name != "Cube1") && (pick->Name != "Cube2"))
  82. pick = NULL;
  83. }
  84. if (pick != CurrentPick)
  85. {
  86. if (CurrentPick)
  87. {
  88. CurrentPick->ShowAxes = false;
  89. CurrentPick->Material->FrontProperties->Emission->Color = clrBlack;
  90. }
  91. CurrentPick = (TGLCustomSceneObject *)(pick);
  92. if (CurrentPick)
  93. {
  94. if (ShowAxes->Checked)
  95. CurrentPick->ShowAxes = true;
  96. }
  97. }
  98. UpdateHUDText();
  99. }
  100. //---------------------------------------------------------------------------
  101. __fastcall TFormObjmove::TFormObjmove(TComponent* Owner)
  102. : TForm(Owner)
  103. {
  104. }
  105. //---------------------------------------------------------------------------
  106. void __fastcall TFormObjmove::SceneMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift,
  107. int X, int Y)
  108. {
  109. TGLBaseSceneObject* pick;
  110. movingOnZ = Shift.Contains(ssShift);
  111. // If an object is picked...
  112. pick = (Scene->Buffer->GetPickedObject(X, Y)); // as TGLCustomSceneObject);
  113. ProcessPick(pick);
  114. // store mouse pos
  115. if (CurrentPick)
  116. lastMouseWorldPos = MouseWorldPos(X, Y);
  117. }
  118. //---------------------------------------------------------------------------
  119. void __fastcall TFormObjmove::SceneMouseMove(TObject *Sender, TShiftState Shift, int X, int Y)
  120. {
  121. TGLVector newPos;
  122. SceneMouseMoveCnt++;
  123. if (Shift.Contains(ssLeft))
  124. {
  125. // handle hold/unhold of shift
  126. if (Shift.Contains(ssShift) != movingOnZ)
  127. {
  128. movingOnZ = (Shift.Contains(ssShift));
  129. lastMouseWorldPos = MouseWorldPos(X, Y);
  130. }
  131. newPos = MouseWorldPos(X, Y);
  132. if (CurrentPick && (VectorNorm(lastMouseWorldPos) != 0))
  133. CurrentPick->Position->Translate(VectorSubtract(newPos, lastMouseWorldPos));
  134. lastMouseWorldPos = newPos;
  135. UpdateHUDText();
  136. }
  137. SceneMouseMoveCnt--;
  138. }
  139. //---------------------------------------------------------------------------
  140. void __fastcall TFormObjmove::ShowAxesClick(TObject *Sender)
  141. {
  142. // Unselect all
  143. ProcessPick(NULL);
  144. }
  145. //---------------------------------------------------------------------------
  146. void __fastcall TFormObjmove::FormMouseWheel(TObject *Sender, TShiftState Shift, int WheelDelta,
  147. TPoint &MousePos, bool &Handled)
  148. {
  149. // Note that 1 wheel-step induces a WheelDelta of 120,
  150. // this code adjusts the distance to target with a 10% per wheel-step ratio
  151. if (WheelDelta != 0)
  152. GLCamera1->AdjustDistanceToTarget(Power(1.1, -WheelDelta / 120));
  153. }
  154. //---------------------------------------------------------------------------
  155. void __fastcall TFormObjmove::FormKeyPress(TObject *Sender, System::WideChar &Key)
  156. {
  157. switch (Key)
  158. {
  159. case '1': GLCamera1->MoveAroundTarget(3, 0); break;
  160. case '2': GLCamera1->MoveAroundTarget(0, -3); break;
  161. case '3': GLCamera1->MoveAroundTarget(0, 3); break;
  162. case '4': GLCamera1->MoveAroundTarget(-3, 0); break;
  163. case '-': GLCamera1->AdjustDistanceToTarget(1.1); break;
  164. case '+': GLCamera1->AdjustDistanceToTarget(1 / 1.1); break;
  165. default: ; break;
  166. }
  167. }
  168. //---------------------------------------------------------------------------
  169. void __fastcall TFormObjmove::FormKeyUp(
  170. TObject* Sender, WORD &Key, TShiftState Shift)
  171. {
  172. if (CurrentPick) {
  173. switch (Key) {
  174. case VK_UP:
  175. if (Shift.Contains(ssShift))
  176. CurrentPick->Translate(0, 0, 0.3);
  177. else
  178. CurrentPick->Translate(-0.3, 0, 0);
  179. break;
  180. case VK_DOWN:
  181. if (Shift.Contains(ssShift))
  182. CurrentPick->Translate(0, 0, -0.3);
  183. else
  184. CurrentPick->Translate(0.3, 0, 0);
  185. break;
  186. case VK_LEFT:
  187. CurrentPick->Translate(0, -0.3, 0);
  188. break;
  189. case VK_RIGHT:
  190. CurrentPick->Translate(0, 0.3, 0);
  191. break;
  192. default:;
  193. }
  194. }
  195. }
  196. //---------------------------------------------------------------------------