2020-07-28 12:46:10 +00:00
|
|
|
// Copyright 2020 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2020-11-18 15:51:07 +00:00
|
|
|
import {FocusEvent, SelectionEvent, SelectTimeEvent, SynchronizeSelectionEvent} from '../../events.mjs';
|
|
|
|
import {kChunkHeight, kChunkWidth} from '../../log/map.mjs';
|
|
|
|
import {MapLogEntry} from '../../log/map.mjs';
|
|
|
|
import {CSSColor, DOM, V8CustomElement} from '../helper.mjs';
|
2020-07-28 12:46:10 +00:00
|
|
|
|
2020-10-26 18:02:54 +00:00
|
|
|
const kColors = [
|
|
|
|
CSSColor.green,
|
|
|
|
CSSColor.violet,
|
|
|
|
CSSColor.orange,
|
|
|
|
CSSColor.yellow,
|
|
|
|
CSSColor.primaryColor,
|
|
|
|
CSSColor.red,
|
|
|
|
CSSColor.blue,
|
|
|
|
CSSColor.yellow,
|
|
|
|
CSSColor.secondaryColor,
|
|
|
|
];
|
|
|
|
|
2020-11-18 15:51:07 +00:00
|
|
|
DOM.defineCustomElement('view/timeline/timeline-track',
|
2020-11-03 08:01:33 +00:00
|
|
|
(templateText) =>
|
|
|
|
class TimelineTrack extends V8CustomElement {
|
|
|
|
// TODO turn into static field once Safari supports it.
|
|
|
|
static get SELECTION_OFFSET() {
|
|
|
|
return 10
|
|
|
|
};
|
|
|
|
_timeline;
|
|
|
|
_nofChunks = 400;
|
|
|
|
_chunks;
|
|
|
|
_selectedEntry;
|
|
|
|
_timeToPixel;
|
|
|
|
_timeSelection = {start: -1, end: Infinity};
|
|
|
|
_timeStartOffset;
|
|
|
|
_selectionOriginTime;
|
|
|
|
_typeToColor;
|
2020-11-16 13:19:25 +00:00
|
|
|
|
|
|
|
_entryTypeDoubleClickHandler = this.handleEntryTypeDoubleClick.bind(this);
|
|
|
|
_chunkMouseMoveHandler = this.handleChunkMouseMove.bind(this);
|
|
|
|
_chunkClickHandler = this.handleChunkClick.bind(this);
|
|
|
|
_chunkDoubleClickHandler = this.handleChunkDoubleClick.bind(this);
|
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
constructor() {
|
|
|
|
super(templateText);
|
|
|
|
this.timeline.addEventListener('scroll', e => this.handleTimelineScroll(e));
|
|
|
|
this.timeline.addEventListener(
|
|
|
|
'mousedown', e => this.handleTimeSelectionMouseDown(e));
|
|
|
|
this.timeline.addEventListener(
|
|
|
|
'mouseup', e => this.handleTimeSelectionMouseUp(e));
|
|
|
|
this.timeline.addEventListener(
|
|
|
|
'mousemove', e => this.handleTimeSelectionMouseMove(e));
|
|
|
|
this.isLocked = false;
|
|
|
|
}
|
2020-07-28 12:46:10 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
handleTimeSelectionMouseDown(e) {
|
|
|
|
let xPosition = e.clientX
|
|
|
|
// Update origin time in case we click on a handle.
|
|
|
|
if (this.isOnLeftHandle(xPosition)) {
|
|
|
|
xPosition = this.rightHandlePosX;
|
2020-09-02 06:56:20 +00:00
|
|
|
}
|
2020-11-03 08:01:33 +00:00
|
|
|
else if (this.isOnRightHandle(xPosition)) {
|
|
|
|
xPosition = this.leftHandlePosX;
|
2020-09-02 06:56:20 +00:00
|
|
|
}
|
2020-11-03 08:01:33 +00:00
|
|
|
this._selectionOriginTime = this.positionToTime(xPosition);
|
|
|
|
}
|
2020-10-19 12:39:55 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
isOnLeftHandle(posX) {
|
|
|
|
return (
|
|
|
|
Math.abs(this.leftHandlePosX - posX) <= TimelineTrack.SELECTION_OFFSET);
|
|
|
|
}
|
|
|
|
|
|
|
|
isOnRightHandle(posX) {
|
|
|
|
return (
|
|
|
|
Math.abs(this.rightHandlePosX - posX) <=
|
|
|
|
TimelineTrack.SELECTION_OFFSET);
|
|
|
|
}
|
2020-09-02 06:56:20 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
handleTimeSelectionMouseMove(e) {
|
|
|
|
if (!this._isSelecting) return;
|
|
|
|
const currentTime = this.positionToTime(e.clientX);
|
|
|
|
this.dispatchEvent(new SynchronizeSelectionEvent(
|
2020-10-19 16:55:20 +00:00
|
|
|
Math.min(this._selectionOriginTime, currentTime),
|
|
|
|
Math.max(this._selectionOriginTime, currentTime)));
|
2020-11-03 08:01:33 +00:00
|
|
|
}
|
2020-10-19 12:39:55 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
handleTimeSelectionMouseUp(e) {
|
|
|
|
this._selectionOriginTime = -1;
|
|
|
|
const delta = this._timeSelection.end - this._timeSelection.start;
|
|
|
|
if (delta <= 1 || isNaN(delta)) return;
|
|
|
|
this.dispatchEvent(new SelectTimeEvent(
|
|
|
|
this._timeSelection.start, this._timeSelection.end));
|
|
|
|
}
|
2020-10-19 16:55:20 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
set timeSelection(selection) {
|
|
|
|
this._timeSelection.start = selection.start;
|
|
|
|
this._timeSelection.end = selection.end;
|
|
|
|
this.updateSelection();
|
|
|
|
}
|
2020-09-02 06:56:20 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
get _isSelecting() {
|
|
|
|
return this._selectionOriginTime >= 0;
|
|
|
|
}
|
2020-10-19 16:55:20 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
updateSelection() {
|
|
|
|
const startPosition = this.timeToPosition(this._timeSelection.start);
|
|
|
|
const endPosition = this.timeToPosition(this._timeSelection.end);
|
|
|
|
const delta = endPosition - startPosition;
|
|
|
|
this.leftHandle.style.left = startPosition + 'px';
|
|
|
|
this.selection.style.left = startPosition + 'px';
|
|
|
|
this.rightHandle.style.left = endPosition + 'px';
|
|
|
|
this.selection.style.width = delta + 'px';
|
|
|
|
}
|
2020-09-02 06:56:20 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
get leftHandlePosX() {
|
|
|
|
return this.leftHandle.getBoundingClientRect().x;
|
|
|
|
}
|
2020-10-19 12:39:55 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
get rightHandlePosX() {
|
|
|
|
return this.rightHandle.getBoundingClientRect().x;
|
|
|
|
}
|
2020-09-02 06:56:20 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
// Maps the clicked x position to the x position on timeline canvas
|
|
|
|
positionOnTimeline(posX) {
|
|
|
|
let rect = this.timeline.getBoundingClientRect();
|
|
|
|
let posClickedX = posX - rect.left + this.timeline.scrollLeft;
|
|
|
|
return posClickedX;
|
|
|
|
}
|
2020-09-02 06:56:20 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
positionToTime(posX) {
|
|
|
|
let posTimelineX = this.positionOnTimeline(posX) + this._timeStartOffset;
|
|
|
|
return posTimelineX / this._timeToPixel;
|
|
|
|
}
|
2020-09-02 06:56:20 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
timeToPosition(time) {
|
|
|
|
let posX = time * this._timeToPixel;
|
|
|
|
posX -= this._timeStartOffset
|
|
|
|
return posX;
|
|
|
|
}
|
2020-09-02 06:56:20 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
get leftHandle() {
|
|
|
|
return this.$('.leftHandle');
|
|
|
|
}
|
2020-10-19 12:39:55 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
get rightHandle() {
|
|
|
|
return this.$('.rightHandle');
|
|
|
|
}
|
2020-10-19 12:39:55 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
get selection() {
|
|
|
|
return this.$('.selection');
|
|
|
|
}
|
2020-09-02 06:56:20 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
get timelineCanvas() {
|
|
|
|
return this.$('#timelineCanvas');
|
|
|
|
}
|
2020-07-28 12:46:10 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
get timelineChunks() {
|
|
|
|
return this.$('#timelineChunks');
|
|
|
|
}
|
2020-07-28 12:46:10 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
get timeline() {
|
|
|
|
return this.$('#timeline');
|
|
|
|
}
|
2020-07-28 12:46:10 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
get timelineLegend() {
|
|
|
|
return this.$('#legend');
|
|
|
|
}
|
2020-07-28 12:46:10 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
get timelineLegendContent() {
|
|
|
|
return this.$('#legendContent');
|
|
|
|
}
|
2020-11-02 09:27:28 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
set data(value) {
|
|
|
|
this._timeline = value;
|
|
|
|
this._resetTypeToColorCache();
|
2020-11-16 13:19:25 +00:00
|
|
|
// Only update legend if the timeline data has changed.
|
|
|
|
this._updateLegend();
|
|
|
|
this._updateChunks();
|
2020-11-10 13:21:40 +00:00
|
|
|
this.update();
|
|
|
|
}
|
|
|
|
|
|
|
|
_update() {
|
|
|
|
this._updateTimeline();
|
2020-11-03 08:01:33 +00:00
|
|
|
}
|
2020-07-28 12:46:10 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
_resetTypeToColorCache() {
|
|
|
|
this._typeToColor = new Map();
|
|
|
|
let lastIndex = 0;
|
|
|
|
for (const type of this.data.uniqueTypes.keys()) {
|
|
|
|
this._typeToColor.set(type, kColors[lastIndex++]);
|
2020-10-19 12:39:55 +00:00
|
|
|
}
|
2020-11-03 08:01:33 +00:00
|
|
|
}
|
2020-10-19 12:39:55 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
get data() {
|
|
|
|
return this._timeline;
|
|
|
|
}
|
2020-07-28 12:46:10 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
set nofChunks(count) {
|
|
|
|
this._nofChunks = count;
|
2020-11-16 13:19:25 +00:00
|
|
|
this._updateChunks();
|
2020-11-10 13:21:40 +00:00
|
|
|
this.update();
|
2020-11-03 08:01:33 +00:00
|
|
|
}
|
2020-10-19 12:39:55 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
get nofChunks() {
|
|
|
|
return this._nofChunks;
|
|
|
|
}
|
2020-10-19 12:39:55 +00:00
|
|
|
|
2020-11-10 13:21:40 +00:00
|
|
|
_updateChunks() {
|
2020-11-03 08:01:33 +00:00
|
|
|
this._chunks = this.data.chunks(this.nofChunks);
|
|
|
|
}
|
2020-10-19 12:39:55 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
get chunks() {
|
|
|
|
return this._chunks;
|
|
|
|
}
|
2020-10-19 12:39:55 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
set selectedEntry(value) {
|
|
|
|
this._selectedEntry = value;
|
|
|
|
if (value.edge) this.redraw();
|
|
|
|
}
|
2020-10-19 12:39:55 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
get selectedEntry() {
|
|
|
|
return this._selectedEntry;
|
|
|
|
}
|
2020-07-28 12:46:10 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
set scrollLeft(offset) {
|
|
|
|
this.timeline.scrollLeft = offset;
|
|
|
|
}
|
2020-07-28 12:46:10 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
typeToColor(type) {
|
|
|
|
return this._typeToColor.get(type);
|
|
|
|
}
|
2020-10-19 12:39:55 +00:00
|
|
|
|
2020-11-16 13:19:25 +00:00
|
|
|
_updateLegend() {
|
2020-11-03 08:01:33 +00:00
|
|
|
let timelineLegendContent = this.timelineLegendContent;
|
|
|
|
DOM.removeAllChildren(timelineLegendContent);
|
|
|
|
this._timeline.uniqueTypes.forEach((entries, type) => {
|
|
|
|
let row = DOM.tr('clickable');
|
|
|
|
row.entries = entries;
|
2020-11-16 13:19:25 +00:00
|
|
|
row.ondblclick = this.entryTypeDoubleClickHandler_;
|
2020-11-03 08:01:33 +00:00
|
|
|
let color = this.typeToColor(type);
|
|
|
|
if (color !== null) {
|
|
|
|
let div = DOM.div('colorbox');
|
|
|
|
div.style.backgroundColor = color;
|
|
|
|
row.appendChild(DOM.td(div));
|
|
|
|
} else {
|
|
|
|
row.appendChild(DOM.td());
|
|
|
|
}
|
|
|
|
let td = DOM.td(type);
|
|
|
|
row.appendChild(td);
|
|
|
|
row.appendChild(DOM.td(entries.length));
|
|
|
|
let percent = (entries.length / this.data.all.length) * 100;
|
|
|
|
row.appendChild(DOM.td(percent.toFixed(1) + '%'));
|
2020-10-19 10:13:33 +00:00
|
|
|
timelineLegendContent.appendChild(row);
|
2020-11-03 08:01:33 +00:00
|
|
|
});
|
|
|
|
// Add Total row.
|
|
|
|
let row = DOM.tr();
|
|
|
|
row.appendChild(DOM.td(''));
|
|
|
|
row.appendChild(DOM.td('All'));
|
|
|
|
row.appendChild(DOM.td(this.data.all.length));
|
|
|
|
row.appendChild(DOM.td('100%'));
|
|
|
|
timelineLegendContent.appendChild(row);
|
2020-11-10 11:47:40 +00:00
|
|
|
this.timelineLegend.appendChild(timelineLegendContent);
|
2020-11-03 08:01:33 +00:00
|
|
|
}
|
2020-07-28 12:46:10 +00:00
|
|
|
|
2020-11-16 13:19:25 +00:00
|
|
|
handleEntryTypeDoubleClick(e) {
|
2020-11-03 08:01:33 +00:00
|
|
|
this.dispatchEvent(new SelectionEvent(e.target.parentNode.entries));
|
|
|
|
}
|
2020-08-04 09:16:41 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
timelineIndicatorMove(offset) {
|
|
|
|
this.timeline.scrollLeft += offset;
|
|
|
|
}
|
2020-07-28 12:46:10 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
handleTimelineScroll(e) {
|
|
|
|
let horizontal = e.currentTarget.scrollLeft;
|
|
|
|
this.dispatchEvent(new CustomEvent(
|
|
|
|
'scrolltrack', {bubbles: true, composed: true, detail: horizontal}));
|
|
|
|
}
|
2020-08-06 04:49:39 +00:00
|
|
|
|
2020-11-16 13:19:25 +00:00
|
|
|
_createBackgroundImage(chunk) {
|
2020-11-03 08:01:33 +00:00
|
|
|
// Render the types of transitions as bar charts
|
|
|
|
const kHeight = chunk.height;
|
2020-11-16 13:19:25 +00:00
|
|
|
const total = chunk.size();
|
|
|
|
let increment = 0;
|
|
|
|
let lastHeight = 0.0;
|
|
|
|
const stops = [];
|
|
|
|
const breakDown = chunk.getBreakdown(map => map.type);
|
|
|
|
for (let i = 0; i < breakDown.length; i++) {
|
|
|
|
let [type, count] = breakDown[i];
|
|
|
|
const color = this.typeToColor(type);
|
|
|
|
increment += count;
|
|
|
|
let height = (increment / total * kHeight) | 0;
|
|
|
|
stops.push(`${color} ${lastHeight}px ${height}px`)
|
|
|
|
lastHeight = height;
|
2020-07-28 12:46:10 +00:00
|
|
|
}
|
2020-11-16 13:19:25 +00:00
|
|
|
return `linear-gradient(0deg,${stops.join(',')})`;
|
2020-11-03 08:01:33 +00:00
|
|
|
}
|
2020-07-28 12:46:10 +00:00
|
|
|
|
2020-11-10 13:21:40 +00:00
|
|
|
_updateTimeline() {
|
2020-11-16 13:19:25 +00:00
|
|
|
const reusableNodes = Array.from(this.timelineChunks.childNodes).reverse();
|
|
|
|
let fragment = new DocumentFragment();
|
2020-11-03 08:01:33 +00:00
|
|
|
let chunks = this.chunks;
|
|
|
|
let max = chunks.max(each => each.size());
|
|
|
|
let start = this.data.startTime;
|
|
|
|
let end = this.data.endTime;
|
|
|
|
let duration = end - start;
|
|
|
|
this._timeToPixel = chunks.length * kChunkWidth / duration;
|
|
|
|
this._timeStartOffset = start * this._timeToPixel;
|
|
|
|
for (let i = 0; i < chunks.length; i++) {
|
|
|
|
let chunk = chunks[i];
|
|
|
|
let height = (chunk.size() / max * kChunkHeight);
|
|
|
|
chunk.height = height;
|
|
|
|
if (chunk.isEmpty()) continue;
|
2020-11-16 13:19:25 +00:00
|
|
|
let node = reusableNodes[reusableNodes.length - 1];
|
|
|
|
let reusedNode = false;
|
|
|
|
if (node?.className == 'chunk') {
|
|
|
|
reusableNodes.pop();
|
|
|
|
reusedNode = true;
|
|
|
|
} else {
|
|
|
|
node = DOM.div('chunk');
|
|
|
|
node.onmousemove = this._chunkMouseMoveHandler;
|
|
|
|
node.onclick = this._chunkClickHandler;
|
|
|
|
node.ondblclick = this.chunkDoubleClickHandler;
|
|
|
|
}
|
|
|
|
const style = node.style;
|
|
|
|
style.left = `${((chunk.start - start) * this._timeToPixel) | 0}px`;
|
|
|
|
style.height = `${height | 0}px`;
|
|
|
|
style.backgroundImage = this._createBackgroundImage(chunk);
|
2020-11-03 08:01:33 +00:00
|
|
|
node.chunk = chunk;
|
2020-11-16 13:19:25 +00:00
|
|
|
if (!reusedNode) fragment.appendChild(node);
|
2020-11-03 08:01:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Put a time marker roughly every 20 chunks.
|
|
|
|
let expected = duration / chunks.length * 20;
|
|
|
|
let interval = (10 ** Math.floor(Math.log10(expected)));
|
|
|
|
let correction = Math.log10(expected / interval);
|
|
|
|
correction = (correction < 0.33) ? 1 : (correction < 0.75) ? 2.5 : 5;
|
|
|
|
interval *= correction;
|
|
|
|
|
|
|
|
let time = start;
|
|
|
|
while (time < end) {
|
2020-11-16 13:19:25 +00:00
|
|
|
let timeNode = DOM.div('timestamp');
|
|
|
|
timeNode.innerText = `${((time - start) / 1000) | 0} ms`;
|
|
|
|
timeNode.style.left = `${((time - start) * this._timeToPixel) | 0}px`;
|
|
|
|
fragment.appendChild(timeNode);
|
2020-11-03 08:01:33 +00:00
|
|
|
time += interval;
|
|
|
|
}
|
2020-11-16 13:19:25 +00:00
|
|
|
|
|
|
|
// Remove superfluos nodes lazily, for Chrome this is a very expensive
|
|
|
|
// operation.
|
|
|
|
if (reusableNodes.length > 0) {
|
|
|
|
for (const node of reusableNodes) {
|
|
|
|
node.style.display = 'none';
|
|
|
|
}
|
|
|
|
setTimeout(() => {
|
|
|
|
const range = document.createRange();
|
|
|
|
const first = reusableNodes[reusableNodes.length - 1];
|
|
|
|
const last = reusableNodes[0];
|
|
|
|
range.setStartBefore(first);
|
|
|
|
range.setEndAfter(last);
|
|
|
|
range.deleteContents();
|
|
|
|
}, 100);
|
|
|
|
}
|
|
|
|
this.timelineChunks.appendChild(fragment);
|
2020-11-03 08:01:33 +00:00
|
|
|
this.redraw();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleChunkMouseMove(event) {
|
|
|
|
if (this.isLocked) return false;
|
|
|
|
if (this._isSelecting) return false;
|
|
|
|
let chunk = event.target.chunk;
|
|
|
|
if (!chunk) return;
|
|
|
|
// topmost map (at chunk.height) == map #0.
|
|
|
|
let relativeIndex =
|
2020-08-13 09:14:29 +00:00
|
|
|
Math.round(event.layerY / event.target.offsetHeight * chunk.size());
|
2020-11-03 08:01:33 +00:00
|
|
|
let map = chunk.at(relativeIndex);
|
|
|
|
this.dispatchEvent(new FocusEvent(map));
|
|
|
|
}
|
2020-07-28 12:46:10 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
handleChunkClick(event) {
|
|
|
|
this.isLocked = !this.isLocked;
|
|
|
|
}
|
2020-07-28 12:46:10 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
handleChunkDoubleClick(event) {
|
|
|
|
let chunk = event.target.chunk;
|
|
|
|
if (!chunk) return;
|
|
|
|
this.dispatchEvent(new SelectTimeEvent(chunk.start, chunk.end));
|
|
|
|
}
|
2020-07-28 12:46:10 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
redraw() {
|
2020-11-16 13:19:25 +00:00
|
|
|
window.requestAnimationFrame(() => this._redraw());
|
|
|
|
}
|
|
|
|
|
|
|
|
_redraw() {
|
|
|
|
if (!(this._timeline.at(0) instanceof MapLogEntry)) return;
|
2020-11-03 08:01:33 +00:00
|
|
|
let canvas = this.timelineCanvas;
|
2020-11-16 13:19:25 +00:00
|
|
|
let width = (this.chunks.length + 1) * kChunkWidth;
|
|
|
|
if (width > 32767) width = 32767;
|
|
|
|
canvas.width = width;
|
2020-11-03 08:01:33 +00:00
|
|
|
canvas.height = kChunkHeight;
|
|
|
|
let ctx = canvas.getContext('2d');
|
|
|
|
ctx.clearRect(0, 0, canvas.width, kChunkHeight);
|
|
|
|
if (!this.selectedEntry || !this.selectedEntry.edge) return;
|
|
|
|
this.drawEdges(ctx);
|
|
|
|
}
|
|
|
|
setMapStyle(map, ctx) {
|
|
|
|
ctx.fillStyle = map.edge && map.edge.from ? CSSColor.onBackgroundColor :
|
|
|
|
CSSColor.onPrimaryColor;
|
|
|
|
}
|
2020-07-28 12:46:10 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
setEdgeStyle(edge, ctx) {
|
|
|
|
let color = this.typeToColor(edge.type);
|
|
|
|
ctx.strokeStyle = color;
|
|
|
|
ctx.fillStyle = color;
|
|
|
|
}
|
2020-07-28 12:46:10 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
markMap(ctx, map) {
|
|
|
|
let [x, y] = map.position(this.chunks);
|
|
|
|
ctx.beginPath();
|
|
|
|
this.setMapStyle(map, ctx);
|
|
|
|
ctx.arc(x, y, 3, 0, 2 * Math.PI);
|
|
|
|
ctx.fill();
|
|
|
|
ctx.beginPath();
|
|
|
|
ctx.fillStyle = CSSColor.onBackgroundColor;
|
|
|
|
ctx.arc(x, y, 2, 0, 2 * Math.PI);
|
|
|
|
ctx.fill();
|
|
|
|
}
|
2020-07-28 12:46:10 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
markSelectedMap(ctx, map) {
|
|
|
|
let [x, y] = map.position(this.chunks);
|
|
|
|
ctx.beginPath();
|
|
|
|
this.setMapStyle(map, ctx);
|
|
|
|
ctx.arc(x, y, 6, 0, 2 * Math.PI);
|
|
|
|
ctx.strokeStyle = CSSColor.onBackgroundColor;
|
|
|
|
ctx.stroke();
|
|
|
|
}
|
2020-07-28 12:46:10 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
drawEdges(ctx) {
|
|
|
|
// Draw the trace of maps in reverse order to make sure the outgoing
|
|
|
|
// transitions of previous maps aren't drawn over.
|
|
|
|
const kMaxOutgoingEdges = 100;
|
|
|
|
let nofEdges = 0;
|
|
|
|
let stack = [];
|
|
|
|
let current = this.selectedEntry;
|
|
|
|
while (current && nofEdges < kMaxOutgoingEdges) {
|
|
|
|
nofEdges += current.children.length;
|
|
|
|
stack.push(current);
|
|
|
|
current = current.parent();
|
|
|
|
}
|
|
|
|
ctx.save();
|
|
|
|
this.drawOutgoingEdges(ctx, this.selectedEntry, 3);
|
|
|
|
ctx.restore();
|
|
|
|
|
|
|
|
let labelOffset = 15;
|
|
|
|
let xPrev = 0;
|
|
|
|
while (current = stack.pop()) {
|
|
|
|
if (current.edge) {
|
|
|
|
this.setEdgeStyle(current.edge, ctx);
|
|
|
|
let [xTo, yTo] = this.drawEdge(ctx, current.edge, true, labelOffset);
|
|
|
|
if (xTo == xPrev) {
|
|
|
|
labelOffset += 8;
|
|
|
|
} else {
|
|
|
|
labelOffset = 15
|
|
|
|
}
|
|
|
|
xPrev = xTo;
|
2020-07-28 12:46:10 +00:00
|
|
|
}
|
2020-11-03 08:01:33 +00:00
|
|
|
this.markMap(ctx, current);
|
|
|
|
current = current.parent();
|
2020-07-28 12:46:10 +00:00
|
|
|
ctx.save();
|
2020-11-03 08:01:33 +00:00
|
|
|
// this.drawOutgoingEdges(ctx, current, 1);
|
2020-07-28 12:46:10 +00:00
|
|
|
ctx.restore();
|
|
|
|
}
|
2020-11-03 08:01:33 +00:00
|
|
|
// Mark selected map
|
|
|
|
this.markSelectedMap(ctx, this.selectedEntry);
|
|
|
|
}
|
2020-07-28 12:46:10 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
drawEdge(ctx, edge, showLabel = true, labelOffset = 20) {
|
|
|
|
if (!edge.from || !edge.to) return [-1, -1];
|
|
|
|
let [xFrom, yFrom] = edge.from.position(this.chunks);
|
|
|
|
let [xTo, yTo] = edge.to.position(this.chunks);
|
|
|
|
let sameChunk = xTo == xFrom;
|
|
|
|
if (sameChunk) labelOffset += 8;
|
|
|
|
|
|
|
|
ctx.beginPath();
|
|
|
|
ctx.moveTo(xFrom, yFrom);
|
|
|
|
let offsetX = 20;
|
|
|
|
let offsetY = 20;
|
|
|
|
let midX = xFrom + (xTo - xFrom) / 2;
|
|
|
|
let midY = (yFrom + yTo) / 2 - 100;
|
|
|
|
if (!sameChunk) {
|
|
|
|
ctx.quadraticCurveTo(midX, midY, xTo, yTo);
|
|
|
|
} else {
|
|
|
|
ctx.lineTo(xTo, yTo);
|
|
|
|
}
|
|
|
|
if (!showLabel) {
|
|
|
|
ctx.stroke();
|
|
|
|
} else {
|
|
|
|
let centerX, centerY;
|
2020-07-28 12:46:10 +00:00
|
|
|
if (!sameChunk) {
|
2020-11-03 08:01:33 +00:00
|
|
|
centerX = (xFrom / 2 + midX + xTo / 2) / 2;
|
|
|
|
centerY = (yFrom / 2 + midY + yTo / 2) / 2;
|
2020-07-28 12:46:10 +00:00
|
|
|
} else {
|
2020-11-03 08:01:33 +00:00
|
|
|
centerX = xTo;
|
|
|
|
centerY = yTo;
|
2020-07-28 12:46:10 +00:00
|
|
|
}
|
2020-11-03 08:01:33 +00:00
|
|
|
ctx.moveTo(centerX, centerY);
|
|
|
|
ctx.lineTo(centerX + offsetX, centerY - labelOffset);
|
|
|
|
ctx.stroke();
|
|
|
|
ctx.textAlign = 'left';
|
|
|
|
ctx.fillStyle = this.typeToColor(edge.type);
|
|
|
|
ctx.fillText(
|
2020-08-13 09:14:29 +00:00
|
|
|
edge.toString(), centerX + offsetX + 2, centerY - labelOffset);
|
2020-07-28 12:46:10 +00:00
|
|
|
}
|
2020-11-03 08:01:33 +00:00
|
|
|
return [xTo, yTo];
|
|
|
|
}
|
2020-07-28 12:46:10 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
drawOutgoingEdges(ctx, map, max = 10, depth = 0) {
|
|
|
|
if (!map) return;
|
|
|
|
if (depth >= max) return;
|
|
|
|
ctx.globalAlpha = 0.5 - depth * (0.3 / max);
|
|
|
|
ctx.strokeStyle = CSSColor.timelineBackgroundColor;
|
|
|
|
const limit = Math.min(map.children.length, 100)
|
|
|
|
for (let i = 0; i < limit; i++) {
|
|
|
|
let edge = map.children[i];
|
|
|
|
this.drawEdge(ctx, edge, true);
|
|
|
|
this.drawOutgoingEdges(ctx, edge.to, max, depth + 1);
|
2020-07-28 12:46:10 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-03 08:01:33 +00:00
|
|
|
});
|