| 1 | package CSS::SpriteMaker::Layout::Packed::Node; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | CSS::SpriteMaker::Layout::Packed::Node - A node of the Packed Layout | ||
| 9 | |||
| 10 | =head1 VERSION | ||
| 11 | |||
| 12 | Version 0.01 | ||
| 13 | |||
| 14 | =cut | ||
| 15 | |||
| 16 | our $VERSION = '0.01'; | ||
| 17 | |||
| 18 | |||
| 19 | =head1 METHODS | ||
| 20 | |||
| 21 | =cut | ||
| 22 | |||
| 23 | =head2 new | ||
| 24 | |||
| 25 | Node constructor. | ||
| 26 | |||
| 27 | Parameters: | ||
| 28 | |||
| 29 | =over 4 | ||
| 30 | |||
| 31 | =item X coordinate. | ||
| 32 | |||
| 33 | =item Y coordinate. | ||
| 34 | |||
| 35 | =item Image width. | ||
| 36 | |||
| 37 | =item Image height. | ||
| 38 | |||
| 39 | =item Flag to determine if the node is used. | ||
| 40 | |||
| 41 | =item Down (Node class). | ||
| 42 | |||
| 43 | =item Right (Node class). | ||
| 44 | |||
| 45 | =back | ||
| 46 | |||
| 47 | =cut | ||
| 48 | |||
| 49 | sub new { | ||
| 50 | my $class = shift; | ||
| 51 | my $x = shift // 0; | ||
| 52 | my $y = shift // 0; | ||
| 53 | my $width = shift // 0; | ||
| 54 | my $height = shift // 0; | ||
| 55 | my $used = shift // 0; | ||
| 56 | my $down = shift; | ||
| 57 | my $right = shift; | ||
| 58 | |||
| 59 | return bless { | ||
| 60 | x => $x, | ||
| 61 | y => $y, | ||
| 62 | width => $width, | ||
| 63 | height => $height, | ||
| 64 | used => $used, | ||
| 65 | right => $right, | ||
| 66 | down => $down | ||
| 67 | }, $class; | ||
| 68 | } | ||
| 69 | |||
| 70 | =head2 find | ||
| 71 | |||
| 72 | Find a node to allocate this image size (width, height). | ||
| 73 | |||
| 74 | Node to search in. | ||
| 75 | |||
| 76 | Parameters: | ||
| 77 | |||
| 78 | =over 4 | ||
| 79 | |||
| 80 | =item Pixels to grow down (width). | ||
| 81 | |||
| 82 | =item Pixels to grow down (height). | ||
| 83 | |||
| 84 | =back | ||
| 85 | |||
| 86 | =cut | ||
| 87 | |||
| 88 | sub find { | ||
| 89 | my $self = shift; | ||
| 90 | my $node = shift; | ||
| 91 | my $width = shift; | ||
| 92 | my $height = shift; | ||
| 93 | |||
| 94 | if ($node->{used}) { | ||
| 95 | return $self->find($node->{right}, $width, $height) | ||
| 96 | || $self->find($node->{down}, $width, $height); | ||
| 97 | } | ||
| 98 | elsif ($node->{width} >= $width && $node->{height} >= $height) { | ||
| 99 | return $node; | ||
| 100 | } | ||
| 101 | |||
| 102 | return 0; | ||
| 103 | } | ||
| 104 | |||
| 105 | =head2 grow | ||
| 106 | |||
| 107 | Grow the canvas to the most appropriate direction. | ||
| 108 | |||
| 109 | Parameters: | ||
| 110 | |||
| 111 | =over 4 | ||
| 112 | |||
| 113 | =item Pixels to grow down (width). | ||
| 114 | |||
| 115 | =item Pixels to grow down (height). | ||
| 116 | |||
| 117 | =back | ||
| 118 | |||
| 119 | =cut | ||
| 120 | |||
| 121 | sub grow { | ||
| 122 | my $self = shift; | ||
| 123 | my $width = shift; | ||
| 124 | my $height = shift; | ||
| 125 | |||
| 126 | my $can_grow_d = $width <= $self->{width}; | ||
| 127 | my $can_grow_r = $height <= $self->{height}; | ||
| 128 | |||
| 129 | my $should_grow_r = $can_grow_r && $self->{height} >= ($self->{width} + $width); | ||
| 130 | my $should_grow_d = $can_grow_d && $self->{width} >= ($self->{height} + $height); | ||
| 131 | |||
| 132 | return $self->grow_right($width, $height) if $should_grow_r; | ||
| 133 | return $self->grow_down($width, $height) if $should_grow_d; | ||
| 134 | return $self->grow_right($width, $height) if $can_grow_r; | ||
| 135 | return $self->grow_down($width, $height) if $can_grow_d; | ||
| 136 | |||
| 137 | return 0; | ||
| 138 | } | ||
| 139 | |||
| 140 | =head2 clone | ||
| 141 | |||
| 142 | Clone this object. | ||
| 143 | |||
| 144 | =cut | ||
| 145 | |||
| 146 | sub clone { | ||
| 147 | my $self = shift; | ||
| 148 | my $copy = bless { %$self }, ref $self; | ||
| 149 | return $copy; | ||
| 150 | } | ||
| 151 | |||
| 152 | =head2 grow_right | ||
| 153 | |||
| 154 | Grow the canvas to the right. | ||
| 155 | |||
| 156 | Parameters: | ||
| 157 | |||
| 158 | =over 4 | ||
| 159 | |||
| 160 | =item Pixels to grow down (width). | ||
| 161 | |||
| 162 | =item Pixels to grow down (height). | ||
| 163 | |||
| 164 | =back | ||
| 165 | |||
| 166 | =cut | ||
| 167 | |||
| 168 | sub grow_right { | ||
| 169 | my $self = shift; | ||
| 170 | my $width = shift; | ||
| 171 | my $height = shift; | ||
| 172 | |||
| 173 | my $old_self = $self->clone(); | ||
| 174 | $self->{used} = 1; | ||
| 175 | $self->{x} = 0; | ||
| 176 | $self->{y} = 0; | ||
| 177 | $self->{width} += $width; | ||
| 178 | $self->{down} = $old_self; | ||
| 179 | $self->{right} = CSS::SpriteMaker::Layout::Packed::Node->new( | ||
| 180 | $old_self->{width}, | ||
| 181 | 0, | ||
| 182 | $width, | ||
| 183 | $self->{height} | ||
| 184 | ); | ||
| 185 | |||
| 186 | my $node = $self->find($self, $width, $height); | ||
| 187 | if ($node) { | ||
| 188 | return $self->split($node, $width, $height); | ||
| 189 | } | ||
| 190 | return 0; | ||
| 191 | } | ||
| 192 | |||
| 193 | =head2 grow_down | ||
| 194 | |||
| 195 | Grow the canvas down. | ||
| 196 | |||
| 197 | Parameters: | ||
| 198 | |||
| 199 | =over 4 | ||
| 200 | |||
| 201 | =item Pixels to grow down (width). | ||
| 202 | |||
| 203 | =item Pixels to grow down (height). | ||
| 204 | |||
| 205 | =back | ||
| 206 | |||
| 207 | =cut | ||
| 208 | |||
| 209 | sub grow_down { | ||
| 210 | my $self = shift; | ||
| 211 | my $width = shift; | ||
| 212 | my $height = shift; | ||
| 213 | |||
| 214 | my $old_self = $self->clone(); | ||
| 215 | $self->{used} = 1; | ||
| 216 | $self->{x} = 0; | ||
| 217 | $self->{y} = 0; | ||
| 218 | $self->{height} += $height; | ||
| 219 | $self->{right} = $old_self; | ||
| 220 | $self->{down} = CSS::SpriteMaker::Layout::Packed::Node->new( | ||
| 221 | 0, | ||
| 222 | $old_self->{height}, | ||
| 223 | $self->{width}, | ||
| 224 | $height | ||
| 225 | ); | ||
| 226 | |||
| 227 | my $node = $self->find($self, $width, $height); | ||
| 228 | if ($node) { | ||
| 229 | return $self->split($node, $width, $height); | ||
| 230 | } | ||
| 231 | |||
| 232 | return 0; | ||
| 233 | } | ||
| 234 | |||
| 235 | =head2 split | ||
| 236 | |||
| 237 | Split the node to allocate a new one of this size. | ||
| 238 | |||
| 239 | Parameters: | ||
| 240 | |||
| 241 | =over 4 | ||
| 242 | |||
| 243 | =item Node to be splitted. | ||
| 244 | |||
| 245 | =item New node width. | ||
| 246 | |||
| 247 | =item New node height. | ||
| 248 | |||
| 249 | =back | ||
| 250 | |||
| 251 | =cut | ||
| 252 | |||
| 253 | sub split { | ||
| 254 | my $self = shift; | ||
| 255 | my $node = shift; | ||
| 256 | my $width = shift; | ||
| 257 | my $height = shift; | ||
| 258 | |||
| 259 | $node->{used} = 1; | ||
| 260 | $node->{down} = CSS::SpriteMaker::Layout::Packed::Node->new( | ||
| 261 | $node->{x}, | ||
| 262 | $node->{y} + $height, | ||
| 263 | $node->{width}, | ||
| 264 | $node->{height} - $height | ||
| 265 | ); | ||
| 266 | $node->{right} = CSS::SpriteMaker::Layout::Packed::Node->new( | ||
| 267 | $node->{x} + $width, | ||
| 268 | $node->{y}, | ||
| 269 | $node->{width} - $width, | ||
| 270 | $height | ||
| 271 | ); | ||
| 272 | |||
| 273 | return $node; | ||
| 274 | } | ||
| 275 | |||
| 276 | 1; |