fGuiPaintC.cpp 7.7 KB

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