-
-
Notifications
You must be signed in to change notification settings - Fork 993
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Structure): provide custom data types to convey better meaning
> **Breaking Changes** In a number of scripts, standard data types were being used but created confusion in meaning, such as using a `Vector2` for a min/max range as this would then display in the editor as `x` and `y` values, with `x` being minimum and the `y` being maximum. This has now been replaced with a new `Limits2D` data type that displays the same 2 float input boxes as a `Vector2` but instead uses the labels of `min` and `max` which makes a lot more sense. The `Limits2D` data type can also have a new custom attribute applied to it called `[MinMaxRange]` which works in a similar way to `[Range]` but it provides 2 slider points that can be dragged to set a minimum and maximum value for the control. It should also work with a Vector2 if required. The Axis Scale Grab Action has also been updated to use the new Vector3State data type when determining which axes should be locked for scaling. This is in place of the previous 3 separate boolean parameters.
- Loading branch information
1 parent
f9e4ba1
commit ef35e19
Showing
27 changed files
with
595 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
Assets/VRTK/Prefabs/SDKSetupSwitcher/VRTK_SDKSetupSwitcher.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
using UnityEngine; | ||
using UnityEditor; | ||
using System.Globalization; | ||
using Supyrb; | ||
using VRTK; | ||
|
||
[CustomPropertyDrawer(typeof(MinMaxRangeAttribute))] | ||
class MinMaxRangeDrawer : PropertyDrawer | ||
{ | ||
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) | ||
{ | ||
label.tooltip = VRTK_EditorUtilities.GetTooltipAttribute(fieldInfo).tooltip; | ||
bool foundGeneric = false; | ||
bool valid = false; | ||
try | ||
{ | ||
Vector2 input = SerializedPropertyExtensions.GetValue<Limits2D>(property).AsVector2(); | ||
Vector2 output = BuildSlider(position, label, input, out valid); | ||
if (valid) | ||
{ | ||
SerializedPropertyExtensions.SetValue<Limits2D>(property, new Limits2D(output)); | ||
} | ||
foundGeneric = true; | ||
} | ||
catch (System.Exception) | ||
{ | ||
Error(position, label); | ||
} | ||
|
||
if (!foundGeneric) | ||
{ | ||
switch (property.propertyType) | ||
{ | ||
case SerializedPropertyType.Vector2: | ||
Vector2 input = property.vector2Value; | ||
Vector2 output = BuildSlider(position, label, input, out valid); | ||
if (valid) | ||
{ | ||
property.vector2Value = output; | ||
} | ||
break; | ||
default: | ||
Error(position, label); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
private Vector2 BuildSlider(Rect position, GUIContent label, Vector2 range, out bool valid) | ||
{ | ||
float fieldWidth = GUI.skin.textField.CalcSize(new GUIContent(1.234f.ToString(CultureInfo.InvariantCulture))).x; ; | ||
float fieldPadding = 5f; | ||
float min = range.x; | ||
float max = range.y; | ||
|
||
MinMaxRangeAttribute attr = attribute as MinMaxRangeAttribute; | ||
EditorGUI.BeginChangeCheck(); | ||
Rect updatedPosition = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label); | ||
min = EditorGUI.FloatField(new Rect(updatedPosition.x, updatedPosition.y, fieldWidth, updatedPosition.height), Mathf.Clamp(min, attr.min, attr.max)); | ||
EditorGUI.MinMaxSlider(new Rect(updatedPosition.x + (fieldWidth + fieldPadding), updatedPosition.y, updatedPosition.width - ((fieldWidth + fieldPadding) * 2f), updatedPosition.height), ref min, ref max, attr.min, attr.max); | ||
max = EditorGUI.FloatField(new Rect(updatedPosition.x + (updatedPosition.width - fieldWidth), updatedPosition.y, fieldWidth, updatedPosition.height), Mathf.Clamp(max, attr.min, attr.max)); | ||
|
||
if (EditorGUI.EndChangeCheck()) | ||
{ | ||
range.x = min; | ||
range.y = max; | ||
valid = true; | ||
return range; | ||
} | ||
valid = false; | ||
return Vector2.zero; | ||
} | ||
|
||
private void Error(Rect position, GUIContent label) | ||
{ | ||
EditorGUI.LabelField(position, label, new GUIContent("Use only with Vector2 or Limits2D")); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
Assets/VRTK/Source/Editor/Attributes/MinMaxRangeDrawer.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using UnityEngine; | ||
using UnityEditor; | ||
using VRTK; | ||
|
||
[CustomPropertyDrawer(typeof(Limits2D))] | ||
public class Limits2DDrawer : PropertyDrawer | ||
{ | ||
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) | ||
{ | ||
label.tooltip = VRTK_EditorUtilities.GetTooltipAttribute(fieldInfo).tooltip; | ||
SerializedProperty minimum = property.FindPropertyRelative("minimum"); | ||
SerializedProperty maximum = property.FindPropertyRelative("maximum"); | ||
|
||
int indent = EditorGUI.indentLevel; | ||
EditorGUI.indentLevel = 0; | ||
position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label); | ||
float updatePositionX = position.x; | ||
float labelWidth = 30f; | ||
float fieldWidth = (position.width / 3f) - labelWidth; | ||
|
||
EditorGUI.LabelField(new Rect(updatePositionX, position.y, labelWidth, position.height), "Min"); | ||
updatePositionX += labelWidth; | ||
minimum.floatValue = EditorGUI.FloatField(new Rect(updatePositionX, position.y, fieldWidth, position.height), minimum.floatValue); | ||
updatePositionX += fieldWidth; | ||
|
||
EditorGUI.LabelField(new Rect(updatePositionX, position.y, labelWidth, position.height), "Max"); | ||
updatePositionX += labelWidth; | ||
maximum.floatValue = EditorGUI.FloatField(new Rect(updatePositionX, position.y, fieldWidth, position.height), maximum.floatValue); | ||
updatePositionX += fieldWidth; | ||
|
||
EditorGUI.indentLevel = indent; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
Assets/VRTK/Source/Editor/DataTypes/Limits2DDrawer.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.