Recent

Author Topic: Control Event Question  (Read 441 times)

JLWest

  • Hero Member
  • *****
  • Posts: 1099
Control Event Question
« on: July 05, 2022, 10:29:16 pm »
I have 52 TImage's on a form representing a deck of cards.
Each TImage onclick event has the same code 'CardAction' coded.
During play of the game North has 13 Cards, South 13 Cards, West 13 Cards and East 13 Cards.

if it is North turn to play I would like to turn the OnClick event 'CardAction'  off for South,East and West.

After North has Played I would like to turn the OnClick event 'CardAction'  back on.

Is this possible to do in code?

Thanks.
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

Josh

  • Hero Member
  • *****
  • Posts: 957
Re: Control Event Question
« Reply #1 on: July 06, 2022, 12:01:10 am »
assuming the following
images 1-13 are north, 14-26 east, 27-39 south , 40-52 west
and you have a variable called player that holds the string north,east,south,west then a simple way would be to add the following code idea to your onclick event,

Code: Pascal  [Select][+][-]
  1.  
  2. onclick event...
  3. var ok:boolean=false;
  4.       cardnum:integer;
  5. begin
  6.   your code to  get card number from the sender name
  7.   case cardnum of
  8.      1..13:ok:=upcase(player)='NORTH';
  9.      14..26:ok:=upcase(player)='EAST';
  10.      27..39:ok:=upcase(player)='SOUTH';    
  11.      40..52:ok:=upcase(player)='WEST';
  12.   end;
  13.   if not ok then exit;
  14.   .....
  15.   your normal on cllick code;
  16. end;
  17.  
;

not compileable code.. as no op code
   
« Last Edit: July 06, 2022, 12:05:08 am by Josh »
Development Installation Lazarus 1.3, FPC 2.7.1,Windows 7/8 32/64, OSX, *nix

Test Environment Lazarus & FPC Trunk on Windows and OSX (Cocoa Mainly on OSX). Testing also Crosscompile windows to OSX.. 
Any posts made from 2015 will be based on Lazarus Trunk.

JLWest

  • Hero Member
  • *****
  • Posts: 1099
Re: Control Event Question
« Reply #2 on: July 06, 2022, 12:48:31 am »
@Josh
Thanks

Images are named I1..I52;

North,South,East and West are hands of 13 cards.
North's hand can contain 1,9,16,17,18,19,22,23,24,25,32,41 and 52
       So  1 would be I1 which is the Ace of Clubs and 9 would be I9 which is the 6 of Clubs, and so on.
 
After the distribution of the cards I always know the card number in two hands  the South hand and the Dummy hand.

I may know the cards in the other two hands if I'm replaying a saved hand from history. If this is not a replay the cards not in South's hand and the dummy hand are in the ThePool.

Every Image onclick event is coded to CardAction
CardAction decides what action need to happen to say the Ace Spades.

If it is North turn to play I don't want East,South or West cards to be active.

That's why I was wondering if there is way to turn OnClick events on and off.

 

Code: Pascal  [Select][+][-]
  1. procedure TForm1.CardAction(Sender: TObject);
  2. Var S: String;
  3.   Hand: HANDS;
  4.   CName: String;
  5.   aName: String;
  6.   CNo: integer=0;
  7.   aLeed: String;
  8.  begin
  9.   if (sender is timage) then begin
  10.      CName := TImage(Sender).Name; {'I40'=Ace Spades}  




                       
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

y.ivanov

  • Sr. Member
  • ****
  • Posts: 427
Re: Control Event Question
« Reply #3 on: July 06, 2022, 09:45:53 am »
Put the card index into the Tag property of the TImage, e.g. TImage with Name of 'I20' to have Tag := 20. Thus later you can check if the corresponding card is present into the North array. (according to https://forum.lazarus.freepascal.org/index.php/topic,59668.msg444985.html#msg444985).
Then later you can check into the 'CardAction':

   
Code: Pascal  [Select][+][-]
  1.     type
  2.       THand = array[1..13] of Integer;
  3.  
  4.     var
  5.       North, East, South, West: THand;
  6.  
  7.     function IsInHand(ACard: Integer; var AHand: THand): Boolean;
  8.     var I: Integer;
  9.     begin
  10.       Result := False;
  11.       for I := Low(AHand) to High(AHand) do
  12.         if ACard = AHand[I] then
  13.           Exit(True);
  14.     end;
  15.  
  16.     procedure TForm1.CardAction(Sender: TObject);
  17.     Var S: String;
  18.       Hand: HANDS;
  19.       CName: String;
  20.       aName: String;
  21.       CNo: integer=0;
  22.       aLeed: String;
  23.       Card: Integer;
  24.      begin
  25.       if (sender is timage) then begin
  26.          CName := TImage(Sender).Name; {'I40'=Ace Spades}
  27.  
  28.          Card := TImage(Sender).Tag;
  29.          if It_is_North_turn and IsInHand(Card, North)  then
  30.            Play_the_Card(Card)
  31.          else
  32.            Exit;
  33.  
  34.  
BTW, How you handle decreasing number of cards during play? Replacing with a special card index? 0? 

As Thaddy suggested (https://forum.lazarus.freepascal.org/index.php/topic,59668.msg445159.html#msg445159) it will be a way simpler to use sets for your player hands, since the check and inclusion/exclusion of cards will be naturally handled, consider:

Code: Pascal  [Select][+][-]
  1. type
  2.   TCard = (1..52);
  3.   TCards = set of TCard;
  4.   THand = TCards;
  5. var
  6.     CardRecs: array[TCard] of CardRecords;
  7.     Deck: TCards;
  8.     North, East, South, West: THand;
  9.     Card: TCard;

then will be no need to use loops for checking or adding/removing:

Code: Pascal  [Select][+][-]
  1.   Include(Card, North); // add the Card to the North hand
  2.   Exclude(Card, North); // remove the Card from the North hand
  3.   if Card in North then ...; // check North player possesses the Card
  4.  

You must use a typecast when assigning from/to Image.Tag property:
Code: Pascal  [Select][+][-]
  1. Image.Tag := Integer(Card);
  2. Card := TCard(Image.Tag);

"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

derek.john.evans

  • Jr. Member
  • **
  • Posts: 53
Re: Control Event Question
« Reply #4 on: July 06, 2022, 10:21:19 am »
You can use 4 TPanel's (NSEW) and place 13 TImage's in each panel, you can disable OnClick's for all TImage's in a TPanel "group" by setting TPanel.Enabled = False

You can also compare the clicked TImage's Parent with each panel to identify its group.

Ideally, all the controls would be created dynamically, and destroyed during play. LCL already contains methods for BringToFront/SendToBack/Parent := .....,

So, game piece movement is mostly implemented by LCL. Yes, Tag or Name can be used for more grouping, but card placement is after all, visual.
« Last Edit: July 06, 2022, 10:27:50 am by derek.john.evans »

y.ivanov

  • Sr. Member
  • ****
  • Posts: 427
Re: Control Event Question
« Reply #5 on: July 06, 2022, 10:44:19 am »
*snip*
So, game piece movement is mostly implemented by LCL. Yes, Tag or Name can be used for more grouping, but card placement is after all, visual.
I should probably mention something about the MVC pattern here, but I'll refrain to...
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

JLWest

  • Hero Member
  • *****
  • Posts: 1099
Re: Control Event Question
« Reply #6 on: July 06, 2022, 04:59:13 pm »
@y.ivanov

Give me a lot to consider thanks. My decreasing card numbering was a mistake. Had to a special functions to determine winner of a hand.
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

 

TinyPortal © 2005-2018