mouse.pp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. {
  2. Mouse example for the PTCPas library
  3. This source code is in the public domain
  4. }
  5. program MouseExample;
  6. {$MODE objfpc}
  7. uses
  8. ptc, SysUtils;
  9. var
  10. console: IPTCConsole;
  11. surface: IPTCSurface;
  12. format: IPTCFormat;
  13. event: IPTCEvent;
  14. pixels: PUint32;
  15. color: Uint32;
  16. width, height: Integer;
  17. I: Integer;
  18. X, Y: Integer;
  19. button: Boolean;
  20. Done: Boolean = False;
  21. begin
  22. try
  23. try
  24. { create console }
  25. console := TPTCConsoleFactory.CreateNew;
  26. { create format }
  27. format := TPTCFormatFactory.CreateNew(32, $FF0000, $FF00, $FF);
  28. { open the console }
  29. console.open('Mouse example', format);
  30. { we're going to draw our own cursor, so disable the default cursor }
  31. console.option('hide cursor');
  32. { create surface matching console dimensions }
  33. surface := TPTCSurfaceFactory.CreateNew(console.width, console.height, format);
  34. { initialization }
  35. X := 0;
  36. Y := 0;
  37. repeat
  38. { wait for events }
  39. console.NextEvent(event, True, PTCAnyEvent);
  40. { handle mouse events }
  41. if Supports(event, IPTCMouseEvent) then
  42. begin
  43. { if there's more than one mouse event, process them all... }
  44. repeat
  45. X := (event as IPTCMouseEvent).X;
  46. Y := (event as IPTCMouseEvent).Y;
  47. button := PTCMouseButton1 in (event as IPTCMouseEvent).ButtonState;
  48. until not console.NextEvent(event, False, [PTCMouseEvent]);
  49. end;
  50. { handle keyboard events }
  51. if Supports(event, IPTCKeyEvent) and (event as IPTCKeyEvent).Press then
  52. begin
  53. case (event as IPTCKeyEvent).Code of
  54. PTCKEY_G: console.Option('grab mouse');
  55. PTCKEY_U: console.Option('ungrab mouse');
  56. PTCKEY_ESCAPE: Done := True;
  57. end;
  58. end;
  59. { clear surface }
  60. surface.clear;
  61. { lock surface }
  62. pixels := surface.lock;
  63. try
  64. { get surface dimensions }
  65. width := surface.width;
  66. height := surface.height;
  67. if button then
  68. color := $00FF00 { green cursor, if button 1 is pressed }
  69. else
  70. color := $FFFFFF; { white cursor if button 1 is not pressed }
  71. { draw a small cross for a cursor }
  72. for I := 2 to 10 do
  73. begin
  74. if (X - I) >= 0 then
  75. pixels[X - I + Y * width] := color;
  76. if (X + I) < width then
  77. pixels[X + I + Y * width] := color;
  78. if (Y - I) >= 0 then
  79. pixels[X + (Y - I) * width] := color;
  80. if (Y + I) < height then
  81. pixels[X + (Y + I) * width] := color;
  82. end;
  83. finally
  84. { unlock surface }
  85. surface.unlock;
  86. end;
  87. { copy to console }
  88. surface.copy(console);
  89. { update console }
  90. console.update;
  91. until Done;
  92. finally
  93. if Assigned(console) then
  94. console.close;
  95. end;
  96. except
  97. on error: TPTCError do
  98. { report error }
  99. error.report;
  100. end;
  101. end.