fGuiPaintD.pas 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. unit fGuiPaintD;
  2. interface
  3. uses
  4. Winapi.OpenGL,
  5. System.SysUtils,
  6. System.Classes,
  7. Vcl.Graphics,
  8. Vcl.Controls,
  9. Vcl.Forms,
  10. Vcl.Dialogs,
  11. Vcl.ExtCtrls,
  12. Vcl.Menus,
  13. GLS.Scene,
  14. GLScene.VectorTypes,
  15. GLS.HUDObjects,
  16. GLS.Objects,
  17. GLS.Cadencer,
  18. GLS.BitmapFont,
  19. GLS.SceneViewer,
  20. GLS.WindowsFont,
  21. GLS.Windows,
  22. GLS.Gui,
  23. GLS.Texture,
  24. GLS.Canvas,
  25. GLS.Material,
  26. GLS.Coordinates,
  27. GLS.BaseClasses,
  28. GLScene.Utils;
  29. type
  30. TFormGuiPaint = class(TForm)
  31. GLScene1: TGLScene;
  32. GLSceneViewer1: TGLSceneViewer;
  33. GLLightSource1: TGLLightSource;
  34. GLCamera1: TGLCamera;
  35. GLCadencer1: TGLCadencer;
  36. Timer1: TTimer;
  37. WindowsBitmapFont1: TGLWindowsBitmapFont;
  38. MainMenu1: TMainMenu;
  39. Font1: TMenuItem;
  40. miWindowsFont1: TMenuItem;
  41. FontDialog1: TFontDialog;
  42. GLGuiLayout1: TGLGuiLayout;
  43. GLForm1: TGLForm;
  44. GLMaterialLibrary1: TGLMaterialLibrary;
  45. BrushButton: TGLButton;
  46. PenButton: TGLButton;
  47. GLPanel1: TGLPanel;
  48. GLCanvas: TGLCustomControl;
  49. WhiteButton: TGLButton;
  50. BlackButton: TGLButton;
  51. RedButton: TGLButton;
  52. GreenButton: TGLButton;
  53. BlueButton: TGLButton;
  54. GuiRoot: TGLBaseControl;
  55. File1: TMenuItem;
  56. miOpen1: TMenuItem;
  57. miSave1: TMenuItem;
  58. OpenDialog1: TOpenDialog;
  59. SaveDialog1: TSaveDialog;
  60. miFPS: TMenuItem;
  61. procedure GLCadencer1Progress(Sender: TObject;
  62. const deltaTime, newTime: Double);
  63. procedure Timer1Timer(Sender: TObject);
  64. procedure miWindowsFont1Click(Sender: TObject);
  65. procedure GLSceneViewer1MouseDown(Sender: TObject; Button: TMouseButton;
  66. Shift: TShiftState; X, Y: Integer);
  67. procedure GLSceneViewer1MouseMove(Sender: TObject; Shift: TShiftState;
  68. X, Y: Integer);
  69. procedure GLSceneViewer1MouseUp(Sender: TObject; Button: TMouseButton;
  70. Shift: TShiftState; X, Y: Integer);
  71. procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  72. procedure FormKeyPress(Sender: TObject; var Key: Char);
  73. procedure FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
  74. procedure GLCanvasMouseDown(Sender: TObject; Button: TMouseButton;
  75. Shift: TShiftState; X, Y: Integer);
  76. procedure GLCanvasMouseMove(Sender: TObject; Shift: TShiftState;
  77. X, Y: Integer);
  78. procedure GLCanvasMouseUp(Sender: TObject; Button: TMouseButton;
  79. Shift: TShiftState; X, Y: Integer);
  80. procedure GLCanvasRender(Sender: TGLCustomControl; Bitmap: TBitmap);
  81. procedure FormCreate(Sender: TObject);
  82. procedure WhiteButtonButtonClick(Sender: TObject);
  83. procedure BlackButtonButtonClick(Sender: TObject);
  84. procedure RedButtonButtonClick(Sender: TObject);
  85. procedure GreenButtonButtonClick(Sender: TObject);
  86. procedure BlueButtonButtonClick(Sender: TObject);
  87. procedure PenButtonButtonClick(Sender: TObject);
  88. procedure BrushButtonButtonClick(Sender: TObject);
  89. procedure GLCanvasAcceptMouseQuery(Sender: TGLBaseControl;
  90. Shift: TShiftState; Action: TGLMouseAction; Button: TMouseButton;
  91. X, Y: Integer; var accept: Boolean);
  92. procedure GLForm1Moving(Sender: TGLForm; var Left, Top: Single);
  93. procedure miOpen1Click(Sender: TObject);
  94. procedure miSave1Click(Sender: TObject);
  95. public
  96. StartX: Integer;
  97. StartY: Integer;
  98. CurrentX: Integer;
  99. CurrentY: Integer;
  100. end;
  101. var
  102. FormGuiPaint: TFormGuiPaint;
  103. implementation
  104. {$R *.DFM}
  105. procedure TFormGuiPaint.FormCreate(Sender: TObject);
  106. begin
  107. var Path: TFileName := GetCurrentAssetPath() + '\button';
  108. SetCurrentDir(Path);
  109. GLMaterialLibrary1.TexturePaths := Path;
  110. GLCanvas.MaxInvalidRenderCount := 40;
  111. StartX := -1;
  112. end;
  113. procedure TFormGuiPaint.GLCadencer1Progress(Sender: TObject;
  114. const deltaTime, newTime: Double);
  115. begin
  116. // set frame rate to 10 when program is not focused to reduce cpu usage...
  117. If FormGuiPaint.Focused then
  118. GLCadencer1.SleepLength := 0
  119. else
  120. GLCadencer1.SleepLength := 100;
  121. // make things move a little
  122. GLForm1.DoChanges;
  123. end;
  124. procedure TFormGuiPaint.Timer1Timer(Sender: TObject);
  125. begin
  126. miFPS.Caption := Format('%.1f FPS', [GLSceneViewer1.FramesPerSecond]);
  127. GLSceneViewer1.ResetPerformanceMonitor;
  128. end;
  129. procedure TFormGuiPaint.miWindowsFont1Click(Sender: TObject);
  130. begin
  131. FontDialog1.Font := WindowsBitmapFont1.Font;
  132. if FontDialog1.Execute then
  133. WindowsBitmapFont1.Font := FontDialog1.Font;
  134. end;
  135. procedure TFormGuiPaint.GLSceneViewer1MouseDown(Sender: TObject;
  136. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  137. begin
  138. GuiRoot.MouseDown(Sender, TMouseButton(Button), Shift, X, Y);
  139. end;
  140. procedure TFormGuiPaint.GLSceneViewer1MouseMove(Sender: TObject;
  141. Shift: TShiftState; X, Y: Integer);
  142. begin
  143. GuiRoot.MouseMove(Sender, Shift, X, Y);
  144. end;
  145. procedure TFormGuiPaint.GLSceneViewer1MouseUp(Sender: TObject;
  146. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  147. begin
  148. GuiRoot.MouseUp(Sender, TMouseButton(Button), Shift, X, Y);
  149. end;
  150. procedure TFormGuiPaint.FormKeyDown(Sender: TObject; var Key: Word;
  151. Shift: TShiftState);
  152. begin
  153. GuiRoot.KeyDown(Sender, Key, Shift);
  154. end;
  155. procedure TFormGuiPaint.FormKeyPress(Sender: TObject; var Key: Char);
  156. begin
  157. GuiRoot.KeyPress(Sender, Key);
  158. end;
  159. procedure TFormGuiPaint.FormKeyUp(Sender: TObject; var Key: Word;
  160. Shift: TShiftState);
  161. begin
  162. GuiRoot.KeyUp(Sender, Key, Shift);
  163. end;
  164. procedure TFormGuiPaint.GLCanvasMouseDown(Sender: TObject; Button: TMouseButton;
  165. Shift: TShiftState; X, Y: Integer);
  166. begin
  167. if Button = mbLeft then
  168. begin
  169. // Make sure all mouse events are sent to the canvas before other GuiComponents, see GLCanvasAcceptMouseQuery.
  170. GuiRoot.ActiveControl := GLCanvas;
  171. // Set a status not to send mouse message to child components if any, see GLCanvasAcceptMouseQuery.
  172. GLCanvas.KeepMouseEvents := True;
  173. StartX := X;
  174. StartY := Y;
  175. end;
  176. end;
  177. procedure TFormGuiPaint.GLCanvasMouseMove(Sender: TObject; Shift: TShiftState;
  178. X, Y: Integer);
  179. begin
  180. CurrentX := X;
  181. CurrentY := Y;
  182. end;
  183. procedure TFormGuiPaint.GLCanvasMouseUp(Sender: TObject; Button: TMouseButton;
  184. Shift: TShiftState; X, Y: Integer);
  185. begin
  186. if Button = mbLeft then
  187. begin
  188. StartX := -1;
  189. StartY := -1;
  190. // Set normal mouse message handling, see GLCanvasAcceptMouseQuery.
  191. GuiRoot.ActiveControl := Nil;
  192. // Set that childs are allowed to get mouse events, meant for then, see GLCanvasAcceptMouseQuery.
  193. GLCanvas.KeepMouseEvents := False;
  194. end;
  195. end;
  196. procedure TFormGuiPaint.GLCanvasRender(Sender: TGLCustomControl;
  197. Bitmap: TBitmap);
  198. begin
  199. Bitmap.Width := Round(GLCanvas.Width);
  200. Bitmap.Height := Round(GLCanvas.Height);
  201. if StartX <> -1 then
  202. begin
  203. Bitmap.Canvas.MoveTo(StartX - Round(Sender.Position.X),
  204. StartY - Round(Sender.Position.Y));
  205. Bitmap.Canvas.LineTo(CurrentX - Round(Sender.Position.X),
  206. CurrentY - Round(Sender.Position.Y));
  207. StartX := CurrentX;
  208. StartY := CurrentY;
  209. end;
  210. end;
  211. procedure TFormGuiPaint.PenButtonButtonClick(Sender: TObject);
  212. begin
  213. GLCanvas.Bitmap.Canvas.Pen.Width := 1;
  214. end;
  215. procedure TFormGuiPaint.BrushButtonButtonClick(Sender: TObject);
  216. begin
  217. GLCanvas.Bitmap.Canvas.Pen.Width := 5;
  218. end;
  219. procedure TFormGuiPaint.WhiteButtonButtonClick(Sender: TObject);
  220. begin
  221. GLCanvas.Bitmap.Canvas.Pen.Color := clWhite;
  222. end;
  223. procedure TFormGuiPaint.BlackButtonButtonClick(Sender: TObject);
  224. begin
  225. GLCanvas.Bitmap.Canvas.Pen.Color := clBlack;
  226. end;
  227. procedure TFormGuiPaint.RedButtonButtonClick(Sender: TObject);
  228. begin
  229. GLCanvas.Bitmap.Canvas.Pen.Color := clRed;
  230. end;
  231. procedure TFormGuiPaint.GreenButtonButtonClick(Sender: TObject);
  232. begin
  233. GLCanvas.Bitmap.Canvas.Pen.Color := clGreen;
  234. end;
  235. procedure TFormGuiPaint.BlueButtonButtonClick(Sender: TObject);
  236. begin
  237. GLCanvas.Bitmap.Canvas.Pen.Color := clBlue;
  238. end;
  239. procedure TFormGuiPaint.GLCanvasAcceptMouseQuery(Sender: TGLBaseControl;
  240. Shift: TShiftState; Action: TGLMouseAction; Button: TMouseButton;
  241. X, Y: Integer; var accept: Boolean);
  242. begin
  243. // Sender.KeepMouseEvents is set when drawing,
  244. // if drawing this component, gets mouse events even if they are out of bounds!
  245. if Sender.KeepMouseEvents then
  246. accept := True;
  247. end;
  248. procedure TFormGuiPaint.GLForm1Moving(Sender: TGLForm; var Left, Top: Single);
  249. begin
  250. // make sure the form isn't moved out of bounds...
  251. if Left > GLSceneViewer1.Width - 32 then
  252. Left := GLSceneViewer1.Width - 32;
  253. if Left + Sender.Width < 32 then
  254. Left := 32 - Sender.Width;
  255. if Top > GLSceneViewer1.Height - 32 then
  256. Top := GLSceneViewer1.Height - 32;
  257. if Top < 0 then
  258. Top := 0;
  259. end;
  260. procedure TFormGuiPaint.miOpen1Click(Sender: TObject);
  261. begin
  262. if OpenDialog1.Execute then
  263. GLCanvas.Bitmap.LoadFromFile(OpenDialog1.FileName);
  264. end;
  265. procedure TFormGuiPaint.miSave1Click(Sender: TObject);
  266. begin
  267. if SaveDialog1.Execute then
  268. GLCanvas.Bitmap.SaveToFile(SaveDialog1.FileName);
  269. end;
  270. end.