Skip to content

Commit

Permalink
Add button for resetting a public variable to its default value
Browse files Browse the repository at this point in the history
- Add a button for resetting value types to their default values on the UdonBehaviour inspector
  • Loading branch information
MerlinVR committed Apr 8, 2020
1 parent 3670343 commit aa93829
Show file tree
Hide file tree
Showing 6 changed files with 256 additions and 3 deletions.
8 changes: 8 additions & 0 deletions Assets/UdonSharp/Editor/Resources.meta

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

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
99 changes: 99 additions & 0 deletions Assets/UdonSharp/Editor/Resources/UndoArrowBlack.png.meta

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

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
99 changes: 99 additions & 0 deletions Assets/UdonSharp/Editor/Resources/UndoArrowLight.png.meta

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

53 changes: 50 additions & 3 deletions Assets/UdonSharp/Editor/UdonSharpProgramAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public class UdonSharpProgramAsset : UdonAssemblyProgramAsset
private UdonBehaviour currentBehaviour = null;

private static GUIStyle errorTextStyle;
private static GUIStyle undoLabelStyle;
private static GUIContent undoArrowLight;
private static GUIContent undoArrowDark;
private static GUIContent undoArrowContent;

private void DrawCompileErrorTextArea()
{
Expand All @@ -66,6 +70,24 @@ private void DrawCompileErrorTextArea()

protected override void DrawProgramSourceGUI(UdonBehaviour udonBehaviour, ref bool dirty)
{
if (undoLabelStyle == null ||
undoArrowDark == null ||
undoArrowLight == null)
{
undoLabelStyle = new GUIStyle(EditorStyles.label);
undoLabelStyle.alignment = TextAnchor.MiddleCenter;
undoLabelStyle.padding = new RectOffset(0, 0, 1, 0);
undoLabelStyle.margin = new RectOffset(0, 0, 0, 0);
undoLabelStyle.border = new RectOffset(0, 0, 0, 0);
undoLabelStyle.stretchWidth = false;
undoLabelStyle.stretchHeight = false;

undoArrowLight = new GUIContent((Texture)EditorGUIUtility.Load("Assets/UdonSharp/Editor/Resources/UndoArrowLight.png"), "Reset to default value");
undoArrowDark = new GUIContent((Texture)EditorGUIUtility.Load("Assets/UdonSharp/Editor/Resources/UndoArrowBlack.png"), "Reset to default value");
}

undoArrowContent = EditorGUIUtility.isProSkin ? undoArrowLight : undoArrowDark;

currentBehaviour = udonBehaviour;

EditorGUI.BeginChangeCheck();
Expand Down Expand Up @@ -427,8 +449,7 @@ private object DrawFieldForType(string fieldName, string symbol, (object value,
fieldLabel = new GUIContent(fieldName, tooltip.tooltip);
else
fieldLabel = new GUIContent(fieldName);



if (declaredType.IsArray)
{
bool foldoutEnabled;
Expand Down Expand Up @@ -713,7 +734,9 @@ protected override object DrawPublicVariableField(string symbol, object variable
EditorGUI.BeginChangeCheck();
object newValue = DrawFieldForType(null, symbol, (variableValue, variableType, fieldDefinition), fieldDefinition != null ? fieldDefinition.fieldSymbol.userCsType : null, ref dirty, enabled);

if (EditorGUI.EndChangeCheck())
bool changed = EditorGUI.EndChangeCheck();

if (changed)
{
if (shouldUseRuntimeValue)
{
Expand All @@ -735,7 +758,31 @@ protected override object DrawPublicVariableField(string symbol, object variable
}

if (!isArray)
{
object originalValue = program.Heap.GetHeapVariable(program.SymbolTable.GetAddressFromSymbol(symbol));

if (originalValue != null && !originalValue.Equals(variableValue))
{
int originalIndent = EditorGUI.indentLevel;
EditorGUI.indentLevel = 0;
// Check if changed because otherwise the UI throw an error since we changed that we want to draw the undo arrow in the middle of drawing when we're modifying stuff like colors
if (!changed && GUI.Button(EditorGUILayout.GetControlRect(GUILayout.Width(14f), GUILayout.Height(11f)), undoArrowContent, undoLabelStyle))
{
if (shouldUseRuntimeValue)
{
currentBehaviour.SetProgramVariable(symbol, originalValue);
}
else
{
dirty = true;
variableValue = originalValue;
}
}
EditorGUI.indentLevel = originalIndent;
}

EditorGUILayout.EndHorizontal();
}
}

EditorGUI.EndDisabledGroup();
Expand Down

0 comments on commit aa93829

Please sign in to comment.