A little gamepad mapping helper plugin

Just a little plugin I'm working on, this is my first plugin ever (just got MV today, actually). Basically, I saw that MV supported gamepads, but I didn't like the default mapping so I came up with my own plugin to override it.

// GamepadInputEx.js//=============================================================================// Gamepad Input Extended// by KillaMaaki// Date: 11/30/2015 //============================================================================= /*: * @plugindesc Allows for switching between multiple controller mappings in the editor * @author KillaMaaki * * @param GamepadType * @desc Which type of gamepad to map [X360,] * @default X360 */ (function() { var parameters = PluginManager.parameters('GamepadInputEx'); var Input_gamepadbuttons_old = Input.gamepadMapper; var Input_updategamepadstate_old = Input._updateGamepadState; var buttonMap = { "X360": { buttons: { 0: 'ok', // A 1: 'cancel', // B 2: 'shift', // X 9: 'menu', // Start 4: 'pageup', // LB 5: 'pagedown', // RB 12: 'up', // D-pad up 13: 'down', // D-pad down 14: 'left', // D-pad left 15: 'right', // D-pad right }, dirAxes: [1,1,0,0], // axes to check for up, down, left, and right axisMag: [-1.0,1.0,-1.0,1.0], // axis multiplier dirButtons: [12,13,14,15] // which button codes to map axis input to (up, down, left, right) }, }; var controllerType = parameters[ "GamepadType" ]; var dirAxes = [1,1,0,0]; var dirButtons = [12,13,14,15]; var axisMag = [ -1.0, 1.0, -1.0, 1.0 ]; if( buttonMap[ controllerType ] ) { Input.gamepadMapper = buttonMap[ controllerType ].buttons; dirAxes = buttonMap[ controllerType ].dirAxes; dirButtons = buttonMap[ controllerType ].dirButtons; axisMag = buttonMap[ controllerType ].axisMag; } Input._updateGamepadState = function(gamepad) { var lastState = this._gamepadStates[gamepad.index] || []; var newState = []; var buttons = gamepad.buttons; var axes = gamepad.axes; var threshold = 0.25; for (var i = 0; i < buttons.length; i++) { newState = buttons.pressed; } if (axes[dirAxes[0]] * axisMag[0] > threshold) { newState[12] = true; // up } else if (axes[dirAxes[1]] * axisMag[1] > threshold) { newState[13] = true; // down } if (axes[dirAxes[2]] * axisMag[2] > threshold) { newState[14] = true; // left } else if (axes[dirAxes[3]] * axisMag[3] > threshold) { newState[15] = true; // right } for (var j = 0; j < newState.length; j++) { if (newState[j] !== lastState[j]) { var buttonName = this.gamepadMapper[j]; if (buttonName) { this._currentState[buttonName] = newState[j]; } } } this._gamepadStates[gamepad.index] = newState; }.bind( Input ); })();The idea is that you add an entry to the buttonMap object, which contains a button map, a dirAxes array (which gamepad axis to check for up, down, left, and right, respectively), an axisMag array (per each axis entry in dirAxes, use this for each direction to flip the result so that 1.0 maps to the direction - for instance, axis 1 reads as "-1.0" when pushing up on the left stick, so axisMag[0] is set to -1.0 to reverse that), and a dirButtons array which is the button codes that axis directions are mapped to (up, down, left, and right, respectively). It's a little weird, but I did this in case a controller has different axes for directions (usually there's just one axis for direction pairs, but you never know..). It's better than being hardcoded anyway, like it is by default.

Then, in the plugin manager, you enter which button mapping to use, so that you can somewhat easily switch between multiple different mappings without modifying any code (I tried to add support for my PS3 controller, but the Chromium engine MV is running on doesn't seem to read the D-Pad correctly at all). Could be useful for making different builds of the game for, say, Razer TV, Fire TV, etc.

I dunno, maybe someone will find this useful (especially since axis -> direction button mapping isn't hardcoded anymore with this plugin), and it would be cool if people contributed extra mappings for different kinds of controllers 🙂

Also, BTW, the X360 mapping I've defined changes the pause button from Y to Start, because I think that makes more sense.

EDIT: Just realized this plugin is probably useful to just about nobody, given that Yanfly's got the gamepad customization thing already... oh well, at least I learned something.

Original thread: https://forums.rpgmakerweb.com/threads/a-little-gamepad-mapping-helper-plugin.51940/