mh-two-thousand-and-two
2024-03-25 b8c93990f3fa5e50a8aca16bdc9c2758168aa0fd
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
 * Displayable for incremental rendering. It will be rendered in a separate layer
 * IncrementalDisplay have two main methods. `clearDisplayables` and `addDisplayables`
 * addDisplayables will render the added displayables incremetally.
 *
 * It use a notClear flag to tell the painter don't clear the layer if it's the first element.
 *
 * It's not available for SVG rendering.
 */
import Displayble from './Displayable';
import BoundingRect from '../core/BoundingRect';
import { MatrixArray } from '../core/matrix';
import Group from './Group';
 
const m: MatrixArray = [];
// TODO Style override ?
 
export default class IncrementalDisplayable extends Displayble {
 
    notClear: boolean = true
 
    incremental = true
 
    private _displayables: Displayble[] = []
    private _temporaryDisplayables: Displayble[] = []
 
    private _cursor = 0
 
    traverse<T>(
        cb: (this: T, el: this) => void,
        context: T
    ) {
        cb.call(context, this);
    }
 
    useStyle() {
        // Use an empty style
        // PENDING
        this.style = {};
    }
 
    // getCurrentCursor / updateCursorAfterBrush
    // is used in graphic.ts. It's not provided for developers
    getCursor() {
        return this._cursor;
    }
    // Update cursor after brush.
    innerAfterBrush() {
        this._cursor = this._displayables.length;
    }
 
    clearDisplaybles() {
        this._displayables = [];
        this._temporaryDisplayables = [];
        this._cursor = 0;
        this.markRedraw();
 
        this.notClear = false;
    }
 
    clearTemporalDisplayables() {
        this._temporaryDisplayables = [];
    }
 
    addDisplayable(displayable: Displayble, notPersistent?: boolean) {
        if (notPersistent) {
            this._temporaryDisplayables.push(displayable);
        }
        else {
            this._displayables.push(displayable);
        }
        this.markRedraw();
    }
 
    addDisplayables(displayables: Displayble[], notPersistent?: boolean) {
        notPersistent = notPersistent || false;
        for (let i = 0; i < displayables.length; i++) {
            this.addDisplayable(displayables[i], notPersistent);
        }
    }
 
    getDisplayables(): Displayble[] {
        return this._displayables;
    }
 
    getTemporalDisplayables(): Displayble[] {
        return this._temporaryDisplayables;
    }
 
    eachPendingDisplayable(cb: (displayable: Displayble) => void) {
        for (let i = this._cursor; i < this._displayables.length; i++) {
            cb && cb(this._displayables[i]);
        }
        for (let i = 0; i < this._temporaryDisplayables.length; i++) {
            cb && cb(this._temporaryDisplayables[i]);
        }
    }
 
    update() {
        this.updateTransform();
        for (let i = this._cursor; i < this._displayables.length; i++) {
            const displayable = this._displayables[i];
            // PENDING
            displayable.parent = this as unknown as Group;
            displayable.update();
            displayable.parent = null;
        }
        for (let i = 0; i < this._temporaryDisplayables.length; i++) {
            const displayable = this._temporaryDisplayables[i];
            // PENDING
            displayable.parent = this as unknown as Group;
            displayable.update();
            displayable.parent = null;
        }
    }
 
    getBoundingRect() {
        if (!this._rect) {
            const rect = new BoundingRect(Infinity, Infinity, -Infinity, -Infinity);
            for (let i = 0; i < this._displayables.length; i++) {
                const displayable = this._displayables[i];
                const childRect = displayable.getBoundingRect().clone();
                if (displayable.needLocalTransform()) {
                    childRect.applyTransform(displayable.getLocalTransform(m));
                }
                rect.union(childRect);
            }
            this._rect = rect;
        }
        return this._rect;
    }
 
    contain(x: number, y: number): boolean {
        const localPos = this.transformCoordToLocal(x, y);
        const rect = this.getBoundingRect();
 
        if (rect.contain(localPos[0], localPos[1])) {
            for (let i = 0; i < this._displayables.length; i++) {
                const displayable = this._displayables[i];
                if (displayable.contain(x, y)) {
                    return true;
                }
            }
        }
        return false;
    }
 
}