SDL-App-FPS
view release on metacpan or search on metacpan
lib/SDL/App/FPS/Button.pm view on Meta::CPAN
$self->{callback} = $callback;
if ($self->{shape} == BUTTON_ELLIPTIC)
{
$self->{hit} = \&_hit_elliptic;
$self->{r2} = $self->{w} * $self->{h};
}
else
{
$self->{hit} = \&_hit_rect;
$self->{x1} = $self->{x} - int($self->{w} / 2);
$self->{y1} = $self->{y} - int($self->{h} / 2);
$self->{x2} = $self->{x} + int($self->{w} / 2);
$self->{y2} = $self->{y} + int($self->{h} / 2);
}
if (($self->{type} & BUTTON_CLICK) != 0)
{
$self->{click_event_state} = 0; # reset state
}
if (($self->{type} & BUTTON_OUT) != 0)
{
$self->{out_event_state} = 0; # reset state (is out)
}
if (($self->{type} & BUTTON_IN) != 0)
{
$self->{in_event_state} = 1; # reset state (is out)
}
$self->{args} = [ @args ];
$self;
}
sub resize ($$$)
{
# set a new w and h of the area
my ($self,$w,$h) = @_;
$self->{w} = abs($w);
$self->{h} = abs($h);
$self->{x1} = $self->{x} - int($self->{w} / 2);
$self->{y1} = $self->{y} - int($self->{h} / 2);
$self->{x2} = $self->{x} + int($self->{w} / 2);
$self->{y2} = $self->{y} + int($self->{h} / 2);
$self;
}
sub move_to ($$$)
{
# set a new x and y of the area
my ($self,$x,$y) = @_;
$self->{x} = int(abs($x));
$self->{y} = int(abs($y));
$self->{x1} = $self->{x} - int($self->{w} / 2);
$self->{y1} = $self->{y} - int($self->{h} / 2);
$self->{x2} = $self->{x} + int($self->{w} / 2);
$self->{y2} = $self->{y} + int($self->{h} / 2);
$self;
}
sub x ($$)
{
my $self = shift;
if (@_ > 0)
{
$self->{x} = abs(shift);
$self->{x1} = $self->{x} - int($self->{w} / 2);
$self->{x2} = $self->{x} + int($self->{w} / 2);
}
$self->{x};
}
sub y ($$)
{
my $self = shift;
if (@_ > 0)
{
$self->{y} = abs(shift);
$self->{y1} = $self->{y} - int($self->{h} / 2);
$self->{y2} = $self->{y} + int($self->{h} / 2);
}
$self->{y};
}
sub width ($$)
{
my $self = shift;
if (@_ > 0)
{
$self->{w} = abs(shift) || 1;
$self->{x1} = $self->{x} - int($self->{w} / 2);
$self->{x2} = $self->{x} + int($self->{w} / 2);
}
$self->{w};
}
sub height ($$)
{
my $self = shift;
if (@_ > 0)
{
$self->{h} = abs(shift) || 1;
$self->{y1} = $self->{y} - int($self->{h} / 2);
$self->{y2} = $self->{y} + int($self->{h} / 2);
}
$self->{h};
}
sub _hit_rect ($$$)
{
# given an x and y coordinates, returns true whether the point x,y is inside
# the area
my ($self,$x,$y) = @_;
1 - ((($x < $self->{x1}) || ($x > $self->{x2}) ||
($y < $self->{y1}) || ($y > $self->{y2})) <=> 0);
}
sub _hit_elliptic ($$$)
{
# given an x and y coordinates, returns true whether the point x,y is inside
# the area
my ($self,$x,$y) = @_;
my $xdiff = $self->{x} - $x; $xdiff *= $xdiff;
my $ydiff = $self->{y} - $y; $ydiff *= $ydiff;
($xdiff + $ydiff <= $self->{r2}) <=> 0;
}
sub check ($$$)
{
# check whether the event occured in our area or not
( run in 1.998 second using v1.01-cache-2.11-cpan-524268b4103 )