Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added mouse section for paint

  • Loading branch information...
commit 2ee131cd07696f6f938c0b924ed76a9ac87114eb 1 parent 73fd254
Kartik Thakore kthakore authored

Showing 2 changed files with 42 additions and 8 deletions. Show diff stats Hide diff stats

  1. +6 3 code_listings/mouse_paint.pl
  2. +36 5 src/03-events.pod
9 code_listings/mouse_paint.pl
@@ -13,7 +13,7 @@ sub quit_event {
13 13 return 1;
14 14 }
15 15
16   -my $drawing = 0;
  16 +
17 17
18 18 my @colors = ( 0xFF0000FF, 0x00FF00FF,
19 19 0x0000FFFF, 0xFFFF00FF,
@@ -22,18 +22,21 @@ sub quit_event {
22 22 0x000000FF, 0xFFFFFFFF );
23 23
24 24 my $brush_color = 0;
  25 +
  26 +
  27 +my $drawing = 0;
25 28 sub mouse_event {
26 29
27 30 my $event = shift;
28 31
29 32 if($event->type == SDL_MOUSEBUTTONDOWN || $drawing)
30 33 {
31   -# now you can handle the details;
  34 +
32 35 $drawing = 1;
33 36 my $x = $event->button_x;
34 37 my $y = $event->button_y;
35 38 $app->draw_rect( [$x,$y, 2, 2], $colors[$brush_color]);
36   - $app->update();
  39 + $app->update();
37 40 }
38 41 $drawing = 0 if($event->type == SDL_MOUSEBUTTONUP );
39 42
41 src/03-events.pod
Source Rendered
@@ -67,8 +67,6 @@ the file 'painted.bmp'.
67 67 \label{fig:Smile
68 68
69 69
70   -
71   -
72 70 =head2 Keyboard
73 71
74 72 To handle the keyboard specifications we will create another event callback.
@@ -128,7 +126,7 @@ To handle the keyboard specifications we will create another event callback.
128 126 my $brush_color = $key_name if $key_name =~ /^\d$/;
129 127
130 128 #Get the keyboard modifier perldoc SDL::Events
131   - # We are using any CTRL so KMOD_CTRL is fine
  129 + #We are using any CTRL so KMOD_CTRL is fine
132 130 my $mod_state = SDL::Events::get_mod_state();
133 131
134 132 #Save the image.
@@ -140,7 +138,6 @@ To handle the keyboard specifications we will create another event callback.
140 138 $app->update();
141 139 }
142 140
143   - ...
144 141 $app->add_event_handler(\&quit_event);
145 142 $app->add_event_handler(\&keyboard_event);
146 143 $app->run()
@@ -159,7 +156,41 @@ The reason for this is so that C<SDLx::App> is aware of the globals before it ca
159 156
160 157 =head2 Mouse
161 158
162   -Swift input for our axis.
  159 +=begin programlisting
  160 +
  161 + #Keep track if we are drawing
  162 + my $drawing = 0;
  163 + sub mouse_event {
  164 +
  165 + my $event = shift;
  166 +
  167 + #We will detect Mouse Button events
  168 + #and check if we already started drawing
  169 + if($event->type == SDL_MOUSEBUTTONDOWN || $drawing)
  170 + {
  171 + # set drawing to 1
  172 + $drawing = 1;
  173 +
  174 + # get the X and Y values of the mouse
  175 + my $x = $event->button_x;
  176 + my $y = $event->button_y;
  177 +
  178 + # Draw a rect at the specified position
  179 + $app->draw_rect( [$x,$y, 2, 2], $colors[$brush_color]);
  180 +
  181 + # Update the application
  182 + $app->update();
  183 + }
  184 +
  185 + # Turn drawing off if we lift the mouse button
  186 + $drawing = 0 if($event->type == SDL_MOUSEBUTTONUP );
  187 + return 1;
  188 + }
  189 +
  190 +
  191 + $app->add_event_handler( \&mouse_event );
  192 +
  193 +=end programlisting
163 194
164 195 =head2 Joystick
165 196

0 comments on commit 2ee131c

Please sign in to comment.
Something went wrong with that request. Please try again.