/
easing2
executable file
·62 lines (52 loc) · 1.23 KB
/
easing2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env perl
use strict;
use warnings;
use SDL;
use SDL::Event;
use SDLx::App;
my $app = SDLx::App->new(
width => 400,
height => 400,
title => 'Easing - Draggable');
my $dragging = 0;
my $easing = 0.5;
my $target = {
x => 200,
y => 200,
};
my $ball = {
x => 0,
y => 0,
r => 10,
};
$app->add_event_handler(\&event);
$app->add_move_handler (\&calc);
$app->add_show_handler (\&render);
$app->run();
sub event {
my $event = shift;
my $controller = shift;
$controller->stop if $event->type == SDL_QUIT;
$dragging = 1 if $event->type == SDL_MOUSEBUTTONDOWN &&
($event->button_x-$ball->{x})**2 +
($event->button_y-$ball->{y})**2 < $ball->{r}**2;
$dragging = 0 if $event->type == SDL_MOUSEBUTTONUP;
if ($dragging && $event->type == SDL_MOUSEMOTION) {
$ball->{x} = $event->button_x;
$ball->{y} = $event->button_y;
}
}
sub calc {
my ($step, $app) = @_;
if (!$dragging) {
$ball->{x} += ($target->{x} - $ball->{x}) * $easing * $step;
$ball->{y} += ($target->{y} - $ball->{y}) * $easing * $step;
}
}
sub render {
my ($delta, $app) = @_;
$app->draw_rect([0, 0, $app->w, $app->h], 0); # clear screen
$app->draw_circle_filled([$ball->{x}, $ball->{y}], $ball->{r},
[255, 0, 0, 255]);
$app->update();
}