Skip to content

Commit

Permalink
Add YGValue property drawer
Browse files Browse the repository at this point in the history
  • Loading branch information
gilzoide committed Nov 25, 2023
1 parent 5bf3aeb commit 3f059dc
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Editor.meta

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

18 changes: 18 additions & 0 deletions Editor/Gilzoide.FlexUi.Editor.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "Gilzoide.FlexUi.Editor",
"rootNamespace": "Gilzoide.FlexUi.Editor",
"references": [
"GUID:f5764d172907845189231762597f67e6"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
7 changes: 7 additions & 0 deletions Editor/Gilzoide.FlexUi.Editor.asmdef.meta

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

81 changes: 81 additions & 0 deletions Editor/YGValuePropertyDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using Gilzoide.FlexUi.Yoga;
using UnityEditor;
using UnityEngine;

namespace Gilzoide.FlexUi.Editor
{
[CustomPropertyDrawer(typeof(YGValue))]
public class YGValuePropertyDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
string value = GetValueAsString(property);
string newValue = EditorGUI.DelayedTextField(position, label, value).ToLowerInvariant().Trim();
if (newValue == "auto")
{
SetValue(property, YGValue.Auto);
}
else if (newValue == "" || newValue == "none" || newValue == "undefined")
{
SetValue(property, YGValue.Undefined);
}
else if (newValue.EndsWith('%') && float.TryParse(newValue.Substring(0, newValue.Length - 1), out float percentValue))
{
SetValue(property, new YGValue(percentValue, Unit.Percent));
}
else if (float.TryParse(newValue, out float pointValue))
{
SetValue(property, new YGValue(pointValue, Unit.Point));
}
}

public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return EditorGUI.GetPropertyHeight(SerializedPropertyType.String, label);
}

private static string GetValueAsString(SerializedProperty property)
{
int unit = property.FindPropertyRelative(nameof(YGValue.Unit)).enumValueIndex;
float value = property.FindPropertyRelative(nameof(YGValue.Value)).floatValue;
switch ((Unit) unit)
{
case Unit.Undefined:
return "none";

case Unit.Point:
if (float.IsNaN(value))
{
return "none";
}
else
{
return value.ToString();
}

case Unit.Percent:
if (float.IsNaN(value))
{
return "none";
}
else
{
return value.ToString() + '%';
}

case Unit.Auto:
return "auto";

default:
throw new ArgumentOutOfRangeException(nameof(YGValue.Unit));
}
}

private void SetValue(SerializedProperty property, YGValue value)
{
property.FindPropertyRelative(nameof(YGValue.Value)).floatValue = value.Value;
property.FindPropertyRelative(nameof(YGValue.Unit)).enumValueIndex = (int) value.Unit;
}
}
}
11 changes: 11 additions & 0 deletions Editor/YGValuePropertyDrawer.cs.meta

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

0 comments on commit 3f059dc

Please sign in to comment.