Skip to content

Commit

Permalink
feat: UserLogic.GetValueIndexを廃止し、代わりにUserLogicExtensionに糖衣構文を追加
Browse files Browse the repository at this point in the history
  • Loading branch information
pspkurara committed Sep 9, 2020
1 parent 5174701 commit c9625b5
Show file tree
Hide file tree
Showing 5 changed files with 297 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,12 @@ public void DrawInspector(EditorSkinPartsPropertry property)
}

SkinnerEditorUtility.MapRuntimePropertyFromEditorProperty(validateProperty, property);
var logic = validateProperty.objectReferenceValues[ScriptableLogic.LogicIndex] as UserLogic;
validateProperty.objectReferenceValues.Remove(logic);
UserLogicExtension.SetActiveUserLogic(logic);
userLogic.ValidateProperty(validateProperty);
UserLogicExtension.ReleaseActiveUserLogic();
validateProperty.objectReferenceValues.Insert(ScriptableLogic.LogicIndex, logic);
SkinnerEditorUtility.MapRuntimePropertyFromEditorProperty(property, validateProperty);
}
}
Expand Down
2 changes: 2 additions & 0 deletions Packages/uGUI-Skinner/Runtime/SkinParts/ScriptableLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ public void SetValues(SkinPartsPropertry property)
ignoredLogicProperty = new SkinPartsPropertry(property);
ignoredLogicProperty.objectReferenceValues.Remove(userLogic);
}
UserLogicExtension.SetActiveUserLogic(userLogic);
userLogic.SetValues(ignoredLogicProperty);
UserLogicExtension.ReleaseActiveUserLogic();
}

}
Expand Down
35 changes: 21 additions & 14 deletions Packages/uGUI-Skinner/Runtime/UserLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Pspkurara.UI.Skinner
/// アセット化して保存していないと設定ができない
/// </summary>
/// <seealso cref="ScriptableLogic"/>
public abstract class UserLogic : ScriptableObject
public abstract class UserLogic : ScriptableObject, IUserLogicExtension
{

/// <summary>
Expand Down Expand Up @@ -52,32 +52,39 @@ public List<UserLogicVariable> variables {
/// <param name="property">プロパティ (ユーザーロジックの参照は持たない)</param>
public abstract void SetValues(SkinPartsPropertry property);

/// <summary>
/// 変数の値の制限を行う
/// 必要に応じてオーバーライドして使う
/// </summary>
/// <param name="property">property</param>
[System.Diagnostics.Conditional("UNITY_EDITOR")]
public virtual void ValidateProperty(SkinPartsPropertry property) { }

#region 内部的に呼び出す

/// <summary>
/// 変数IDを元にフィールド配列インデックス取得する
/// </summary>
/// <param name="variableId">変数ID</param>
/// <returns></returns>
protected int GetValueIndex(int variableId)
/// <param name="valueIndex">変数番号</param>
/// <returns>変数IDが見つかった場合は真</returns>
bool IUserLogicExtension.TryGetValueIndex(int variableId, out int valueIndex)
{
if (m_VariableIdMap == null)
{
m_VariableIdMap = SkinnerUtility.CreateVariableIdToIndexDictionary(variables);
}
if (m_VariableIdMap.ContainsKey(variableId))
{
return m_VariableIdMap[variableId];
valueIndex = m_VariableIdMap[variableId];
return true;
}
// 見つからないときはとりあえず-1を返しておく
return -1;
// 見つからないときはとりあえず0を返しておく
valueIndex = 0;
return false;
}

/// <summary>
/// 変数の値の制限を行う
/// 必要に応じてオーバーライドして使う
/// </summary>
/// <param name="property">property</param>
[System.Diagnostics.Conditional("UNITY_EDITOR")]
public virtual void ValidateProperty(SkinPartsPropertry property) { }
#endregion

}

Expand All @@ -89,7 +96,7 @@ public sealed class UserLogicVariable

/// <summary>
/// 変数ID
/// 指定しておくと<see cref="UserLogic.GetValueIndex(int)"/>でインデックスが取得できるようになる
/// 指定しておくと<see cref="UserLogicExtension"/>の関数から値を取得できるようになる
/// </summary>
public int ? VariableId = null;

Expand Down
258 changes: 258 additions & 0 deletions Packages/uGUI-Skinner/Runtime/UserLogicExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
using UnityEngine;

namespace Pspkurara.UI.Skinner
{

/// <summary>
/// <see cref="UserLogicExtension">が<see cref="UserLogic"/>からデータを受け取るためのインターフェイス
/// </summary>
internal interface IUserLogicExtension
{

/// <summary>
/// 変数IDを元にフィールド配列インデックス取得する
/// </summary>
/// <param name="variableId">変数ID</param>
/// <param name="valueIndex">変数番号</param>
/// <returns>変数IDが見つかった場合は真</returns>
bool TryGetValueIndex(int variableId, out int valueIndex);

}

/// <summary>
/// <see cref="UserLogic"/>を記述する際に便利になる関数群
/// 同期処理前提
/// </summary>
public static class UserLogicExtension
{

#region 内部メンバ

/// <summary>
/// 同期処理前提で動作する
/// </summary>
private static IUserLogicExtension m_ActiveUserLogic { get; set; }

/// <summary>
/// 指定したロジックをアクティブにする
/// クラス内の関数が使える場所に来る直前に必ず呼び出す
/// </summary>
/// <param name="userLogic">アクティブにするユーザーロジック</param>
internal static void SetActiveUserLogic(IUserLogicExtension userLogic)
{
m_ActiveUserLogic = userLogic;
}

/// <summary>
/// ロジックを非アクティブにする
/// クラス内の関数が使える場所が終了した直後に必ず呼び出す
/// </summary>
internal static void ReleaseActiveUserLogic()
{
m_ActiveUserLogic = null;
}

#endregion

#region 取得系関数

/// <summary>
/// <see cref="SkinPartsPropertry.objectReferenceValues">を型変換して取得する
/// </summary>
/// <typeparam name="T">変換する型</typeparam>
/// <param name="variableId">ユーザー変数ID</param>
public static T GetObjectReference<T>(this SkinPartsPropertry property, int variableId) where T : Object
{
int valueIndex;
if (m_ActiveUserLogic.TryGetValueIndex(variableId, out valueIndex))
{
return property.objectReferenceValues[valueIndex] as T;
}
return null;
}

/// <summary>
/// <see cref="SkinPartsPropertryWithoutObjectReference.boolValues">を取得する
/// </summary>
/// <param name="variableId">ユーザー変数ID</param>
public static bool GetBool(this SkinPartsPropertry property, int variableId)
{
int valueIndex;
if (m_ActiveUserLogic.TryGetValueIndex(variableId, out valueIndex))
{
return property.boolValues[valueIndex];
}
return default;
}

/// <summary>
/// <see cref="SkinPartsPropertryWithoutObjectReference.intValues">を取得する
/// </summary>
/// <param name="variableId">ユーザー変数ID</param>
public static int GetInt(this SkinPartsPropertry property, int variableId)
{
int valueIndex;
if (m_ActiveUserLogic.TryGetValueIndex(variableId, out valueIndex))
{
return property.intValues[valueIndex];
}
return default;
}

/// <summary>
/// <see cref="SkinPartsPropertryWithoutObjectReference.floatValues">を取得する
/// </summary>
/// <param name="variableId">ユーザー変数ID</param>
public static float GetFloat(this SkinPartsPropertry property, int variableId)
{
int valueIndex;
if (m_ActiveUserLogic.TryGetValueIndex(variableId, out valueIndex))
{
return property.floatValues[valueIndex];
}
return default;
}

/// <summary>
/// <see cref="SkinPartsPropertryWithoutObjectReference.colorValues">を取得する
/// </summary>
/// <param name="variableId">ユーザー変数ID</param>
public static Color GetColor(this SkinPartsPropertry property, int variableId)
{
int valueIndex;
if (m_ActiveUserLogic.TryGetValueIndex(variableId, out valueIndex))
{
return property.colorValues[valueIndex];
}
return default;
}

/// <summary>
/// <see cref="SkinPartsPropertryWithoutObjectReference.vector4Values">を取得する
/// </summary>
/// <param name="variableId">ユーザー変数ID</param>
public static Vector4 GetVector4(this SkinPartsPropertry property, int variableId)
{
int valueIndex;
if (m_ActiveUserLogic.TryGetValueIndex(variableId, out valueIndex))
{
return property.vector4Values[valueIndex];
}
return default;
}

/// <summary>
/// <see cref="SkinPartsPropertryWithoutObjectReference.stringValues">を取得する
/// </summary>
/// <param name="variableId">ユーザー変数ID</param>
public static string GetString(this SkinPartsPropertry property, int variableId)
{
int valueIndex;
if (m_ActiveUserLogic.TryGetValueIndex(variableId, out valueIndex))
{
return property.stringValues[valueIndex];
}
return default;
}

#endregion

#region セット系関数

/// <summary>
/// <see cref="SkinPartsPropertry.objectReferenceValues">に値をセットする
/// </summary>
/// <typeparam name="T">変換する型</typeparam>
/// <param name="variableId">ユーザー変数ID</param>
public static void SetObjectReference(this SkinPartsPropertry property, int variableId, Object value)
{
int valueIndex;
if (m_ActiveUserLogic.TryGetValueIndex(variableId, out valueIndex))
{
property.objectReferenceValues[valueIndex] = value;
}
}

/// <summary>
/// <see cref="SkinPartsPropertryWithoutObjectReference.boolValues">に値をセットする
/// </summary>
/// <param name="variableId">ユーザー変数ID</param>
public static void SetBool(this SkinPartsPropertry property, int variableId, bool value)
{
int valueIndex;
if (m_ActiveUserLogic.TryGetValueIndex(variableId, out valueIndex))
{
property.boolValues[valueIndex] = value;
}
}

/// <summary>
/// <see cref="SkinPartsPropertryWithoutObjectReference.intValues">に値をセットする
/// </summary>
/// <param name="variableId">ユーザー変数ID</param>
public static void SetInt(this SkinPartsPropertry property, int variableId, int value)
{
int valueIndex;
if (m_ActiveUserLogic.TryGetValueIndex(variableId, out valueIndex))
{
property.intValues[valueIndex] = value;
}
}

/// <summary>
/// <see cref="SkinPartsPropertryWithoutObjectReference.floatValues">に値をセットする
/// </summary>
/// <param name="variableId">ユーザー変数ID</param>
public static void SetFloat(this SkinPartsPropertry property, int variableId, float value)
{
int valueIndex;
if (m_ActiveUserLogic.TryGetValueIndex(variableId, out valueIndex))
{
property.floatValues[valueIndex] = value;
}
}

/// <summary>
/// <see cref="SkinPartsPropertryWithoutObjectReference.colorValues">に値をセットする
/// </summary>
/// <param name="variableId">ユーザー変数ID</param>
public static void SetColor(this SkinPartsPropertry property, int variableId, Color value)
{
int valueIndex;
if (m_ActiveUserLogic.TryGetValueIndex(variableId, out valueIndex))
{
property.colorValues[valueIndex] = value;
}
}

/// <summary>
/// <see cref="SkinPartsPropertryWithoutObjectReference.vector4Values">に値をセットする
/// </summary>
/// <param name="variableId">ユーザー変数ID</param>
public static void SetVector4(this SkinPartsPropertry property, int variableId, Vector4 value)
{
int valueIndex;
if (m_ActiveUserLogic.TryGetValueIndex(variableId, out valueIndex))
{
property.vector4Values[valueIndex] = value;
}
}

/// <summary>
/// <see cref="SkinPartsPropertryWithoutObjectReference.stringValues">に値をセットする
/// </summary>
/// <param name="variableId">ユーザー変数ID</param>
public static void SetString(this SkinPartsPropertry property, int variableId, string value)
{
int valueIndex;
if (m_ActiveUserLogic.TryGetValueIndex(variableId, out valueIndex))
{
property.stringValues[valueIndex] = value;
}
}

#endregion

}

}
11 changes: 11 additions & 0 deletions Packages/uGUI-Skinner/Runtime/UserLogicExtension.cs.meta

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

0 comments on commit c9625b5

Please sign in to comment.