Forked from phosphoer/ControllerIconMapDefinition.cs
Created
November 1, 2020 06:36
-
-
Save thecrazy/0b04becad2b866db438554d61395a469 to your computer and use it in GitHub Desktop.
Rewired Glyph Mappings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.UI; | |
using System.Collections.Generic; | |
[CreateAssetMenu(fileName = "new-controller-icon-mapping", menuName = "Controller Icon Mapping")] | |
public class ControllerIconMapDefinition : ScriptableObject | |
{ | |
[SerializeField] | |
private InputIconMapDefinition gamepadMap = null; | |
[SerializeField] | |
private InputIconMapDefinition keyboardMap = null; | |
[SerializeField] | |
private InputIconMapDefinition mouseMap = null; | |
private List<Rewired.ControllerTemplateElementTarget> _elementTargets = new List<Rewired.ControllerTemplateElementTarget>(); | |
public bool GetIconsForAction(int actionId, Rewired.Player player, List<InputIcon> inputIcons) | |
{ | |
Rewired.Controller lastController = player.controllers.GetLastActiveController(); | |
if (lastController.type == Rewired.ControllerType.Joystick) | |
{ | |
FillInputIcons(inputIcons, gamepadMap, player, lastController, actionId); | |
} | |
else | |
{ | |
FillInputIcons(inputIcons, keyboardMap, player, Rewired.ReInput.controllers.Keyboard, actionId); | |
FillInputIcons(inputIcons, mouseMap, player, Rewired.ReInput.controllers.Mouse, actionId); | |
} | |
return inputIcons.Count > 0; | |
} | |
private void FillInputIcons(List<InputIcon> inputIcons, InputIconMapDefinition iconMap, Rewired.Player player, Rewired.Controller controller, int actionId) | |
{ | |
foreach (var elementMap in player.controllers.maps.ElementMapsWithAction(controller, actionId, skipDisabledMaps: false)) | |
{ | |
int inputId = elementMap.elementIdentifierId; | |
if (controller.templateCount > 0) | |
{ | |
_elementTargets.Clear(); | |
controller.Templates[0].GetElementTargets(elementMap, _elementTargets); | |
if (_elementTargets.Count > 0) | |
inputId = _elementTargets[0].element.id; | |
} | |
var inputIcon = iconMap.GetInputIcon(inputId); | |
inputIcons.Add(inputIcon); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.UI; | |
[System.Serializable] | |
public struct InputIcon | |
{ | |
// A comment to identify this input icon in inspector | |
public string Comment; | |
// The input ID that this icon represents, maps to Rewired's 'elementIdentifierId' | |
public int InputId; | |
// The sprite for this icon | |
public Sprite IconSprite; | |
// An optional label to display on the sprite | |
public string IconLabel; | |
// Offset from icon center for the label | |
public Vector3 IconLabelOffset; | |
// Whether to flip the icon horizontally | |
public bool FlipX; | |
} | |
[CreateAssetMenu(fileName = "new-input-icon-mapping", menuName = "Input Icon Mapping")] | |
public class InputIconMapDefinition : ScriptableObject | |
{ | |
[SerializeField] | |
private InputIcon[] inputIcons = null; | |
public InputIcon GetInputIcon(int inputId) | |
{ | |
foreach (InputIcon inputIcon in inputIcons) | |
{ | |
if (inputIcon.InputId == inputId) | |
{ | |
return inputIcon; | |
} | |
} | |
return default(InputIcon); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment