| 1 | package Ogre::BetaGUI::GUI; | ||
| 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 | # Porting notes: | ||
| 10 | # Wherever there is a Vector2 in the C++ version, expect a 2-element array ref. | ||
| 11 | # I'll have to be in a really good mood to add docs, since BetaGUI doesn't | ||
| 12 | # come with any. | ||
| 13 | |||
| 14 | use strict; | ||
| 15 | use warnings; | ||
| 16 | use List::MoreUtils qw(firstidx); | ||
| 17 | use Scalar::Util qw(refaddr); # note: in core as of Perl 5.8 | ||
| 18 | |||
| 19 | use Ogre 0.35 qw(:GuiMetricsMode); | ||
| 20 | use Ogre::OverlayManager; | ||
| 21 | |||
| 22 | use Ogre::BetaGUI::Window; | ||
| 23 | |||
| 24 | sub new { | ||
| 25 | my ($pkg, $baseOverlay, $font, $fontSize) = @_; | ||
| 26 | |||
| 27 | my $mO = Ogre::OverlayManager->getSingletonPtr->create($baseOverlay); | ||
| 28 | $mO->show(); | ||
| 29 | |||
| 30 | my $self = bless { | ||
| 31 | mO => $mO, | ||
| 32 | WN => [], | ||
| 33 | mXW => undef, | ||
| 34 | mMP => undef, | ||
| 35 | mFont => $font, | ||
| 36 | mFontSize => $fontSize, | ||
| 37 | wc => 0, | ||
| 38 | bc => 0, | ||
| 39 | tc => 0, | ||
| 40 | }, $pkg; | ||
| 41 | |||
| 42 | return $self; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub setZOrder { | ||
| 46 | my ($self, $z) = @_; | ||
| 47 | $self->{mO}->setZOrder($z); | ||
| 48 | } | ||
| 49 | |||
| 50 | sub injectMouse { | ||
| 51 | my ($self, $x, $y, $lmb) = @_; | ||
| 52 | |||
| 53 | if ($self->{mMP}) { | ||
| 54 | $self->{mMP}->setPosition($x, $y); | ||
| 55 | } | ||
| 56 | |||
| 57 | if ($self->{mXW}) { | ||
| 58 | # hopefully refaddr works, otherwise might have to add | ||
| 59 | # a unique "ID" attribute to Ogre::BetaGUI::Window | ||
| 60 | my $idx = firstidx { refaddr($_) == refaddr($self->{mXW}) } @{ $self->{WN} }; | ||
| 61 | if ($idx != -1) { | ||
| 62 | # remove and delete the Ogre::BetaGUI::Window | ||
| 63 | my $win = splice @{ $self->{WN} }, $idx, 1; | ||
| 64 | delete $self->{mXW}; | ||
| 65 | return 0; | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | foreach my $wn (@{ $self->{WN} }) { | ||
| 70 | return 1 if $wn->check($x, $y, $lmb); | ||
| 71 | } | ||
| 72 | return 0; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub injectKey { | ||
| 76 | my ($self, $key, $x, $y) = @_; | ||
| 77 | |||
| 78 | foreach my $wn (@{ $self->{WN} }) { | ||
| 79 | return 1 if $wn->checkKey($key, $x, $y); | ||
| 80 | } | ||
| 81 | return 0; | ||
| 82 | } | ||
| 83 | |||
| 84 | sub injectBackspace { | ||
| 85 | my ($self, $x, $y) = @_; | ||
| 86 | $self->injectKey("!b", $x, $y); | ||
| 87 | } | ||
| 88 | |||
| 89 | sub createWindow { | ||
| 90 | my ($self, $Dimensions, $Material, $type, $caption) = @_; | ||
| 91 | $caption = '' unless defined $caption; | ||
| 92 | |||
| 93 | my $window = Ogre::BetaGUI::Window->new($Dimensions, $Material, $type, $caption, $self); | ||
| 94 | push @{ $self->{WN} }, $window; | ||
| 95 | return $window; | ||
| 96 | } | ||
| 97 | |||
| 98 | sub destroyWindow { | ||
| 99 | my ($self, $window) = @_; | ||
| 100 | $self->{mXW} = $window; | ||
| 101 | # that gets actually removed in injectMouse | ||
| 102 | } | ||
| 103 | |||
| 104 | sub createOverlay { | ||
| 105 | my ($self, $name, $position, $dimensions, $material, $caption, $autoAdd) = @_; | ||
| 106 | $material = '' unless defined $material; | ||
| 107 | $caption = '' unless defined $caption; | ||
| 108 | $autoAdd = 1 unless defined $autoAdd; | ||
| 109 | |||
| 110 | my $om = Ogre::OverlayManager->getSingletonPtr; | ||
| 111 | my $e = ($caption eq '') | ||
| 112 | ? $om->createPanelOverlayElement("Panel", $name) | ||
| 113 | : $om->createTextAreaOverlayElement("TextArea", $name); | ||
| 114 | |||
| 115 | $e->setMetricsMode(GMM_PIXELS); | ||
| 116 | $e->setDimensions($dimensions->[0], $dimensions->[1]); | ||
| 117 | $e->setPosition($position->[0], $position->[1]); | ||
| 118 | $e->setMaterialName($material) unless $material eq ''; | ||
| 119 | unless ($caption eq '') { | ||
| 120 | $e->setCaption($caption); | ||
| 121 | $e->setParameter("font_name", $self->{mFont}); | ||
| 122 | $e->setParameter("char_height", $self->{mFontSize}); | ||
| 123 | } | ||
| 124 | |||
| 125 | if ($autoAdd) { | ||
| 126 | $self->{mO}->add2D($e); | ||
| 127 | $e->show(); | ||
| 128 | } | ||
| 129 | |||
| 130 | return $e; | ||
| 131 | } | ||
| 132 | |||
| 133 | sub createMousePointer { | ||
| 134 | my ($self, $d, $m) = @_; | ||
| 135 | |||
| 136 | my $o = Ogre::OverlayManager->getSingletonPtr->create("BetaGUI.MP"); | ||
| 137 | $o->setZOrder(649); | ||
| 138 | $self->{mMP} = $self->createOverlay("bg.mp", [0, 0], $d, $m, '', 0); | ||
| 139 | $o->add2D($self->{mMP}); | ||
| 140 | $o->show(); | ||
| 141 | $self->{mMP}->show(); | ||
| 142 | |||
| 143 | return $self->{mMP}; | ||
| 144 | } | ||
| 145 | |||
| 146 | |||
| 147 | 1; |