2
0

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