Skip to content

Commit

Permalink
重构多重控制,增加判断k,v相等的虚方法
Browse files Browse the repository at this point in the history
  • Loading branch information
KumoKyaku committed Aug 29, 2023
1 parent 3eb3f73 commit a39d8a4
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 35 deletions.
6 changes: 3 additions & 3 deletions Megumin.Explosion/Megumin.Explosion.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<Authors>云却</Authors>

<!--版本配置-->
<AssemblyVersion>1.0.1.0</AssemblyVersion>
<FileVersion>1.0.1.0</FileVersion>
<Version>1.0.1.0</Version>
<AssemblyVersion>1.0.2.0</AssemblyVersion>
<FileVersion>1.0.2.0</FileVersion>
<Version>1.0.2.0</Version>

<!--包配置-->
<Product>MeguminLibrary</Product>
Expand Down
52 changes: 50 additions & 2 deletions Megumin.Explosion/Megumin/Class/Multiple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected virtual void ApplyValue()
Current = newValue;
CurrentKey = newKey;

bool flagV = EqualityComparer<V>.Default.Equals(oldValue, newValue);
bool flagV = EqualsValue(oldValue, newValue);
//if (old is IEquatable<V> oe)
//{
// //转成接口必然装箱
Expand All @@ -50,12 +50,60 @@ protected virtual void ApplyValue()
OnValueChangedKV((newKey, newValue), (oldKey, oldValue));
}

if (!flagV || !EqualityComparer<K>.Default.Equals(oldKey, newKey))
if (!flagV || !EqualsKey(oldKey, newKey))
{
OnKeyValueChanged((newKey, newValue), (oldKey, oldValue));
}
}

/// <summary>
/// 判定Key是否相等
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
/// <remarks>
/// 必须使用EqualsKey,EqualsValue两个函数名,不能同名使用重载,否则在子类重写时会提示二义性,无法重写,编译错误。
/// </remarks>
protected virtual bool EqualsKey(K x, K y)
{
bool flag = EqualityComparer<K>.Default.Equals(x, 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
61 changes: 41 additions & 20 deletions Megumin.Explosion/Megumin/Class/MultipleControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ protected override (K Key, V Value) CalNewValue()
/// <para>可以将ascending参数设置为true,改为 只要有个一个源为false,结果就是false.</para>
/// </summary>
/// <remarks>处理黑屏,碰撞盒开闭</remarks>
[Obsolete("use AnyTrueControl instead")]
public class ActiveControl : MultipleControl<object, bool>
{
/// <summary>
Expand All @@ -227,34 +228,54 @@ public ActiveControl(object defaultHandle,
{

}
}

/// <summary>
/// <inheritdoc/>
/// 重写比较方法提高了性能
/// </summary>
protected override void ApplyValue()
/// <summary>
/// 开启控制,只要有个一个控制源为true,结果就是true.
/// </summary>
/// <remarks>
/// 例如处理黑屏过渡
/// </remarks>
public class AnyTrueControl : MultipleControl<object, bool>
{
public AnyTrueControl(object defaultHandle,
bool defaultValue,
OnValueChanged<(object, bool)> onValueChangedKV = null,
OnValueChanged<bool> onValueChanged = null)
: base(defaultHandle, defaultValue, onValueChangedKV, onValueChanged, false)
{
var old = Current;
var oldK = CurrentKey;
var result = CalNewValue();
Current = result.Value;
CurrentKey = result.Key;

bool flagV = old == result.Value;
}

if (!flagV)
{
OnValueChanged(result.Value, old);
OnValueChangedKV((result.Key, result.Value), (oldK, old));
}
public AnyTrueControl(OnValueChanged<(object, bool)> onValueChangedKV = null,
OnValueChanged<bool> onValueChanged = null)
: this(new(), false, onValueChangedKV, onValueChanged)
{

if (!flagV || Equals(oldK, result.Key))
{
OnKeyValueChanged((result.Key, result.Value), (oldK, old));
}
}
}

/// <summary>
/// 关闭控制,只要有一个控制源为false,结果就是false.
/// </summary>
public class AnyFalseControl : MultipleControl<object, bool>
{
public AnyFalseControl(object defaultHandle,
bool defaultValue,
OnValueChanged<(object, bool)> onValueChangedKV = null,
OnValueChanged<bool> onValueChanged = null)
: base(defaultHandle, defaultValue, onValueChangedKV, onValueChanged, true)
{

}

public AnyFalseControl(OnValueChanged<(object, bool)> onValueChangedKV = null,
OnValueChanged<bool> onValueChanged = null)
: this(new(), true, onValueChangedKV, onValueChanged)
{

}
}

}

Expand Down
8 changes: 5 additions & 3 deletions Megumin.Explosion/Megumin/Interface/IMultiple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace Megumin
{

/// <summary>
/// 对象有多个部分构成
/// 对象有多个部分构成。
/// <para/> 根据一个或多个值,得到一个结果值。多个部分可能随时添加或者移除
/// <para/> 运算规则可能为排序,求和,均值等多种算法,用于不同业务场景。
/// <para/> 例如:是否静音由多个业务模块同时控制。最大血量可能由多个装备求和。
/// </summary>
public interface IMultiple<K, V>
{
Expand All @@ -32,7 +34,7 @@ public interface IMultiple<K, V>
V Current { get; }

/// <summary>
/// 加入一个构成项
/// 添加一个构成项
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
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.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.megumin.explosion4unity",
"displayName": "Explosion",
"version": "4.1.0",
"version": "4.2.0",
"unity": "2021.3",
"description": "KumoKyaku's personal common library.\n\n[KumoKyaku](https://github.com/KumoKyaku)\u7684\u79c1\u4eba\u516c\u5171\u7c7b\u5e93\uff0c\u5305\u542b\u4e00\u4e9b\u5e38\u7528\u7684\u7c7b\u548c\u6269\u5c55\u51fd\u6570\u3002",
"documentationUrl": "https://github.com/KumoKyaku/Megumin.Explosion",
Expand Down
2 changes: 1 addition & 1 deletion UnitTestProject1/Megumin/Class/ActiveControlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class ActiveControlTests
[TestMethod()]
public void ActiveControlRentAutoReturn()
{
ActiveControl control = new ActiveControl(new object(), false);
AnyTrueControl control = new AnyTrueControl();
Assert.AreEqual(false, control);

var c1 = new object();
Expand Down

0 comments on commit a39d8a4

Please sign in to comment.