start new Controls menu

This commit is contained in:
George FunBook 2021-02-23 18:59:36 -06:00
parent d3e17274a8
commit 5b6862d6cf
4 changed files with 321 additions and 9 deletions

223
source/InputFormatter.hx Normal file
View file

@ -0,0 +1,223 @@
package ;
import Controls;
import flixel.FlxG;
import flixel.input.gamepad.FlxGamepad;
import flixel.input.gamepad.FlxGamepadInputID;
import flixel.input.keyboard.FlxKey;
using flixel.util.FlxStringUtil;
class InputFormatter
{
static public function format(id:Int, device:Device):String
{
return switch (device)
{
case Keys: getKeyName(id);
case Gamepad(gamepadID): getButtonName(id, FlxG.gamepads.getByID(gamepadID));
}
}
static public function getKeyName(id:Int):String
{
return switch(id)
{
case ZERO : "0";
case ONE : "1";
case TWO : "2";
case THREE : "3";
case FOUR : "4";
case FIVE : "5";
case SIX : "6";
case SEVEN : "7";
case EIGHT : "8";
case NINE : "9";
case PAGEUP : "PgU";
case PAGEDOWN : "PgD";
case HOME : "Hm";
case END : "End";
case INSERT : "Ins";
case ESCAPE : "Esc";
case MINUS : "-";
case PLUS : "+";
case DELETE : "Del";
case BACKSPACE : "Bck";
case LBRACKET : "[";
case RBRACKET : "]";
case BACKSLASH : "\\";
case CAPSLOCK : "Cap";
case SEMICOLON : ";";
case QUOTE : "'";
case ENTER : "Ent";
case SHIFT : "Shf";
case COMMA : ",";
case PERIOD : ".";
case SLASH : "/";
case GRAVEACCENT : "`";
case CONTROL : "Ctl";
case ALT : "Alt";
case SPACE : "Spc";
case UP : "Up";
case DOWN : "Dn";
case LEFT : "Lf";
case RIGHT : "Rt";
case TAB : "Tab";
case PRINTSCREEN : "Prt";
case NUMPADZERO : "#0";
case NUMPADONE : "#1";
case NUMPADTWO : "#2";
case NUMPADTHREE : "#3";
case NUMPADFOUR : "#4";
case NUMPADFIVE : "#5";
case NUMPADSIX : "#6";
case NUMPADSEVEN : "#7";
case NUMPADEIGHT : "#8";
case NUMPADNINE : "#9";
case NUMPADMINUS : "#-";
case NUMPADPLUS : "#+";
case NUMPADPERIOD : "#.";
case NUMPADMULTIPLY: "#*";
default: titleCaseTrim(FlxKey.toStringMap[id]);
}
}
static var dirReg = ~/^(l|r).?-(left|right|down|up)$/;
inline static public function getButtonName(id:Int, gamepad:FlxGamepad):String
{
return switch(gamepad.getInputLabel(id))
{
// case null | "": shortenButtonName(FlxGamepadInputID.toStringMap[id]);
case label: shortenButtonName(label);
}
}
static function shortenButtonName(name:String)
{
return switch (name == null ? "" : name.toLowerCase())
{
case "": "[?]";
case "square" : "[]";
case "circle" : "()";
case "triangle": "/\\";
case "plus" : "+";
case "minus" : "-";
case "home" : "Hm";
case "guide" : "Gd";
case "back" : "Bk";
case "select" : "Bk";
case "start" : "St";
case "left" : "Lf";
case "right" : "Rt";
case "down" : "Dn";
case "up" : "Up";
case dir if (dirReg.match(dir)):
dirReg.matched(1).toUpperCase() + "-"
+ switch (dirReg.matched(2))
{
case "left" : "L";
case "right": "R";
case "down" : "D";
case "up" : "U";
default: throw "Unreachable exaustiveness case";
};
case label: titleCaseTrim(label);
}
}
inline static function titleCaseTrim(str:String, length = 3)
{
return str.charAt(0).toUpperCase() + str.substr(1, length - 1).toLowerCase();
}
inline static public function parsePadName(name:String):ControllerName
{
return ControllerName.parseName(name);
}
inline static public function getPadName(gamepad:FlxGamepad):ControllerName
{
return ControllerName.getName(gamepad);
}
inline static public function getPadNameById(id:Int):ControllerName
{
return ControllerName.getNameById(id);
}
}
@:forward
enum abstract ControllerName(String) from String to String
{
var OUYA = "Ouya" ;
var PS4 = "PS4" ;
var LOGI = "Logi" ;
var XBOX = "XBox" ;
var XINPUT = "XInput" ;
var WII = "Wii" ;
var PRO_CON = "Pro_Con" ;
var JOYCONS = "Joycons" ;
var JOYCON_L = "Joycon_L";
var JOYCON_R = "Joycon_R";
var MFI = "MFI" ;
var PAD = "Pad" ;
static public function getAssetByDevice(device:Device):String
{
return switch (device)
{
case Keys: getAsset(null);
case Gamepad(id): getAsset(FlxG.gamepads.getByID(id));
}
}
static public function getAsset(gamepad:FlxGamepad):String
{
if (gamepad == null)
return 'assets/images/ui/devices/Keys.png';
final name = parseName(gamepad.name);
var path = 'assets/images/ui/devices/$name.png';
if (openfl.utils.Assets.exists(path))
return path;
return 'assets/images/ui/devices/Pad.png';
}
inline static public function getNameById(id:Int):ControllerName return getName(FlxG.gamepads.getByID(id));
inline static public function getName(gamepad:FlxGamepad):ControllerName return parseName(gamepad.name);
static public function parseName(name:String):ControllerName
{
name = name.toLowerCase().remove("-").remove("_");
return
if (name.contains("ouya"))
OUYA;
else if (name.contains("wireless controller") || name.contains("ps4"))
PS4;
else if (name.contains("logitech"))
LOGI;
else if (name.contains("xbox"))
XBOX
else if (name.contains("xinput"))
XINPUT;
else if (name.contains("nintendo rvlcnt01tr") || name.contains("nintendo rvlcnt01"))
WII;
else if (name.contains("mayflash wiimote pc adapter"))
WII;
else if (name.contains("pro controller"))
PRO_CON;
else if (name.contains("joycon l+r"))
JOYCONS;
else if (name.contains("joycon (l)"))
JOYCON_L;
else if (name.contains("joycon (r)"))
JOYCON_R;
else if (name.contains("mfi"))
MFI;
else
PAD;
}
}

