fGuiPaint.pas 8.6 KB

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