blib/lib/Win32/GUITaskAutomate.pm

filter: Statementstmt 85.7% 12 / 14 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 89.4% 17 / 19 CC 1 SCAR 0.6
CC
1
Cov
86%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Win32::GUITaskAutomate;
2  
3 2 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8 our $VERSION = '0.07';
9  
10 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 0 ▶use Win32::GUIRobot qw(:all);
Subroutine BEGIN: called CC=1 SCAR=2.6
12 use Win32::Clipboard;
13  
14  
15 sub new {
16 my ( $class, %args ) = @_;
17
18 my $self = { PICS => {} };
19 bless $self, $class;
20
21 if ( ref $args{load} ) {
22 if ( ref $args{load} eq 'HASH' ) {
23 $self->load( $args{load} );
24 }
25 else {
26 croak "`load` argument must be a hashref";
27 }
28 }
29 $self->clip( Win32::Clipboard );
30
31 return $self;
32 }
33  
34 sub load {
35 my ( $self, $pics_ref )= @_;
36
37 keys %$pics_ref;
38 my $robot_pics_ref = $self->pics;
39 while ( my ( $name, $filename ) = each %$pics_ref ) {
40 my $image = LoadImage( $filename );
41 croak "Cannot load '$filename': $@" unless $image;
42  
43 croak "Image '$filename' is of different depth than the screen"
44 if ImageDepth( $image ) != ScreenDepth();
45 croak "Image '$filename' is wider than the screen"
46 if ImageWidth( $image ) > ScreenWidth();
47 croak "Image '$filename' is higher than the screen"
48 if ImageHeight( $image ) > ScreenHeight();
49  
50 $robot_pics_ref->{ $name } = $image;
51 }
52 }
53
54 sub pics {
55 my $self = shift;
56 if ( @_ ) {
57 $self->{ PICS } = shift;
58 }
59 return $self->{ PICS };
60 }
61  
62 sub find_do {
63 my ( $self, $name, $what_ref, $wait) = @_;
64 croak '$what_ref must be an ARRAYREF!!'
65 unless ref $what_ref eq 'ARRAY';
66
67 $wait ||= 100.1;
68 my $pic = $self->pics->{ $name }
69 or croak "Invalid image name in find_do{}";
70 my $wait_ref = WaitForImage( $pic, maxwait => $wait );
71
72 unless ( ref $wait_ref and exists $wait_ref->{ok} ) {
73 carp "Could not find image $name :(";
74 return;
75 }
76  
77 $self->do( $what_ref, @{ $wait_ref }{ qw(x y) } );
78
79 return 1;
80 }
81  
82 sub do {
83 my ( $self, $what_ref, $origin_x, $origin_y ) = @_;
84 $origin_x ||= 0;
85 $origin_y ||= 0;
86 foreach my $action ( @$what_ref ) {
87 if ( ref $action eq 'HASH' ) {
88 $action->{x} ||= 0;
89 $action->{y} ||= 0;
90  
91 my $m_x = $origin_x + $action->{x};
92 my $m_y = $origin_y + $action->{y};
93
94 if ( $action->{lmb} ) {
95 $self->click_mouse( $m_x, $m_y, 'Left' );
96 }
97 elsif ( $action->{rmb} ) {
98 $self->click_mouse( $m_x, $m_y, 'Right' );
99 }
100 elsif ( $action->{lmbd} ) {
101 $self->click_mouse( $m_x, $m_y, 'Left', 2);
102 }
103 elsif ( $action->{rmbd} ) {
104 $self->click_mouse( $m_x, $m_y, 'Right', 2);
105 }
106 elsif ( $action->{mw} ) {
107 MouseMoveWheel( $action->{mw} );
108 }
109 elsif ( $action->{mmb} ) {
110 $self->click_mouse( $m_x, $m_y, 'Middle' );
111 }
112 elsif ( $action->{mmbd} ) {
113 $self->click_mouse( $m_x, $m_y, 'Middle', 2 );
114 }
115 elsif ( $action->{drag} ) {
116 $self->drag_mouse(
117 'LEFT',
118 $m_x,
119 $m_y,
120 $origin_x + $action->{d_x},
121 $origin_y + $action->{d_y},
122 );
123 }
124 elsif ( $action->{rdrag} ) {
125 $self->drag_mouse(
126 'RIGHT',
127 $m_x,
128 $m_y,
129 $origin_x + $action->{d_x},
130 $origin_y + $action->{d_y},
131 );
132 }
133 elsif ( $action->{mdrag} ) {
134 $self->drag_mouse(
135 'MIDDLE',
136 $m_x,
137 $m_y,
138 $origin_x + $action->{d_x},
139 $origin_y + $action->{d_y},
140 );
141 }
142 elsif ( $action->{save} ) {
143 $self->mouse_coords( [ GetCursorPos() ] );
144 }
145 elsif ( $action->{restore} ) {
146 return 1
147 unless ref $self->mouse_coords() eq 'ARRAY';
148 MouseMoveAbsPix( @{ $self->mouse_coords } );
149 }
150 }
151 elsif ( ref $action eq 'ARRAY' ) {
152 Sleep( $action->[0] );
153 }
154 elsif ( ref $action eq 'SCALAR' ) {
155 $self->set_clip( $$action );
156 }
157 else {
158 SendKeys( $action );
159 }
160 }
161
162 return 1;
163 }
164  
165 sub drag_mouse {
166 my ( $self, $button, $x, $y, $d_x, $d_y ) = @_;
167 $button = ucfirst lc $button;
168 croak "Invalid mouse button in ->drag_mouse (must be 'Left',"
169 . " 'Right', or 'Middle'"
170 unless $button eq 'Left'
171 or $button eq 'Right'
172 or $button eq 'Middle';
173
174 $x ||= 0;
175 $y ||= 0;
176 $d_x ||= 0;
177 $d_y ||= 0;
178  
179 MouseMoveAbsPix( $x, $y );
180 SendMouse( "{${button}DOWN}" );
181 MouseMoveAbsPix( $d_x, $d_y );
182 SendMouse( "{${button}UP}" );
183 return 1;
184 }
185  
186 sub click_mouse {
187 my ( $self, $x, $y, $button, $times ) = @_;
188 $button = ucfirst lc $button;
189 croak "Invalid mouse button in ->click_mouse (must be 'Left',"
190 . " 'Right', or 'Middle'"
191 unless $button eq 'Left'
192 or $button eq 'Right'
193 or $button eq 'Middle';
194
195 $times ||= 1;
196 SendMouseClick( $x, $y, $button ) for 1 .. $times;
197 return 1;
198 }
199  
200 sub get_clip {
201 my $self = shift;
202 return $self->clip->Get;
203 }
204  
205 sub set_clip {
206 my ( $self, $what ) = @_;
207 $self->clip->Set( $what );
208 return 1;
209 }
210  
211 sub clip {
212 my $self = shift;
213 if ( @_ ) {
214 $self->{ CLIP } = shift;
215 }
216 return $self->{ CLIP };
217 }
218  
219 sub mouse_coords {
220 my $self = shift;
221 if ( @_ ) {
222 $self->{ MOUSE_COORDS } = shift;
223 }
224 return $self->{ MOUSE_COORDS };
225 }
226  
227  
228 1;
229  
230  
231  
232 1;
233 __END__
↑ Summary