tengsx
2023-04-07 36c045a4f518f6506eab91e48ee48ce7d1aaa21d
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
/**
 * @Author: Caven
 * @Date: 2020-01-31 19:45:32
 */
 
import { PlotEventType } from '@/utils/event/index.js'
 
class Draw {
    constructor(style) {
        this._style = style
        this._viewer = undefined
        this._layer = undefined
        this._delegate = undefined
        this._options = {}
        this._positions = []
    }
 
    get curDrawTool () {
        return this._viewer.curDrawTool
    }
 
    /**
     * The hook for mount viewer
     * Subclasses need to be overridden
     * @private
     */
    _mountedHook () { }
 
    /**
     * The hook for mount stop
     * Subclasses need to be overridden
     * @private
     */
    _stopdHook () { }
 
    /**
     *
     * @param position
     * @private
     */
    _onDrawAnchor (position) { }
 
    /**
     *
     * @param position
     * @private
     */
    _onAnchorMoving (position) {
        this._positions.pop()
        this._positions.push(position)
    }
 
    /**
     *
     * @private
     */
    _onDrawStop () {
        this._unbindEvent()
        this._viewer.curDrawTool.deactivate()
        this._layer.entities.remove(this._delegate)
        this._stopdHook()
    }
 
    /**
     *
     * @private
     */
    _bindEvent () {
        this.curDrawTool.on(PlotEventType.DRAW_ANCHOR, this._onDrawAnchor, this)
        this.curDrawTool.on(PlotEventType.ANCHOR_MOVING, this._onAnchorMoving, this)
        this.curDrawTool.on(PlotEventType.DRAW_STOP, this._onDrawStop, this)
    }
 
    /**
     *
     * @private
     */
    _unbindEvent () {
        this.curDrawTool.off(PlotEventType.DRAW_ANCHOR, this._onDrawAnchor, this)
        this.curDrawTool.off(PlotEventType.ANCHOR_MOVING, this._onAnchorMoving, this)
        this.curDrawTool.off(PlotEventType.DRAW_STOP, this._onDrawStop, this)
    }
 
    /**
     *
     * @param plot
     * @param options
     * @returns {Draw}
     */
    start (plot, options) {
        this._viewer = plot.viewer
        this._layer = plot.layer
        this._options = options
        this._viewer.editTool.deactivate()
        this._viewer.curDrawTool.activate(options)
        this._mountedHook()
        this._unbindEvent()
        this._bindEvent()
        return this
    }
}
 
export default Draw