[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] Re: <no subject>



> We want to put up a window with a tic tac toe grid and then let the human
> player click on the spot where he/she wants to move.
<snip>
> We're stuck now because we can't figure out how to make the program
> respond to the human player's mouse click on the appropriate spot.

Here is a example I put together from code I wrote for a previous
project:

#!perl -w

use Mac::Events;
use Mac::Windows;
use Mac::QuickDraw;

$win = new MacWindow (
  new Rect (100, 100, 300, 300),
  'Grid Click Demo',
  1,
  documentProc,
  1,
);

$win->sethook(layout => sub {
  my $wRect = $win->window->portRect;
  $CWidth = int($wRect->right / 3);
  $CHeight = int($wRect->bottom / 3);
  SizeWindow $win->window, $CWidth * 3, $CHeight * 3;
  SetPort $win->window; InvalRect $wRect;
});
$win->layout;

$win->sethook(redraw => sub {
  PenSize(2, 2);
  MoveTo($CWidth   - 1, 0);
  LineTo($CWidth   - 1, $CHeight*3);
  MoveTo($CWidth*2 - 1, 0);
  LineTo($CWidth*2 - 1, $CHeight*3);
  MoveTo(0,         $CHeight - 1);
  LineTo($CWidth*3, $CHeight - 1);
  MoveTo(0,         $CHeight*2 - 1);
  LineTo($CWidth*3, $CHeight*2 - 1);
});

$win->sethook(click => sub {
  my $pt = $_[1];

  my $gridh = int($pt->h/$CWidth);
  my $gridv = int($pt->v/$CHeight);

  my $r = new Rect(
    $gridh * $CWidth + ($CWidth * .1),
    $gridv * $CHeight + ($CWidth * .1),
    ($gridh+1) * $CWidth - ($CWidth * .1),
    ($gridv+1) * $CHeight - ($CWidth * .1)
  );

  SetPort($win->window);
  my ($in, $oin) = (1, 1);
  InvertRect($r);
  while (StillDown()) {
    $in = PtInRect(GetMouse, $r);
    if ($in != $oin) {
      InvertRect($r);
    } 
    $oin = $in;
  }
  return unless $in;
  InvertRect($r);

  print "Clicked $gridh, $gridv\n";
});

while ($win->window) {
  WaitNextEvent;
}

END {$win->dispose}

__END__

-- 
 Kevin Reid: |    Macintosh:      
  "I'm me."  | Think different.

===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org