-
Notifications
You must be signed in to change notification settings - Fork 49
/
PlotToolEventDispatcher.kt
166 lines (144 loc) · 6.41 KB
/
PlotToolEventDispatcher.kt
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
/*
* Copyright (c) 2024. JetBrains s.r.o.
* Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/
package org.jetbrains.letsPlot.core.plot.builder.interact
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import org.jetbrains.letsPlot.commons.debounce
import org.jetbrains.letsPlot.commons.geometry.DoubleRectangle
import org.jetbrains.letsPlot.commons.registration.Registration
import org.jetbrains.letsPlot.core.interact.DrawRectFeedback
import org.jetbrains.letsPlot.core.interact.PanGeomFeedback
import org.jetbrains.letsPlot.core.interact.WheelZoomFeedback
import org.jetbrains.letsPlot.core.interact.event.ToolEventDispatcher
import org.jetbrains.letsPlot.core.interact.event.ToolEventSpec.EVENT_INTERACTION_NAME
import org.jetbrains.letsPlot.core.interact.event.ToolEventSpec.EVENT_INTERACTION_ORIGIN
import org.jetbrains.letsPlot.core.interact.event.ToolEventSpec.EVENT_NAME
import org.jetbrains.letsPlot.core.interact.event.ToolEventSpec.EVENT_RESULT_DATA_BOUNDS
import org.jetbrains.letsPlot.core.interact.event.ToolEventSpec.INTERACTION_ACTIVATED
import org.jetbrains.letsPlot.core.interact.event.ToolEventSpec.INTERACTION_COMPLETED
import org.jetbrains.letsPlot.core.interact.event.ToolEventSpec.INTERACTION_DEACTIVATED
import org.jetbrains.letsPlot.core.interact.event.ToolInteractionSpec
import org.jetbrains.letsPlot.core.plot.builder.PlotInteractor
internal class PlotToolEventDispatcher(
private val plotInteractor: PlotInteractor
) : ToolEventDispatcher {
private val interactionsByOrigin: MutableMap<
String, // origin
MutableList<InteractionInfo>> = HashMap()
private lateinit var toolEventCallback: (Map<String, Any>) -> Unit
override fun initToolEventCallback(callback: (Map<String, Any>) -> Unit) {
check(!this::toolEventCallback.isInitialized) { "Repeated initialization of 'toolEventCallback'." }
toolEventCallback = callback
}
override fun activateInteractions(origin: String, interactionSpecList: List<Map<String, Any>>) {
interactionSpecList.forEach { interactionSpec ->
activateInteraction(origin, interactionSpec)
}
}
private fun activateInteraction(origin: String, interactionSpec: Map<String, Any>) {
deactivateOverlappingInteractions(origin, interactionSpec)
val interactionName = interactionSpec.getValue(ToolInteractionSpec.NAME) as String
val completeInteractionDebounced =
debounce<DoubleRectangle>(DEBOUNCE_DELAY_MS, CoroutineScope(Dispatchers.Default)) { dataBounds ->
println("Debounced interaction: $interactionName, dataBounds: $dataBounds")
completeInteraction(origin, interactionName, dataBounds)
}
// ToDo: sent "completed" event in "onCompleted"
val feedback = when (interactionName) {
ToolInteractionSpec.DRAG_PAN -> PanGeomFeedback(
onCompleted = { dataBounds ->
println("Pan tool: apply $dataBounds")
completeInteraction(origin, interactionName, dataBounds)
}
)
ToolInteractionSpec.BOX_ZOOM -> DrawRectFeedback(
onCompleted = { dataBounds ->
println("client: data $dataBounds")
completeInteraction(origin, interactionName, dataBounds)
}
)
ToolInteractionSpec.WHEEL_ZOOM -> WheelZoomFeedback(
onCompleted = { dataBounds ->
completeInteractionDebounced(dataBounds)
}
)
else -> {
// ToDo: send an error event
throw IllegalStateException("Unsupported interaction: $interactionName")
}
}
val feedbackRegistration = plotInteractor.startToolFeedback(feedback)
interactionsByOrigin.getOrPut(origin) { ArrayList() }.add(
InteractionInfo(
interactionSpec = interactionSpec,
feedbackReg = feedbackRegistration
)
)
toolEventCallback.invoke(
mapOf(
EVENT_NAME to INTERACTION_ACTIVATED,
EVENT_INTERACTION_ORIGIN to origin,
EVENT_INTERACTION_NAME to interactionName
)
)
}
private fun completeInteraction(
origin: String,
interactionName: String,
dataBounds: DoubleRectangle
) {
toolEventCallback.invoke(
mapOf(
EVENT_NAME to INTERACTION_COMPLETED,
EVENT_INTERACTION_ORIGIN to origin,
EVENT_INTERACTION_NAME to interactionName,
EVENT_RESULT_DATA_BOUNDS to listOf(
dataBounds.left, dataBounds.top,
dataBounds.right, dataBounds.bottom
)
)
)
}
override fun deactivateInteractions(origin: String) {
interactionsByOrigin.remove(origin)?.forEach { interactionInfo ->
interactionInfo.feedbackReg.dispose()
toolEventCallback.invoke(
mapOf(
EVENT_NAME to INTERACTION_DEACTIVATED,
EVENT_INTERACTION_ORIGIN to origin,
EVENT_INTERACTION_NAME to interactionInfo.interactionName
)
)
}
}
override fun deactivateAllSilently(): Map<String, List<Map<String, Any>>> {
val deactivatedInteractions = interactionsByOrigin.mapValues { (_, interactionInfoList) ->
interactionInfoList.map {
it.feedbackReg.dispose()
it.interactionSpec
}
}
interactionsByOrigin.clear()
return deactivatedInteractions
}
private fun deactivateOverlappingInteractions(
originBeingActivated: String,
interactionSpecBeingActivated: Map<String, Any>
) {
// For now just deactivate all active interactions
ArrayList(interactionsByOrigin.keys)
.filter { origin -> origin != originBeingActivated }
.forEach { origin -> deactivateInteractions(origin) }
}
private class InteractionInfo(
val interactionSpec: Map<String, Any>,
val feedbackReg: Registration
) {
val interactionName = interactionSpec.getValue(ToolInteractionSpec.NAME) as String
}
companion object {
private const val DEBOUNCE_DELAY_MS = 30L
}
}