Skip to content

Commit

Permalink
事件值抽象相等比较
Browse files Browse the repository at this point in the history
  • Loading branch information
KumoKyaku committed Dec 24, 2023
1 parent 03adcf3 commit ced4033
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 38 deletions.
54 changes: 54 additions & 0 deletions Megumin.Explosion/Megumin/Class/EqualComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;

namespace Megumin
{
/// <summary>
/// 解决泛型不能直接判断 == 问题
/// <seealso cref="System.Numerics.IEqualityOperators{TSelf, TOther, TResult}"/>"/>
/// </summary>
/// <typeparam name="V"></typeparam>
public class EqualComparer<V>
{
protected virtual bool EqualsKey(object x, object y)
{
return x == y;
}

protected virtual bool EqualsValue(object x, object y)
{
return x == y;
}

protected bool EqualsValue(bool x, bool y)
{
return x == y;
}

protected bool EqualsValue(int x, int y)
{
return x == y;
}

protected bool EqualsValue(float x, float y)
{
return x == y;
}

protected virtual bool EqualsValue(V x, V y)
{
bool flag = EqualityComparer<V>.Default.Equals(x, y);
return flag;
}

protected virtual bool EqualsValue<N>(N x, N y)
where N : IEquatable<N>
{
bool flag = x.Equals(y);
return flag;
}
}
}



30 changes: 26 additions & 4 deletions Megumin.Explosion/Megumin/Class/EventValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace Megumin
{
[Serializable]
public partial class EventValue<T>
public partial class EventValue<T> : EqualComparer<T>
{
/// <summary>
/// 这里public是为了能在unity中序列化,这里没办法标记SerializeField
Expand Down Expand Up @@ -40,14 +40,36 @@ public T Value
}
}

public void SetValue(T value)
/// <summary>
///
/// </summary>
/// <param name="value"></param>
/// <param name="raiseEvent">Ignore = -1,Force:101, <seealso cref="RaiseEvent"/></param>
public void SetValue(T value, int raiseEvent = 0)
{
Value = value;
var old = this.value;
this.value = value;

//先赋值完成在触发回调
if (raiseEvent >= 0)
{
OnValueSet?.Invoke(value, old);
if (raiseEvent > 100)
{
if (OnValueChanged != null)
{
if (!EqualsValue(value, old))
{
OnValueChanged.Invoke(value, old);
}
}
}
}
}

public void SetValueSilent(T value)
{
this.value = value;
SetValue(value, -1);
}
}

Expand Down
35 changes: 1 addition & 34 deletions Megumin.Explosion/Megumin/Class/Multiple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Megumin
/// </summary>
/// <typeparam name="K"></typeparam>
/// <typeparam name="V"></typeparam>
public abstract partial class Multiple<K, V> : IMultiple<K, V>
public abstract partial class Multiple<K, V> : EqualComparer<V>, IMultiple<K, V>
{
protected readonly Dictionary<K, V> ElementDic = new Dictionary<K, V>();
public event OnChanged<V> ValueChanged;
Expand Down Expand Up @@ -92,39 +92,6 @@ protected virtual bool EqualsKey(K x, K y)
return flag;
}

protected virtual bool EqualsValue(V x, V y)
{
bool flag = EqualityComparer<V>.Default.Equals(x, y);
return flag;
}

//public bool Equals(object objA, object objB)
//{
// return object.Equals(objA, objB);
//}

protected virtual bool EqualsKey(object x, object y)
{
return Equals(x, y);
}

protected bool EqualsValue(bool x, bool y)
{
return x == y;
}

protected bool EqualsValue(int x, int y)
{
return x == y;
}

protected virtual bool EqualsValue<N>(N x, N y)
where N : IEquatable<N>
{
bool flag = x.Equals(y);
return flag;
}

/// <summary>
/// 计算新的值, 返回值也可以用KeyValuePair,没什么区别.
/// </summary>
Expand Down
Binary file not shown.
Binary file not shown.

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

0 comments on commit ced4033

Please sign in to comment.