Caca.pm

filter: Statementstmt 92.3% 24 / 26 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 94.2% 33 / 35 CC 1 SCAR 0.3
CC
1
Cov
92%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Caca/Sprite.pm ↑ Summary Caca/Constants.pm »
1 package Term::Caca;
2 our $AUTHORITY = 'cpan:YANICK';
3 #ABSTRACT: perl interface for libcaca (Colour AsCii Art library)
4 $Term::Caca::VERSION = '3.0.1';
5 8 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶no warnings qw/ uninitialized /;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use List::MoreUtils qw/ uniq /;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use List::Util qw/ pairmap /;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 8 ▶use FFI::Platypus::Memory;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 8 ▶use Term::Caca::FFI ':all';
Subroutine BEGIN: called CC=1 SCAR=2.6
16 use Term::Caca::Constants qw/ :colors :events /;
17  
18 use Term::Caca::Event::Key::Press;
19 use Term::Caca::Event::Key::Release;
20 use Term::Caca::Event::Mouse::Motion;
21 use Term::Caca::Event::Mouse::Button::Press;
22 use Term::Caca::Event::Mouse::Button::Release;
23 use Term::Caca::Event::Resize;
24 use Term::Caca::Event::Quit;
25  
26 use Moose;
27  
28 use MooseX::Attribute::Chained;
29  
30 use MooseX::MungeHas {
31 has_ro => [ 'is_ro' ],
32 has_rw => [ 'is_rw' ],
33 };
34  
35 use experimental qw/
36 signatures
37 postderef
38 /;
39  
40  
41 sub driver_list {
42 +{ caca_get_display_driver_list()->@* }
43 }
44  
45 sub drivers {
46 keys driver_list()->%*;
47 }
48  
49 has_ro driver =>
50 predicate => 1;
51  
52 has_ro display =>
53 predicate => 1,
54 lazy => 1,
55 default => sub($self) {
56 ( $self->has_driver
57 ? caca_create_display_with_driver(undef,$self->driver)
58 : caca_create_display(undef) ) or croak "couldn't create display";
59 };
60  
61 has_ro canvas => sub($self) { caca_get_canvas($self->display) };
62  
63 has_rw title => (
64 traits => [ 'Chained' ],
65 trigger => sub($self,$title) {
66 caca_set_display_title($self->display, $title);
67 }
68 );
69  
70 has_rw refresh_delay => (
71 traits => [ 'Chained' ],
72 trigger => sub($self,$seconds) {
73 caca_set_display_time($self->display,int( $seconds * 1_000_000 ));
74 }
75 );
76  
77 sub refresh ($self) {
78 caca_refresh_display($self->display);
79 return $self
80 }
81  
82 sub rendering_time($self) {
83 return caca_get_display_time($self->display)/1_000_000;
84 }
85  
86 # TODO fix the colors when they are a constant
87 sub set_color( $self, $foreground, $background ) {
88  
89 if( ref $foreground or 4 == length $foreground ) {
90 for( $foreground, $background ) {
91 $_ = $self->_arg_to_color($_);
92 }
93 caca_set_color_argb($self->canvas, $foreground, $background );
94 }
95 else {
96 caca_set_color_ansi($self->canvas, $foreground, $background );
97 }
98  
99 return $self;
100 }
101  
102 sub _arg_to_color($self,$arg) {
103  
104 return hex $arg unless ref $arg;
105  
106 return hex sprintf "%x%x%x%x", @$arg;
107 }
108  
109 sub char ( $self, $coord, $char = undef ) {
110 if( defined $char ) {
111 caca_put_char( $self->canvas, @$coord, ord $char );
112 }
113 return $self;
114 }
115  
116 sub mouse_position($self) {
117 [ caca_get_mouse_x( $self->display ), caca_get_mouse_y( $self->display ) ];
118 }
119  
120 sub triangle ( $self, $pa, $pb, $pc, $char = undef, $fill = undef ){
121 $char //= $fill;
122  
123 my @args = ( $self->canvas, @$pa, @$pb, @$pc );
124  
125 if ( defined $fill ) {
126 caca_fill_triangle(@args, ord $char);
127 }
128 elsif( defined $char ) {
129 caca_draw_triangle(@args, ord $char);
130 }
131 else {
132 caca_draw_thin_triangle(@args);
133 }
134  
135 return $self;
136 }
137  
138 sub clear ($self) {
139 caca_clear_canvas($self->canvas);
140 return $self;
141 }
142  
143 sub _expand_drawing_options( $self, @rest ) {
144 @rest = ( thin => 1 ) unless @rest;
145 unshift @rest, 'char' if @rest == 1;
146  
147 return @rest;
148 }
149  
150 sub _primitive ( $self, $funcs, $args, @rest ) {
151 my %opt = $self->_expand_drawing_options(@rest);
152 my( $draw, $thin, $fill ) = @$funcs;
153  
154 my @args = ( $self->canvas, @$args );
155  
156 $fill->(@args, ord $opt{fill}) if $opt{fill};
157  
158 $draw->(@args, ord $opt{char}) if $opt{char};
159  
160 $thin->(@args) if $opt{thin};
161  
162 return $self;
163 }
164  
165 sub box ( $self, $c1, $c2, @rest ){
166  
167 $self->_primitive(
168 [ \&caca_draw_box, \&caca_draw_thin_box, \&caca_fill_box ],
169 [ @$c1, @$c2 ],
170 @rest,
171 );
172  
173 }
174  
175 sub ellipse ( $self, $center, $rx, $ry, @rest ) {
176 $self->_primitive(
177 [ \&caca_draw_ellipse, \&caca_draw_thin_ellipse, \&caca_fill_ellipse ],
178 [ @$center, $rx, $ry ],
179 @rest,
180 );
181 }
182  
183 sub circle ( $self, $center, $radius, @rest ) {
184 $self->_primitive(
185 [ \&caca_draw_ellipse, \&caca_draw_thin_ellipse, \&caca_fill_ellipse ],
186 [ @$center, $radius, $radius ],
187 @rest,
188 );
189 }
190  
191 sub text ( $self, $coord, $text ) {
192 length $text > 1
193 ? caca_put_str( $self->canvas, @$coord, $text )
194 : caca_put_char( $self->canvas, @$coord, ord $text );
195  
196 return $self;
197 }
198  
199 sub polyline( $self, $points, @rest ) {
200 my %opts = $self->_expand_drawing_options(@rest);
201  
202 my @x = map { $_->[0] } @$points;
203 my @y = map { $_->[1] } @$points;
204 my $n = @x - !$opts{close};
205  
206 $opts{char} ? caca_draw_polyline( $self->canvas, \@x, \@y, $n, ord $opts{char} )
207 : caca_draw_thin_polyline( $self->canvas, \@x, \@y, $n );
208  
209 return $self;
210 }
211  
212 sub line ( $self, $pa, $pb, $char = undef ) {
213  
214 defined ( $char )
215 ? caca_draw_line($self->canvas, @$pa, @$pb, ord $char)
216 : caca_draw_thin_line($self->canvas, @$pa, @$pb );
217  
218 return $self;
219 }
220  
221 sub canvas_width($self) {
222 return caca_get_canvas_width($self->canvas);
223 }
224  
225 sub canvas_height($self) {
226 return caca_get_canvas_height($self->canvas);
227 }
228  
229  
230 # TODO: troff seems to trigger a segfault
231 my @export_formats = qw/ caca ansi text html html3 irc ps svg tga /;
232  
233 sub export( $self, $format = 'caca' ) {
234  
235 croak "format '$format' not supported"
236 unless grep { $format eq $_ } @export_formats;
237  
238 my $size = malloc UINT_SIZE();
239  
240 my $export = caca_export_canvas_to_memory( $self->canvas, $format eq 'text' ? 'ansi' : $format, $size );
241  
242 no warnings 'uninitialized';
243 $export =~ s/\e\[?.*?[\@-~]//g if $format eq 'text';
244
245 return $export;
246 }
247  
248 sub canvas_size($self) {
249 [ $self->canvas_width, $self->canvas_height ];
250 }
251  
252 sub set_ansi_color( $self, $foreground, $background ) {
253 caca_set_color_ansi( $self->canvas, $foreground, $background );
254  
255 return $self;
256 }
257  
258 my %event_map = pairmap { $a => 'Term::Caca::Event::' . $b } (
259 KEY_PRESS ,=> 'Key::Press',
260 KEY_RELEASE ,=> 'Key::Release',
261 MOUSE_MOTION ,=> 'Mouse::Motion',
262 MOUSE_PRESS ,=> 'Mouse::Button::Press',
263 MOUSE_RELEASE ,=> 'Mouse::Button::Release',
264 RESIZE ,=> 'Resize',
265 QUIT ,=> 'Quit',
266 );
267  
268 sub wait_for_event ( $self, $mask = ANY_EVENT, $timeout = 0 ) {
269  
270 $mask //= ANY_EVENT;
271 $timeout *= 1_000_000 unless $timeout == -1;
272  
273  
274 # TODO change the 36 to the local size of the caca_event_t
275 my $event = malloc 36;
276  
277 caca_get_event( $self->display, $mask, $event, $timeout, 1 );
278  
279 my $class = $event_map{ caca_get_event_type( $event ) } or return;
280  
281 return $class->new( event => $event );
282  
283 }
284  
285 sub DEMOLISH ($self,@) {
286 free $self->display if $self->has_display;
287 }
288  
289 'Term::Caca';
290  
291 __END__
292  
293 =pod
294  
295 =encoding UTF-8
296  
297 =head1 NAME
298  
299 Term::Caca - perl interface for libcaca (Colour AsCii Art library)
300  
301 =head1 VERSION
302  
303 version 3.0.1
304  
305 =head1 SYNOPSIS
306  
307 use Term::Caca;
308  
309 my $caca = Term::Caca->new;
310 $caca->text( [5, 5], "pwn3d");
311 $caca->refresh;
312 sleep 3;
313  
314 =head1 DESCRIPTION
315  
316 C<Term::Caca> is an API for the ASCII drawing library I<libcaca>.
317  
318 This version of C<Term::Caca> is compatible with the I<1.x> version of
319 the libcaca library (development has been made against version
320 0.99.beta19 of the library).
321  
322 =head1 EXPORTS
323  
324 See L<Term::Caca::Constants> for exportable constants.
325  
326 =head1 CLASS METHODS
327  
328 =head3 driver_list
329  
330 Returns an hash which keys are the available display drivers
331 and the values their descriptions.
332  
333 =head3 drivers
334  
335 Returns the list of available drivers.
336  
337 =head1 METHODS
338  
339 =head2 Constructor
340  
341 =head3 new
342  
343 Instantiates a Term::Caca object.
344  
345 The optional argument I<driver> can be passed to select a specific display
346 driver. If it's not given, the best available driver will be used.
347  
348 =head2 Display and Canvas
349  
350 =head3 title( $title )
351  
352 Getter/setter for the window title.
353  
354 The setter returns the invocant I<Term::Caca> object.
355  
356 =head3 refresh
357  
358 Refreshes the display.
359  
360 Returns the invocant I<Term::Caca> object.
361  
362 =head3 set_refresh_delay( $seconds )
363  
364 Sets the refresh delay in seconds. The refresh delay is used by
365 C<refresh> to achieve constant framerate.
366  
367 If the time is zero, constant framerate is disabled. This is the
368 default behaviour.
369  
370 Returns the invocant I<Term::Caca> object.
371  
372 =head3 rendering_time()
373  
374 Returns the average rendering time, which is measured as the time between
375 two C<refresh()> calls in seconds. If constant framerate is enabled via
376 C<set_refresh_delay()>, the average rendering time will be close to the
377 requested delay even if the real rendering time was shorter.
378  
379 =head3 clear()
380  
381 Clears the canvas using the current background color.
382  
383 Returns the invocant object.
384  
385 =head3 canvas_size
386  
387 Returns the width and height of the canvas,
388 as an array ref.
389  
390 =head2 canvas_width
391  
392 Returns the canvas width.
393  
394 =head3 canvas_height
395  
396 Returns the canvas height.
397  
398 =head3 mouse_position
399  
400 Returns the position of the mouse as an array ref
401  
402 This function is not reliable if the ncurses or S-Lang
403 drivers are being used, because mouse position is only detected whe
404 the mouse is clicked. Other drivers such as X11 work well.
405  
406 =head2 Export
407  
408 =head3 export( $format )
409  
410 Returns the canvas in the given format.
411  
412 Supported formats are
413  
414 =over
415  
416 =item "caca": native libcaca files.
417  
418 =item "ansi": ANSI art (CP437 charset with ANSI colour codes).
419  
420 =item "text": ASCII text file.
421  
422 =item "html": an HTML page with CSS information.
423  
424 =item "html3": an HTML table that should be compatible with most navigators, including textmode ones.
425  
426 =item "irc": UTF-8 text with mIRC colour codes.
427  
428 =item "ps": a PostScript document.
429  
430 =item "svg": an SVG vector image.
431  
432 =item "tga": a TGA image.
433  
434 =back
435  
436 If no format is provided, defaults to C<caca>.
437  
438 =head2 Colors
439  
440 =head3 set_ansi_color( $foreground, $background )
441  
442 Sets the foreground and background colors used by primitives,
443 using colors as defined by the color constants.
444  
445 $t->set_ansi_color( LIGHTRED, WHITE );
446  
447 Returns the invocant object.
448  
449 =head3 set_color( $foreground, $background )
450  
451 Sets the foreground and background colors used by primitives.
452  
453 Each color is an array ref to a ARGB (transparency + RGB) set of values,
454 all between 0 and 15. Alternatively, they can be given as a string of the direct
455 hexadecimal value.
456  
457 # red on white
458 $t->set_color( [ 15, 15, 0, 0 ], 'ffff' );
459  
460 Returns the invocant object.
461  
462 =head2 Text
463  
464 =head3 text( \@coord, $text )
465  
466 Prints I<$text> at the given coordinates.
467  
468 Returns the invocant C<Term::Caca> object.
469  
470 =head3 char( \@coord, $char )
471  
472 Prints the character I<$char> at the given coordinates.
473 If I<$char> is a string of more than one character, only
474 the first character is printed.
475  
476 Returns the invocant C<Term::Caca> object.
477  
478 =head2 Primitives Drawing
479  
480 The drawing of all primitive is controlled by C<drawing_options>.
481 Unless specified otherwise, the possible options are:
482  
483 If no option is given, or if the option C<thin => 1> is given,
484 the primitive will be drawn using ascii art.
485  
486 If a single character or the C<char => $x> pair is given,
487 then this character it will be used to trace
488 the primitive.
489  
490 If C<fill => $y> is given, then that character will be used to
491 fill the primitive.
492  
493 C<fill> and C<char> or C<thin> can be used in combination
494 to produce a primitive drawn with one char and filled with the other.
495  
496 =head3 line( \@point_a, \@point_b, @drawing_options )
497  
498 Draws a line from I<@point_a> to I<@point_b>. In this instance
499 C<@drawing_options> only accept C<thin> or C<char>.
500  
501 Returns the invocant object.
502  
503 =head3 polyline( \@points, @drawing_options )
504  
505 Draws the polyline defined by I<@points>, where each point is an array ref
506 of the coordinates. E.g.
507  
508 $t->polyline( [ [ 0,0 ], [ 10,15 ], [ 20, 15 ] ] );
509  
510 The additional option I<close> can be given as part of the C<drawing_options>.
511 If true, the end point of the polyline will be connected to the first point.
512  
513 Returns the invocant I<Term::Caca> object.
514  
515 =head3 circle( \@center, $radius, @drawing_options )
516  
517 Draws a circle centered at I<@center> with a radius
518 of I<$radius>.
519  
520 Returns the invocant object.
521  
522 =head3 ellipse( \@center, $radius_x, $radius_y, @drawing_options )
523  
524 Draws an ellipse centered at I<@center> with an x-axis
525 radius of I<$radius_x> and a y-radius of I<$radius_y>.
526  
527 Returns the invocant object.
528  
529 =head3 box( \@top_corner, $width, $height, @drawing_options )
530  
531 Draws a rectangle of dimensions I<$width> and
532 I<$height> with its upper-left corner at I<@top_corner>.
533  
534 Returns the invocant object.
535  
536 =head3 triangle( \@point_a, \@point_b, \@point_c, @drawing_options )
537  
538 Draws a triangle defined by the three given points.
539  
540 Returns the invocant object.
541  
542 =head2 Event Handling
543  
544 =head3 wait_for_event( $mask, $timeout )
545  
546 Waits and returns a C<Term::Caca::Event> object matching the mask.
547  
548 C<$timeout> is in seconds. If set to 0 (the default), the method returns immediatly and,
549 if no event was found, returns nothing. If C<$timeout> is negative,
550 the method waits forever for an event matching the mask.
551  
552 # wait for 5 seconds for a key press or the closing of the window
553 my $event = $t->wait_for_event( KEY_PRESS | QUIT, 5 );
554  
555 say "user is idle" unless defined $event;
556  
557 exit if $event->isa( 'Term::Caca::Event::Quit' );
558  
559 say "user typed ", $event->char;
560  
561 =head1 SEE ALSO
562  
563 libcaca - L<https://github.com/cacalabs/libcaca> and L<http://caca.zoy.org/>
564  
565 =head1 AUTHORS
566  
567 =over 4
568  
569 =item *
570  
571 John Beppu <beppu@cpan.org>
572  
573 =item *
574  
575 Yanick Champoux <yanick@cpan.org>
576  
577 =back
578  
579 =head1 COPYRIGHT AND LICENSE
580  
581 This software is Copyright (c) 2019, 2018, 2013, 2011 by John Beppu.
582  
583 This is free software, licensed under:
584  
585 DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE, Version 2, December 2004
586  
587 =cut
« Caca/Sprite.pm ↑ Summary Caca/Constants.pm »