| 1 | package Ogre::BetaGUI::Window; | ||
| 2 | |||
| 3 | ### Note: this was ported to Perl from: | ||
| 4 | # /// Betajaen's GUI 016 Uncompressed | ||
| 5 | # /// Written by Robin "Betajaen" Southern 07-Nov-2006, http://www.ogre3d.org/wiki/index.php/BetaGUI | ||
| 6 | # /// This code is under the Whatevar! licence. Do what you want; but keep the original copyright header. | ||
| 7 | ### | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use warnings; | ||
| 11 | use Scalar::Util qw(refaddr); # note: in core as of Perl 5.8 | ||
| 12 | |||
| 13 | use Ogre 0.33; | ||
| 14 | |||
| 15 | use Ogre::BetaGUI qw(:wt); | ||
| 16 | use Ogre::BetaGUI::Button; | ||
| 17 | use Ogre::BetaGUI::Callback; | ||
| 18 | use Ogre::BetaGUI::TextInput; | ||
| 19 | |||
| 20 | |||
| 21 | # note: $Dimensions is an array ref with 4 values (Vector4) | ||
| 22 | sub new { | ||
| 23 | my ($pkg, $Dimensions, $Material, $t, $caption, $gui) = @_; | ||
| 24 | |||
| 25 | # Perl doesn't have enums (:wt exported above), so this sucks | ||
| 26 | unless ($t == NONE or $t == MOVE or $t == RESIZE or $t == RESIZE_AND_MOVE) { | ||
| 27 | die __PACKAGE__ . ": invalid type number '$t' (see :wt in Ogre::BetaGUI)"; | ||
| 28 | } | ||
| 29 | |||
| 30 | my $self = bless { | ||
| 31 | 'x' => $Dimensions->[0], # x | ||
| 32 | 'y' => $Dimensions->[1], # y | ||
| 33 | 'w' => $Dimensions->[2], # z | ||
| 34 | 'h' => $Dimensions->[3], # w | ||
| 35 | mGUI => $gui, # GUI * | ||
| 36 | mTB => undef, # Button * | ||
| 37 | mRZ => undef, # Button * | ||
| 38 | mATI => undef, # TextInput * | ||
| 39 | mAB => undef, # Button * | ||
| 40 | mO => undef, # OverlayContainer * | ||
| 41 | mB => [], # Buttons | ||
| 42 | mT => [], # TextInputs | ||
| 43 | }, $pkg; | ||
| 44 | |||
| 45 | $self->{mO} = $gui->createOverlay($gui->{mO}->getName . ".w" . $gui->{wc}, | ||
| 46 | [$self->{'x'}, $self->{'y'}], | ||
| 47 | [$self->{'w'}, $self->{'h'}], $Material); | ||
| 48 | $gui->{wc}++; | ||
| 49 | |||
| 50 | if ($t >= 2) { # RESIZE | ||
| 51 | my $c = Ogre::BetaGUI::Callback->new(); | ||
| 52 | $c->{t} = 4; | ||
| 53 | |||
| 54 | $self->{mRZ} = $self->createButton([($Dimensions->[2] - 16), ($Dimensions->[3] - 16), 16, 16], | ||
| 55 | $Material . ".resize", "", $c); | ||
| 56 | } | ||
| 57 | |||
| 58 | if ($t == 1 || $t == 3) { # MOVE or RESIZE_AND_MOVE | ||
| 59 | my $c = Ogre::BetaGUI::Callback->new(); | ||
| 60 | $c->{t} = 3; | ||
| 61 | |||
| 62 | $self->{mTB} = $self->createButton([0, 0, $Dimensions->[2], 22], | ||
| 63 | $Material . ".titlebar", $caption, $c); | ||
| 64 | } | ||
| 65 | |||
| 66 | return $self; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub DESTROY { | ||
| 70 | my ($self) = @_; | ||
| 71 | $self->{mGUI}{mO}->remove2D($self->{mO}) | ||
| 72 | if defined($self->{mGUI}) && defined($self->{mGUI}{mO}) && defined($self->{mO}); | ||
| 73 | } | ||
| 74 | |||
| 75 | sub createButton { | ||
| 76 | my ($self, $Dimensions, $Material, $Text, $callback) = @_; | ||
| 77 | |||
| 78 | my $x = Ogre::BetaGUI::Button->new($Dimensions, $Material, $Text, $callback, $self); | ||
| 79 | push @{ $self->{mB} }, $x; | ||
| 80 | return $x; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub createTextInput { | ||
| 84 | my ($self, $Dimensions, $Material, $Value, $length) = @_; | ||
| 85 | my $x = Ogre::BetaGUI::TextInput->new($Dimensions, $Material, $Value, $length, $self); | ||
| 86 | push @{ $self->{mT} }, $x; | ||
| 87 | return $x; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub createStaticText { | ||
| 91 | my ($self, $Dimensions, $Text) = @_; | ||
| 92 | |||
| 93 | my $x = $self->{mGUI}->createOverlay($self->{mO}->getName . "st." . $self->{mGUI}{tc}, | ||
| 94 | [$Dimensions->[0], $Dimensions->[1]], | ||
| 95 | [$Dimensions->[2], $Dimensions->[3]], | ||
| 96 | '', $Text, 0); | ||
| 97 | $self->{mGUI}{tc}++; | ||
| 98 | |||
| 99 | $self->{mO}->addChild($x); | ||
| 100 | $x->show(); | ||
| 101 | return $x; | ||
| 102 | } | ||
| 103 | |||
| 104 | sub hide { $_[0]->{mO}->hide() } | ||
| 105 | sub show { $_[0]->{mO}->show() } | ||
| 106 | sub isVisible { $_[0]->{mO}->isVisible() } | ||
| 107 | |||
| 108 | sub setPosition { | ||
| 109 | my ($self, $X, $Y) = @_; | ||
| 110 | $self->{'x'} = $X; | ||
| 111 | $self->{'y'} = $Y; | ||
| 112 | $self->{mO}->setPosition($X, $Y); | ||
| 113 | } | ||
| 114 | |||
| 115 | # these return array ref, not Vector2 | ||
| 116 | sub getPosition { [$_[0]->{'x'}, $_[0]->{'y'}] } | ||
| 117 | sub getSize { [$_[0]->{'w'}, $_[0]->{'h'}] } | ||
| 118 | |||
| 119 | sub setSize { | ||
| 120 | my ($self, $W, $H) = @_; | ||
| 121 | |||
| 122 | $self->{'w'} = $W; | ||
| 123 | $self->{'h'} = $H; | ||
| 124 | $self->{mO}->setDimensions($W, $H); | ||
| 125 | |||
| 126 | $self->{mRZ}{'x'} = $self->{'w'} - 16; | ||
| 127 | $self->{mRZ}{'y'} = $self->{'h'} - 16; | ||
| 128 | $self->{mRZ}{mO}->setPosition($self->{mRZ}{'x'}, $self->{mRZ}{'y'}); | ||
| 129 | |||
| 130 | if ($self->{mTB}) { | ||
| 131 | $self->{mTB}{'w'} = $self->{'w'}; | ||
| 132 | $self->{mTB}{mO}->setWidth($self->{mTB}{'w'}); | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | sub check { | ||
| 137 | my ($self, $px, $py, $lmb) = @_; | ||
| 138 | |||
| 139 | return 0 unless $self->{mO}->isVisible; | ||
| 140 | |||
| 141 | if (!($px >= $self->{'x'} && $py >= $self->{'y'}) || | ||
| 142 | !($px <= $self->{'x'} + $self->{'w'} && $py <= $self->{'y'} + $self->{'h'})) | ||
| 143 | { | ||
| 144 | if ($self->{mAB}) { | ||
| 145 | my $cb = $self->{mAB}{callback}; | ||
| 146 | if ($cb->{t} == 2) { | ||
| 147 | if (exists($cb->{LS}) && defined($cb->{LS})) { | ||
| 148 | $cb->{LS}->onButtonPress($self->{mAB}, 3); | ||
| 149 | } | ||
| 150 | else { | ||
| 151 | warn "onButtonPress not called: LS undef at ", __FILE__, ":", __LINE__, $/; | ||
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 | $self->{mAB}->activate(0); | ||
| 156 | undef $self->{mAB}; | ||
| 157 | } | ||
| 158 | return 0; | ||
| 159 | } | ||
| 160 | |||
| 161 | foreach my $mb (@{ $self->{mB} }) { | ||
| 162 | next if $mb->in($px, $py, $self->{'x'}, $self->{'y'}); | ||
| 163 | |||
| 164 | if ($self->{mAB}) { | ||
| 165 | if (refaddr($mb) != refaddr($self->{mAB})) { | ||
| 166 | $self->{mAB}->activate(0); | ||
| 167 | |||
| 168 | # xxx: there's something wrong here.. when you drag a window | ||
| 169 | # around quickly, and the mouse cursor detaches from the window | ||
| 170 | # (which is a separate bug....) it can go across another button/window | ||
| 171 | # and apparently get in here with $cb->{LS} undef. | ||
| 172 | my $cb = $self->{mAB}{callback}; | ||
| 173 | if (exists($cb->{LS}) && defined($cb->{LS})) { | ||
| 174 | $cb->{LS}->onButtonPress($self->{mAB}, 3); | ||
| 175 | } | ||
| 176 | else { | ||
| 177 | warn "onButtonPress not called: LS undef at ", __FILE__, ":", __LINE__, $/; | ||
| 178 | } | ||
| 179 | } | ||
| 180 | } | ||
| 181 | |||
| 182 | $self->{mAB} = $mb; | ||
| 183 | $self->{mAB}->activate(1); | ||
| 184 | |||
| 185 | if ($self->{mATI} && $lmb) { | ||
| 186 | $self->{mATI}{mO}->setMaterialName($self->{mATI}{mmn}); | ||
| 187 | undef $self->{mATI}; | ||
| 188 | } | ||
| 189 | |||
| 190 | my $t = $self->{mAB}{callback}{t}; | ||
| 191 | if ($t == 1) { | ||
| 192 | $self->{mAB}{callback}{fp}->($self->{mAB}, $lmb); | ||
| 193 | } | ||
| 194 | elsif ($t == 2) { | ||
| 195 | my $cb = $self->{mAB}{callback}; | ||
| 196 | if (exists($cb->{LS}) && defined($cb->{LS})) { | ||
| 197 | $cb->{LS}->onButtonPress($self->{mAB}, $lmb); | ||
| 198 | } | ||
| 199 | else { | ||
| 200 | warn "onButtonPress not called: LS undef at ", __FILE__, ":", __LINE__, $/; | ||
| 201 | } | ||
| 202 | } | ||
| 203 | elsif ($t == 3) { | ||
| 204 | if ($lmb) { | ||
| 205 | $self->setPosition($px - ($self->{mAB}{'w'} / 2), $py - ($self->{mAB}{'h'} / 2)); | ||
| 206 | } | ||
| 207 | } | ||
| 208 | elsif ($t == 4) { | ||
| 209 | if ($lmb) { | ||
| 210 | $self->setSize($px - $self->{'x'} + 8, $py - $self->{'y'} + 8); | ||
| 211 | } | ||
| 212 | } | ||
| 213 | |||
| 214 | return 1; | ||
| 215 | } | ||
| 216 | |||
| 217 | return 0 unless $lmb; | ||
| 218 | |||
| 219 | foreach my $mt (@{ $self->{mT} }) { | ||
| 220 | next if $mt->in($px, $py, $self->{'x'}, $self->{'y'}); | ||
| 221 | |||
| 222 | $self->{mATI} = $mt; | ||
| 223 | $self->{mATI}{mO}->setMaterialName($self->{mATI}{mma}); | ||
| 224 | return 1; | ||
| 225 | } | ||
| 226 | |||
| 227 | if ($self->{mATI}) { | ||
| 228 | $self->{mATI}{mO}->setMaterialName($self->{mATI}{mmn}); | ||
| 229 | undef $self->{mATI}; | ||
| 230 | return 1; | ||
| 231 | } | ||
| 232 | |||
| 233 | return 0; | ||
| 234 | } | ||
| 235 | |||
| 236 | sub checkKey { | ||
| 237 | my ($self, $key, $px, $py) = @_; | ||
| 238 | |||
| 239 | return 0 unless $self->{mO}->isVisible; | ||
| 240 | |||
| 241 | return 0 if !($px >= $self->{'x'} && $py >= $self->{'y'}) || | ||
| 242 | !($px <= $self->{'x'} + $self->{'w'} && $py <= $self->{'y'} + $self->{'h'}); | ||
| 243 | |||
| 244 | return 0 unless defined $self->{mATI}; | ||
| 245 | |||
| 246 | if ($key eq '!b') { | ||
| 247 | chop $self->{mATI}{value}; | ||
| 248 | $self->{mATI}->setValue($self->{mATI}{value}); | ||
| 249 | } | ||
| 250 | |||
| 251 | return 1 if length($self->{mATI}{value}) >= $self->{mATI}{length}; | ||
| 252 | |||
| 253 | $self->{mATI}{value} .= $key; | ||
| 254 | $self->{mATI}{mCP}->setCaption($self->{mATI}{value}); | ||
| 255 | return 1; | ||
| 256 | } | ||
| 257 | |||
| 258 | |||
| 259 | 1; |