Yarn/Graphene.pm

filter: Statementstmt 66.6% 4 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 75.0% 6 / 8 CC 1 SCAR 1.4
CC
1
Cov
67%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
« Yarn.pm ↑ Summary Yarn/Types.pm »
1 6 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package Intertangle::Yarn::Graphene;
3 # ABSTRACT: Load the Graphene graphic types library
4 $Intertangle::Yarn::Graphene::VERSION = '0.001';
5 6 ▶use Glib::Object::Introspection;
Subroutine BEGIN: called CC=1 SCAR=2.6
6 use DynaLoader;
7  
8 my $_GRAPHENE_BASENAME = 'Graphene';
9 my $_GRAPHENE_VERSION = '1.0';
10 my $_GRAPHENE_PACKAGE = __PACKAGE__;
11  
12 my @_FLATTEN_ARRAY_REF_RETURN_FOR = qw/
13 /;
14  
15 use Env qw(@GI_TYPELIB_PATH @PATH);
16 use Alien::Graphene;
17  
18 sub import {
19 my @search_path = ();
20 if( Alien::Graphene->install_type eq 'share' ) {
21 unshift @GI_TYPELIB_PATH, Alien::Graphene->gi_typelib_path;
22 @search_path = ( search_path => Alien::Graphene->gi_typelib_path );
23 if( $^O eq 'MSWin32' ) {
24 push @PATH, Alien::Graphene->rpath;
25 } else {
26 push @DynaLoader::dl_library_path, Alien::Graphene->rpath;
27 my @files = DynaLoader::dl_findfile("-lgraphene-1.0");
28 DynaLoader::dl_load_file($files[0]) if @files;
29 }
30 }
31  
32 Glib::Object::Introspection->setup(
33 basename => $_GRAPHENE_BASENAME,
34 version => $_GRAPHENE_VERSION,
35 package => $_GRAPHENE_PACKAGE,
36 flatten_array_ref_return_for => \@_FLATTEN_ARRAY_REF_RETURN_FOR,
37 @search_path,
38 );
39 }
40  
41 sub Inline {
42 return unless $_[-1] eq 'C';
43  
44 require Intertangle::API::Glib;
45 require Hash::Merge;
46 my $glib = Intertangle::API::Glib->Inline($_[-1]);
47  
48 my @nosearch = $^O eq 'MSWin32' ? (':nosearch') : ();
49 my @search = $^O eq 'MSWin32' ? ( ':search' ) : ();
50 my $graphene = {
51 CCFLAGSEX => join(" ", delete $glib->{CCFLAGSEX}, Alien::Graphene->cflags),
52 LIBS => join(" ", @nosearch, delete $glib->{LIBS}, Alien::Graphene->libs, @search),
53 AUTO_INCLUDE => <<C,
54 #include <graphene.h>
55 #include <graphene-gobject.h>
56 C
57 };
58  
59 my $merge = Hash::Merge->new('RETAINMENT_PRECEDENT');
60 $merge->merge( $glib, $graphene );
61 }
62  
63 package Intertangle::Yarn::Graphene::DataPrinterRole {
64 use Role::Tiny;
65 use Module::Load;
66  
67 BEGIN {
68 eval {
69 autoload Data::Printer::Filter;
70 autoload Term::ANSIColor;
71 autoload Package::Stash;
72 };
73 }
74  
75 sub _data_printer {
76 my ($self, $prop) = @_;
77  
78 my $FIELDS = Package::Stash->new( ref $self )->get_symbol( '@FIELDS' );
79 my $data = {
80 map { $_ => $self->$_ } @$FIELDS
81 };
82  
83 my $text = '';
84  
85 $text .= $prop->{colored} ? "(@{[colored(['green'], ref($self))]}) " : "(@{[ ref($self) ]}) ";
86 $text .= Data::Printer::np($data, %$prop, _current_indent => 0, multiline => 0, );
87  
88 $text;
89 }
90 }
91 $Intertangle::Yarn::Graphene::DataPrinterRole::VERSION = '0.001';
92  
93  
94 package Intertangle::Yarn::Graphene::Size {
95 our @FIELDS = qw(width height);
96 use Role::Tiny::With;
97 with 'Intertangle::Yarn::Graphene::DataPrinterRole';
98 use overload
99 '""' => \&op_str,
100 'eq' => \&op_eq,
101 '==' => \&op_eq;
102  
103 sub op_str {
104 "[w: @{[ $_[0]->width ]}, h: @{[ $_[0]->height ]}]"
105 }
106  
107 sub op_eq {
108 $_[0]->width == (Scalar::Util::blessed $_[1] ? $_[1]->width : $_[1]->[0] )
109 && $_[0]->height == (Scalar::Util::blessed $_[1] ? $_[1]->height : $_[1]->[1] )
110 }
111  
112 sub to_HashRef {
113 +{ map { $_ => $_[0]->$_ } @FIELDS };
114 }
115 }
116 $Intertangle::Yarn::Graphene::Size::VERSION = '0.001';
117  
118 package Intertangle::Yarn::Graphene::Point {
119 our @FIELDS = qw(x y);
120 use Scalar::Util;
121 use Role::Tiny::With;
122 with 'Intertangle::Yarn::Graphene::DataPrinterRole';
123 use overload
124 '""' => \&op_str,
125 'eq' => \&op_eq,
126 '==' => \&op_eq,
127 'neg' => \&op_neg;
128  
129 sub op_str {
130 "[x: @{[ $_[0]->x ]}, y: @{[ $_[0]->y ]}]";
131 }
132  
133 sub op_eq {
134 $_[0]->x == (Scalar::Util::blessed $_[1] ? $_[1]->x : $_[1]->[0] )
135 && $_[0]->y == (Scalar::Util::blessed $_[1] ? $_[1]->y : $_[1]->[1] )
136 }
137  
138 sub op_neg {
139 Intertangle::Yarn::Graphene::Point->new(
140 x => - $_[0]->x,
141 y => - $_[0]->y,
142 );
143 }
144  
145 sub to_HashRef {
146 +{ map { $_ => $_[0]->$_ } @FIELDS };
147 }
148  
149 sub to_ArrayRef {
150 [ map { $_[0]->$_ } @FIELDS ];
151 }
152  
153 sub to_Point3D {
154 my ($self) = @_;
155 my $point3d = Intertangle::Yarn::Graphene::Point3D->new(
156 x => $self->x,
157 y => $self->y,
158 z => 0,
159 );
160 }
161 }
162 $Intertangle::Yarn::Graphene::Point::VERSION = '0.001';
163  
164 package Intertangle::Yarn::Graphene::Point3D {
165 our @FIELDS = qw(x y z);
166 use Scalar::Util;
167 use Role::Tiny::With;
168 with 'Intertangle::Yarn::Graphene::DataPrinterRole';
169 use overload
170 '""' => \&op_str,
171 'eq' => \&op_eq,
172 '==' => \&op_eq;
173  
174 sub op_str {
175 "[x: @{[ $_[0]->x ]}, y: @{[ $_[0]->y ]}], y: @{[ $_[0]->z ]}";
176 }
177  
178 sub op_eq {
179 $_[0]->x == (Scalar::Util::blessed $_[1] ? $_[1]->x : $_[1]->[0] )
180 && $_[0]->y == (Scalar::Util::blessed $_[1] ? $_[1]->y : $_[1]->[1] )
181 && $_[0]->z == (Scalar::Util::blessed $_[1] ? $_[1]->z : $_[1]->[1] )
182 }
183  
184 sub to_HashRef {
185 +{ map { $_ => $_[0]->$_ } @FIELDS };
186 }
187 }
188 $Intertangle::Yarn::Graphene::Point3D::VERSION = '0.001';
189  
190 package Intertangle::Yarn::Graphene::Vec2 {
191 our @FIELDS = qw(x y);
192 use Scalar::Util;
193 use Role::Tiny::With;
194 with 'Intertangle::Yarn::Graphene::DataPrinterRole';
195 use overload
196 '""' => \&op_str,
197 'eq' => \&op_eq,
198 '==' => \&op_eq,
199 'neg' => \&op_neg;
200  
201 sub new {
202 my ($class, %args) = @_;
203  
204 my $vec2 = Intertangle::Yarn::Graphene::Vec2->alloc();
205 $vec2->init( $args{x}, $args{y} );
206  
207 $vec2;
208 }
209  
210 sub x {
211 $_[0]->dot( Intertangle::Yarn::Graphene::Vec2::x_axis() );
212 }
213  
214 sub y {
215 $_[0]->dot( Intertangle::Yarn::Graphene::Vec2::y_axis() );
216 }
217  
218 sub op_str {
219 "[x: @{[ $_[0]->x ]}, y: @{[ $_[0]->y ]}]";
220 }
221  
222 sub op_eq {
223 $_[0]->x == (Scalar::Util::blessed $_[1] ? $_[1]->x : $_[1]->[0] )
224 && $_[0]->y == (Scalar::Util::blessed $_[1] ? $_[1]->y : $_[1]->[1] )
225 }
226  
227 sub op_neg {
228 $_[0]->negate;
229 }
230  
231 sub to_HashRef {
232 +{ map { $_ => $_[0]->$_ } @FIELDS };
233 }
234  
235 sub to_Vec3 {
236 my ($self) = @_;
237 my $point3d = Intertangle::Yarn::Graphene::Vec3->new(
238 x => $self->x,
239 y => $self->y,
240 z => 0,
241 );
242 }
243 }
244 $Intertangle::Yarn::Graphene::Vec2::VERSION = '0.001';
245  
246 package Intertangle::Yarn::Graphene::Vec3 {
247 our @FIELDS = qw(x y z);
248 use Scalar::Util;
249 use Role::Tiny::With;
250 with 'Intertangle::Yarn::Graphene::DataPrinterRole';
251 use overload
252 '""' => \&op_str,
253 'eq' => \&op_eq,
254 '==' => \&op_eq;
255  
256 sub new {
257 my ($class, %args) = @_;
258  
259 my $vec3 = Intertangle::Yarn::Graphene::Vec3->alloc();
260 $vec3->init( $args{x}, $args{y}, $args{z} );
261  
262 $vec3;
263 }
264  
265 sub x {
266 $_[0]->dot( Intertangle::Yarn::Graphene::Vec3::x_axis() );
267 }
268  
269 sub y {
270 $_[0]->dot( Intertangle::Yarn::Graphene::Vec3::y_axis() );
271 }
272  
273 sub z {
274 $_[0]->dot( Intertangle::Yarn::Graphene::Vec3::z_axis() );
275 }
276  
277 sub op_str {
278 "[x: @{[ $_[0]->x ]}, y: @{[ $_[0]->y ]}, z: @{[ $_[0]->z ]}]";
279 }
280  
281 sub op_eq {
282 $_[0]->x == (Scalar::Util::blessed $_[1] ? $_[1]->x : $_[1]->[0] )
283 && $_[0]->y == (Scalar::Util::blessed $_[1] ? $_[1]->y : $_[1]->[1] )
284 && $_[0]->z == (Scalar::Util::blessed $_[1] ? $_[1]->z : $_[1]->[2] )
285 }
286  
287 sub to_HashRef {
288 +{ map { $_ => $_[0]->$_ } @FIELDS };
289 }
290 }
291 $Intertangle::Yarn::Graphene::Vec3::VERSION = '0.001';
292  
293 package Intertangle::Yarn::Graphene::Rect {
294 our @FIELDS = qw(origin size);
295 use Role::Tiny::With;
296 with 'Intertangle::Yarn::Graphene::DataPrinterRole';
297  
298 sub new {
299 my ($class, %args) = @_;
300  
301 my $rect = Intertangle::Yarn::Graphene::Rect::alloc();
302 $rect->init(
303 $args{origin}->x, $args{origin}->y,
304 $args{size}->width, $args{size}->height,
305 );
306  
307 $rect;
308 }
309 }
310 $Intertangle::Yarn::Graphene::Rect::VERSION = '0.001';
311  
312 package Intertangle::Yarn::Graphene::Matrix {
313 use Module::Load;
314 use overload
315 '""' => \&op_str,
316 '*' => \&op_transform,
317 'x' => \&op_matmult;
318  
319 sub new_from_arrayref {
320 my ($class, $data) = @_;
321 my $obj = $class->new;
322 $obj->init_from_arrayref( $data );
323  
324 $obj;
325 }
326  
327 sub new_from_float {
328 my ($class, $data) = @_;
329 my $obj = $class->new;
330 $obj->init_from_float( $data );
331  
332 $obj;
333 }
334  
335 sub init_from_arrayref {
336 my ($class, $data) = @_;
337  
338 unless(
339 @$data == 4
340 && @{ $data->[0] } == 4
341 && @{ $data->[1] } == 4
342 && @{ $data->[2] } == 4
343 && @{ $data->[3] } == 4
344 ) {
345 die "Matrix data must be a 4x4 ArrayRef";
346 }
347  
348 $class->init_from_float(
349 [
350 @{ $data->[0] },
351 @{ $data->[1] },
352 @{ $data->[2] },
353 @{ $data->[3] },
354 ]
355 );
356 }
357  
358 sub to_ArrayRef {
359 my ($self) = @_;
360 my $data = [
361 map {
362 my $row = $self->get_row($_);
363 [ $row->get_x, $row->get_y, $row->get_z, $row->get_w ]
364 } 0..3
365 ];
366  
367 $data;
368 }
369  
370 sub _data_printer {
371 my ($self, $prop) = @_;
372  
373 BEGIN {
374 eval {
375 autoload Data::Printer::Filter;
376 autoload Term::ANSIColor;
377 };
378 }
379  
380 my $text = '';
381  
382 $text .= $prop->{colored} ? "(@{[colored(['green'], ref($self))]}) " : "(@{[ ref($self) ]}) ";
383 $text .= Data::Printer::np($self->to_ArrayRef, %$prop, _current_indent => 0, multiline => 0, );
384  
385 $text;
386 }
387  
388 sub op_str {
389 my $row_text = sub { "@{[ $_[0]->get_x ]} @{[ $_[0]->get_y ]} @{[ $_[0]->get_z ]} @{[ $_[0]->get_w ]}" };
390 my $text = "";
391 $text .= "[\n";
392 $text .= " "x4 . $row_text->( $_[0]->get_row(0) ) . "\n";
393 $text .= " "x4 . $row_text->( $_[0]->get_row(1) ) . "\n";
394 $text .= " "x4 . $row_text->( $_[0]->get_row(2) ) . "\n";
395 $text .= " "x4 . $row_text->( $_[0]->get_row(3) ) . "\n";
396 $text .= "]\n";
397 }
398  
399 sub transform_point {
400 my ($self, $point ) = @_;
401  
402 my $point3d = $point->to_Point3D;
403 my $t_point3d = $self->transform_point3d( $point3d );
404  
405 return Intertangle::Yarn::Graphene::Point->new(
406 x => $t_point3d->x,
407 y => $t_point3d->y,
408 );
409 };
410  
411 sub transform {
412 my ($matrix, $other) = @_;
413  
414 my $result;
415  
416 if( $other->isa('Intertangle::Yarn::Graphene::Vec4') ) { $result = $matrix->transform_vec4( $other )
417 } elsif( $other->isa('Intertangle::Yarn::Graphene::Vec3') ) { $result = $matrix->transform_vec3( $other )
418 } elsif( $other->isa('Intertangle::Yarn::Graphene::Point') ) { $result = $matrix->transform_point( $other )
419 } elsif( $other->isa('Intertangle::Yarn::Graphene::Point3D') ) { $result = $matrix->transform_point3d( $other )
420 } elsif( $other->isa('Intertangle::Yarn::Graphene::Rect') ) { $result = $matrix->transform_rect( $other )
421 } elsif( $other->isa('Intertangle::Yarn::Graphene::Bounds') ) { $result = $matrix->transform_bounds( $other )
422 } elsif( $other->isa('Intertangle::Yarn::Graphene::Box') ) { $result = $matrix->transform_box( $other )
423 } elsif( $other->isa('Intertangle::Yarn::Graphene::Sphere') ) { $result = $matrix->transform_sphere( $other )
424 } else {
425 die "Unknown type for transformation: @{[ ref $other ]}";
426 }
427  
428  
429 $result;
430 }
431  
432 sub op_transform {
433 $_[0]->transform( $_[1] );
434 }
435  
436 sub op_matmult {
437 $_[0]->multiply( $_[1] );
438 }
439 }
440 $Intertangle::Yarn::Graphene::Matrix::VERSION = '0.001';
441  
442 1;
443  
444 __END__
« Yarn.pm ↑ Summary Yarn/Types.pm »