Skip to content

Commit

Permalink
Animate red flash for blocking pause dialogs (#430)
Browse files Browse the repository at this point in the history
Co-authored-by: SaberShip <[email protected]>
  • Loading branch information
SaberShip and SaberShip authored Apr 5, 2024
1 parent 58c6735 commit d77273d
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion Source/Client/AsyncTime/TimeControlUI.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using HarmonyLib;
Expand Down Expand Up @@ -283,6 +283,10 @@ public static class ColonistBarTimeControl
public static float btnWidth = TimeControls.TimeButSize.x;
public static float btnHeight = TimeControls.TimeButSize.y;

private static Dictionary<Vector2, float> flashDict = new Dictionary<Vector2, float>();
private static float flashInterval = 6f;
private static Color flashColor = Color.red;

static void Prefix(ref bool __state)
{
if (Event.current.type is EventType.MouseDown or EventType.MouseUp)
Expand Down Expand Up @@ -316,6 +320,7 @@ static void DrawButtons()
Rect groupBar = bar.drawer.GroupFrameRect(entry.group);
float drawXPos = groupBar.x;
Color bgColor = (entryTickable.ActualRateMultiplier(TimeSpeed.Normal) == 0f) ? pauseBgColor : normalBgColor;
Vector2 flashPos = new Vector2(drawXPos + btnWidth / 2, groupBar.yMax + btnHeight / 2);

if (Multiplayer.GameComp.asyncTime)
{
Expand All @@ -337,6 +342,24 @@ static void DrawButtons()
Rect button = new Rect(drawXPos, groupBar.yMax, btnWidth, btnHeight);
MpTimeControls.TimeIndicateBlockingPause(button, bgColor);
drawXPos += TimeControls.TimeButSize.x;

float pauseTime = flashDict.GetValueOrDefault(flashPos);
if (flashDict.ContainsKey(flashPos))
{
// Draw flash for ongoing blocking pause
DrawPauseFlash(flashPos);
}
else
{
// There is a new blocking pause
flashDict.Add(flashPos, Time.time);
}
}

if (entryTickable.ActualRateMultiplier(TimeSpeed.Normal) != 0f && flashDict.ContainsKey(flashPos))
{
// No longer any blocking pauses, stop flashing here
flashDict.Remove(flashPos);
}

List<FloatMenuOption> options = GetBlockingWindowOptions(entry, entryTickable);
Expand Down Expand Up @@ -375,6 +398,18 @@ static void SwitchToMapOrWorld(Map map)
Current.Game.CurrentMap = map;
}
}

static void DrawPauseFlash(Vector2 pos)
{
float pauseTime = flashDict.GetValueOrDefault(pos);
float pauseDuration = Time.time - pauseTime;

// Only flash at flashInterval from the time the blocking pause began
if (pauseDuration > 0f && pauseDuration % flashInterval < 1f)
{
GenUI.DrawFlash(pos.x, pos.y, UI.screenWidth * 0.6f, Pulser.PulseBrightness(1f, 1f, pauseDuration) * 0.4f, flashColor);
}
}
}

[HarmonyPatch(typeof(MainButtonWorker), nameof(MainButtonWorker.DoButton))]
Expand Down

0 comments on commit d77273d

Please sign in to comment.