Skip to content
This repository has been archived by the owner on Aug 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #107 from techno-dwarf-works/dev
Browse files Browse the repository at this point in the history
Update drawers
  • Loading branch information
uurha authored Oct 21, 2023
2 parents 35361ec + 0fbf003 commit c83f477
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Text.RegularExpressions;
using Better.Attributes.EditorAddons.Drawers.Utilities;
using Better.EditorTools;
using Better.EditorTools.Drawers.Base;
using Better.EditorTools.Utilities;
using UnityEditor;
using UnityEngine;
Expand Down Expand Up @@ -88,21 +89,23 @@ public virtual bool Validate()
}
}

private protected void SetValueAndApply<T>(T value) where T : struct
private protected void SetValueAndApply(object value)
{
if (!Validate())
{
return;
}

if (_fieldType.IsEquivalentTo(typeof(T)))
{
_serializedProperty.SetValueNoRecord(value);
}
if (_fieldType.IsEquivalentTo(typeof(Vector2)))
_serializedProperty.vector2Value = (Vector2)value;
else if (_fieldType.IsEquivalentTo(typeof(Vector3)))
_serializedProperty.vector3Value = (Vector3)value;
else if (_fieldType.IsEquivalentTo(typeof(Bounds)))
_serializedProperty.boundsValue = (Bounds)value;
else if (_fieldType.IsEquivalentTo(typeof(Quaternion)))
_serializedProperty.quaternionValue = (Quaternion)value;
else
{
throw new ArgumentOutOfRangeException();
}

_serializedProperty.serializedObject.ApplyModifiedProperties();
}
Expand All @@ -128,5 +131,10 @@ private protected virtual Vector3 GetPosition(Vector3 position, Quaternion rotat
return rotation * (position + Vector3.up * HandleUtility.GetHandleSize(position) +
sceneView.camera.transform.right * 0.2f * HandleUtility.GetHandleSize(position));
}

