summaryrefslogtreecommitdiff
path: root/tools/timeline_editor/timeline-viewport.js
blob: becf8e92f25f9826089f77d933b2e60d0348d534 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// timeline-viewport.js - Viewport zoom/scroll control

export class ViewportController {
    constructor(state, dom, renderCallback) {
        this.state = state;
        this.dom = dom;
        this.renderCallback = renderCallback;

        // Constants
        this.TIMELINE_LEFT_PADDING = 20;
        this.SCROLL_VIEWPORT_FRACTION = 0.4;
        this.SMOOTH_SCROLL_SPEED = 0.1;
        this.VERTICAL_SCROLL_SPEED = 0.3;

        this.init();
    }

    init() {
        // Zoom controls
        this.dom.zoomSlider.addEventListener('input', e => this.handleZoomSlider(e));

        // Scroll sync
        this.dom.timelineContent.addEventListener('scroll', () => this.handleScroll());

        // Wheel handling
        const wheelHandler = e => this.handleWheel(e);
        this.dom.timelineContent.addEventListener('wheel', wheelHandler, { passive: false });
        this.dom.waveformContainer.addEventListener('wheel', wheelHandler, { passive: false });

        // Prevent wheel bubbling from UI containers
        document.querySelector('header').addEventListener('wheel', e => e.stopPropagation());
        this.dom.propertiesPanel.addEventListener('wheel', e => e.stopPropagation());
        document.querySelector('.zoom-controls').addEventListener('wheel', e => e.stopPropagation());
        document.querySelector('.stats').addEventListener('wheel', e => e.stopPropagation());
    }

    handleZoomSlider(e) {
        this.state.pixelsPerSecond = parseInt(e.target.value);
        this.dom.zoomLevel.textContent = `${this.state.pixelsPerSecond}%`;
        this.renderCallback('zoom');
    }

    handleScroll() {
        const scrollLeft = this.dom.timelineContent.scrollLeft;
        this.dom.cpuLoadCanvas.style.left = `-${scrollLeft}px`;
        this.dom.waveformCanvas.style.left = `-${scrollLeft}px`;
        document.getElementById('timeMarkers').style.transform = `translateX(-${scrollLeft}px)`;
        this.updateIndicatorPosition(this.timeToBeats(this.state.playbackOffset), false);
    }

    handleWheel(e) {
        e.preventDefault();

        // Zoom with ctrl/cmd
        if (e.ctrlKey || e.metaKey) {
            this.handleZoomWheel(e);
            return;
        }

        // Horizontal scroll
        this.dom.timelineContent.scrollLeft += e.deltaY;

        // Auto-scroll to active sequence
        this.autoScrollToSequence();
    }

    handleZoomWheel(e) {
        const rect = this.dom.timelineContent.getBoundingClientRect();
        const mouseX = e.clientX - rect.left;
        const scrollLeft = this.dom.timelineContent.scrollLeft;
        const timeUnderCursor = (scrollLeft + mouseX) / this.state.pixelsPerSecond;

        const zoomDelta = e.deltaY > 0 ? -10 : 10;
        const newPixelsPerSecond = Math.max(10, Math.min(500, this.state.pixelsPerSecond + zoomDelta));

        if (newPixelsPerSecond !== this.state.pixelsPerSecond) {
            this.state.pixelsPerSecond = newPixelsPerSecond;
            this.dom.zoomSlider.value = this.state.pixelsPerSecond;
            this.dom.zoomLevel.textContent = `${this.state.pixelsPerSecond}%`;
            this.renderCallback('zoomWheel');
            this.dom.timelineContent.scrollLeft = timeUnderCursor * newPixelsPerSecond - mouseX;
            this.updateIndicatorPosition(this.timeToBeats(this.state.playbackOffset), false);
        }
    }

    autoScrollToSequence() {
        const currentScrollLeft = this.dom.timelineContent.scrollLeft;
        const viewportWidth = this.dom.timelineContent.clientWidth;
        const slack = (viewportWidth / this.state.pixelsPerSecond) * 0.1;
        const currentTime = (currentScrollLeft / this.state.pixelsPerSecond) + slack;

        let targetSeqIndex = 0;
        for (let i = 0; i < this.state.sequences.length; i++) {
            if (this.state.sequences[i].startTime <= currentTime) targetSeqIndex = i;
            else break;
        }

        if (targetSeqIndex !== this.state.lastActiveSeqIndex && this.state.sequences.length > 0) {
            this.state.lastActiveSeqIndex = targetSeqIndex;
            const seqDivs = this.dom.timeline.querySelectorAll('.sequence');
            if (seqDivs[targetSeqIndex]) {
                seqDivs[targetSeqIndex].classList.add('active-flash');
                setTimeout(() => seqDivs[targetSeqIndex]?.classList.remove('active-flash'), 600);
            }
        }

        const targetScrollTop = this.state.sequences[targetSeqIndex]?._yPosition || 0;
        const currentScrollTop = this.dom.timelineContent.scrollTop;
        const scrollDiff = targetScrollTop - currentScrollTop;
        if (Math.abs(scrollDiff) > 5) {
            this.dom.timelineContent.scrollTop += scrollDiff * this.VERTICAL_SCROLL_SPEED;
        }
    }

    updateIndicatorPosition(beats, smoothScroll = false) {
        const timelineX = beats * this.state.pixelsPerSecond;
        const scrollLeft = this.dom.timelineContent.scrollLeft;
        this.dom.playbackIndicator.style.left = `${timelineX - scrollLeft + this.TIMELINE_LEFT_PADDING}px`;

        if (smoothScroll) {
            const targetScroll = timelineX - this.dom.timelineContent.clientWidth * this.SCROLL_VIEWPORT_FRACTION;
            const scrollDiff = targetScroll - scrollLeft;
            if (Math.abs(scrollDiff) > 5) {
                this.dom.timelineContent.scrollLeft += scrollDiff * this.SMOOTH_SCROLL_SPEED;
            }
        }
    }

    // Helper
    timeToBeats(seconds) {
        return seconds * this.state.bpm / 60.0;
    }
}