2
0

Unit1.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #include <tchar.h>
  4. #pragma hdrstop
  5. #include "Unit1.h"
  6. //---------------------------------------------------------------------------
  7. #pragma package(smart_init)
  8. #pragma link "GLBaseClasses"
  9. #pragma link "GLBitmapFont"
  10. #pragma link "GLCadencer"
  11. #pragma link "GLCoordinates"
  12. #pragma link "GLCrossPlatform"
  13. #pragma link "GLGui"
  14. #pragma link "GLMaterial"
  15. #pragma link "GLScene"
  16. #pragma link "GLWin32Viewer"
  17. #pragma link "GLWindows"
  18. #pragma link "GLWindowsFont"
  19. #pragma resource "*.dfm"
  20. TForm1 *Form1;
  21. //---------------------------------------------------------------------------
  22. __fastcall TForm1::TForm1(TComponent* Owner)
  23. : TForm(Owner)
  24. {
  25. }
  26. //---------------------------------------------------------------------------
  27. void __fastcall TForm1::FormCreate(TObject *Sender)
  28. {
  29. SetGLSceneMediaDir();
  30. GLCanvas->MaxInvalidRenderCount = 40;
  31. StartX = -1;
  32. }
  33. //---------------------------------------------------------------------------
  34. void __fastcall TForm1::GLCadencer1Progress(TObject *Sender, const double deltaTime,
  35. const double newTime)
  36. {
  37. // set frame rate to 10 when program is not focused to reduce cpu usage...
  38. if (Form1->Focused())
  39. GLCadencer1->SleepLength = 0;
  40. else
  41. GLCadencer1->SleepLength = 100;
  42. // make things move a little
  43. GLForm1->DoChanges();
  44. }
  45. //---------------------------------------------------------------------------
  46. void __fastcall TForm1::Timer1Timer(TObject *Sender)
  47. {
  48. miFPS->Caption = Format("%.1f FPS",
  49. ARRAYOFCONST ((GLSceneViewer1->FramesPerSecond())));
  50. GLSceneViewer1->ResetPerformanceMonitor();
  51. }
  52. //---------------------------------------------------------------------------
  53. void __fastcall TForm1::miWindowsFont1Click(TObject *Sender)
  54. {
  55. FontDialog1->Font = WindowsBitmapFont1->Font;
  56. if (FontDialog1->Execute())
  57. WindowsBitmapFont1->Font = FontDialog1->Font;
  58. }
  59. //---------------------------------------------------------------------------
  60. void __fastcall TForm1::GLSceneViewer1MouseDown(TObject *Sender, TMouseButton Button,
  61. TShiftState Shift, int X, int Y)
  62. {
  63. GuiRoot->MouseDown(Sender,TMouseButton(Button),Shift,X,Y);
  64. }
  65. //---------------------------------------------------------------------------
  66. void __fastcall TForm1::GLSceneViewer1MouseMove(TObject *Sender, TShiftState Shift,
  67. int X, int Y)
  68. {
  69. GuiRoot->MouseMove(Sender,Shift,X,Y);
  70. }
  71. //---------------------------------------------------------------------------
  72. void __fastcall TForm1::GLSceneViewer1MouseUp(TObject *Sender, TMouseButton Button,
  73. TShiftState Shift, int X, int Y)
  74. {
  75. GuiRoot->MouseUp(Sender,TMouseButton(Button),Shift,X,Y);
  76. }
  77. //---------------------------------------------------------------------------
  78. void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
  79. {
  80. GuiRoot->KeyDown(Sender,Key,Shift);
  81. }
  82. //---------------------------------------------------------------------------
  83. void __fastcall TForm1::FormKeyPress(TObject *Sender, System::WideChar &Key)
  84. {
  85. GuiRoot->KeyPress(Sender,Key);
  86. }
  87. //---------------------------------------------------------------------------
  88. void __fastcall TForm1::FormKeyUp(TObject *Sender, WORD &Key, TShiftState Shift)
  89. {
  90. GuiRoot->KeyUp(Sender,Key,Shift);
  91. }
  92. //---------------------------------------------------------------------------
  93. void __fastcall TForm1::GLCanvasMouseDown(TObject *Sender, TMouseButton Button,
  94. TShiftState Shift, int X, int Y)
  95. {
  96. if (Button == mbLeft)
  97. {
  98. // Make sure all mouse events are sent to the canvas before other GuiComponents, see GLCanvasAcceptMouseQuery.
  99. GuiRoot->ActiveControl = GLCanvas;
  100. // Set a status not to send mouse message to child components if any, see GLCanvasAcceptMouseQuery.
  101. GLCanvas->KeepMouseEvents = true;
  102. StartX = X;
  103. StartY = Y;
  104. }
  105. }
  106. //---------------------------------------------------------------------------
  107. void __fastcall TForm1::GLCanvasMouseMove(TObject *Sender, TShiftState Shift, int X,
  108. int Y)
  109. {
  110. CurrentX = X;
  111. CurrentY = Y;
  112. }
  113. //---------------------------------------------------------------------------
  114. void __fastcall TForm1::GLCanvasMouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift,
  115. int X, int Y)
  116. {
  117. if (Button == mbLeft)
  118. {
  119. StartX = -1;
  120. StartY = -1;
  121. // Set normal mouse message handling, see GLCanvasAcceptMouseQuery.
  122. GuiRoot->ActiveControl = NULL;
  123. // Set that childs are allowed to get mouse events, meant for then, see GLCanvasAcceptMouseQuery.
  124. GLCanvas->KeepMouseEvents = false;
  125. }
  126. }
  127. //---------------------------------------------------------------------------
  128. void __fastcall TForm1::GLCanvasRender(TGLCustomControl *Sender, TBitmap *Bitmap)
  129. {
  130. Bitmap->Width = Int(GLCanvas->Width);
  131. Bitmap->Height = Int(GLCanvas->Height);
  132. if (StartX != -1)
  133. {
  134. Bitmap->Canvas->MoveTo(StartX-Int(Sender->Position->X),StartY-Int(Sender->Position->Y));
  135. Bitmap->Canvas->LineTo(CurrentX-Int(Sender->Position->X),CurrentY-Int(Sender->Position->Y));
  136. StartX = CurrentX;
  137. StartY = CurrentY;
  138. }
  139. }
  140. //---------------------------------------------------------------------------
  141. void __fastcall TForm1::PenButtonButtonClick(TObject *Sender)
  142. {
  143. GLCanvas->Bitmap->Canvas->Pen->Width = 1;
  144. }
  145. //---------------------------------------------------------------------------
  146. void __fastcall TForm1::BrushButtonButtonClick(TObject *Sender)
  147. {
  148. GLCanvas->Bitmap->Canvas->Pen->Width = 5;
  149. }
  150. //---------------------------------------------------------------------------
  151. void __fastcall TForm1::WhiteButtonButtonClick(TObject *Sender)
  152. {
  153. GLCanvas->Bitmap->Canvas->Pen->Color = clWhite;
  154. }
  155. //---------------------------------------------------------------------------
  156. void __fastcall TForm1::BlackButtonButtonClick(TObject *Sender)
  157. {
  158. GLCanvas->Bitmap->Canvas->Pen->Color = clBlack;
  159. }
  160. //---------------------------------------------------------------------------
  161. void __fastcall TForm1::RedButtonButtonClick(TObject *Sender)
  162. {
  163. GLCanvas->Bitmap->Canvas->Pen->Color = clRed;
  164. }
  165. //---------------------------------------------------------------------------
  166. void __fastcall TForm1::GreenButtonButtonClick(TObject *Sender)
  167. {
  168. GLCanvas->Bitmap->Canvas->Pen->Color = clGreen;
  169. }
  170. //---------------------------------------------------------------------------
  171. void __fastcall TForm1::BlueButtonButtonClick(TObject *Sender)
  172. {
  173. GLCanvas->Bitmap->Canvas->Pen->Color = clBlue;
  174. }
  175. //---------------------------------------------------------------------------
  176. void __fastcall TForm1::GLCanvasAcceptMouseQuery(TGLBaseControl *Sender, TShiftState Shift,
  177. TGLMouseAction Action, TMouseButton Button, int X, int Y, bool &Accept)
  178. {
  179. // Sender.KeepMouseEvents is set when drawing,
  180. // if drawing this component, gets mouse events even if they are out of bounds!
  181. if (Sender->KeepMouseEvents) Accept = true;
  182. }
  183. //---------------------------------------------------------------------------
  184. void __fastcall TForm1::GLForm1Moving(TGLForm *Sender, float &Left, float &Top)
  185. {
  186. // make sure the form isn't moved out of bounds...
  187. if (Left > GLSceneViewer1->Width-32)
  188. Left = GLSceneViewer1->Width-32;
  189. if (Left + Sender->Width < 32)
  190. Left = 32 - Sender->Width;
  191. if (Top > GLSceneViewer1->Height-32)
  192. Top = GLSceneViewer1->Height-32;
  193. if (Top < 0)
  194. Top = 0;
  195. }
  196. //---------------------------------------------------------------------------
  197. void __fastcall TForm1::miOpen1Click(TObject *Sender)
  198. {
  199. if (OpenDialog1->Execute())
  200. GLCanvas->Bitmap->LoadFromFile(OpenDialog1->FileName);
  201. }
  202. //---------------------------------------------------------------------------
  203. void __fastcall TForm1::miSave1Click(TObject *Sender)
  204. {
  205. if (SaveDialog1->Execute())
  206. GLCanvas->Bitmap->SaveToFile(SaveDialog1->FileName);
  207. }
  208. //---------------------------------------------------------------------------