guanqb
2024-01-02 432456dea4e9f370f76a42f7b341596012c3c38f
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/**
 * @Author: Caven
 * @Date: 2021-07-14 20:28:14
 */
 
import { PlotEventType, PlotEvent } from '@/utils/event/index.js'
 
const IMG_CIRCLE_RED = '/images/circle_red.png'
 
const IMG_CIRCLE_YELLOW = '/images/circle_yellow.png'
 
const DEF_OPTS = {
    icon_center: IMG_CIRCLE_YELLOW,
    icon_anchor: IMG_CIRCLE_RED,
    icon_size: [12, 12],
    clampToModel: false
}
 
class DrawTool {
    constructor() {
        this._viewer = undefined
        this._anchorLayer = new global.DC.Namespace.Cesium.CustomDataSource('draw-anchor-layer')
        this._floatingAnchor = undefined
        this._options = {}
        this._plotEvent = new PlotEvent()
        this._tooltipMess = undefined
    }
 
    set tooltipMess (tooltipMess) {
        this._tooltipMess = tooltipMess
        return this
    }
 
    /**
     *
     * @param e
     * @returns {boolean}
     * @private
     */
    _onClick (e) {
        let position =
            this._options.clampToModel && e.position ? e.position : e.surfacePosition
        if (!position) {
            return false
        }
 
        if (!this._floatingAnchor) {
            this._floatingAnchor = this._onCreateAnchor({ position })
        }
 
        this._plotEvent.fire(PlotEventType.DRAW_ANCHOR, position)
    }
 
    /**
     *
     * @param e
     * @private
     */
    _onMouseMove (e) {
        e.windowPosition.x = e.windowPosition.x + 20
        this._viewer.tooltip.showAt(e.windowPosition, this._tooltipMess)
        let position =
            this._options.clampToModel && e.position ? e.position : e.surfacePosition
        if (!position) {
            return false
        }
        this._floatingAnchor && this._floatingAnchor.position.setValue(position)
        this._plotEvent.fire(PlotEventType.ANCHOR_MOVING, position)
    }
 
    /**
     *
     * @param e
     * @private
     */
    _onRightClick (e) {
        this._plotEvent.fire(
            PlotEventType.DRAW_STOP,
            this._options.clampToModel && e.position ? e.position : e.surfacePosition
        )
    }
 
    /**
     *
     * @param position
     * @param isCenter
     * @returns {*}
     * @private
     */
    _onCreateAnchor ({ position, isCenter = false }) {
        console.log(this._options, 6633333)
        return this._anchorLayer.entities.add({
            position: position,
            billboard: {
                image: isCenter ? this._options.icon_center : this._options.icon_anchor,
                width: this._options.icon_size[0],
                height: this._options.icon_size[1],
                eyeOffset: new global.DC.Namespace.Cesium.Cartesian3(0, 0, -4),
                heightReference:
                    this._viewer.scene.mode === global.DC.Namespace.Cesium.SceneMode.SCENE3D &&
                        !this._options.clampToModel
                        ? global.DC.Namespace.Cesium.HeightReference.CLAMP_TO_GROUND
                        : global.DC.Namespace.Cesium.HeightReference.NONE
            }
        })
    }
 
    /**
     *
     * @private
     */
    _onClearAnchor () {
        this._anchorLayer.entities.removeAll()
    }
 
    /**
     *
     * @private
     */
    _bindEvent () {
        this._viewer.on(global.DC.MouseEventType.CLICK, this._onClick, this)
        this._viewer.on(global.DC.MouseEventType.MOUSE_MOVE, this._onMouseMove, this)
        this._viewer.on(global.DC.MouseEventType.RIGHT_CLICK, this._onRightClick, this)
        this._plotEvent.on(PlotEventType.CREATE_ANCHOR, this._onCreateAnchor, this)
        this._plotEvent.on(PlotEventType.CLEAR_ANCHOR, this._onClearAnchor, this)
    }
 
    /**
     *
     * @private
     */
    _unbindEvent () {
        this._viewer.off(global.DC.MouseEventType.CLICK, this._onClick, this)
        this._viewer.off(global.DC.MouseEventType.MOUSE_MOVE, this._onMouseMove, this)
        this._viewer.off(global.DC.MouseEventType.RIGHT_CLICK, this._onRightClick, this)
        this._plotEvent.off(PlotEventType.CREATE_ANCHOR, this._onCreateAnchor, this)
        this._plotEvent.off(PlotEventType.CLEAR_ANCHOR, this._onClearAnchor, this)
    }
 
    /**
     *
     * @param type
     * @param callback
     * @param context
     * @returns {DrawTool}
     */
    on (type, callback, context) {
        this._plotEvent.on(type, callback, context || this)
        return this
    }
 
    /**
     *
     * @param type
     * @param callback
     * @param context
     * @returns {DrawTool}
     */
    off (type, callback, context) {
        this._plotEvent.off(type, callback, context || this)
        return this
    }
 
    /**
     *
     * @param type
     * @param parmas
     * @returns {DrawTool}
     */
    fire (type, parmas) {
        this._plotEvent.fire(type, parmas)
        return this
    }
 
    /**
     *
     * @param options
     * @returns {DrawTool}
     */
    activate (options = {}) {
        this._viewer.tooltip.enable = true
        this._options = { ...DEF_OPTS, ...options }
        this._unbindEvent()
        this._bindEvent()
        this.fire(PlotEventType.DRAW_START, this._options)
        return this
    }
 
    /**
     *
     * @returns {DrawTool}
     */
    deactivate () {
        this._unbindEvent()
        this._viewer.tooltip.enable = false
        this._anchorLayer.entities.removeAll()
        this._floatingAnchor = undefined
        return this
    }
 
    /**
     *
     * @param viewer
     */
    install (viewer) {
        this._viewer = viewer
        this._viewer.dataSources.add(this._anchorLayer)
        Object.defineProperty(this._viewer, 'curDrawTool', {
            value: this,
            writable: true,
            configurable: true,
            enumerable: true
        })
    }
}
 
export default DrawTool