From c6173c03179c40441a7fa0dc355e9907bbcde22f Mon Sep 17 00:00:00 2001 From: Vice <67132971+viceversagames@users.noreply.github.com> Date: Sun, 24 Apr 2022 22:44:37 -0400 Subject: [PATCH 1/2] Add MoveUI RPC with easing --- CommunityEntity.UI.cs | 103 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) diff --git a/CommunityEntity.UI.cs b/CommunityEntity.UI.cs index 3bef59f..54f615e 100644 --- a/CommunityEntity.UI.cs +++ b/CommunityEntity.UI.cs @@ -40,6 +40,109 @@ private static void RegisterUi( GameObject go ) AllUi.Add( go ); UiDict[go.name] = go; } + + + [RPC_Client] + public void MoveUI( RpcMessage msg ) + { + var str = msg.read.StringRaw(); + + if (string.IsNullOrEmpty(str)) return; + + var jsonArray = JSON.Array.Parse( str ); + if (jsonArray == null) return; + + foreach (var value in jsonArray) + { + var json = value.Obj; + var parentPanel = json.GetString("name", "Overlay"); + + // break if existing panel cannot be found, name is null, or is not registered + GameObject go; + if (string.IsNullOrEmpty(parentPanel) || !UiDict.TryGetValue(parentPanel, out go)) + { + return; + } + + StartCoroutine( + MoveUIRoutine(go, + json.GetFloat("duration", 0f), + json.GetString("easing", null), + Vector2Ex.Parse( json.GetString( "anchormin", "0.0 0.0" ) ), + Vector2Ex.Parse( json.GetString( "anchormax", "1.0 1.0" ) ), + Vector2Ex.Parse( json.GetString( "offsetmin", "0.0 0.0" ) ), + Vector2Ex.Parse( json.GetString( "offsetmax", "1.0 1.0" ) )) + ); + + } + } + + + private IEnumerator MoveUIRoutine(GameObject go, float lerpDuration, string easing, Vector2 anchorMin, Vector2 anchorMax, Vector2 offsetMin, Vector2 offsetMax) + { + var rt = go.GetComponent(); + if (!rt) + yield break; + + Vector2 fromAnchorMin = rt.anchorMin; + Vector2 fromAnchorMax = rt.anchorMax; + Vector2 fromOffsetMin = rt.offsetMin; + Vector2 fromOffsetMax = rt.offsetMax; + + if (lerpDuration > 0) + { + float time = 0; + while (time < lerpDuration) + { + time += Time.deltaTime; + float lerpInterval = time / lerpDuration; + + switch (easing) + { + case "in-out": + lerpInterval = EaseInOut(lerpInterval); + break; + case "out": + lerpInterval = EaseOut(lerpInterval); + break; + case "in": + lerpInterval = EaseIn(lerpInterval); + break; + } + + rt.anchorMin = Vector2.Lerp(fromAnchorMin, anchorMin, lerpInterval); + rt.anchorMax = Vector2.Lerp(fromAnchorMax, anchorMax, lerpInterval); + rt.offsetMin = Vector2.Lerp(fromOffsetMin, offsetMin, lerpInterval); + rt.offsetMax = Vector2.Lerp(fromOffsetMax, offsetMax, lerpInterval); + yield return null; + } + } + + rt.anchorMin = anchorMin; + rt.anchorMax = anchorMax; + rt.offsetMin = offsetMin; + rt.offsetMax = offsetMax; + } + + private float Flip(float f) + { + return 1 - f; + } + + private float EaseIn(float f) + { + return f * f; + } + + private float EaseOut(float f) + { + return Flip(EaseIn(Flip(f))); + } + + private float EaseInOut(float f) + { + return Mathf.Lerp(EaseIn(f), EaseOut(f), f); + } [RPC_Client] public void AddUI( RPCMessage msg ) From 0aa2282e143126d09713a77bb8560c3596840b20 Mon Sep 17 00:00:00 2001 From: Vice <67132971+viceversagames@users.noreply.github.com> Date: Sun, 24 Apr 2022 22:51:29 -0400 Subject: [PATCH 2/2] Clean up flip function --- CommunityEntity.UI.cs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/CommunityEntity.UI.cs b/CommunityEntity.UI.cs index 54f615e..95d456f 100644 --- a/CommunityEntity.UI.cs +++ b/CommunityEntity.UI.cs @@ -124,11 +124,6 @@ private IEnumerator MoveUIRoutine(GameObject go, float lerpDuration, string easi rt.offsetMax = offsetMax; } - private float Flip(float f) - { - return 1 - f; - } - private float EaseIn(float f) { return f * f; @@ -136,7 +131,7 @@ private float EaseIn(float f) private float EaseOut(float f) { - return Flip(EaseIn(Flip(f))); + return 1 - EaseIn(1 - f); } private float EaseInOut(float f)