View file

@ -19,12 +19,12 @@ import lime.app.Application;
#if newgrounds #if newgrounds
import io.newgrounds.NG; import io.newgrounds.NG;
import ui.NgPrompt;
#end #end
import ui.MenuList;
import ui.AtlasMenuList; import ui.AtlasMenuList;
import ui.MenuList;
import ui.Prompt; import ui.Prompt;
import ui.NgPrompt;
using StringTools; using StringTools;
@ -89,13 +89,13 @@ class MainMenuState extends MusicBeatState
#if CAN_OPEN_LINKS #if CAN_OPEN_LINKS
menuItems.createItem('donate', selectDonate, hasPopupBlocker); menuItems.createItem('donate', selectDonate, hasPopupBlocker);
#end #end
// menuItems.createItem('options', function () startExitState(new OptionsMenu())); menuItems.createItem('options', function () startExitState(new OptionsMenu()));
#if newgrounds // #if newgrounds
if (NGio.isLoggedIn) // if (NGio.isLoggedIn)
menuItems.createItem("logout", selectLogout); // menuItems.createItem("logout", selectLogout);
else // else
menuItems.createItem("login", selectLogin); // menuItems.createItem("login", selectLogin);
#end // #end
// center vertically // center vertically
var spacing = 160; var spacing = 160;

View file

@ -13,6 +13,14 @@ import flixel.util.FlxColor;
import lime.utils.Assets; import lime.utils.Assets;
class OptionsMenu extends MusicBeatState class OptionsMenu extends MusicBeatState
{
override function create()
{
add(new ui.ControlsMenu());
}
}
class OptionsMenu_old extends MusicBeatState
{ {
var selector:FlxText; var selector:FlxText;
var curSelected:Int = 0; var curSelected:Int = 0;

81
source/ui/ControlsMenu.hx Normal file
View file

@ -0,0 +1,81 @@
package ui;
import flixel.FlxG;
import flixel.FlxCamera;
import flixel.FlxObject;
import flixel.FlxSprite;
import flixel.group.FlxGroup;
import flixel.input.keyboard.FlxKey;
import Controls;
import ui.AtlasText;
import ui.TextMenuList;
class ControlsMenu extends flixel.group.FlxGroup
{
var controlGrid:TextMenuList;
var labels:FlxTypedGroup<AtlasText>;
var menuCamera:FlxCamera;
public function new()
{
super();
var menuBG = new FlxSprite().loadGraphic(Paths.image('menuDesat'));
menuBG.color = 0xFFea71fd;
menuBG.setGraphicSize(Std.int(menuBG.width * 1.1));
menuBG.updateHitbox();
menuBG.screenCenter();
add(menuBG);
camera = FlxG.camera;
FlxG.cameras.add(menuCamera = new FlxCamera());
menuCamera.bgColor = 0x0;
add(labels = new FlxTypedGroup<AtlasText>());
labels.camera = menuCamera;
add(controlGrid = new TextMenuList(Columns(2)));
controlGrid.camera = menuCamera;
// FlxG.debugger.drawDebug = true;
var controlList = Control.createAll();
for (i in 0...controlList.length)
{
var control = controlList[i];
var name = control.getName();
var y = (70 * i) + 30;
var label = labels.add(new BoldText(0, y, name));
label.x += 100;
createItem(500, y, control, 0);
createItem(700, y, control, 1);
}
var selected = controlGrid.members[0];
var camFollow = new FlxObject(FlxG.width / 2, selected.y);
menuCamera.follow(camFollow, LOCKON, 0.06);
controlGrid.onChange.add(function (selected) camFollow.y = selected.y);
}
function createItem(x = 0.0, y = 0.0, control:Control, index:Int)
{
var list = PlayerSettings.player1.controls.getInputsFor(control, Keys);
var name = "---";
if (list.length > index)
{
if (list[index] == FlxKey.ESCAPE)
return createItem(x, y, control, 2);
name = InputFormatter.format(list[index], Keys);
}
trace(control.getName() + " " + index + ": " + name);
return controlGrid.createItem(x, y, name, Default, onSelect.bind(name, control, index));
}
function onSelect(name:String, control:Control, index:Int):Void
{
controlGrid.enabled = false;
// var prompt = new Prompt();
}
}