gamepad support and keybind cleaning up
This commit is contained in:
@ -3,6 +3,7 @@ package;
|
||||
/// Code created by Rozebud for FPS Plus (thanks rozebud)
|
||||
// modified by KadeDev for use in Kade Engine/Tricky
|
||||
|
||||
import flixel.input.gamepad.FlxGamepad;
|
||||
import flixel.util.FlxAxes;
|
||||
import flixel.FlxSubState;
|
||||
import Options.Option;
|
||||
@ -36,15 +37,19 @@ class KeyBindMenu extends FlxSubState
|
||||
var warningTween:FlxTween;
|
||||
var keyText:Array<String> = ["LEFT", "DOWN", "UP", "RIGHT"];
|
||||
var defaultKeys:Array<String> = ["A", "S", "W", "D", "R"];
|
||||
var defaultGpKeys:Array<String> = ["DPAD_LEFT", "DPAD_DOWN", "DPAD_UP", "DPAD_RIGHT"];
|
||||
var curSelected:Int = 0;
|
||||
|
||||
var keys:Array<String> = [FlxG.save.data.leftBind,
|
||||
FlxG.save.data.downBind,
|
||||
FlxG.save.data.upBind,
|
||||
FlxG.save.data.rightBind];
|
||||
|
||||
var gpKeys:Array<String> = [FlxG.save.data.gpleftBind,
|
||||
FlxG.save.data.gpdownBind,
|
||||
FlxG.save.data.gpupBind,
|
||||
FlxG.save.data.gprightBind];
|
||||
var tempKey:String = "";
|
||||
var blacklist:Array<String> = ["ESCAPE", "ENTER", "BACKSPACE", "SPACE"];
|
||||
var blacklist:Array<String> = ["ESCAPE", "ENTER", "BACKSPACE", "SPACE", "TAB"];
|
||||
|
||||
var blackBox:FlxSprite;
|
||||
var infoText:FlxText;
|
||||
@ -60,10 +65,17 @@ class KeyBindMenu extends FlxSubState
|
||||
if (k == null)
|
||||
keys[i] = defaultKeys[i];
|
||||
}
|
||||
|
||||
for (i in 0...gpKeys.length)
|
||||
{
|
||||
var k = gpKeys[i];
|
||||
if (k == null)
|
||||
gpKeys[i] = defaultGpKeys[i];
|
||||
}
|
||||
|
||||
//FlxG.sound.playMusic('assets/music/configurator' + TitleState.soundExt);
|
||||
|
||||
persistentUpdate = persistentDraw = true;
|
||||
persistentUpdate = true;
|
||||
|
||||
keyTextDisplay = new FlxText(-10, 0, 1280, "", 72);
|
||||
keyTextDisplay.scrollFactor.set(0, 0);
|
||||
@ -74,7 +86,7 @@ class KeyBindMenu extends FlxSubState
|
||||
blackBox = new FlxSprite(0,0).makeGraphic(FlxG.width,FlxG.height,FlxColor.BLACK);
|
||||
add(blackBox);
|
||||
|
||||
infoText = new FlxText(-10, 580, 1280, "(Escape to save, backspace to leave without saving)", 72);
|
||||
infoText = new FlxText(-10, 580, 1280, 'Current Mode: ${KeyBinds.gamepad ? 'GAMEPAD' : 'KEYBOARD'}. Press TAB to switch\n(${KeyBinds.gamepad ? 'RIGHT Trigger' : 'Escape'} to save, ${KeyBinds.gamepad ? 'LEFT Trigger' : 'Backspace'} to leave without saving. ${KeyBinds.gamepad ? 'START To change a keybind' : ''})', 72);
|
||||
infoText.scrollFactor.set(0, 0);
|
||||
infoText.setFormat("VCR OSD Mono", 24, FlxColor.WHITE, FlxTextAlign.CENTER, FlxTextBorderStyle.OUTLINE, FlxColor.BLACK);
|
||||
infoText.borderSize = 2;
|
||||
@ -98,23 +110,36 @@ class KeyBindMenu extends FlxSubState
|
||||
super.create();
|
||||
}
|
||||
|
||||
var frames = 0;
|
||||
|
||||
override function update(elapsed:Float)
|
||||
{
|
||||
var gamepad:FlxGamepad = FlxG.gamepads.lastActive;
|
||||
|
||||
if (frames <= 10)
|
||||
frames++;
|
||||
|
||||
switch(state){
|
||||
|
||||
case "select":
|
||||
if (FlxG.keys.justPressed.UP)
|
||||
{
|
||||
FlxG.sound.play(Paths.sound('scrollMenu'));
|
||||
changeItem(-1);
|
||||
}
|
||||
{
|
||||
FlxG.sound.play(Paths.sound('scrollMenu'));
|
||||
changeItem(-1);
|
||||
}
|
||||
|
||||
if (FlxG.keys.justPressed.DOWN)
|
||||
{
|
||||
FlxG.sound.play(Paths.sound('scrollMenu'));
|
||||
changeItem(1);
|
||||
}
|
||||
if (FlxG.keys.justPressed.DOWN)
|
||||
{
|
||||
FlxG.sound.play(Paths.sound('scrollMenu'));
|
||||
changeItem(1);
|
||||
}
|
||||
|
||||
if (FlxG.keys.justPressed.TAB)
|
||||
{
|
||||
KeyBinds.gamepad = !KeyBinds.gamepad;
|
||||
infoText.text = 'Current Mode: ${KeyBinds.gamepad ? 'GAMEPAD' : 'KEYBOARD'}. Press TAB to switch\n(${KeyBinds.gamepad ? 'RIGHT Trigger' : 'Escape'} to save, ${KeyBinds.gamepad ? 'LEFT Trigger' : 'Backspace'} to leave without saving. ${KeyBinds.gamepad ? 'START To change a keybind' : ''})';
|
||||
textUpdate();
|
||||
}
|
||||
|
||||
if (FlxG.keys.justPressed.ENTER){
|
||||
FlxG.sound.play(Paths.sound('scrollMenu'));
|
||||
@ -123,31 +148,87 @@ class KeyBindMenu extends FlxSubState
|
||||
else if(FlxG.keys.justPressed.ESCAPE){
|
||||
quit();
|
||||
}
|
||||
else if (FlxG.keys.justPressed.BACKSPACE){
|
||||
else if (FlxG.keys.justPressed.BACKSPACE){
|
||||
reset();
|
||||
}
|
||||
if (gamepad != null) // GP Logic
|
||||
{
|
||||
if (gamepad.justPressed.DPAD_UP)
|
||||
{
|
||||
FlxG.sound.play(Paths.sound('scrollMenu'));
|
||||
changeItem(-1);
|
||||
textUpdate();
|
||||
}
|
||||
if (gamepad.justPressed.DPAD_DOWN)
|
||||
{
|
||||
FlxG.sound.play(Paths.sound('scrollMenu'));
|
||||
changeItem(1);
|
||||
textUpdate();
|
||||
}
|
||||
|
||||
if (gamepad.justPressed.START && frames > 10){
|
||||
FlxG.sound.play(Paths.sound('scrollMenu'));
|
||||
state = "input";
|
||||
}
|
||||
else if(gamepad.justPressed.LEFT_TRIGGER){
|
||||
quit();
|
||||
}
|
||||
else if (gamepad.justPressed.RIGHT_TRIGGER){
|
||||
reset();
|
||||
}
|
||||
}
|
||||
|
||||
case "input":
|
||||
tempKey = keys[curSelected];
|
||||
keys[curSelected] = "?";
|
||||
if (KeyBinds.gamepad)
|
||||
gpKeys[curSelected] = "?";
|
||||
textUpdate();
|
||||
state = "waiting";
|
||||
|
||||
case "waiting":
|
||||
if(FlxG.keys.justPressed.ESCAPE){
|
||||
keys[curSelected] = tempKey;
|
||||
state = "select";
|
||||
FlxG.sound.play(Paths.sound('confirmMenu'));
|
||||
if (gamepad != null && KeyBinds.gamepad) // GP Logic
|
||||
{
|
||||
if(FlxG.keys.justPressed.ESCAPE){ // just in case you get stuck
|
||||
gpKeys[curSelected] = tempKey;
|
||||
state = "select";
|
||||
FlxG.sound.play(Paths.sound('confirmMenu'));
|
||||
}
|
||||
|
||||
if (gamepad.justPressed.START)
|
||||
{
|
||||
addKeyGamepad(defaultKeys[curSelected]);
|
||||
save();
|
||||
state = "select";
|
||||
}
|
||||
|
||||
if (gamepad.justPressed.ANY)
|
||||
{
|
||||
trace(gamepad.firstJustPressedID());
|
||||
addKeyGamepad(gamepad.firstJustPressedID());
|
||||
save();
|
||||
state = "select";
|
||||
textUpdate();
|
||||
}
|
||||
|
||||
}
|
||||
else if(FlxG.keys.justPressed.ENTER){
|
||||
addKey(defaultKeys[curSelected]);
|
||||
save();
|
||||
state = "select";
|
||||
}
|
||||
else if(FlxG.keys.justPressed.ANY){
|
||||
addKey(FlxG.keys.getIsDown()[0].ID.toString());
|
||||
save();
|
||||
state = "select";
|
||||
else
|
||||
{
|
||||
if(FlxG.keys.justPressed.ESCAPE){
|
||||
keys[curSelected] = tempKey;
|
||||
state = "select";
|
||||
FlxG.sound.play(Paths.sound('confirmMenu'));
|
||||
}
|
||||
else if(FlxG.keys.justPressed.ENTER){
|
||||
addKey(defaultKeys[curSelected]);
|
||||
save();
|
||||
state = "select";
|
||||
}
|
||||
else if(FlxG.keys.justPressed.ANY){
|
||||
addKey(FlxG.keys.getIsDown()[0].ID.toString());
|
||||
save();
|
||||
state = "select";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -170,11 +251,24 @@ class KeyBindMenu extends FlxSubState
|
||||
|
||||
keyTextDisplay.text = "\n\n";
|
||||
|
||||
for(i in 0...4){
|
||||
if (KeyBinds.gamepad)
|
||||
{
|
||||
for(i in 0...4){
|
||||
|
||||
var textStart = (i == curSelected) ? "> " : " ";
|
||||
keyTextDisplay.text += textStart + keyText[i] + ": " + ((keys[i] != keyText[i]) ? (keys[i] + " / ") : "" ) + keyText[i] + " ARROW\n";
|
||||
var textStart = (i == curSelected) ? "> " : " ";
|
||||
trace(gpKeys[i]);
|
||||
keyTextDisplay.text += textStart + keyText[i] + ": " + gpKeys[i] + "\n";
|
||||
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for(i in 0...4){
|
||||
|
||||
var textStart = (i == curSelected) ? "> " : " ";
|
||||
keyTextDisplay.text += textStart + keyText[i] + ": " + ((keys[i] != keyText[i]) ? (keys[i] + " / ") : "" ) + keyText[i] + " ARROW\n";
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
keyTextDisplay.screenCenter();
|
||||
@ -187,6 +281,11 @@ class KeyBindMenu extends FlxSubState
|
||||
FlxG.save.data.downBind = keys[1];
|
||||
FlxG.save.data.leftBind = keys[0];
|
||||
FlxG.save.data.rightBind = keys[3];
|
||||
|
||||
FlxG.save.data.gpupBind = gpKeys[2];
|
||||
FlxG.save.data.gpdownBind = gpKeys[1];
|
||||
FlxG.save.data.gpleftBind = gpKeys[0];
|
||||
FlxG.save.data.gprightBind = gpKeys[3];
|
||||
|
||||
FlxG.save.flush();
|
||||
|
||||
@ -217,6 +316,38 @@ class KeyBindMenu extends FlxSubState
|
||||
}
|
||||
|
||||
|
||||
function addKeyGamepad(r:String){
|
||||
|
||||
var shouldReturn:Bool = true;
|
||||
|
||||
var notAllowed:Array<String> = ["START", "RIGHT_TRIGGER", "LEFT_TRIGGER"];
|
||||
|
||||
for(x in 0...gpKeys.length)
|
||||
{
|
||||
var oK = gpKeys[x];
|
||||
if(oK == r)
|
||||
gpKeys[x] = null;
|
||||
if (notAllowed.contains(oK))
|
||||
{
|
||||
gpKeys[x] = null;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if(shouldReturn){
|
||||
gpKeys[curSelected] = r;
|
||||
FlxG.sound.play(Paths.sound('scrollMenu'));
|
||||
}
|
||||
else{
|
||||
gpKeys[curSelected] = tempKey;
|
||||
FlxG.sound.play(Paths.sound('scrollMenu'));
|
||||
keyWarning.alpha = 1;
|
||||
warningTween.cancel();
|
||||
warningTween = FlxTween.tween(keyWarning, {alpha: 0}, 0.5, {ease: FlxEase.circOut, startDelay: 2});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function addKey(r:String){
|
||||
|
||||
var shouldReturn:Bool = true;
|
||||
@ -233,7 +364,10 @@ class KeyBindMenu extends FlxSubState
|
||||
if(oK == r)
|
||||
keys[x] = null;
|
||||
if (notAllowed.contains(oK))
|
||||
{
|
||||
keys[x] = null;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (r.contains("NUMPAD"))
|
||||
|
Reference in New Issue
Block a user