Glen Morse
Glen Morse

Reputation: 2593

Disable mouse click till procedure is done

Currently I have blocks (3d cubes) , when you click on one , it will create another block on top of it. like so

procedure TForm2.cubeClick(sender: TObject);
var
  cube: Tcube;
begin
cube := Sender as Tcube;
if setblocks then
  begin
    totalblocks := totalblocks +1 ;
    CreateCube[totalblocks]:=tcube.Create(self);
    CreateCube[totalblocks].Visible := true;
    CreateCube[totalblocks].Name := 'cubename'+inttostr(totalblocks);
    CreateCube[totalblocks].Position.x := cube.Position.X;
    CreateCube[totalblocks].Position.Y := cube.Position.y;
    CreateCube[totalblocks].Position.Z := cube.Position.Z -1;
    CreateCube[totalblocks].Material.Texture.CreateFromFile(gamedir+'\pics\'+blocktype);
    CubeData[totalblocks] := blocktype;
    CreateCube[totalblocks].Material.Lighting := false;
    CreateCube[totalblocks].Material.Modulation := TTextureMode.tmReplace;
    CreateCube[totalblocks].Parent := viewport3d1;
    CreateCube[totalblocks].OnClick := cubeClick;
    CreateCube[totalblocks].OnMouseDown := mousedown;
  end;
   label2.Text := inttostr(10000 - totalblocks);
end;

Issue i am having is if i click fast enough, it stops adding block, unsure why. but I think if i could disable the mouse click till this whole procedure is done, then it would take care of it. Is this something you can do in firemonkey, anyone know how?

Thanks Glen

Upvotes: 0

Views: 2673

Answers (2)

David Heffernan
David Heffernan

Reputation: 612854

You can certainly disable an OnClick event handler. There are a number of ways but the simplest is to set the OnClick property to nil.

However, doing so will have no impact or bearing on your program. The reason being that the value of the OnClick property is never used by the framework during the execution of the OnClick event. Input events are not re-entrant. If they were then it would be next to impossible to write UI programs.

In short, your question has a simple answer, but it will not help you solve your problem Setting OnClick to nil for the duration of your event handler will not change the behaviour of your program.

Upvotes: 0

Ken White
Ken White

Reputation: 125661

Certainly. First, move your logic out of the CubeClick event into another method. (The code below presumes your Cube is Cube1 - adjust it if it isn't.)

procedure TForm2.HandleCubeClick(Sender: TObject);
var
  cube: Tcube;
  OldCubeClick: TNotifyEvent;
begin
  // Save old handler
  OldCubeClick := Cube1.OnClick;
  // Clear it to disable
  Cube1.OnClick := nil;
  try
    cube := Sender as Tcube;
    if setblocks then
    begin
      totalblocks := totalblocks +1 ;
      CreateCube[totalblocks]:=tcube.Create(self);
      CreateCube[totalblocks].Visible := true;
      // Rest of code here

    end;
  finally
    // Reset handler again to enable
    Cube1.OnClick := OldCubeClick;
  end;
end;

Now change your CubeClick event to call that new procedure:

procedure TForm2.CubeClick(Sender: TObject);
begin
  HandleCubeClick(Sender);
end;

Upvotes: 4

Related Questions