SpriteMaker/Layout/DirectoryBased.pm untested

filter: Statementstmt 0.0% 0 / 45 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 72 CC 6 SCAR 28.2
CC
6
Cov
0%
_layout_items
30.0
new
6.0
BEGIN
2.0
CRAP 16.8
1 package CSS::SpriteMaker::Layout::DirectoryBased;
2  
3 use strict;
4 use warnings;
5  
6 use base 'CSS::SpriteMaker::Layout';
7  
8 =head1 NAME
9  
10 CSS::SpriteMaker::Layout::DirectoryBased
11  
12 my $DirectoryBasedLayout = CSS::SpriteMaker::Layout::DirectoryBased->new(
13 {
14 "1" => {
15 width => 128,
16 height => 128,
17 pathname => '/full/path/to/file1.png',
18 parentdir => '/full/path/to',
19 },
20 ...
21 }
22 );
23  
24 Layout items based on the directory they are contained in and their filename.
25  
26 All items contained in the same sub directory are cascaded on the same row of
27 the layout.
28  
29 Input hashref items must contain the following keys
30 for this layout to produce a result:
31  
32 - pathname : the full pathname of the file;
33  
34 - width : the width in pixels of the image;
35  
36 - height : the height in pixels of the image;
37  
38 - parentdir: the full pathname of the parent directory the image is contained
39 in.
40  
41  
42 =head1 VERSION
43  
44 Version 0.02
45  
46 =cut
47  
48 our $VERSION = '0.02';
49  
50 =head2 new
51  
52 Instantiates the layout:
53  
54 my $DirectoryBasedLayout = CSS::SpriteMaker::Layout::DirectoryBased->new(
55 $rh_item_info
56 );
57  
58 =cut
59  
60 sub new {
61 my $class = shift;
62 my $rh_items_info = shift;
63  
64 my $self = bless {}, $class;
65  
66 if (!$rh_items_info) {
67 die 'no items info hashref was passed in construction to this layout';
68 }
69  
70 $self->_layout_items($rh_items_info);
71 $self->finalize();
72  
73 return $self;
74 }
75  
76 =head2 _layout_items
77  
78  
79  
80 =cut
81  
82 sub _layout_items {
83 my $self = shift;
84 my $rh_items_info = shift;
85  
86 # 1. sort items by directory, then filename
87 my @items_id_sorted =
88 sort {
89 $rh_items_info->{$a}{pathname}
90 cmp
91 $rh_items_info->{$b}{pathname}
92  
93 || $a <=> $b
94 }
95 keys %$rh_items_info;
96
97 # 2. put items from the same directory in the same row
98 my $x = 0;
99 my $y = 0;
100 my $total_height = 0;
101 my $total_width = 0;
102 my $row_height = 0;
103  
104 my $parentdir_prev;
105 for my $id (@items_id_sorted) {
106 my $w = $rh_items_info->{$id}{width};
107 my $h = $rh_items_info->{$id}{height};
108 my $parentdir = $rh_items_info->{$id}{parentdir};
109  
110 if (defined $parentdir_prev && $parentdir ne $parentdir_prev) {
111 # next row!
112 $y += $row_height;
113 $x = 0;
114 $row_height = 0;
115 }
116  
117 # chain on this row...
118 $self->set_item_coord($id, $x, $y);
119  
120 $x += $w;
121 $row_height = $h if $h > $row_height;
122 $total_width = $x if $x > $total_width;
123 $total_height = $y + $row_height if $y + $row_height > $total_height;
124  
125 $parentdir_prev = $parentdir;
126 }
127  
128 $self->{width} = $total_width;
129 $self->{height} = $total_height;
130  
131 return;
132 }
133  
134 1;