Skip to content

Commit

Permalink
CHANGE: Pretty-print JSON of .inputactions for a mergeable form.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rene Damm committed Aug 16, 2018
1 parent 1a1855c commit 8219367
Show file tree
Hide file tree
Showing 8 changed files with 377 additions and 4 deletions.
1 change: 1 addition & 0 deletions Assets/Demo/DemoControls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ private void Initialize()
m_gameplay_jump = m_gameplay.GetAction("jump");
m_Initialized = true;
}

// gameplay
private UnityEngine.Experimental.Input.InputActionMap m_gameplay;
private UnityEngine.Experimental.Input.InputAction m_gameplay_fire;
Expand Down
204 changes: 203 additions & 1 deletion Assets/Demo/DemoControls.inputactions
Original file line number Diff line number Diff line change
@@ -1 +1,203 @@
{"maps":[{"name":"gameplay","actions":[{"name":"fire","bindings":[]},{"name":"move","bindings":[]},{"name":"look","bindings":[]},{"name":"jump","bindings":[]}],"bindings":[{"name":"","path":"*/{PrimaryAction}","interactions":"Tap,SlowTap","processors":"","groups":"","action":"fire","chainWithPrevious":false,"isComposite":false,"isPartOfComposite":false,"modifiers":""},{"name":"","path":"<Gamepad>/leftStick","interactions":"","processors":"","groups":"","action":"move","chainWithPrevious":false,"isComposite":false,"isPartOfComposite":false,"modifiers":""},{"name":"Dpad","path":"Dpad","interactions":"","processors":"","groups":"","action":"move","chainWithPrevious":false,"isComposite":true,"isPartOfComposite":false,"modifiers":""},{"name":"up","path":"<Keyboard>/w","interactions":"","processors":"","groups":"","action":"move","chainWithPrevious":false,"isComposite":false,"isPartOfComposite":true,"modifiers":""},{"name":"down","path":"<Keyboard>/s","interactions":"","processors":"","groups":"","action":"move","chainWithPrevious":false,"isComposite":false,"isPartOfComposite":true,"modifiers":""},{"name":"left","path":"<Keyboard>/a","interactions":"","processors":"","groups":"","action":"move","chainWithPrevious":false,"isComposite":false,"isPartOfComposite":true,"modifiers":""},{"name":"right","path":"<Keyboard>/d","interactions":"","processors":"","groups":"","action":"move","chainWithPrevious":false,"isComposite":false,"isPartOfComposite":true,"modifiers":""},{"name":"","path":"<Gamepad>/rightStick","interactions":"","processors":"","groups":"","action":"look","chainWithPrevious":false,"isComposite":false,"isPartOfComposite":false,"modifiers":""},{"name":"","path":"<Pointer>/delta","interactions":"","processors":"","groups":"","action":"look","chainWithPrevious":false,"isComposite":false,"isPartOfComposite":false,"modifiers":""},{"name":"","path":"<Keyboard>/space","interactions":"","processors":"","groups":"","action":"jump","chainWithPrevious":false,"isComposite":false,"isPartOfComposite":false,"modifiers":""}]}]}
{
"maps" : [
{
"name" : "gameplay",
"actions" : [
{
"name" : "fire",
"expectedControlLayout" : "Button",
"bindings" : [
]
},
{
"name" : "move",
"expectedControlLayout" : "Stick",
"bindings" : [
]
},
{
"name" : "look",
"expectedControlLayout" : "Vector2",
"bindings" : [
]
},
{
"name" : "jump",
"expectedControlLayout" : "Button",
"bindings" : [
]
}
],
"bindings" : [
{
"name" : "",
"path" : "*/{PrimaryAction}",
"interactions" : "Tap,SlowTap",
"processors" : "",
"groups" : "",
"action" : "fire",
"chainWithPrevious" : false,
"isComposite" : false,
"isPartOfComposite" : false,
"modifiers" : ""
},
{
"name" : "",
"path" : "<SteamDemoController>/fire",
"interactions" : "",
"processors" : "",
"groups" : "",
"action" : "fire",
"chainWithPrevious" : false,
"isComposite" : false,
"isPartOfComposite" : false,
"modifiers" : ""
},
{
"name" : "",
"path" : "<Gamepad>/leftStick",
"interactions" : "",
"processors" : "",
"groups" : "",
"action" : "move",
"chainWithPrevious" : false,
"isComposite" : false,
"isPartOfComposite" : false,
"modifiers" : ""
},
{
"name" : "Dpad",
"path" : "Dpad",
"interactions" : "",
"processors" : "",
"groups" : "",
"action" : "move",
"chainWithPrevious" : false,
"isComposite" : true,
"isPartOfComposite" : false,
"modifiers" : ""
},
{
"name" : "up",
"path" : "<Keyboard>/w",
"interactions" : "",
"processors" : "",
"groups" : "",
"action" : "move",
"chainWithPrevious" : false,
"isComposite" : false,
"isPartOfComposite" : true,
"modifiers" : ""
},
{
"name" : "down",
"path" : "<Keyboard>/s",
"interactions" : "",
"processors" : "",
"groups" : "",
"action" : "move",
"chainWithPrevious" : false,
"isComposite" : false,
"isPartOfComposite" : true,
"modifiers" : ""
},
{
"name" : "left",
"path" : "<Keyboard>/a",
"interactions" : "",
"processors" : "",
"groups" : "",
"action" : "move",
"chainWithPrevious" : false,
"isComposite" : false,
"isPartOfComposite" : true,
"modifiers" : ""
},
{
"name" : "right",
"path" : "<Keyboard>/d",
"interactions" : "",
"processors" : "",
"groups" : "",
"action" : "move",
"chainWithPrevious" : false,
"isComposite" : false,
"isPartOfComposite" : true,
"modifiers" : ""
},
{
"name" : "",
"path" : "<SteamDemoController>/move",
"interactions" : "",
"processors" : "",
"groups" : "",
"action" : "move",
"chainWithPrevious" : false,
"isComposite" : false,
"isPartOfComposite" : false,
"modifiers" : ""
},
{
"name" : "",
"path" : "<Gamepad>/rightStick",
"interactions" : "",
"processors" : "",
"groups" : "",
"action" : "look",
"chainWithPrevious" : false,
"isComposite" : false,
"isPartOfComposite" : false,
"modifiers" : ""
},
{
"name" : "",
"path" : "<Pointer>/delta",
"interactions" : "",
"processors" : "",
"groups" : "",
"action" : "look",
"chainWithPrevious" : false,
"isComposite" : false,
"isPartOfComposite" : false,
"modifiers" : ""
},
{
"name" : "",
"path" : "<SteamDemoController>/look",
"interactions" : "",
"processors" : "",
"groups" : "",
"action" : "look",
"chainWithPrevious" : false,
"isComposite" : false,
"isPartOfComposite" : false,
"modifiers" : ""
},
{
"name" : "",
"path" : "<Keyboard>/space",
"interactions" : "",
"processors" : "",
"groups" : "",
"action" : "jump",
"chainWithPrevious" : false,
"isComposite" : false,
"isPartOfComposite" : false,
"modifiers" : ""
},
{
"name" : "",
"path" : "<Gamepad>/buttonNorth",
"interactions" : "",
"processors" : "",
"groups" : "",
"action" : "jump",
"chainWithPrevious" : false,
"isComposite" : false,
"isPartOfComposite" : false,
"modifiers" : ""
}
]
}
]
}
2 changes: 2 additions & 0 deletions Assets/Demo/DemoControls.inputactions.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Assets/Demo/SimpleController_UsingActions_InAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void Awake()
controls.gameplay.jump.performed += ctx =>
{
var jump = new Vector3(0.0f, jumpForce, 0.0f);
if(isGrounded)
if (isGrounded)
{
m_Rigidbody.AddForce(jump * jumpForce, ForceMode.Impulse);
isGrounded = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.IMGUI.Controls;
using UnityEngine.Experimental.Input.Utilities;

namespace UnityEngine.Experimental.Input.Editor
{
Expand Down Expand Up @@ -160,9 +161,18 @@ internal void Apply()

void SaveChangesToAsset()
{
////TODO: has to be made to work with version control
var asset = (InputActionAsset)m_ReferencedObject;
var path = AssetDatabase.GetAssetPath(asset);
File.WriteAllText(path, asset.ToJson());
var json = asset.ToJson();
var prettyJson = StringHelpers.PrettyPrintJSON(json);
var existingJson = File.ReadAllText(path);
if (prettyJson != existingJson)
{
File.WriteAllText(path, prettyJson);
////FIXME: this needs to trigger a refresh but that breaks the entire asset handling logic in here
//AssetDatabase.Refresh();
}
}

class AssetChangeWatch : AssetPostprocessor
Expand Down
108 changes: 108 additions & 0 deletions Packages/com.unity.inputsystem/InputSystem/Utilities/StringHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,5 +247,113 @@ public static unsafe string ReadStringFromBuffer(IntPtr buffer, int bufferSize,
offset = (uint)endOffset;
return text;
}

/// <summary>
/// Take a string in JSON format and return a pretty-printed version of it.
/// </summary>
/// <param name="json"></param>
/// <param name="indent"></param>
/// <returns></returns>
public static string PrettyPrintJSON(string json, int indent = 4)
{
if (string.IsNullOrEmpty(json))
return string.Empty;

var builder = new StringBuilder();
var length = json.Length;
var position = 0;
var indentLevel = 0;
var newline = false;
var inString = false;
var isAfterColon = false;
var isNewLine = true;

while (position < length)
{
var ch = json[position];
++position;

if (!inString && char.IsWhiteSpace(ch))
continue;

if (ch == '\\' && position < length)
{
ch = json[position];
++position;
}
else if (ch == '"')
inString = !inString;
else if (!inString && (ch == '}' || ch == ']'))
--indentLevel;

if (newline)
{
for (var n = 0; n < indentLevel; ++n)
for (var i = 0; i < indent; ++i)
builder.Append(' ');
newline = false;
isNewLine = true;
}

if (!inString && (ch == '{' || ch == '['))
{
if (!isNewLine && !isAfterColon)
builder.Append(' ');

builder.Append(ch);
builder.Append('\n');
newline = true;
isAfterColon = false;
++indentLevel;
}
else if (!inString && (ch == '}' || ch == ']'))
{
if (!isNewLine)
{
builder.Append('\n');
for (var n = 0; n < indentLevel; ++n)
for (var i = 0; i < indent; ++i)
builder.Append(' ');
}
builder.Append(ch);
////FIXME: should take whitespace into account
if (position < length && json[position] == ',')
{
++position;
builder.Append(',');
}
builder.Append('\n');
newline = true;
isAfterColon = false;
}
else if (!inString && ch == ':')
{
builder.Append(" : ");
isAfterColon = true;
isNewLine = false;
}
else if (!inString && ch == ',')
{
builder.Append(",\n");
newline = true;
isAfterColon = false;
isNewLine = false;
}
else if (!inString && !char.IsWhiteSpace(ch))
{
builder.Append(ch);
isAfterColon = false;
isNewLine = false;
}
else if (inString)
{
builder.Append(ch);
isAfterColon = false;
isNewLine = false;
}
}

return builder.ToString();
}
}
}
Loading

0 comments on commit 8219367

Please sign in to comment.