| 1 | package Image::Button::Set; | ||
| 2 | |||
| 3 | # $Id: Set.pm,v 1.6 2003/03/04 15:58:15 joanmg Exp $ | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use vars qw($VERSION); | ||
| 7 | |||
| 8 | $VERSION = "0.53"; | ||
| 9 | |||
| 10 | |||
| 11 | sub new | ||
| 12 | { | ||
| 13 | my $pkg = shift; | ||
| 14 | my %args = (buttons => undef, | ||
| 15 | button => undef, | ||
| 16 | textlist => [], | ||
| 17 | @_,); | ||
| 18 | |||
| 19 | my $self = bless { | ||
| 20 | buttons => [], | ||
| 21 | }, $pkg; | ||
| 22 | |||
| 23 | $self->push(%args); | ||
| 24 | return $self; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub push | ||
| 28 | { | ||
| 29 | my $self = shift; | ||
| 30 | my %args = (buttons => undef, # expect [] | ||
| 31 | button => undef, # expect Image::Button::* | ||
| 32 | textlist => [], | ||
| 33 | @_,); | ||
| 34 | push @{ $self->{buttons} }, @{ $args{buttons} } if defined $args{buttons}; | ||
| 35 | push @{ $self->{buttons} }, $args{button} if defined $args{button}; | ||
| 36 | |||
| 37 | my $labels = $args{textlist}; | ||
| 38 | if (@$labels) { | ||
| 39 | my $button = $self->{buttons}[-1]; | ||
| 40 | unless (defined $button) { | ||
| 41 | die "Need a button to clone\n"; | ||
| 42 | } | ||
| 43 | foreach my $label (@$labels) { | ||
| 44 | $self->push(button => $button->copy(text => $label, | ||
| 45 | file => '')); | ||
| 46 | } | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | sub print | ||
| 51 | { | ||
| 52 | my $self = shift; | ||
| 53 | my %args = (sameWidth => 0, | ||
| 54 | sameHeight => 0, | ||
| 55 | prefix => '', | ||
| 56 | postfix => '', | ||
| 57 | override => {}, | ||
| 58 | @_,); | ||
| 59 | |||
| 60 | my $sw = $args{sameWidth}; | ||
| 61 | my $sh = $args{sameHeight}; | ||
| 62 | my $pre = $args{prefix}; | ||
| 63 | my $pos = $args{postfix}; | ||
| 64 | |||
| 65 | my $th = 0; | ||
| 66 | my $tw = 0; | ||
| 67 | if ($sw or $sh) { | ||
| 68 | foreach my $b (@{ $self->{buttons} }) { | ||
| 69 | my ($w, $h) = $b->getSize; | ||
| 70 | if ($sw) { | ||
| 71 | $tw = ($w > $tw) ? $w : $tw; | ||
| 72 | } | ||
| 73 | if ($sh) { | ||
| 74 | $th = ($h > $th) ? $h : $th; | ||
| 75 | } | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | foreach my $b (@{ $self->{buttons} }) { | ||
| 80 | $b->textSize(texth => $th, | ||
| 81 | textw => $tw); | ||
| 82 | $b->override(self => $args{override}); | ||
| 83 | $b->print(prefix => $pre, postfix => $pos); | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | sub flush | ||
| 88 | { | ||
| 89 | my $self = shift; | ||
| 90 | my %args = (print => 1, | ||
| 91 | @_); | ||
| 92 | if ($args{print}) { $self->print(%args) } | ||
| 93 | $self->{buttons} = []; | ||
| 94 | } | ||
| 95 | |||
| 96 | 1; | ||
| 97 | |||
| 98 | =head1 NAME | ||
| 99 | |||
| 100 | Image::Button::Set - Builds a set of related PNG buttons | ||
| 101 | |||
| 102 | =head1 SYNOPSIS | ||
| 103 | |||
| 104 | use Image::Button::Rect; | ||
| 105 | use Image::Button::Plain; | ||
| 106 | use Image::Button::Set; | ||
| 107 | |||
| 108 | my $b1 = new Image::Button::Rect(text => 'text b1', | ||
| 109 | font => 'newsgotn.ttf', | ||
| 110 | fontsize => 20, | ||
| 111 | fgcolor => [0, 0, 0], | ||
| 112 | file => 'b1.png'); | ||
| 113 | |||
| 114 | my $b2 = $b1->copy(text => 'text b2', file => 'b2.png'); | ||
| 115 | |||
| 116 | my $set = new Image::Button::Set(buttons => [ $b1, $b2 ]); | ||
| 117 | |||
| 118 | my $b3 = $b1->copy(text => 'text b3', file => 'b3.png'); | ||
| 119 | |||
| 120 | $set->push(button => $b3); | ||
| 121 | |||
| 122 | my $b4 = $b1->copy(text => 'text b4', file => 'b4.png'); | ||
| 123 | my $b5 = $b1->copy(text => 'text b5', file => 'b5.png'); | ||
| 124 | |||
| 125 | my $b6 = new Image::Button::Plain(text => 'text b6', | ||
| 126 | font => 'newsgotn.ttf', | ||
| 127 | bdcolor => [10, 10, 10]); | ||
| 128 | |||
| 129 | $set->push(buttons => [$b4, $b5, $b6]); | ||
| 130 | |||
| 131 | $set->print(sameWidth => 0, | ||
| 132 | sameHeight => 1, | ||
| 133 | prefix => 'w-'); | ||
| 134 | $set->flush(sameWidth => 1, | ||
| 135 | sameHeight => 0, | ||
| 136 | postfix => '-h', | ||
| 137 | override => { bdcolor => [0, 0, 20], | ||
| 138 | btcolor => [20, 0, 0] }); | ||
| 139 | |||
| 140 | =head1 DESCRIPTION | ||
| 141 | |||
| 142 | Builds a set of related buttons. They might be forced to have the | ||
| 143 | same width and/or height. Make sure you specify an output file in the | ||
| 144 | constructor of the buttons, because otherwise they will all go to | ||
| 145 | stdout and the result won't be what you expect. | ||
| 146 | |||
| 147 | =head1 FUNCTIONS | ||
| 148 | |||
| 149 | =head2 Constructor | ||
| 150 | |||
| 151 | my $set = new Image::Button::Set(buttons => [ $b1, $b2 ]); | ||
| 152 | |||
| 153 | Takes the same arguments as flush, coming next. | ||
| 154 | |||
| 155 | =head2 Add buttons | ||
| 156 | |||
| 157 | Either | ||
| 158 | |||
| 159 | $set->push(button => $b1, | ||
| 160 | textlist => ['text b2', 'text b3', 'text b4']); | ||
| 161 | |||
| 162 | or | ||
| 163 | |||
| 164 | $set->push(buttons => [$b1, $b2], | ||
| 165 | textlist => ['text b3', 'text b4']); | ||
| 166 | |||
| 167 | or | ||
| 168 | $set->push(button => $b1); | ||
| 169 | $set->push(textlist => ['text b2', 'text b3', 'text b4']); | ||
| 170 | |||
| 171 | Possible arguments are: | ||
| 172 | |||
| 173 | =over 4 | ||
| 174 | |||
| 175 | =item button => $button | ||
| 176 | |||
| 177 | A button to add to the list. | ||
| 178 | |||
| 179 | =item buttons => [$button1, $button2] | ||
| 180 | |||
| 181 | A reference to an array of buttons to add to the list. | ||
| 182 | |||
| 183 | =item textlist => ['text b1', 'text b2'] | ||
| 184 | |||
| 185 | A list of button texts with which buttons will be built and appended. | ||
| 186 | They will clone the last button on the list, so there has to be at | ||
| 187 | least one. If called with a I<button> or I<buttons> argument it will | ||
| 188 | go lat, so it will clone the last button you just added. | ||
| 189 | |||
| 190 | =back | ||
| 191 | |||
| 192 | =head2 Print the buttons | ||
| 193 | |||
| 194 | $set->print(sameWidth => 1, | ||
| 195 | sameHeight => 0, | ||
| 196 | prefix => 'pre-', | ||
| 197 | postfix => '-pos', | ||
| 198 | override => { fontsize => 23, | ||
| 199 | fgcolor => [0, 0, 0] }); | ||
| 200 | |||
| 201 | or | ||
| 202 | |||
| 203 | $set->flush(sameWidth => 1, | ||
| 204 | sameHeight => 0, | ||
| 205 | prefix => 'pre-', | ||
| 206 | postfix => '-pos'); | ||
| 207 | |||
| 208 | They both take the same arguments: | ||
| 209 | |||
| 210 | =over 4 | ||
| 211 | |||
| 212 | =item sameWidth => 0 | ||
| 213 | |||
| 214 | If 1 makes all the buttons of the same width, which will equal the | ||
| 215 | width of the widest one. Defaults to 0. | ||
| 216 | |||
| 217 | =item sameHeight => 0 | ||
| 218 | |||
| 219 | If 1 makes all the buttons of the same height, which will equal the | ||
| 220 | height of the tallest one. Defaults to 0. | ||
| 221 | |||
| 222 | =item prefix => '' | ||
| 223 | |||
| 224 | Adds a prefix to the file name of each button. Useful when you want | ||
| 225 | to print a list of buttons several times in different configurations | ||
| 226 | to different files. | ||
| 227 | |||
| 228 | =item postfix => '' | ||
| 229 | |||
| 230 | Adds a postfix before the extension to the file name of each button. | ||
| 231 | |||
| 232 | =item override => { font => 'newfont.ttf', fontsize => 10 } | ||
| 233 | |||
| 234 | A dictionary reference with parameters to override on the buttons to | ||
| 235 | print. Any parameter that the button's constructor understands can be | ||
| 236 | overriden here. The buttons will actually be changed. | ||
| 237 | |||
| 238 | =back | ||
| 239 | |||
| 240 | After I<flush> the button list is cleared. Flush understands an | ||
| 241 | additional parameter, I<print>, which defaults to 1. If set to zero | ||
| 242 | the list will be cleared, but nothing will be printed. | ||
| 243 | |||
| 244 | =head1 SEE ALSO | ||
| 245 | |||
| 246 | F<Image::Button> for a description of how to make buttons, and a list | ||
| 247 | of available button types. | ||
| 248 | |||
| 249 | =head1 AUTHOR | ||
| 250 | |||
| 251 | Juan M. García-Reyero E<lt>joanmg@twostones.orgE<gt> | ||
| 252 | |||
| 253 | =head1 COPYRIGHT | ||
| 254 | |||
| 255 | Copyright (C) 2003 Juan M. García-Reyero. All Rights Reserved. | ||
| 256 | |||
| 257 | This module is free software; you can redistribute it and/or | ||
| 258 | modify it under the same terms as Perl itself. | ||
| 259 |