Permalink
Browse files

Updated 2nd example for keyboard input.

  • Loading branch information...
1 parent 2f44d6a commit b823c12f8409b9250a395eb8f8cfc39d61ebc7e7 @kthakore kthakore committed Sep 27, 2010
Showing with 76 additions and 30 deletions.
  1. +38 −29 code_listings/mouse_paint.pl
  2. +38 −1 src/03-events.pod
@@ -5,11 +5,6 @@
use SDLx::App;
my $app = SDLx::App->new( w => 200, h => 200, d => 32, title => "Quit Events");
-$app->add_event_handler( \&quit_event );
-$app->add_event_handler( \&mouse_event );
-$app->add_event_handler( \&keyboard_event );
-$app->run();
-
sub quit_event {
my $event = shift;
@@ -18,38 +13,52 @@ sub quit_event {
}
my $drawing = 0;
-my $brush_color = 0;
+
+my @colors = ( 0xFF0000FF, 0x00FF00FF,
+ 0x0000FFFF, 0xFFFF00FF,
+ 0xFF00FFFF, 0x00FFFFFF,
+ 0xCCFFCCFF, 0xFFCC33FF,
+ 0x000000FF, 0xFFFFFFFF );
+
+my $brush_color = 0;
sub mouse_event {
-
- my $event = shift;
-
- if($event->type == SDL_MOUSEBUTTONDOWN || $drawing)
- {
- # now you can handle the details
- $drawing = 1;
- my $x = $event->button_x;
- my $y = $event->button_y;
- my $colors = [ 0xFF0000FF, 0x00FF00FF, 0x0000FFFF, 0, 0xFFFFFFFF ];
- $app->[$x][$y] = $colors->[$brush_color]
- $app->update();
- }
- $drawing = 0 if($event->type == SDL_MOUSEBUTTONUP );
-
-
- return 1;
+
+ my $event = shift;
+
+ if($event->type == SDL_MOUSEBUTTONDOWN || $drawing)
+ {
+# now you can handle the details;
+ $drawing = 1;
+ my $x = $event->button_x;
+ my $y = $event->button_y;
+ $app->[$x][$y] = $colors[$brush_color];
+ $app->update();
+ }
+ $drawing = 0 if($event->type == SDL_MOUSEBUTTONUP );
+
+
+ return 1;
}
sub keyboard_event {
my $event = shift;
if ( $event->type == SDL_KEYDOWN )
- {
- my $key_name = SDL::Events::get_key_name( $event->key_sym );
+ {
+ my $key_name = SDL::Events::get_key_name( $event->key_sym );
+
+ $brush_color = $key_name if $key_name =~ /\d/;
- $brush_color = $key_name if $key_name =~ /\d/;
-
- warn 'Brush color is '.$brush_color;
- }
+ warn 'Brush color is '.$brush_color;
+ }
return 1;
}
+
+
+$app->add_event_handler( \&quit_event );
+$app->add_event_handler( \&mouse_event );
+$app->add_event_handler( \&keyboard_event );
+$app->run();
+
+
View
@@ -62,7 +62,44 @@ colors. By pressing 'q' or 'Q' we will exit. Pressing 'ctrl-s' will save our ima
=head2 Keyboard
-The ultimate input device.
+To handle the keyboard specifications we will create another event callback.
+
+=begin programlisting
+
+ ...
+
+ my @colors = ( 0xFF0000FF, 0x00FF00FF,
+ 0x0000FFFF, 0xFFFF00FF,
+ 0xFF00FFFF, 0x00FFFFFF,
+ 0xCCFFCCFF, 0xFFCC33FF,
+ 0x000000FF, 0xFFFFFFFF );
+
+ my $brush_color = 0;
+
+ ...
+
+ sub keyboard_event
+ {
+ my $event = shift;
+
+ #Check that our type of event press is a SDL_KEYDOWN
+ if( $event->type == SDL_KEYDOWN )
+ {
+ #Convert the key_symbol (integer) to a keyname
+ my $key_name = SDL::Events::get_key_name( $event->key_sym );
+
+ #if our $key_name is a digit use it as a color
+ my $brush_color = $key_name if $key_name =~ /^\d$/;
+
+ }
+ }
+
+ ...
+
+ $app->add_event_handler(\&keyboard_event);
+ $app->run()
+
+=end programlisting
=head2 Mouse

0 comments on commit b823c12

Please sign in to comment.