Skip to content

Commit 2e08a1c

Browse files
committed
Add PercentageVariable and PercentageReference
1 parent 3d80231 commit 2e08a1c

File tree

6 files changed

+164
-0
lines changed

6 files changed

+164
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using UnityEditor;
2+
using UnityEngine;
3+
4+
namespace Zigurous.Architecture.Editor
5+
{
6+
[CustomPropertyDrawer(typeof(PercentageReference), true)]
7+
public class PercentageReferencePropertyDrawer : PropertyDrawer
8+
{
9+
private readonly string[] popupOptions = { "Fixed Value", "Variable" };
10+
private static GUIStyle popupStyle;
11+
12+
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
13+
{
14+
if (popupStyle == null)
15+
{
16+
popupStyle = new GUIStyle(GUI.skin.GetStyle("PaneOptions"));
17+
popupStyle.imagePosition = ImagePosition.ImageOnly;
18+
}
19+
20+
SerializedProperty useVariable = property.FindPropertyRelative("useVariable");
21+
SerializedProperty variable = property.FindPropertyRelative("variable");
22+
SerializedProperty fixedValue = property.FindPropertyRelative("fixedValue");
23+
24+
label = EditorGUI.BeginProperty(position, label, property);
25+
position = EditorGUI.PrefixLabel(position, label);
26+
27+
int indent = EditorGUI.indentLevel;
28+
EditorGUI.indentLevel = 0;
29+
30+
Rect popupRect = new Rect(position);
31+
popupRect.width = popupStyle.fixedWidth + popupStyle.margin.right;
32+
popupRect.height = popupStyle.fixedHeight;
33+
popupRect.x += position.width - popupRect.width;
34+
popupRect.y += (EditorGUIUtility.singleLineHeight - popupStyle.fixedHeight) / 2f;
35+
position.width -= popupRect.width;
36+
37+
EditorGUI.BeginChangeCheck();
38+
39+
int result = EditorGUI.Popup(popupRect, useVariable.boolValue ? 1 : 0, popupOptions, popupStyle);
40+
useVariable.boolValue = result == 1;
41+
42+
if (useVariable.boolValue) {
43+
EditorGUI.PropertyField(position, variable, GUIContent.none, true);
44+
} else {
45+
fixedValue.floatValue = EditorGUI.Slider(position, fixedValue.floatValue, 0f, 1f);
46+
}
47+
48+
if (EditorGUI.EndChangeCheck()) {
49+
property.serializedObject.ApplyModifiedProperties();
50+
}
51+
52+
EditorGUI.indentLevel = indent;
53+
EditorGUI.EndProperty();
54+
}
55+
56+
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
57+
{
58+
SerializedProperty useVariable = property.FindPropertyRelative("useVariable");
59+
SerializedProperty variable = property.FindPropertyRelative("variable");
60+
SerializedProperty fixedValue = property.FindPropertyRelative("fixedValue");
61+
62+
return EditorGUI.GetPropertyHeight(useVariable.boolValue ? variable : fixedValue, true);
63+
}
64+
65+
}
66+
67+
}

Editor/Variables/PercentageReferencePropertyDrawer.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace Zigurous.Architecture
2+
{
3+
/// <summary>
4+
/// A reference to a percentage value, either a constant or <see cref="PercentageVariable"/>.
5+
/// </summary>
6+
[System.Serializable]
7+
public class PercentageReference : ValueReference<float, PercentageVariable>
8+
{
9+
/// <summary>
10+
/// Creates a new percentage reference.
11+
/// </summary>
12+
public PercentageReference() {}
13+
14+
/// <summary>
15+
/// Creates a new percentage reference with the fixed value.
16+
/// </summary>
17+
/// <param name="value">The fixed value to set.</param>
18+
public PercentageReference(float value) : base(value) {}
19+
20+
/// <summary>
21+
/// Creates a new percentage reference to the variable value.
22+
/// </summary>
23+
/// <param name="variable">The variable to reference.</param>
24+
public PercentageReference(PercentageVariable variable) : base(variable) {}
25+
26+
/// <summary>
27+
/// Implicitly converts the reference to a float.
28+
/// </summary>
29+
/// <param name="reference">The reference to convert.</param>
30+
/// <returns>The float value.</returns>
31+
public static implicit operator float(PercentageReference reference) => reference.value;
32+
}
33+
34+
}

Runtime/Variables/PercentageReference.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using UnityEngine;
2+
3+
namespace Zigurous.Architecture
4+
{
5+
/// <summary>
6+
/// A percentage variable saved as a ScriptableObject. Percentages are float
7+
/// values in the range [0..1].
8+
/// </summary>
9+
[CreateAssetMenu(menuName = "Zigurous/Variables/Percentage")]
10+
[HelpURL("https://docs.zigurous.com/com.zigurous.architecture/api/Zigurous.Architecture/PercentageVariable")]
11+
public class PercentageVariable : ScriptableVariable<float>
12+
{
13+
/// <summary>
14+
/// The value of the variable.
15+
/// </summary>
16+
[SerializeField]
17+
[Tooltip("The value of the variable.")]
18+
[Range(0f, 1f)]
19+
private float m_Value;
20+
21+
/// <inheritdoc/>
22+
public override float value
23+
{
24+
get => m_Value;
25+
set => m_Value = value;
26+
}
27+
28+
}
29+
30+
}

Runtime/Variables/PercentageVariable.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)