Move CachedTransform to OpenRA.Primitives.

This commit is contained in:
Matthias Mailänder
2024-08-01 10:05:49 +02:00
committed by Gustas
parent 88fb83bc57
commit b8756c4737
26 changed files with 113 additions and 64 deletions

View File

@@ -0,0 +1,41 @@
#region Copyright & License Information
/*
* Copyright (c) The OpenRA Developers and Contributors
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System;
namespace OpenRA.Primitives
{
public class CachedTransform<T, U>
{
readonly Func<T, U> transform;
bool initialized;
T lastInput;
U lastOutput;
public CachedTransform(Func<T, U> transform)
{
this.transform = transform;
}
public U Update(T input)
{
if (initialized && ((input == null && lastInput == null) || (input != null && input.Equals(lastInput))))
return lastOutput;
lastInput = input;
lastOutput = transform(input);
initialized = true;
return lastOutput;
}
}
}

View File

@@ -0,0 +1,51 @@
#region Copyright & License Information
/*
* Copyright (c) The OpenRA Developers and Contributors
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System;
namespace OpenRA.Primitives
{
public class PredictedCachedTransform<T, U>
{
readonly Func<T, U> transform;
bool initialized;
T lastInput;
U lastOutput;
bool predicted;
U prediction;
public PredictedCachedTransform(Func<T, U> transform)
{
this.transform = transform;
}
public void Predict(U value)
{
predicted = true;
prediction = value;
}
public U Update(T input)
{
if ((predicted || initialized) && ((input == null && lastInput == null) || (input != null && input.Equals(lastInput))))
return predicted ? prediction : lastOutput;
predicted = false;
initialized = true;
lastInput = input;
lastOutput = transform(input);
return lastOutput;
}
}
}

View File

@@ -13,7 +13,6 @@ using System.Collections.Generic;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Graphics;
using OpenRA.Mods.Common.Widgets;
using OpenRA.Primitives;
using OpenRA.Traits;

View File

@@ -12,6 +12,7 @@
using System.Collections.Generic;
using System.Linq;
using OpenRA.Mods.Common.Widgets;
using OpenRA.Primitives;
using OpenRA.Traits;
using OpenRA.Widgets;

View File

@@ -13,6 +13,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Primitives;
using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets

View File

@@ -11,6 +11,7 @@
using System;
using OpenRA.Graphics;
using OpenRA.Primitives;
using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets

View File

@@ -11,6 +11,7 @@
using OpenRA.Graphics;
using OpenRA.Mods.Common.Traits;
using OpenRA.Primitives;
using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets.Logic.Ingame

View File

@@ -11,6 +11,7 @@
using System;
using OpenRA.Mods.Common.Traits;
using OpenRA.Primitives;
using OpenRA.Traits;
using OpenRA.Widgets;

View File

@@ -12,6 +12,7 @@
using System;
using OpenRA.Mods.Common.Traits;
using OpenRA.Network;
using OpenRA.Primitives;
using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets.Logic

View File

@@ -12,6 +12,7 @@
using System;
using System.Globalization;
using OpenRA.Mods.Common.Traits;
using OpenRA.Primitives;
using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets.Logic

View File

@@ -15,6 +15,7 @@ using OpenRA.Mods.Common.Commands;
using OpenRA.Mods.Common.Lint;
using OpenRA.Mods.Common.Traits;
using OpenRA.Network;
using OpenRA.Primitives;
using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets.Logic

View File

@@ -16,6 +16,7 @@ using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using OpenRA.FileSystem;
using OpenRA.Primitives;
using OpenRA.Support;
using OpenRA.Widgets;

View File

@@ -15,6 +15,7 @@ using System.IO;
using System.Linq;
using System.Threading.Tasks;
using OpenRA.Mods.Common.Installer;
using OpenRA.Primitives;
using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets.Logic

View File

@@ -12,6 +12,7 @@
using System;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Traits;
using OpenRA.Primitives;
using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets.Logic

View File

@@ -15,6 +15,7 @@ using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Traits;
using OpenRA.Network;
using OpenRA.Primitives;
using OpenRA.Traits;
using OpenRA.Widgets;

View File

@@ -13,6 +13,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Network;
using OpenRA.Primitives;
using OpenRA.Traits;
using OpenRA.Widgets;

View File

@@ -13,6 +13,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Network;
using OpenRA.Primitives;
using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets.Logic

View File

@@ -11,6 +11,7 @@
using System;
using System.Linq;
using OpenRA.Primitives;
using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets.Logic

View File

@@ -12,6 +12,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Primitives;
using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets.Logic

View File

@@ -18,6 +18,7 @@ using System.Threading;
using System.Threading.Tasks;
using OpenRA.FileFormats;
using OpenRA.Network;
using OpenRA.Primitives;
using OpenRA.Traits;
using OpenRA.Widgets;

View File

@@ -13,6 +13,7 @@ using System;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Traits;
using OpenRA.Primitives;
using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets.Logic

View File

@@ -16,6 +16,7 @@ using System.Globalization;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Traits;
using OpenRA.Primitives;
using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets.Logic

View File

@@ -11,6 +11,7 @@
using System;
using System.Collections.Generic;
using OpenRA.Primitives;
using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets.Logic

View File

@@ -11,6 +11,7 @@
using System;
using OpenRA.Graphics;
using OpenRA.Primitives;
using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets

View File

@@ -396,66 +396,4 @@ namespace OpenRA.Mods.Common.Widgets
notificationWidget.Bounds.Width = boxWidth - notificationWidget.Bounds.X;
}
}
public class CachedTransform<T, U>
{
readonly Func<T, U> transform;
bool initialized;
T lastInput;
U lastOutput;
public CachedTransform(Func<T, U> transform)
{
this.transform = transform;
}
public U Update(T input)
{
if (initialized && ((input == null && lastInput == null) || (input != null && input.Equals(lastInput))))
return lastOutput;
lastInput = input;
lastOutput = transform(input);
initialized = true;
return lastOutput;
}
}
public class PredictedCachedTransform<T, U>
{
readonly Func<T, U> transform;
bool initialized;
T lastInput;
U lastOutput;
bool predicted;
U prediction;
public PredictedCachedTransform(Func<T, U> transform)
{
this.transform = transform;
}
public void Predict(U value)
{
predicted = true;
prediction = value;
}
public U Update(T input)
{
if ((predicted || initialized) && ((input == null && lastInput == null) || (input != null && input.Equals(lastInput))))
return predicted ? prediction : lastOutput;
predicted = false;
initialized = true;
lastInput = input;
lastOutput = transform(input);
return lastOutput;
}
}
}

View File

@@ -15,7 +15,6 @@ using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Orders;
using OpenRA.Mods.Common.Traits;
using OpenRA.Mods.Common.Widgets;
using OpenRA.Primitives;
using OpenRA.Traits;