Replace input layout JSON with typed projection
This commit is contained in:
parent
93fa34dd09
commit
ccdfc1fbfc
8 changed files with 84 additions and 325 deletions
|
|
@ -2,7 +2,6 @@ using System;
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Ink.Runtime;
|
||||
using TMPro;
|
||||
using UniRx;
|
||||
using UnityEngine;
|
||||
|
|
@ -23,7 +22,6 @@ public class InputDisplayLayout : MonoBehaviour
|
|||
public InputDisplayButton MouseForward;
|
||||
public InputDisplayButton MouseBack;
|
||||
public VerticalLayoutGroup RowContainer;
|
||||
public TextAsset LayoutFile;
|
||||
public Prototype LabelPrototype;
|
||||
public Prototype LinePrototype;
|
||||
//public UILineRendererList LineRenderer;
|
||||
|
|
@ -82,7 +80,7 @@ public class InputDisplayLayout : MonoBehaviour
|
|||
void Start()
|
||||
{
|
||||
_canvas = transform.root.GetComponent<Canvas>();
|
||||
_inputLayout = ParseKeyboardLayoutProjection(LayoutFile.text);
|
||||
_inputLayout = InputLayout.CreateAnsi104();
|
||||
DisplayLayout(_inputLayout);
|
||||
|
||||
_buttonMappings.Add(MapMouseButton(MouseLeft, "<Mouse>/leftButton"));
|
||||
|
|
@ -376,93 +374,6 @@ public class InputDisplayLayout : MonoBehaviour
|
|||
}
|
||||
}
|
||||
|
||||
public InputLayout ParseKeyboardLayoutProjection(string layout)
|
||||
{
|
||||
var rows = new List<InputLayoutRow>();
|
||||
var nextWidth = 1f;
|
||||
var nextHeight = 1;
|
||||
|
||||
foreach (var rowValue in SimpleJson.TextToArray(layout))
|
||||
{
|
||||
if (!(rowValue is List<object> parsedRow))
|
||||
throw new FormatException("Unexpected keyboard layout JSON format: expected a row array.");
|
||||
|
||||
var row = new InputLayoutKeyRow();
|
||||
var columns = new List<InputLayoutColumn>();
|
||||
|
||||
foreach (var columnValue in parsedRow)
|
||||
{
|
||||
switch (columnValue)
|
||||
{
|
||||
case Dictionary<string, object> options:
|
||||
ApplyLayoutOptions(options, rows, columns, ref nextWidth, ref nextHeight);
|
||||
break;
|
||||
case string label:
|
||||
columns.Add(CreateLayoutKey(label, ref nextWidth, ref nextHeight));
|
||||
break;
|
||||
default:
|
||||
throw new FormatException("Unexpected keyboard layout JSON format: expected a key label or options object.");
|
||||
}
|
||||
}
|
||||
|
||||
row.Columns = columns.ToArray();
|
||||
rows.Add(row);
|
||||
}
|
||||
|
||||
return new InputLayout {Rows = rows.ToArray()};
|
||||
}
|
||||
|
||||
private static void ApplyLayoutOptions(
|
||||
Dictionary<string, object> options,
|
||||
ICollection<InputLayoutRow> rows,
|
||||
ICollection<InputLayoutColumn> columns,
|
||||
ref float nextWidth,
|
||||
ref int nextHeight)
|
||||
{
|
||||
foreach (var option in options)
|
||||
{
|
||||
switch (option.Key)
|
||||
{
|
||||
case "a":
|
||||
break;
|
||||
case "w":
|
||||
nextWidth = Convert.ToSingle(option.Value);
|
||||
break;
|
||||
case "h":
|
||||
nextHeight = Convert.ToInt32(option.Value);
|
||||
break;
|
||||
case "y":
|
||||
rows.Add(new InputLayoutRowSpacer {Height = Convert.ToSingle(option.Value)});
|
||||
break;
|
||||
case "x":
|
||||
columns.Add(new InputLayoutColumnSpacer {Width = Convert.ToSingle(option.Value)});
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static InputLayoutKey CreateLayoutKey(string label, ref float nextWidth, ref int nextHeight)
|
||||
{
|
||||
InputLayoutKey key;
|
||||
var trimmedLabel = label.Trim();
|
||||
if (!string.IsNullOrEmpty(trimmedLabel))
|
||||
{
|
||||
var labels = trimmedLabel.Split('\n');
|
||||
key = nextHeight != 1 ? new InputLayoutMultiRowKey {Height = nextHeight} : new InputLayoutBindableKey();
|
||||
nextHeight = 1;
|
||||
((InputLayoutBindableKey) key).MainLabel = labels.Length == 2 ? labels[1] : trimmedLabel;
|
||||
((InputLayoutBindableKey) key).AltLabel = labels.Length == 2 ? labels[0] : "";
|
||||
}
|
||||
else
|
||||
{
|
||||
key = new InputLayoutKey();
|
||||
}
|
||||
|
||||
key.Width = nextWidth;
|
||||
nextWidth = 1f;
|
||||
return key;
|
||||
}
|
||||
|
||||
private IEnumerator AssociateInputKeys()
|
||||
{
|
||||
var path = "";
|
||||
|
|
|
|||
|
|
@ -4,6 +4,76 @@ public class InputLayout
|
|||
{
|
||||
public InputLayoutRow[] Rows;
|
||||
|
||||
public static InputLayout CreateAnsi104()
|
||||
{
|
||||
return new InputLayout
|
||||
{
|
||||
Rows = new InputLayoutRow[]
|
||||
{
|
||||
Row(
|
||||
Key("Esc"), Spacer(1), Key("F1"), Key("F2"), Key("F3"), Key("F4"), Spacer(.5f),
|
||||
Key("F5"), Key("F6"), Key("F7"), Key("F8"), Spacer(.5f),
|
||||
Key("F9"), Key("F10"), Key("F11"), Key("F12"), Spacer(.25f),
|
||||
Key(""), Key(""), Key("")),
|
||||
RowSpacer(.5f),
|
||||
Row(
|
||||
Key("~\n`"), Key("!\n1"), Key("@\n2"), Key("#\n3"), Key("$\n4"), Key("%\n5"), Key("^\n6"),
|
||||
Key("&\n7"), Key("*\n8"), Key("(\n9"), Key(")\n0"), Key("_\n-"), Key("+\n="), Key("Backspace", 2),
|
||||
Spacer(.25f), Key("Insert"), Key("Home"), Key("PgUp"), Spacer(.25f), Key(""), Key("/"), Key("*"), Key("-")),
|
||||
Row(
|
||||
Key("Tab", 1.5f), Key("Q"), Key("W"), Key("E"), Key("R"), Key("T"), Key("Y"), Key("U"), Key("I"), Key("O"), Key("P"),
|
||||
Key("{\n["), Key("}\n]"), Key("|\n\\", 1.5f), Spacer(.25f),
|
||||
Key("Delete"), Key("End"), Key("PgDn"), Spacer(.25f), Key("Home\n7"), Key("Up\n8"), Key("PgUp\n9"), Key("+", 1, 2)),
|
||||
Row(
|
||||
Key("Caps Lock", 1.75f), Key("A"), Key("S"), Key("D"), Key("F"), Key("G"), Key("H"), Key("J"), Key("K"), Key("L"),
|
||||
Key(":\n;"), Key("\"\n'"), Key("Enter", 2.25f), Spacer(3.5f), Key("Left\n4"), Key("5"), Key("Right\n6")),
|
||||
Row(
|
||||
Key("Shift", 2.25f), Key("Z"), Key("X"), Key("C"), Key("V"), Key("B"), Key("N"), Key("M"),
|
||||
Key("<\n,"), Key(">\n."), Key("?\n/"), Key("Shift", 2.75f), Spacer(1.25f), Key("Up"), Spacer(1.25f),
|
||||
Key("End\n1"), Key("Down\n2"), Key("PgDn\n3"), Key("Enter", 1, 2)),
|
||||
Row(
|
||||
Key("Ctrl", 1.25f), Key("", 1.25f), Key("Alt", 1.25f), Key("Spacebar", 6.25f), Key("Alt", 1.25f),
|
||||
Key("", 1.25f), Key("", 1.25f), Key("Ctrl", 1.25f), Spacer(.25f),
|
||||
Key("Left"), Key("Down"), Key("Right"), Spacer(.25f), Key("Ins\n0", 2), Key("Del\n."))
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private static InputLayoutRowSpacer RowSpacer(float height)
|
||||
{
|
||||
return new InputLayoutRowSpacer {Height = height};
|
||||
}
|
||||
|
||||
private static InputLayoutKeyRow Row(params InputLayoutColumn[] columns)
|
||||
{
|
||||
return new InputLayoutKeyRow {Columns = columns};
|
||||
}
|
||||
|
||||
private static InputLayoutColumnSpacer Spacer(float width)
|
||||
{
|
||||
return new InputLayoutColumnSpacer {Width = width};
|
||||
}
|
||||
|
||||
private static InputLayoutKey Key(string label, float width = 1f, int height = 1)
|
||||
{
|
||||
InputLayoutKey key;
|
||||
var trimmedLabel = label.Trim();
|
||||
if (!string.IsNullOrEmpty(trimmedLabel))
|
||||
{
|
||||
var labels = trimmedLabel.Split('\n');
|
||||
key = height != 1 ? new InputLayoutMultiRowKey {Height = height} : new InputLayoutBindableKey();
|
||||
((InputLayoutBindableKey) key).MainLabel = labels.Length == 2 ? labels[1] : trimmedLabel;
|
||||
((InputLayoutBindableKey) key).AltLabel = labels.Length == 2 ? labels[0] : "";
|
||||
}
|
||||
else
|
||||
{
|
||||
key = new InputLayoutKey();
|
||||
}
|
||||
|
||||
key.Width = width;
|
||||
return key;
|
||||
}
|
||||
|
||||
public IEnumerable<InputLayoutBindableKey> GetBindableKeys()
|
||||
{
|
||||
foreach (var row in Rows)
|
||||
|
|
|
|||
Loading…
Reference in a new issue