| 1 | package CSS::SpriteMaker::Layout; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use List::Util qw(max min); | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | CSS::SpriteMaker::Layout - Layout interface for items placed on a 2D grid. | ||
| 11 | |||
| 12 | Allows to access coordinates of items laid out on a 2D grid. | ||
| 13 | |||
| 14 | Shouldn't be instantiated directly, but subclasses should be instantiated | ||
| 15 | instead. | ||
| 16 | |||
| 17 | =head1 VERSION | ||
| 18 | |||
| 19 | Version 0.04 | ||
| 20 | |||
| 21 | =cut | ||
| 22 | |||
| 23 | our $VERSION = '0.04'; | ||
| 24 | |||
| 25 | =head2 _layout_items | ||
| 26 | |||
| 27 | Lays out the items given their properties. These properties can be global or | ||
| 28 | about individual items, and have the form of the following hashref: | ||
| 29 | |||
| 30 | { | ||
| 31 | '<item_id>' : { | ||
| 32 | width => <integer>, | ||
| 33 | height => <integer>, | ||
| 34 | first_pixel_x => <integer>, | ||
| 35 | first_pixel_y => <integer>, | ||
| 36 | ... other arbitrary properties, if any | ||
| 37 | }, | ||
| 38 | ... | ||
| 39 | } | ||
| 40 | |||
| 41 | This method should never be called on this class, but on a subclass. It contains | ||
| 42 | the implementation of the specific layout after all. | ||
| 43 | |||
| 44 | =cut | ||
| 45 | |||
| 46 | sub _layout_items { | ||
| 47 | my $self = shift; | ||
| 48 | my $rh_item_info = shift; | ||
| 49 | die "you shouldn't be calling layout_items directly on this object, but a subclass should implement it!"; | ||
| 50 | } | ||
| 51 | |||
| 52 | =head2 get_item_coord | ||
| 53 | |||
| 54 | Gets the coordinates of a specific item within the layout. | ||
| 55 | |||
| 56 | my ($x, $y) = $Layout->get_item_coord("129"); | ||
| 57 | |||
| 58 | Returns a list containing the x and the y coordinates of the specified element | ||
| 59 | respectively. | ||
| 60 | |||
| 61 | =cut | ||
| 62 | |||
| 63 | sub get_item_coord { | ||
| 64 | my $self = shift; | ||
| 65 | my $id = shift; | ||
| 66 | |||
| 67 | die "finalize() was not called on this class!" if !$self->{_layout_ran}; | ||
| 68 | |||
| 69 | if (!defined $self->{items} || !defined $self->{items}{$id}) { | ||
| 70 | warn "item id: $id doesn't appear to be part of this layout"; | ||
| 71 | return; | ||
| 72 | } | ||
| 73 | |||
| 74 | my $rh_coords = $self->{items}{$id}; | ||
| 75 | return ($rh_coords->{x}, $rh_coords->{y}); | ||
| 76 | } | ||
| 77 | |||
| 78 | =head2 set_item_coord | ||
| 79 | |||
| 80 | Sets the coordinates of a layout item. | ||
| 81 | |||
| 82 | # sets coordinates of item #129 to x: 100 y: 200 | ||
| 83 | $Layout->set_item_coord("129", 100, 200); | ||
| 84 | |||
| 85 | Sets coordinates of the given element internally and returns undef. | ||
| 86 | |||
| 87 | =cut | ||
| 88 | |||
| 89 | sub set_item_coord { | ||
| 90 | my $self = shift; | ||
| 91 | my $id = shift; | ||
| 92 | my $x = shift; | ||
| 93 | my $y = shift; | ||
| 94 | |||
| 95 | $self->{items} = {} if !defined $self->{items}; | ||
| 96 | |||
| 97 | $self->{items}{$id} = { | ||
| 98 | x => $x, | ||
| 99 | y => $y, | ||
| 100 | }; | ||
| 101 | |||
| 102 | return; | ||
| 103 | } | ||
| 104 | |||
| 105 | =head2 move_items | ||
| 106 | |||
| 107 | Moves all the items in this layout by the given deltay and deltax. | ||
| 108 | |||
| 109 | =cut | ||
| 110 | |||
| 111 | sub move_items { | ||
| 112 | my $self = shift; | ||
| 113 | my ($dx, $dy) = @_; | ||
| 114 | for my $id ($self->get_item_ids) { | ||
| 115 | my ($x, $y) = $self->get_item_coord($id); | ||
| 116 | $self->set_item_coord($id, $x+$dx, $y+$dy); | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | =head2 delete_item | ||
| 121 | |||
| 122 | Deletes the item with the specified id from the internal list of items that | ||
| 123 | have been layed out. | ||
| 124 | |||
| 125 | WARNING - this doesn't trigger a re-layout: will result in having a hole in | ||
| 126 | the current layout. Be aware of it. | ||
| 127 | |||
| 128 | Triggers a warning if the element with the specified id doesn't exist in | ||
| 129 | the current layout. | ||
| 130 | |||
| 131 | =cut | ||
| 132 | |||
| 133 | sub delete_item { | ||
| 134 | my $self = shift; | ||
| 135 | my $item_id = shift; | ||
| 136 | |||
| 137 | if (!exists $self->{items}{$item_id}) { | ||
| 138 | warn "the item with id \"$item_id\" you are trying to delete doesn't exist in the current layout"; | ||
| 139 | return; | ||
| 140 | } | ||
| 141 | delete $self->{items}{$item_id}; | ||
| 142 | return; | ||
| 143 | } | ||
| 144 | |||
| 145 | =head2 merge_with | ||
| 146 | |||
| 147 | Merges the current layout with the one specified. For a successful merge to | ||
| 148 | happen, items in the old and in the new layout must have different ids. | ||
| 149 | |||
| 150 | =cut | ||
| 151 | |||
| 152 | sub merge_with { | ||
| 153 | my $self = shift; | ||
| 154 | my $Layout = shift; | ||
| 155 | |||
| 156 | my ($minx, $miny); | ||
| 157 | for my $id ($Layout->get_item_ids()) { | ||
| 158 | my ($x, $y) = $Layout->get_item_coord($id); | ||
| 159 | |||
| 160 | # check that the id doesn't exist | ||
| 161 | if (exists $self->{items}{$id}) { | ||
| 162 | warn "the id $id already exists in the target layout!"; | ||
| 163 | } | ||
| 164 | |||
| 165 | # merge | ||
| 166 | $self->set_item_coord($id, $x, $y); | ||
| 167 | } | ||
| 168 | |||
| 169 | return; | ||
| 170 | } | ||
| 171 | |||
| 172 | =head2 get_item_ids | ||
| 173 | |||
| 174 | Returns the id of each item into an array. | ||
| 175 | |||
| 176 | my @ids = $Layout->get_item_ids(); | ||
| 177 | |||
| 178 | =cut | ||
| 179 | |||
| 180 | sub get_item_ids { | ||
| 181 | my $self = shift; | ||
| 182 | return sort { $a <=> $b } keys %{$self->{items}}; | ||
| 183 | } | ||
| 184 | |||
| 185 | =head2 get_layout_ascii_string | ||
| 186 | |||
| 187 | Returns the current layout in an ascii string suitable for console printing. | ||
| 188 | |||
| 189 | Optionally takes width and height of the canvas in px if the canvas is to be | ||
| 190 | reduced to a specific dimension in pixels. | ||
| 191 | |||
| 192 | my $ascii_string = $Layout->get_layout_ascii_string(); | ||
| 193 | |||
| 194 | OR | ||
| 195 | |||
| 196 | my $ascii_string = $Layout->get_layout_ascii_string({ | ||
| 197 | canvas_width => 80, | ||
| 198 | canvas_height => 60, | ||
| 199 | rh_item_info => { | ||
| 200 | '<item_id>' => { | ||
| 201 | width => <integer>, | ||
| 202 | height => <integer>, | ||
| 203 | }, | ||
| 204 | ... | ||
| 205 | } | ||
| 206 | }); | ||
| 207 | |||
| 208 | NOTE: the $Layout->finalize() method should've been called prior calling this | ||
| 209 | method. | ||
| 210 | |||
| 211 | =cut | ||
| 212 | |||
| 213 | sub get_layout_ascii_string { | ||
| 214 | my $self = shift; | ||
| 215 | my $rh_options = shift; | ||
| 216 | |||
| 217 | die "finalize() was not called on this layout class!" if !$self->{_layout_ran}; | ||
| 218 | |||
| 219 | my %canvas; | ||
| 220 | my %ids; | ||
| 221 | |||
| 222 | # find max id and min id | ||
| 223 | my @item_ids = $self->get_item_ids; | ||
| 224 | my $max_item_id = max(@item_ids); | ||
| 225 | my $min_item_id = min(@item_ids); | ||
| 226 | |||
| 227 | # find canvas size | ||
| 228 | my $remap_x = 0; | ||
| 229 | my $remap_y = 0; | ||
| 230 | |||
| 231 | my ($original_width, $original_height); | ||
| 232 | if (my $rh_info = $rh_options->{rh_item_info}) { | ||
| 233 | $original_width = (max map { | ||
| 234 | my ($x, undef) = $self->get_item_coord($_); | ||
| 235 | $x + $rh_info->{$_}{width} | ||
| 236 | } @item_ids); | ||
| 237 | |||
| 238 | $original_height = (max map { | ||
| 239 | my (undef, $y) = $self->get_item_coord($_); | ||
| 240 | $y + $rh_info->{$_}{height} | ||
| 241 | } @item_ids); | ||
| 242 | } | ||
| 243 | else { | ||
| 244 | $original_height = 1 + (max map { my ($x, $y) = $self->get_item_coord($_); $x } @item_ids); | ||
| 245 | $original_width = 1 + (max map { my ($x, $y) = $self->get_item_coord($_); $y } @item_ids); | ||
| 246 | } | ||
| 247 | |||
| 248 | my $canvas_width; | ||
| 249 | my $canvas_height; | ||
| 250 | |||
| 251 | if (exists $rh_options->{canvas_width}) { | ||
| 252 | $canvas_width = $rh_options->{canvas_width}; | ||
| 253 | $remap_x = 1; | ||
| 254 | } | ||
| 255 | else { | ||
| 256 | $canvas_width = $original_width; | ||
| 257 | } | ||
| 258 | |||
| 259 | if (exists $rh_options->{canvas_height}) { | ||
| 260 | $canvas_height = $rh_options->{canvas_height}; | ||
| 261 | $remap_y = 1; | ||
| 262 | } | ||
| 263 | else { | ||
| 264 | $canvas_height = $original_height; | ||
| 265 | } | ||
| 266 | |||
| 267 | my $rc_x = sub { | ||
| 268 | my $x = shift; | ||
| 269 | |||
| 270 | if ($remap_x) { | ||
| 271 | $x = ($x * ($canvas_width - 1)) / $original_width; | ||
| 272 | # round | ||
| 273 | $x = int($x + ($x < 0 ? -0.5 : 0.5)); | ||
| 274 | } | ||
| 275 | |||
| 276 | return $x; | ||
| 277 | }; | ||
| 278 | my $rc_y = sub { | ||
| 279 | my $y = shift; | ||
| 280 | if ($remap_y) { | ||
| 281 | |||
| 282 | $y = ($y * ($canvas_height - 1)) / $original_height; | ||
| 283 | # round | ||
| 284 | $y = int($y + ($y < 0 ? -0.5 : 0.5)); | ||
| 285 | } | ||
| 286 | return $y; | ||
| 287 | }; | ||
| 288 | |||
| 289 | # now draw each pixel in the canvas | ||
| 290 | for my $id (@item_ids) { | ||
| 291 | my ($x, $y) = $self->get_item_coord($id); | ||
| 292 | |||
| 293 | # remap start points | ||
| 294 | $x = $rc_x->($x); | ||
| 295 | $y = $rc_y->($y); | ||
| 296 | |||
| 297 | # remap end points if provided | ||
| 298 | if (my $rh_info = $rh_options->{rh_item_info}) { | ||
| 299 | my $w = $rc_x->($rh_info->{$id}{width}); | ||
| 300 | my $h = $rc_y->($rh_info->{$id}{height}); | ||
| 301 | |||
| 302 | my $end_x = $x + $w - 1; | ||
| 303 | my $end_y = $y + $h - 1; | ||
| 304 | |||
| 305 | # fill box | ||
| 306 | for my $y_pt ($y .. $end_y) { | ||
| 307 | for my $x_pt ($x .. $end_x) { | ||
| 308 | $canvas{$x_pt}{$y_pt} = '+'; | ||
| 309 | } | ||
| 310 | } | ||
| 311 | |||
| 312 | # plot the other 3 corners | ||
| 313 | $canvas{$end_x}{$end_y} = 'o'; | ||
| 314 | } | ||
| 315 | # plot the top left corner | ||
| 316 | $canvas{$x}{$y} = '.'; | ||
| 317 | |||
| 318 | $ids{$x}{$y} //= []; | ||
| 319 | push @{$ids{$x}{$y}}, $id; | ||
| 320 | } | ||
| 321 | |||
| 322 | # now write the canvas into chars | ||
| 323 | my $skip_pixels = 0; | ||
| 324 | my @ascii_chars; | ||
| 325 | for my $y (0 .. $canvas_height - 1) { | ||
| 326 | |||
| 327 | if ($skip_pixels) { | ||
| 328 | $skip_pixels--; | ||
| 329 | } | ||
| 330 | else { | ||
| 331 | push @ascii_chars, "|"; | ||
| 332 | } | ||
| 333 | |||
| 334 | for my $x (0 .. $canvas_width - 1) { | ||
| 335 | |||
| 336 | # write the position first | ||
| 337 | if (exists $canvas{$x} && exists $canvas{$x}{$y}) { | ||
| 338 | |||
| 339 | if ($canvas{$x}{$y} eq '.') { | ||
| 340 | push @ascii_chars, $canvas{$x}{$y}; | ||
| 341 | |||
| 342 | # now append the label and skip the same amount of pixel, | ||
| 343 | my $label = join "+", @{$ids{$x}{$y}}; | ||
| 344 | $skip_pixels += (length $label); | ||
| 345 | |||
| 346 | push @ascii_chars, (split "", $label); | ||
| 347 | } | ||
| 348 | else { | ||
| 349 | if ($skip_pixels) { | ||
| 350 | $skip_pixels--; | ||
| 351 | } | ||
| 352 | else { | ||
| 353 | push @ascii_chars, $canvas{$x}{$y}; | ||
| 354 | } | ||
| 355 | } | ||
| 356 | } | ||
| 357 | else { | ||
| 358 | |||
| 359 | if ($skip_pixels) { | ||
| 360 | $skip_pixels--; | ||
| 361 | } | ||
| 362 | else { | ||
| 363 | push @ascii_chars, ' '; | ||
| 364 | } | ||
| 365 | } | ||
| 366 | } | ||
| 367 | if ($skip_pixels) { | ||
| 368 | $skip_pixels--; | ||
| 369 | } | ||
| 370 | else { | ||
| 371 | push @ascii_chars, "|"; | ||
| 372 | } | ||
| 373 | push @ascii_chars, "\n"; | ||
| 374 | } | ||
| 375 | |||
| 376 | return join('', @ascii_chars); | ||
| 377 | } | ||
| 378 | |||
| 379 | =head2 width | ||
| 380 | |||
| 381 | Returns the width of the overall layout in pixels. | ||
| 382 | |||
| 383 | my $width = $Layout->width(); | ||
| 384 | |||
| 385 | =cut | ||
| 386 | |||
| 387 | sub width { | ||
| 388 | my $self = shift; | ||
| 389 | return $self->{width}; | ||
| 390 | } | ||
| 391 | |||
| 392 | =head2 height | ||
| 393 | |||
| 394 | Returns the height of the overall layout in pixels. | ||
| 395 | |||
| 396 | my $width = $Layout->height(); | ||
| 397 | |||
| 398 | =cut | ||
| 399 | |||
| 400 | sub height { | ||
| 401 | my $self = shift; | ||
| 402 | return $self->{height}; | ||
| 403 | } | ||
| 404 | |||
| 405 | =head2 finalize | ||
| 406 | |||
| 407 | Sets this layout as instantiated. To only be called by a subclass of this base | ||
| 408 | class once class is instantiated. | ||
| 409 | |||
| 410 | $Layout->finalize(); | ||
| 411 | |||
| 412 | Once called, some checks are performed on the layout and | ||
| 413 | warnings are issued emitted if something is wrong. | ||
| 414 | |||
| 415 | Always returns undef. | ||
| 416 | |||
| 417 | =cut | ||
| 418 | |||
| 419 | sub finalize { | ||
| 420 | my $self = shift; | ||
| 421 | |||
| 422 | for my $attribute (qw/width height/) { | ||
| 423 | if (!defined $self->{$attribute}) { | ||
| 424 | warn "attribute $attribute should always be set in the layout class!"; | ||
| 425 | } | ||
| 426 | } | ||
| 427 | |||
| 428 | $self->{_layout_ran} = 1; | ||
| 429 | return; | ||
| 430 | } | ||
| 431 | |||
| 432 | 1; |