public virtual HeightCache GetHeight(GUIContent label)
{
return HeightCache.GetAdditive(0f);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,14 @@ protected override bool PreDraw(ref Rect position, SerializedProperty property,

protected override void DrawField(Rect position, SerializedProperty property, GUIContent label)
{
var cache = ValidateCachedProperties(property, GizmoUtility.Instance);
if (!cache.IsValid)
if (!Collection.IsValid(property))
{
var cache = ValidateCachedProperties(property, GizmoUtility.Instance);

var fieldType = GetFieldOrElementType();
Collection.SetProperty(property, fieldType);
cache.Value.Wrapper.SetProperty(property, fieldType);
}

Collection.DrawField(position, property, label);
}

Expand Down Expand Up @@ -154,7 +155,7 @@ protected override HeightCache GetPropertyHeight(SerializedProperty property, GU
return HeightCache.GetAdditive(additive + DrawersHelper.SpaceHeight * 2);
}

return HeightCache.GetAdditive(0);
return Collection.GetHeight(property, label);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Better.EditorTools;
using Better.EditorTools.Drawers.Base;
using Better.Extensions.Runtime.MathfExtensions;
using UnityEditor;
using UnityEngine;
Expand Down Expand Up @@ -36,17 +37,26 @@ public override void Apply(SceneView sceneView)

public override void DrawField(Rect position, GUIContent label)
{
using (var change = new EditorGUI.ChangeCheckScope())
if (_serializedProperty.IsTargetComponent(out var component))
{
var eulerRotation = EditorGUI.Vector3Field(position, label, _quaternion.eulerAngles);
_quaternion = Quaternion.Euler(eulerRotation);
if (change.changed)
using (var change = new EditorGUI.ChangeCheckScope())
{
SetValueAndApply(_quaternion);
var eulerRotation = EditorGUI.Vector3Field(position, label, _quaternion.eulerAngles);

if (change.changed)
{
_quaternion = Quaternion.Euler(eulerRotation);
SetValueAndApply(_quaternion);
}
}
}
}

public override HeightCache GetHeight(GUIContent label)
{
return HeightCache.GetFull(EditorGUIUtility.singleLineHeight);
}

public override void SetProperty(SerializedProperty property, Type fieldType)
{
_quaternion = property.quaternionValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public override void Apply(SceneView sceneView)

if (!Vector3Math.Approximately(_vector2, (Vector2)buffer))
{
_vector2 = buffer;
SetValueAndApply(_vector2);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public override void Apply(SceneView sceneView)

if (!Vector3Math.Approximately(_vector3, buffer))
{
_vector3 = buffer;
SetValueAndApply(_vector3);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Better.EditorTools.Drawers.Base;
using Better.Extensions.Runtime.MathfExtensions;
using UnityEditor;
using UnityEngine;
Expand All @@ -22,23 +23,29 @@ public override void Apply(SceneView sceneView)

if (!Vector3Math.Approximately(_quaternion, buffer))
{
_quaternion = buffer;
SetValueAndApply(_quaternion);
}
}

public override void DrawField(Rect position, GUIContent label)
{
using (var change = new EditorGUI.ChangeCheckScope())
{
var eulerRotation = EditorGUI.Vector3Field(position, label, _quaternion.eulerAngles);
_quaternion = Quaternion.Euler(eulerRotation);
if (change.changed)
{
_quaternion = Quaternion.Euler(eulerRotation);
SetValueAndApply(_quaternion);
}
}
}

public override HeightCache GetHeight(GUIContent label)
{
return HeightCache.GetFull(EditorGUIUtility.singleLineHeight);
}

public override void SetProperty(SerializedProperty property, Type fieldType)
{
_quaternion = property.quaternionValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public override void Apply(SceneView sceneView)

if (!Vector3Math.Approximately(_vector2, (Vector2)buffer))
{
_vector2 = buffer;
SetValueAndApply(_vector2);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public override void Apply(SceneView sceneView)

if (!Vector3Math.Approximately(_vector3, buffer))
{
_vector3 = buffer;
SetValueAndApply(_vector3);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void Apply(SceneView sceneView)
foreach (var gizmo in this)
{
var valueWrapper = gizmo.Value.Wrapper;
if(valueWrapper.Validate())
if (valueWrapper.Validate())
{
valueWrapper.Apply(sceneView);
}
Expand All @@ -25,6 +25,7 @@ public void Apply(SceneView sceneView)
{
keysToRemove = new List<SerializedProperty>();
}

keysToRemove.Add(gizmo.Key);
}
}
Expand Down Expand Up @@ -71,5 +72,25 @@ public void DrawField(Rect position, SerializedProperty property, GUIContent lab
gizmoWrapper.Wrapper.DrawField(position, label);
}
}

public bool IsValid(SerializedProperty property)
{
if (TryGetValue(property, out var gizmoWrapper))
{
return gizmoWrapper.Wrapper.Validate();
}

return false;
}

public HeightCache GetHeight(SerializedProperty property, GUIContent label)
{
if (TryGetValue(property, out var wrapper))
{
return wrapper.Wrapper.GetHeight(label);
}

return HeightCache.GetAdditive(0);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,8 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2489651872190957466}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalRotation: {x: 0.3576463, y: -0.6084306, z: -0.6162748, w: -0.3494378}
m_LocalPosition: {x: 1.806, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children:
Expand All @@ -455,7 +455,7 @@ Transform:
- {fileID: 7922880496053330287}
- {fileID: 758694063010868647}
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_LocalEulerAnglesHint: {x: 270.92, y: 643.7, z: 196.3}
--- !u!114 &-745862845828164141
MonoBehaviour:
m_ObjectHideFlags: 0
Expand All @@ -470,4 +470,4 @@ MonoBehaviour:
m_EditorClassIdentifier:
sprite: {fileID: 21300000, guid: 78b772f3f305147419de3cc97ca72bd7, type: 3}
pos: {x: 0, y: 1.1718663, z: 1.3363248}
rot: {x: -0.068464614, y: 0.018447453, z: 0.0024131201, w: 0.99748003}
rot: {x: 0.000000019045045, y: 0.5995843, z: -0.000000026187257, w: 0.80031174}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class PreviewTest : MonoBehaviour
{
[SerializeField] private Sprite sprite;
[Gizmo] [SerializeField] private Vector3 pos;
[Gizmo] [SerializeField] private Quaternion rot;
[GizmoLocal] [SerializeField] private Quaternion rot;

}
}
2 changes: 1 addition & 1 deletion Assets/BetterAttributes/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.uurha.betterattributes",
"displayName": "Better Attributes",
"version": "2.1.42",
"version": "2.1.44",
"unity": "2020.1",
"description": "Unity attributes, allows to serialize interfaces, draw handles for Vector3/Vector2/Quaternion/Bounds, create read only fields.",
"dependencies": {
Expand Down

0 comments on commit c83f477

Please sign in to comment.