2
0

fConsoleD.pas 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. unit fConsoleD;
  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.StdCtrls,
  12. Vcl.ExtCtrls,
  13. GLS.Scene,
  14. GLS.Objects,
  15. GLS.Cadencer,
  16. GLS.SceneViewer,
  17. GLS.BaseClasses,
  18. GLS.Texture,
  19. GLS.BitmapFont,
  20. GLS.WindowsFont,
  21. GLS.Behaviours,
  22. GLS.Console,
  23. GLS.Coordinates,
  24. GLS.SimpleNavigation,
  25. GLScene.Utils;
  26. type
  27. TFormConsole = class(TForm)
  28. Viewer: TGLSceneViewer;
  29. GLCadencer1: TGLCadencer;
  30. Scene: TGLScene;
  31. GLCamera1: TGLCamera;
  32. Font1: TGLWindowsBitmapFont;
  33. GLCube1: TGLCube;
  34. GLLightSource1: TGLLightSource;
  35. Splitter1: TSplitter;
  36. Panel1: TPanel;
  37. GroupBox1: TGroupBox;
  38. ListBox1: TListBox;
  39. Splitter2: TSplitter;
  40. CheckBox1: TCheckBox;
  41. CheckBox2: TCheckBox;
  42. CheckBox3: TCheckBox;
  43. Button1: TButton;
  44. Button2: TButton;
  45. Timer1: TTimer;
  46. Label1: TLabel;
  47. Label2: TLabel;
  48. Button6: TButton;
  49. Button7: TButton;
  50. GLSimpleNavigation1: TGLSimpleNavigation;
  51. procedure GLCadencer1Progress(Sender: TObject;
  52. const deltaTime, newTime: double);
  53. procedure FormCreate(Sender: TObject);
  54. procedure FormKeyPress(Sender: TObject; var Key: char);
  55. procedure FormKeyDown(Sender: TObject; var Key: word; Shift: TShiftState);
  56. procedure ViewerMouseDown(Sender: TObject; Button: TMouseButton;
  57. Shift: TShiftState; X, Y: integer);
  58. procedure FormResize(Sender: TObject);
  59. procedure CheckBox1Click(Sender: TObject);
  60. procedure CheckBox2Click(Sender: TObject);
  61. procedure CheckBox3Click(Sender: TObject);
  62. procedure Button1Click(Sender: TObject);
  63. procedure Button2Click(Sender: TObject);
  64. procedure Button6Click(Sender: TObject);
  65. procedure Button7Click(Sender: TObject);
  66. procedure FormClose(Sender: TObject; var Action: TCloseAction);
  67. private
  68. procedure OnHelloCommand(const Sender: TGLConsoleCommand;
  69. const Console: TGLCustomConsole; var Command: TGLUserInputCommand);
  70. public
  71. procedure OnCommand(const Sender: TGLConsoleCommand;
  72. const Console: TGLCustomConsole; var Command: TGLUserInputCommand);
  73. end;
  74. var
  75. FormConsole: TFormConsole;
  76. Console: TGLConsole;
  77. implementation
  78. {$R *.DFM}
  79. procedure TFormConsole.OnHelloCommand(const Sender: TGLConsoleCommand;
  80. const Console: TGLCustomConsole; var Command: TGLUserInputCommand);
  81. begin
  82. Console.AddLine('Hi, dude!');
  83. end;
  84. procedure TFormConsole.OnCommand(const Sender: TGLConsoleCommand;
  85. const Console: TGLCustomConsole; var Command: TGLUserInputCommand);
  86. var
  87. I: integer;
  88. str: string;
  89. begin
  90. if Command.CommandCount = 0 then
  91. exit;
  92. Command.strings[0] := lowercase(Command.strings[0]);
  93. if Command.strings[0] = 'echo' then
  94. begin
  95. for I := 1 to Command.CommandCount - 1 do
  96. str := str + Command.strings[I];
  97. Console.AddLine('You just typed: ' + str);
  98. Command.UnknownCommand := False;
  99. end
  100. else if Command.strings[0] = 'exit' then
  101. begin
  102. Application.Terminate;
  103. Command.UnknownCommand := False; // user won't see it anyway, but you should
  104. // get used to puting this line in every
  105. // command you recognize :)
  106. end;
  107. if Command.UnknownCommand then
  108. Console.AddLine('Current supported external commands are:' +
  109. '"echo" and "exit"!');
  110. end;
  111. procedure TFormConsole.FormCreate(Sender: TObject);
  112. begin
  113. Console := TGLConsole.CreateAsChild(Scene.Objects);
  114. Console.Visible := False;
  115. Console.SceneViewer := Viewer;
  116. Console.Font := Font1;
  117. // optional stuff:
  118. var Path: TFileName := GetCurrentAssetPath();
  119. SetCurrentDir(Path + '\texture');
  120. Console.HudSprite.Material.Texture.Image.LoadFromFile('GLScene.bmp');
  121. Console.AddLine('Console started');
  122. Console.HUDSpriteColor := clWhite;
  123. Console.FontColor := clBlack;
  124. Console.Size := 1;
  125. // two ways of processing commands:
  126. // 1) manual
  127. Console.OnCommandIssued := OnCommand;
  128. // 2)using built-in objects (prefered)
  129. Console.Commands.Add.CommandName := 'hello';
  130. Console.Commands.Add.ShortHelp := 'Says hi to you too';
  131. Console.Commands.Add.LongHelp.Add
  132. ('Well, the console really does say "Hi, dude" to you, because');
  133. Console.Commands.Add.LongHelp.Add
  134. ('it is roude not to greet someone, when he says "hello" to you ;)');
  135. Console.Commands.Add.OnCommand := OnHelloCommand;
  136. // register additional commands to enable auto-completion function
  137. Console.AdditionalCommands.Add('echo');
  138. Console.AdditionalCommands.Add('exit');
  139. // for console saved output and loading .ini files
  140. SetCurrentDir(Path + '\script');
  141. end;
  142. procedure TFormConsole.GLCadencer1Progress(Sender: TObject;
  143. const deltaTime, newTime: double);
  144. begin
  145. Viewer.Invalidate();
  146. end;
  147. procedure TFormConsole.FormKeyPress(Sender: TObject; var Key: char);
  148. begin
  149. Console.ProcessKeyPress(Key);
  150. end;
  151. procedure TFormConsole.FormKeyDown(Sender: TObject; var Key: word;
  152. Shift: TShiftState);
  153. begin
  154. Console.ProcessKeyDown(Key);
  155. end;
  156. procedure TFormConsole.ViewerMouseDown(Sender: TObject; Button: TMouseButton;
  157. Shift: TShiftState; X, Y: integer);
  158. begin
  159. Console.Visible := not Console.Visible;
  160. end;
  161. procedure TFormConsole.FormResize(Sender: TObject);
  162. begin
  163. Console.RefreshHudSize();
  164. end;
  165. procedure TFormConsole.CheckBox1Click(Sender: TObject);
  166. begin
  167. if CheckBox1.Checked then
  168. Console.Options := Console.Options + [coAutoCompleteCommandsOnKeyPress]
  169. else
  170. Console.Options := Console.Options - [coAutoCompleteCommandsOnKeyPress];
  171. Viewer.SetFocus();
  172. end;
  173. procedure TFormConsole.CheckBox2Click(Sender: TObject);
  174. begin
  175. if CheckBox2.Checked then
  176. Console.Options := Console.Options + [coAutoCompleteCommandsOnEnter]
  177. else
  178. Console.Options := Console.Options - [coAutoCompleteCommandsOnEnter];
  179. Viewer.SetFocus();
  180. end;
  181. procedure TFormConsole.CheckBox3Click(Sender: TObject);
  182. begin
  183. if CheckBox3.Checked then
  184. Console.Options := Console.Options + [coShowConsoleHelpIfUnknownCommand]
  185. else
  186. Console.Options := Console.Options - [coShowConsoleHelpIfUnknownCommand];
  187. Viewer.SetFocus();
  188. end;
  189. procedure TFormConsole.Button1Click(Sender: TObject);
  190. begin
  191. Console.TypedCommands.SaveToFile('saved_typed_commands.ini');
  192. Viewer.SetFocus();
  193. end;
  194. procedure TFormConsole.Button2Click(Sender: TObject);
  195. begin
  196. Console.ColsoleLog.SaveToFile('saved_console_output.ini');
  197. Viewer.SetFocus();
  198. end;
  199. procedure TFormConsole.Button6Click(Sender: TObject);
  200. begin
  201. Console.TypedCommands.LoadFromFile('saved_typed_commands.ini');
  202. Viewer.SetFocus();
  203. end;
  204. procedure TFormConsole.Button7Click(Sender: TObject);
  205. begin
  206. Console.ColsoleLog.LoadFromFile('saved_console_output.ini');
  207. Console.RefreshHudSize();
  208. Viewer.SetFocus();
  209. end;
  210. procedure TFormConsole.FormClose(Sender: TObject; var Action: TCloseAction);
  211. begin
  212. GLCadencer1.Enabled := False;
  213. Console.Destroy;
  214. end;
  215. end.