Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // -*- compile-command: "castle-engine simple-compile button_alt.lpr && ./button_alt" -*-
- { Demo of a button that causes "click" when you release pointer,
- regardless if you release the pointer above the same button. }
- uses CastleWindow, CastleUIControls, CastleControls, CastleFlashEffect,
- CastleColors, CastleKeysMouse;
- var
- FlashEffect: TCastleFlashEffect;
- { TMyCastleButton ------------------------------------------------------------ }
- type
- { Like standard button, but you can release the mouse/touch anywhere
- (not necessarily above the same button) to make a "click". }
- TMyCastleButton = class(TCastleButton)
- strict private
- ClickStarted: Boolean;
- public
- function Press(const Event: TInputPressRelease): Boolean; override;
- function Release(const Event: TInputPressRelease): Boolean; override;
- end;
- function TMyCastleButton.Press(const Event: TInputPressRelease): Boolean;
- var
- OldEnabled: Boolean;
- begin
- { Calling inherited with Enabled = false,
- this way standard (in TCastleButton) detection of clicks will not work.
- In effect, calling "inherited" only processes the TCastleButton children, if any.
- You could remove this completely, if your buttons never have children. }
- OldEnabled := Enabled;
- Enabled := false;
- Result := inherited;
- Enabled := OldEnabled;
- { Below, only capture mouse clicks (or touches), ignore e.g. keyboard presses. }
- if Result or (Event.EventType <> itMouseButton) then Exit;
- { Start click. Code similar to TCastleButton.Press implementation. }
- Result := ExclusiveEvents;
- if Enabled then
- begin
- if not Toggle then
- Pressed := true;
- // regardless of Toggle value, set ClickStarted, to be able to reach OnClick.
- ClickStarted := true;
- end;
- end;
- function TMyCastleButton.Release(const Event: TInputPressRelease): Boolean;
- begin
- Result := inherited;
- if Result or (Event.EventType <> itMouseButton) then Exit;
- { Code similar to TCastleButton.Press,
- but we remove the "CapturesEventsAtPosition(Event.Position)" condition
- before calling "DoClick". }
- if ClickStarted then
- begin
- Result := ExclusiveEvents;
- if not Toggle then Pressed := false;
- ClickStarted := false;
- if Enabled then
- DoClick;
- end;
- end;
- { The rest of the example ---------------------------------------------------- }
- type
- TEventsHandler = class
- class procedure ClickButton(Sender: TObject);
- end;
- class procedure TEventsHandler.ClickButton(Sender: TObject);
- begin
- { We use FlashEffect.Flash just to notify visually that button was clicked. }
- FlashEffect.Flash(Yellow, false);
- end;
- var
- Window: TCastleWindowBase;
- Button: TMyCastleButton;
- begin
- Window := TCastleWindowBase.Create(Application);
- Window.Open;
- { Note: in cross-platform CGE application, the code below
- (adding controls to Window.Controls) would be placed
- in Application.OnInitialize event.
- It was just easier to write it here for this simple demo. }
- Button := TMyCastleButton.Create(Application);
- Button.Caption := 'Click me!';
- Button.OnClick := @TEventsHandler(nil).ClickButton;
- Button.Anchor(hpLeft, 10);
- Button.Anchor(vpBottom, 10);
- Window.Controls.InsertFront(Button);
- FlashEffect := TCastleFlashEffect.Create(Application);
- FlashEffect.FullSize := false;
- FlashEffect.Width := 100;
- FlashEffect.Height := 100;
- FlashEffect.Anchor(hpLeft, 10);
- FlashEffect.Anchor(vpBottom, 100);
- Window.Controls.InsertFront(FlashEffect);
- Application.Run;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement