Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

raidboss: fix inconsistent "soon" timeline colors #5576

Merged
merged 2 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 48 additions & 40 deletions ui/raidboss/html_timeline_ui.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Lang, langToLocale } from '../../resources/languages';
import { UnreachableCode } from '../../resources/not_reached';
import TimerBar from '../../resources/timerbar';
import { LooseTimelineTrigger } from '../../types/trigger';

Expand Down Expand Up @@ -72,6 +73,12 @@ const computeBackgroundFrom = (element: HTMLElement, classList: string): string
return color;
};

export type ActiveBar = {
bar: TimerBar;
soonTimeout?: number;
expireTimeout?: number;
};

export class HTMLTimelineUI extends TimelineUI {
private init = false;
private lang: Lang;
Expand All @@ -81,8 +88,7 @@ export class HTMLTimelineUI extends TimelineUI {
private barExpiresSoonColor: string | null = null;
private timerlist: HTMLElement | null = null;

private activeBars: { [activebar: string]: TimerBar } = {};
private expireTimers: { [expireTimer: string]: number } = {};
private activeBars: { [activebar: string]: ActiveBar } = {};

private debugElement: HTMLElement | null = null;
private debugFightTimer: TimerBar | null = null;
Expand Down Expand Up @@ -120,7 +126,6 @@ export class HTMLTimelineUI extends TimelineUI {
}

this.activeBars = {};
this.expireTimers = {};
}

protected override AddDebugInstructions(): void {
Expand Down Expand Up @@ -167,6 +172,7 @@ export class HTMLTimelineUI extends TimelineUI {
if (this.debugElement)
this.debugElement.innerHTML = '';
this.debugFightTimer = null;
// TODO: clear all timeouts?
this.activeBars = {};
}
}
Expand All @@ -186,71 +192,73 @@ export class HTMLTimelineUI extends TimelineUI {
if (e.style)
bar.applyStyles(e.style);

// Adding a timer with the same id immediately removes the previous.
const activeBar = this.activeBars[e.id];
if (activeBar) {
const parentDiv = activeBar.bar.parentNode;
parentDiv?.parentNode?.removeChild(parentDiv);
// Expiry timeout must be cleared so that it will not remove this new bar.
if (activeBar.expireTimeout !== undefined) {
window.clearTimeout(activeBar.expireTimeout);
activeBar.expireTimeout = undefined;
}
// Soon timeout is just an optimization to remove, as it's unnecessary.
if (activeBar.soonTimeout !== undefined) {
window.clearTimeout(activeBar.soonTimeout);
activeBar.soonTimeout = undefined;
}
}

let soonTimeout: number | undefined = undefined;
if (!channeling && e.time - fightNow > this.options.BarExpiresSoonSeconds) {
bar.fg = this.barColor;
window.setTimeout(
this.OnTimerExpiresSoon.bind(this, e.id),
soonTimeout = window.setTimeout(
() => bar.fg = this.barExpiresSoonColor,
(e.time - fightNow - this.options.BarExpiresSoonSeconds) * 1000,
);
} else {
bar.fg = this.barExpiresSoonColor;
}

// Adding a timer with the same id immediately removes the previous.
const activeBar = this.activeBars[e.id];
if (activeBar) {
const div = activeBar.parentNode;
div?.parentNode?.removeChild(div);
}

if (e.sortKey)
div.style.order = e.sortKey.toString();
div.id = e.id.toString();
this.timerlist?.appendChild(div);
this.activeBars[e.id] = bar;
if (e.id in this.expireTimers) {
window.clearTimeout(this.expireTimers[e.id]);
delete this.expireTimers[e.id];
}
}

public override OnTimerExpiresSoon(id: number): void {
const bar = this.activeBars[id];
if (bar)
bar.fg = this.barExpiresSoonColor;
this.activeBars[e.id] = {
bar: bar,
soonTimeout: soonTimeout,
};
}

public override OnRemoveTimer(e: Event, expired: boolean, force = false): void {
const activeBar = this.activeBars[e.id];
if (!activeBar)
return;

if (activeBar.expireTimeout !== undefined)
window.clearTimeout(activeBar.expireTimeout);

if (!force && expired && this.options.KeepExpiredTimerBarsForSeconds) {
this.expireTimers[e.id] = window.setTimeout(
this.OnRemoveTimer.bind(this, e, false),
activeBar.expireTimeout = window.setTimeout(
() => this.OnRemoveTimer(e, false),
this.options.KeepExpiredTimerBarsForSeconds * 1000,
);
return;
} else if (e.id in this.expireTimers) {
window.clearTimeout(this.expireTimers[e.id]);
delete this.expireTimers[e.id];
}

const bar = this.activeBars[e.id];
if (!bar)
return;

const div = bar.parentNode;
const element = document.getElementById(e.id.toString());
if (!element)
return;
const div = activeBar.bar.parentNode;
if (!(div instanceof HTMLElement))
throw new UnreachableCode();

const removeBar = () => {
div?.parentNode?.removeChild(div);
delete this.activeBars[e.id];
};

if (!force)
element.classList.add('animate-timer-bar-removed');
if (window.getComputedStyle(element).animationName !== 'none') {
div.classList.add('animate-timer-bar-removed');
if (window.getComputedStyle(div).animationName !== 'none') {
// Wait for animation to finish
element.addEventListener('animationend', removeBar);
div.addEventListener('animationend', removeBar);
} else {
removeBar();
}
Expand Down
4 changes: 0 additions & 4 deletions ui/raidboss/timeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ export class TimelineUI {
/* noop */
}

public OnTimerExpiresSoon(_id: number): void {
/* noop */
}

public OnRemoveTimer(_e: Event, _expired: boolean, _force = false): void {
/* noop */
}
Expand Down