| 1 | ########################################################################## | ||
| 2 | package Games::PangZero::Joystick; | ||
| 3 | ########################################################################## | ||
| 4 | |||
| 5 | use vars qw(@Games::PangZero::Joysticks @Games::PangZero::JoystickButtons); | ||
| 6 | |||
| 7 | sub InitJoystick { | ||
| 8 | my ($numJoysticks, $joystick, $numButtons, $i); | ||
| 9 | |||
| 10 | $numJoysticks = SDL::Joystick::num_joysticks(); | ||
| 11 | for ($i = 0; $i < $numJoysticks; $i++) { | ||
| 12 | print STDERR "Found joystick " , $i+1 , ": " , SDL::Joystick::name($i), "\n"; | ||
| 13 | $joystick = SDL::Joystick->new($i); | ||
| 14 | next unless $joystick; | ||
| 15 | $numButtons = SDL::Joystick::num_buttons($joystick); | ||
| 16 | next unless $numButtons; | ||
| 17 | push @Games::PangZero::Joysticks, $joystick; | ||
| 18 | push @Games::PangZero::JoystickButtons, $numButtons; | ||
| 19 | print STDERR "Joystick opened, $numButtons buttons.\n"; | ||
| 20 | } | ||
| 21 | } | ||
| 22 | |||
| 23 | sub ReadJoystick { | ||
| 24 | my ($readBothAxes) = @_; | ||
| 25 | my ($i, $button, $buttonPressed); | ||
| 26 | |||
| 27 | $i = 0; | ||
| 28 | foreach my $joystick (@Games::PangZero::Joysticks) { | ||
| 29 | my $axis = SDL::Joystick::get_axis($joystick, 0); | ||
| 30 | if ($axis <= -10000) { | ||
| 31 | $Games::PangZero::Events{"L$i"} = $Games::PangZero::MenuEvents{LEFT} = 1 unless $Games::PangZero::Keys{"L$i"}; | ||
| 32 | $Games::PangZero::Keys{"L$i"} = 1; | ||
| 33 | $Games::PangZero::Keys{"R$i"} = 0; | ||
| 34 | } elsif ($axis >= 10000) { | ||
| 35 | $Games::PangZero::Events{"R$i"} = $Games::PangZero::MenuEvents{RIGHT} = 1 unless $Games::PangZero::Keys{"R$i"}; | ||
| 36 | $Games::PangZero::Keys{"R$i"} = 1; | ||
| 37 | $Games::PangZero::Keys{"L$i"} = 0; | ||
| 38 | } else { | ||
| 39 | $Games::PangZero::Keys{"L$i"} = 0; | ||
| 40 | $Games::PangZero::Keys{"R$i"} = 0; | ||
| 41 | } | ||
| 42 | |||
| 43 | if ($readBothAxes) { | ||
| 44 | $axis = SDL::Joystick::get_axis($joystick, 1); | ||
| 45 | if ($axis <= -10000) { | ||
| 46 | $Games::PangZero::Events{"U$i"} = $Games::PangZero::MenuEvents{UP} = 1 unless $Games::PangZero::Keys{"U$i"}; | ||
| 47 | $Games::PangZero::Keys{"U$i"} = 1; | ||
| 48 | $Games::PangZero::Keys{"D$i"} = 0; | ||
| 49 | } elsif ($axis >= 10000) { | ||
| 50 | $Games::PangZero::Events{"D$i"} = $Games::PangZero::MenuEvents{DOWN} = 1 unless $Games::PangZero::Keys{"D$i"}; | ||
| 51 | $Games::PangZero::Keys{"D$i"} = 1; | ||
| 52 | $Games::PangZero::Keys{"U$i"} = 0; | ||
| 53 | } else { | ||
| 54 | $Games::PangZero::Keys{"D$i"} = 0; | ||
| 55 | $Games::PangZero::Keys{"U$i"} = 0; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | $buttonPressed = 0; | ||
| 60 | for ($button = 0; $button < $Games::PangZero::JoystickButtons[$i]; $button++) { | ||
| 61 | if (SDL::Joystick::get_button($joystick, $button)) { | ||
| 62 | $buttonPressed = 1; | ||
| 63 | last; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | if ($buttonPressed and not $Games::PangZero::Keys{"B$i"}) { | ||
| 67 | $Games::PangZero::Events{"B$i"} = $Games::PangZero::MenuEvents{BUTTON} = 1; | ||
| 68 | } | ||
| 69 | $Games::PangZero::Keys{"B$i"} = $buttonPressed; | ||
| 70 | $i++; | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | 1; |