Skip to content

Added Ref class #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Editor/N8.Utils.SOA.Editor.asmdef
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"name": "N8.Utils.SOA.Editor",
"rootNamespace": "N8.Utils.SOA.Editor",
"references": [],
"references": [
"N8.Utils.SOA"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
Expand Down
50 changes: 50 additions & 0 deletions Editor/RefPropertyDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;

namespace N8.Utils.SOA.Editor
{
//[CustomPropertyDrawer(type: typeof(IRef), useForChildren: true)]
public sealed class RefPropertyDrawer : PropertyDrawer
{
public override VisualElement CreatePropertyGUI(SerializedProperty property)
{
VisualElement container = new VisualElement();

Debug.Log("Active");

FloatField floatField = new FloatField(label: "Fancy Label");

floatField.RegisterCallback<ChangeEvent<float>>(callback: evt =>
{
Debug.Log(evt.newValue);
});

//PropertyField useDefault = new PropertyField(property.FindPropertyRelative(_USE_DEFAULT));
//PropertyField defaultValue = new PropertyField(property.FindPropertyRelative(_DEFAULT_VALUE));
//PropertyField variable = new PropertyField(property.FindPropertyRelative(_VAR));

//SerializedProperty useDefault = property.FindPropertyRelative(_USE_DEFAULT);
//SerializedProperty defaultValue = property.FindPropertyRelative(_DEFAULT_VALUE);
//SerializedProperty variable = property.FindPropertyRelative(_VAR);

//var t = new PopupField<float>(label: "Test", choices: new List<Single> {1f, 2f, 3f}, defaultValue: 1f);
//container.Add(t);

//container.Add(useDefault);
//container.Add(defaultValue);
//container.Add(variable);

return container;
}

/*
private const String _USE_DEFAULT = "useDefault";
private const String _DEFAULT_VALUE = "defaultValue";
private const String _VAR = "Var";
*/
}
}
96 changes: 96 additions & 0 deletions Runtime/Refs/Ref.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using System;
using System.Runtime.CompilerServices;
using static System.Runtime.CompilerServices.MethodImplOptions;

using UnityEngine;

namespace N8.Utils.SOA
{
using Vars;

public interface IRef { } //for PropertyDrawer purposes

[Serializable]
public class Ref<T, TVar> : IRef where TVar : Var<T>
{
#region Fields & Properties

[SerializeField] protected Boolean useDefault;
[SerializeField] protected T defaultValue;

[field: SerializeField]
protected TVar Var { get; private set; }

private Boolean IsUsingDefault => (useDefault || (Var == null));

public T Value
{
get => IsUsingDefault ? defaultValue : (T)Var;
set
{
if (IsUsingDefault)
{
defaultValue = value;
}
else
{
Var.Value = value;
}
}
}

#endregion

#region Structors

public Ref() { }

public Ref(T val)
{
this.Value = val;
}

public Ref(TVar var)
{
this.Var = var;
}

/*
public Ref(T value, Boolean useDefault = true)
{
this.useDefault = useDefault;
this.Value = value;
}

public Ref(TVar variable, Boolean useDefault = false)
{
this.useDefault = useDefault;
this.variable = variable;
}
*/

#endregion

#region Methods

public override String ToString() => Value.ToString();

#endregion

#region Operators

//TODO: Conversion Operators default check.

[MethodImpl(AggressiveInlining)]
public static implicit operator T (Ref<T, TVar> input) => input.Value;
[MethodImpl(AggressiveInlining)]
public static implicit operator TVar (Ref<T, TVar> input) => input.Var;

[MethodImpl(AggressiveInlining)]
public static implicit operator Ref<T, TVar> (T val) => new Ref<T, TVar>(val: val);
[MethodImpl(AggressiveInlining)]
public static implicit operator Ref<T, TVar> (TVar var) => new Ref<T, TVar>(var: var);

#endregion
}
}
30 changes: 30 additions & 0 deletions Runtime/Refs/Vector3Ref.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Runtime.CompilerServices;
using static System.Runtime.CompilerServices.MethodImplOptions;

using UnityEngine;

namespace N8.Utils.SOA.Refs
{
using Vars;

[Serializable]
public sealed class Vector3Ref : Ref<Vector3, Vector3Var>
{
public Vector3Ref() : base() { }
public Vector3Ref(Vector3 val) : base(val: val) { }
public Vector3Ref(Vector3Var var) : base(var: var) { }
//public Vector3Ref(Vector3 value, Boolean useDefault = true) : base(value: value, useDefault: useDefault) { }
//public Vector3Ref(Vector3Var variable, Boolean useDefault = false) : base(variable: variable, useDefault: useDefault) { }

[MethodImpl(AggressiveInlining)]
public static implicit operator Vector3 (Vector3Ref input) => input.Value;
[MethodImpl(AggressiveInlining)]
public static implicit operator Vector3Var (Vector3Ref input) => input.Var;

[MethodImpl(AggressiveInlining)]
public static implicit operator Vector3Ref (Vector3 val) => new Vector3Ref(val: val);
[MethodImpl(AggressiveInlining)]
public static implicit operator Vector3Ref (Vector3Var var) => new Vector3Ref(var: var);
}
}
21 changes: 0 additions & 21 deletions Runtime/Variables/Variable.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using UnityEngine;

using Sirenix.OdinInspector;
using UnityEngine;

namespace N8.Utils.SOA.Variables
namespace N8.Utils.SOA.Vars
{
[CreateAssetMenu(fileName = "NewFloatVariable", menuName = "N8Dev/SOA/Variables/Float"), InlineEditor]
public sealed class FloatVariable : Variable<float>
public sealed class FloatVar : Var<float>
{

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using UnityEngine;

using Sirenix.OdinInspector;
using UnityEngine;

namespace N8.Utils.SOA.Variables
namespace N8.Utils.SOA.Vars
{
[CreateAssetMenu(fileName = "NewFloatVariable", menuName = "N8Dev/SOA/Variables/Float"), InlineEditor] //TODO: That won't work, it uses the same menuName as the regular float.
public sealed class ReadonlyFloatVariable : ReadonlyVariable<float>
public sealed class ReadonlyFloatVar : ReadonlyVar<float>
{

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using UnityEngine;

namespace N8.Utils.SOA.Variables
namespace N8.Utils.SOA.Vars
{
public abstract class ReadonlyVariable<T> : ScriptableObject, ISerializationCallbackReceiver
public abstract class ReadonlyVar<T> : ScriptableObject, ISerializationCallbackReceiver
{
[SerializeField]
private T _value;
Expand All @@ -14,7 +14,7 @@ public abstract class ReadonlyVariable<T> : ScriptableObject, ISerializationCall
public void OnBeforeSerialize() => Value = _value;
public void OnAfterDeserialize() { }

public static implicit operator T(ReadonlyVariable<T> input) => input.Value;
public static implicit operator T(ReadonlyVar<T> input) => input.Value;

public override string ToString() => Value.ToString();
}
Expand Down
57 changes: 57 additions & 0 deletions Runtime/Vars/Var.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using UnityEngine;

namespace N8.Utils.SOA.Vars
{
public abstract class Var<T> : ScriptableObject, ISerializationCallbackReceiver
{
#region Fields & Properties

[SerializeField]
private T _value;

public T Value { get; set; }

#endregion

/*
//TODO: Figure these out.
#region "Structors"

public static Var<T> New()
{
Var<T> newVar = CreateInstance<Var<T>>();

return newVar;
}

public static Var<T> New(T val)
{
Var<T> newVar = CreateInstance<Var<T>>();

newVar.Value = val;

return newVar;
}

#endregion
*/

#region Methods

public void OnBeforeSerialize() => Value = _value;
public void OnAfterDeserialize() { }


public override String ToString() => Value.ToString();

#endregion

#region Operators

//Can't work in reverse because ScriptableObjects can't be created via constructor.
public static implicit operator T(Var<T> input) => input.Value;

#endregion
}
}
29 changes: 29 additions & 0 deletions Runtime/Vars/Vector3Var.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Sirenix.OdinInspector;
using UnityEngine;

namespace N8.Utils.SOA.Vars
{
[CreateAssetMenu(fileName = "NewVector3Var", menuName = "N8Dev/SOA/Variables/Vector3"), InlineEditor]
public sealed class Vector3Var : Var<Vector3>
{
#region "Structors"

public new static Vector3Var New()
{
Vector3Var newVar = CreateInstance<Vector3Var>();

return newVar;
}

public new static Vector3Var New(Vector3 val)
{
Vector3Var newVar = CreateInstance<Vector3Var>();

newVar.Value = val;

return newVar;
}

#endregion
}
}
24 changes: 24 additions & 0 deletions Tests/Editor/N8.Utils.SOA.Tests.Editor.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "N8.Utils.SOA.Tests.Editor",
"rootNamespace": "N8.Utils.SOA.Tests.Editor",
"references": [
"UnityEngine.TestRunner",
"UnityEditor.TestRunner",
"N8.Utils.SOA"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": true,
"precompiledReferences": [
"nunit.framework.dll"
],
"autoReferenced": false,
"defineConstraints": [
"UNITY_INCLUDE_TESTS"
],
"versionDefines": [],
"noEngineReferences": false
}
Loading