blib/lib/CSS/SpriteMaker/Layout/DirectoryBased.pm untested

filter: Statementstmt 0.0% 0 / 45 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 65 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.01
45  
46 =cut
47  
48 our $VERSION = '0.01';
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 keys %$rh_items_info;
94
95 # 2. put items from the same directory in the same row
96 my $x = 0;
97 my $y = 0;
98 my $total_height = 0;
99 my $total_width = 0;
100 my $row_height = 0;
101  
102 my $parentdir_prev;
103 for my $id (@items_id_sorted) {
104 my $w = $rh_items_info->{$id}{width};
105 my $h = $rh_items_info->{$id}{height};
106 my $parentdir = $rh_items_info->{$id}{parentdir};
107  
108 if (defined $parentdir_prev && $parentdir ne $parentdir_prev) {
109 # next row!
110 $y += $row_height;
111 $x = 0;
112 $row_height = 0;
113 }
114  
115 # chain on this row...
116 $self->set_item_coord($id, $x, $y);
117  
118 $x += $w;
119 $row_height = $h if $h > $row_height;
120 $total_width = $x if $x > $total_width;
121 $total_height = $y + $row_height if $y + $row_height > $total_height;
122  
123 $parentdir_prev = $parentdir;
124 }
125  
126 $self->{width} = $total_width;
127 $self->{height} = $total_height;
128  
129 return;
130 }
131  
132 1;