1
1
package com.github.ilovegamecoding.intellijcodexp.listeners
2
2
3
3
import com.github.ilovegamecoding.intellijcodexp.enums.Event
4
- import com.github.ilovegamecoding.intellijcodexp.services.CodeXPService
5
- import com.intellij.openapi.actionSystem.*
4
+ import com.intellij.openapi.actionSystem.AnAction
5
+ import com.intellij.openapi.actionSystem.AnActionEvent
6
+ import com.intellij.openapi.actionSystem.AnActionResult
7
+ import com.intellij.openapi.actionSystem.DataContext
6
8
import com.intellij.openapi.actionSystem.ex.AnActionListener
7
9
import com.intellij.openapi.application.ApplicationManager
8
- import com.intellij.openapi.diagnostic.thisLogger
9
- import com.intellij.ui.JBColor
10
- import java.awt.Color
11
- import java.awt.Font
12
- import javax.swing.JComponent
13
- import javax.swing.JLabel
14
- import kotlin.math.max
15
10
16
11
/* *
17
12
* CodeXPEventListener class
18
13
*
19
14
* This class listens to events from the IDE and fires them to the message bus.
20
15
*/
21
16
internal class CodeXPEventListener : AnActionListener {
22
- /* *
23
- * Fading labels in each swing component.
24
- */
25
- private val fadingLabels: MutableMap <JComponent , FadingLabel > = mutableMapOf ()
26
-
27
- /* *
28
- * Current value of the fading label.
29
- */
30
- private var currentXPGainValue: Int = 0
31
-
32
17
override fun afterEditorTyping (c : Char , dataContext : DataContext ) {
33
18
super .afterEditorTyping(c, dataContext)
34
19
35
- fireEventAndDisplayXPLabel (Event .TYPING , dataContext)
20
+ fireEvent (Event .TYPING , dataContext)
36
21
}
37
22
38
23
override fun afterActionPerformed (action : AnAction , event : AnActionEvent , result : AnActionResult ) {
39
24
super .afterActionPerformed(action, event, result)
40
- thisLogger().warn(" Action performed: ${action.templateText} " )
41
25
42
- when (action.templateText) { // Fire event based on action.
26
+ when (action.templateText) {
43
27
" Run" -> fireEvent(Event .RUN )
44
28
" Save All" -> fireEvent(Event .SAVE )
45
29
" Debug" -> fireEvent(Event .DEBUG )
46
30
" Build Project" -> fireEvent(Event .BUILD )
47
31
" Rebuild Project" -> fireEvent(Event .BUILD )
48
- " Cut" -> fireEventAndDisplayXPLabel (Event .CUT , event.dataContext)
49
- " Copy" -> fireEventAndDisplayXPLabel (Event .COPY , event.dataContext)
50
- " Paste" -> fireEventAndDisplayXPLabel (Event .PASTE , event.dataContext)
51
- " Backspace" -> fireEventAndDisplayXPLabel (Event .BACKSPACE , event.dataContext)
52
- " Tab" -> fireEventAndDisplayXPLabel (Event .TAB , event.dataContext)
53
- " Enter" -> fireEventAndDisplayXPLabel (Event .ENTER , event.dataContext)
32
+ " Cut" -> fireEvent (Event .CUT , event.dataContext)
33
+ " Copy" -> fireEvent (Event .COPY , event.dataContext)
34
+ " Paste" -> fireEvent (Event .PASTE , event.dataContext)
35
+ " Backspace" -> fireEvent (Event .BACKSPACE , event.dataContext)
36
+ " Tab" -> fireEvent (Event .TAB , event.dataContext)
37
+ " Enter" -> fireEvent (Event .ENTER , event.dataContext)
54
38
else -> fireEvent(Event .ACTION )
55
39
}
56
40
}
57
41
58
- /* *
59
- * Fire event to the message bus and display XP label.
60
- *
61
- * @param event The event to fire.
62
- * @param dataContext The data context of the event.
63
- */
64
- private fun fireEventAndDisplayXPLabel (event : Event , dataContext : DataContext ) {
65
- fireEvent(event)
66
- displayXPLabel(event, dataContext)
67
- }
68
-
69
42
/* *
70
43
* Fire event to the message bus.
71
44
*
72
45
* @param event The event to fire.
73
- */
74
- private fun fireEvent (event : Event ) {
75
- ApplicationManager .getApplication().messageBus.syncPublisher(CodeXPListener .CODEXP_EVENT )
76
- .eventOccurred(event)
77
- }
78
-
79
- /* *
80
- * Display XP label at the caret position.
81
- *
82
- * @param event The event to fire.
83
46
* @param dataContext The data context of the event.
84
47
*/
85
- private fun displayXPLabel (event : Event , dataContext : DataContext ) {
86
- val codeXPConfiguration =
87
- ApplicationManager .getApplication().getService(CodeXPService ::class .java).state.codeXPConfiguration
88
-
89
- if (! codeXPConfiguration.showGainedXP) {
90
- return
91
- }
92
-
93
- val editor = CommonDataKeys .EDITOR .getData(dataContext) ? : return
94
- val caretModel = editor.caretModel
95
- val fadingLabelPosition = editor.visualPositionToXY(caretModel.visualPosition)
96
-
97
- val component = editor.contentComponent
98
-
99
- fadingLabels[component]?.let { fadingLabel ->
100
- fadingLabel.cancelFadeOut()
101
- currentXPGainValue = fadingLabel.value
102
- component.remove(fadingLabel)
103
- component.revalidate()
104
- component.repaint()
105
- }
106
-
107
- currentXPGainValue + = event.xpValue.toInt()
108
-
109
- val newFadingLabel = FadingLabel (currentXPGainValue).apply {
110
- font = Font (font.fontName, Font .BOLD , editor.colorsScheme.editorFontSize)
111
- size = preferredSize
112
- val caretHeight = editor.lineHeight
113
- fadingLabelPosition.let { point ->
114
- val position = codeXPConfiguration.positionToDisplayGainedXP
115
- val xOffset = when {
116
- position.name.contains(" LEFT" ) -> - width
117
- position.name.contains(" RIGHT" ) -> 0
118
- else -> - width / 2
119
- }
120
- point.translate(
121
- (position.x * 4 ) + xOffset,
122
- position.y * (caretHeight / 2 )
123
- )
124
- location = point
125
- }
126
- startFadeOut()
127
- }
128
-
129
- fadingLabels[component] = newFadingLabel
130
-
131
- component.add(newFadingLabel)
132
- component.revalidate()
133
- component.repaint()
134
- }
135
-
136
- internal class FadingLabel (initialValue : Int ) : JLabel(if (initialValue == 0) " 0 xp" else " +$initialValue XP" ) {
137
- private lateinit var timer: javax.swing.Timer
138
-
139
- /* *
140
- * Start fade out animation.
141
- */
142
- fun startFadeOut () {
143
- timer = javax.swing.Timer (100 , null )
144
- timer.addActionListener {
145
- val newAlpha = max(foreground.alpha - 255 / 10 , 0 )
146
- if (newAlpha <= 0 ) {
147
- timer.stop()
148
- value = 0
149
- } else {
150
- foreground =
151
- Color (JBColor .foreground().red, JBColor .foreground().green, JBColor .foreground().blue, newAlpha)
152
- }
153
- }
154
- timer.start()
155
- }
156
-
157
- /* *
158
- * Cancel fade out animation.
159
- */
160
- fun cancelFadeOut () {
161
- timer.stop()
162
- }
163
-
164
- /* *
165
- * Current value of the fading label.
166
- */
167
- var value: Int = initialValue
168
- set(newValue) {
169
- field = newValue
170
- text = if (newValue == 0 ) " 0 xp" else " +$newValue xp"
171
- if (newValue == 0 ) {
172
- timer.stop()
173
- }
174
- }
48
+ private fun fireEvent (event : Event , dataContext : DataContext ? = null) {
49
+ ApplicationManager .getApplication().messageBus.syncPublisher(CodeXPListener .CODEXP_EVENT )
50
+ .eventOccurred(event, dataContext)
175
51
}
176
52
}
0 commit comments