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

[MacPerl] WindowBounce



#!perl -w

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

use constant LOSS => 0.98;
use constant GRAV => 0.8;
use constant BOOSTVEL => 32;

sub CircDef_1 {
  my ($variant, $win, $msg, $param) = @_;
  if ($msg == wCalcRgns) {
    my $r = OffsetRect($win->portRect,
      -$win->portBits->bounds->left,
      -$win->portBits->bounds->top);
    my $circ = NewRgn();
    OpenRgn;
    FrameOval $r;
    CloseRgn $circ;
    CopyRgn $circ, $win->contRgn;
    CopyRgn $circ, $win->strucRgn;
  } elsif ($msg == wHit) {
    if (PtInRgn($param, $win->contRgn)) {
      return wInContent;
    }
  }
  return 0;
}

$BoundRgn = XorRgn(GetGrayRgn(), FrontWindow()->strucRgn);

$GWindow = new MacColorWindow (
  $wrect = new Rect(0,0,10,10),
  'WBouncer',
  1,
  \&CircDef_1,
  0, 0, FrontWindow
);
$GWindow->sethook(redraw => sub {
  my $rect = $wrect;

  for (my $shade = 65535; $shade >= 0; $shade -= 15000) {
    RGBForeColor(new RGBColor($shade, 0, 0));
    PaintOval($rect);
    $rect = InsetRect($rect, 1, 1);
  }
});
WaitNextEvent;
WaitNextEvent;
WaitNextEvent;
SetPort $GWindow->window;
InvertRgn($BoundRgn);

%pos    = ('x' => -50, 'y' => -50),
%oldpos = ('x' => 0, 'y' => 0),
%vel    = ('x' => rand 10, 'y' => rand 10),

$tick = 0;
while ($tick++, $GWindow->window) {


  foreach (qw(x y)) {
    my $did = 0;

    $oldpos{$_} = $pos{$_};
    $pos{$_} += $vel{$_};
    if (!PtInRgn(new Point($pos{'x'}+5, $pos{'y'}+5), $BoundRgn)) {
      $vel{$_} *= - LOSS;
      $pos{$_} = $oldpos{$_};
      $did = 1;
    }
    if ($did and abs($vel{$_}) < 1.5) {
      $vel{$_} = BOOSTVEL;
    } 
  }

  MoveWindow $GWindow->window, $pos{'x'}, $pos{'y'}, 0;

  $vel{'y'} += GRAV;

  unless ($tick % 20) {
    SendBehind($GWindow->window, FrontWindow());
    next if ${FrontWindow()} == ${$GWindow->window};

    DisposeRgn $BoundRgn;
    $BoundRgn = XorRgn(GetGrayRgn(), FrontWindow()->strucRgn);
    until (PtInRgn(new Point($pos{'x'}+5, $pos{'y'}+5), $BoundRgn)) {
      $pos{'x'} = rand $BoundRgn->rgnBBox->right;
      $pos{'y'} = rand $BoundRgn->rgnBBox->bottom;
    }
  }
  WaitNextEvent;
}

END {
  $GWindow->dispose if $GWindow;
}

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