| 1 | package CSS::SpriteMaker::Layout::FixedDimension; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use base 'CSS::SpriteMaker::Layout'; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | CSS::SpriteMaker::Layout::FixedDimension | ||
| 11 | |||
| 12 | my $FixedDimensionLayout = CSS::SpriteMaker::Layout::FixedDimension->new( | ||
| 13 | # example $rh_item_info input structure | ||
| 14 | { | ||
| 15 | "1" => { | ||
| 16 | width => 128, | ||
| 17 | height => 128, | ||
| 18 | }, | ||
| 19 | ... | ||
| 20 | }, | ||
| 21 | # max 10 items on the same row, | ||
| 22 | { | ||
| 23 | dimension => 'horizontal', | ||
| 24 | n => 10 | ||
| 25 | }, | ||
| 26 | ); | ||
| 27 | |||
| 28 | Layout maximum I<n> items on a row. | ||
| 29 | |||
| 30 | Items are chosen at random. | ||
| 31 | |||
| 32 | Input $rh_item_info structure must contain the following keys for this layout | ||
| 33 | to produce a result: | ||
| 34 | |||
| 35 | - width : the width in pixels of the image; | ||
| 36 | |||
| 37 | - height : the height in pixels of the image; | ||
| 38 | |||
| 39 | The following input parameters B<must> be specified: | ||
| 40 | |||
| 41 | - n : number of maximum items to place on the same row | ||
| 42 | |||
| 43 | The following input parameters are optional: | ||
| 44 | |||
| 45 | - dimension: can be 'horizontal' (default) or 'vertical'. | ||
| 46 | |||
| 47 | =head1 VERSION | ||
| 48 | |||
| 49 | Version 0.02 | ||
| 50 | |||
| 51 | =cut | ||
| 52 | |||
| 53 | our $VERSION = '0.02'; | ||
| 54 | |||
| 55 | =head2 new | ||
| 56 | |||
| 57 | Instantiates the layout: | ||
| 58 | |||
| 59 | my $FixedDimensionLayout = CSS::SpriteMaker::Layout::FixedDimension->new( | ||
| 60 | $rh_item_info, | ||
| 61 | { dimension => 'vertical' # 'horizontal' is the default | ||
| 62 | n => 10 # compulsory! | ||
| 63 | } | ||
| 64 | ); | ||
| 65 | |||
| 66 | =cut | ||
| 67 | |||
| 68 | sub new { | ||
| 69 | my $class = shift; | ||
| 70 | my $rh_items_info = shift; | ||
| 71 | my $rh_input_params = shift; | ||
| 72 | |||
| 73 | # defaults | ||
| 74 | $rh_input_params->{dimension} //= 'horizontal'; | ||
| 75 | |||
| 76 | my $self = bless {}, $class; | ||
| 77 | |||
| 78 | if (!$rh_items_info) { | ||
| 79 | die 'no items info hashref was passed in construction to this layout'; | ||
| 80 | } | ||
| 81 | |||
| 82 | $self->_layout_items($rh_items_info, $rh_input_params); | ||
| 83 | $self->finalize(); | ||
| 84 | |||
| 85 | return $self; | ||
| 86 | } | ||
| 87 | |||
| 88 | =head2 _layout_items | ||
| 89 | |||
| 90 | =cut | ||
| 91 | |||
| 92 | sub _layout_items { | ||
| 93 | my $self = shift; | ||
| 94 | my $rh_items_info = shift; | ||
| 95 | my $rh_input_params = shift; | ||
| 96 | |||
| 97 | if (!exists $rh_input_params->{n} || !defined $rh_input_params->{n}) { | ||
| 98 | die "the layout parameter 'n' is required for this layout to produce a result."; | ||
| 99 | } | ||
| 100 | |||
| 101 | my $n = $rh_input_params->{n}; | ||
| 102 | my $dimension = $rh_input_params->{dimension}; | ||
| 103 | |||
| 104 | # 1. put items from the same directory in the same row | ||
| 105 | my $dimension_id = $dimension eq 'horizontal' ? '0' : '1'; | ||
| 106 | |||
| 107 | my @xy = (0, 0); | ||
| 108 | |||
| 109 | my @total_wh = (0, 0); | ||
| 110 | my $dimension_size = 0; | ||
| 111 | |||
| 112 | my $i = 0; | ||
| 113 | for my $id (sort keys %$rh_items_info) { | ||
| 114 | my @wh = ($rh_items_info->{$id}{width}, $rh_items_info->{$id}{height}); | ||
| 115 | |||
| 116 | # condition to switch to the next row | ||
| 117 | if ($i == $n) { | ||
| 118 | # this item must go on the next row | ||
| 119 | $xy[1-$dimension_id] += $dimension_size; | ||
| 120 | $xy[$dimension_id] = 0; | ||
| 121 | $dimension_size = 0; | ||
| 122 | |||
| 123 | # reset condition | ||
| 124 | $i = 0; | ||
| 125 | } | ||
| 126 | |||
| 127 | # chain on the current row... | ||
| 128 | $self->set_item_coord($id, @xy); | ||
| 129 | |||
| 130 | # prepare next element | ||
| 131 | $xy[$dimension_id] += $wh[$dimension_id]; | ||
| 132 | |||
| 133 | # calculate dimension size (height or width based on dimension) | ||
| 134 | $dimension_size = $wh[1-$dimension_id] if $wh[1-$dimension_id] > $dimension_size; | ||
| 135 | |||
| 136 | # update min/max sizes | ||
| 137 | if ($xy[$dimension_id] > $total_wh[$dimension_id]) { | ||
| 138 | $total_wh[$dimension_id] = $xy[$dimension_id]; | ||
| 139 | } | ||
| 140 | if ($xy[1-$dimension_id] + $dimension_size > $total_wh[1-$dimension_id]) { | ||
| 141 | $total_wh[1-$dimension_id] = $xy[1-$dimension_id] + $dimension_size | ||
| 142 | } | ||
| 143 | |||
| 144 | $i++; | ||
| 145 | } | ||
| 146 | |||
| 147 | $self->{width} = $total_wh[0]; | ||
| 148 | $self->{height} = $total_wh[1]; | ||
| 149 | |||
| 150 | return; | ||
| 151 | } | ||
| 152 | |||
| 153 | 1; |