Advertisement
Guest User

Untitled

a guest
Jul 15th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. // -*- compile-command: "castle-engine simple-compile button_alt.lpr && ./button_alt" -*-
  2.  
  3. { Demo of a button that causes "click" when you release pointer,
  4. regardless if you release the pointer above the same button. }
  5.  
  6. uses CastleWindow, CastleUIControls, CastleControls, CastleFlashEffect,
  7. CastleColors, CastleKeysMouse;
  8.  
  9. var
  10. FlashEffect: TCastleFlashEffect;
  11.  
  12. { TMyCastleButton ------------------------------------------------------------ }
  13.  
  14. type
  15. { Like standard button, but you can release the mouse/touch anywhere
  16. (not necessarily above the same button) to make a "click". }
  17. TMyCastleButton = class(TCastleButton)
  18. strict private
  19. ClickStarted: Boolean;
  20. public
  21. function Press(const Event: TInputPressRelease): Boolean; override;
  22. function Release(const Event: TInputPressRelease): Boolean; override;
  23. end;
  24.  
  25. function TMyCastleButton.Press(const Event: TInputPressRelease): Boolean;
  26. var
  27. OldEnabled: Boolean;
  28. begin
  29. { Calling inherited with Enabled = false,
  30. this way standard (in TCastleButton) detection of clicks will not work.
  31. In effect, calling "inherited" only processes the TCastleButton children, if any.
  32. You could remove this completely, if your buttons never have children. }
  33. OldEnabled := Enabled;
  34. Enabled := false;
  35. Result := inherited;
  36. Enabled := OldEnabled;
  37.  
  38. { Below, only capture mouse clicks (or touches), ignore e.g. keyboard presses. }
  39. if Result or (Event.EventType <> itMouseButton) then Exit;
  40.  
  41. { Start click. Code similar to TCastleButton.Press implementation. }
  42. Result := ExclusiveEvents;
  43. if Enabled then
  44. begin
  45. if not Toggle then
  46. Pressed := true;
  47. // regardless of Toggle value, set ClickStarted, to be able to reach OnClick.
  48. ClickStarted := true;
  49. end;
  50. end;
  51.  
  52. function TMyCastleButton.Release(const Event: TInputPressRelease): Boolean;
  53. begin
  54. Result := inherited;
  55. if Result or (Event.EventType <> itMouseButton) then Exit;
  56.  
  57. { Code similar to TCastleButton.Press,
  58. but we remove the "CapturesEventsAtPosition(Event.Position)" condition
  59. before calling "DoClick". }
  60. if ClickStarted then
  61. begin
  62. Result := ExclusiveEvents;
  63. if not Toggle then Pressed := false;
  64. ClickStarted := false;
  65. if Enabled then
  66. DoClick;
  67. end;
  68. end;
  69.  
  70. { The rest of the example ---------------------------------------------------- }
  71.  
  72. type
  73. TEventsHandler = class
  74. class procedure ClickButton(Sender: TObject);
  75. end;
  76.  
  77. class procedure TEventsHandler.ClickButton(Sender: TObject);
  78. begin
  79. { We use FlashEffect.Flash just to notify visually that button was clicked. }
  80. FlashEffect.Flash(Yellow, false);
  81. end;
  82.  
  83. var
  84. Window: TCastleWindowBase;
  85. Button: TMyCastleButton;
  86. begin
  87. Window := TCastleWindowBase.Create(Application);
  88. Window.Open;
  89.  
  90. { Note: in cross-platform CGE application, the code below
  91. (adding controls to Window.Controls) would be placed
  92. in Application.OnInitialize event.
  93. It was just easier to write it here for this simple demo. }
  94.  
  95. Button := TMyCastleButton.Create(Application);
  96. Button.Caption := 'Click me!';
  97. Button.OnClick := @TEventsHandler(nil).ClickButton;
  98. Button.Anchor(hpLeft, 10);
  99. Button.Anchor(vpBottom, 10);
  100. Window.Controls.InsertFront(Button);
  101.  
  102. FlashEffect := TCastleFlashEffect.Create(Application);
  103. FlashEffect.FullSize := false;
  104. FlashEffect.Width := 100;
  105. FlashEffect.Height := 100;
  106. FlashEffect.Anchor(hpLeft, 10);
  107. FlashEffect.Anchor(vpBottom, 100);
  108. Window.Controls.InsertFront(FlashEffect);
  109.  
  110. Application.Run;
  111. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement