+85
-1
lines changedFilter options
+85
-1
lines changed Original file line number Diff line number Diff line change
@@ -120,11 +120,27 @@ class Input {
120
120
*/
121
121
private val currentActions = FXCollections.observableArrayList<UserAction>()
122
122
123
+
/**
124
+
* K - user action.
125
+
* V - time in seconds when last triggered.
126
+
*/
127
+
private val actionTimes = hashMapOf<UserAction, Double>()
128
+
123
129
private val activeTriggers = arrayListOf<Trigger>()
124
130
private val listeners = arrayListOf<TriggerListener>()
125
131
126
132
private val inputQueue = ArrayDeque<KeyCode>()
127
133
134
+
/**
135
+
* Time accumulated by this input in seconds.
136
+
*/
137
+
private var time = 0.0
138
+
139
+
/**
140
+
* Time in seconds within which a double press can occur.
141
+
*/
142
+
var doublePressTimeThreshold = 0.25
143
+
128
144
/**
129
145
* If action events should be processed.
130
146
*/
@@ -259,6 +275,8 @@ class Input {
259
275
}
260
276
261
277
fun update(tpf: Double) {
278
+
time += tpf
279
+
262
280
if (isCapturing) {
263
281
currentCapture!!.update(tpf)
264
282
}
@@ -353,8 +371,19 @@ class Input {
353
371
.forEach { (act, _) ->
354
372
currentActions.add(act)
355
373
356
-
if (processInput)
374
+
if (processInput) {
357
375
act.begin()
376
+
377
+
val lastTime = actionTimes[act] ?: -Int.MAX_VALUE.toDouble()
378
+
379
+
actionTimes[act] = time
380
+
381
+
if (time - lastTime <= doublePressTimeThreshold) {
382
+
// this resets action time, so that 3rd action will not trigger double action
383
+
actionTimes.remove(act)
384
+
act.beginDoubleAction()
385
+
}
386
+
}
358
387
}
359
388
360
389
if (event.eventType == KeyEvent.KEY_PRESSED) {
@@ -444,7 +473,10 @@ class Input {
444
473
currentActions.forEach { it.end() }
445
474
}
446
475
476
+
time = 0.0
477
+
447
478
currentActions.clear()
479
+
actionTimes.clear()
448
480
activeTriggers.clear()
449
481
450
482
stopCapture()
Original file line number Diff line number Diff line change
@@ -37,6 +37,7 @@ abstract class UserAction(
37
37
internal fun begin() = onActionBegin()
38
38
internal fun action() = onAction()
39
39
internal fun end() = onActionEnd()
40
+
internal fun beginDoubleAction() = onDoubleActionBegin()
40
41
41
42
/**
42
43
* Called once in the same tick when triggered.
@@ -53,4 +54,10 @@ abstract class UserAction(
53
54
* Called once in the same tick when trigger was released.
54
55
*/
55
56
protected open fun onActionEnd() {}
57
+
58
+
/**
59
+
* Called once in the same tick when triggered for the second time
60
+
* within a specified period of time.
61
+
*/
62
+
protected open fun onDoubleActionBegin() {}
56
63
}
Original file line number Diff line number Diff line change
@@ -1012,6 +1012,51 @@ class InputTest {
1012
1012
assertThat(capture1, `is`(not(capture3)))
1013
1013
}
1014
1014
1015
+
@Test
1016
+
fun `Double press action`() {
1017
+
var calls = 0
1018
+
1019
+
val action = object : UserAction("Action1") {
1020
+
override fun onDoubleActionBegin() {
1021
+
calls++
1022
+
}
1023
+
}
1024
+
1025
+
input.addAction(action, KeyCode.A)
1026
+
1027
+
input.mockKeyPress(KeyCode.A)
1028
+
input.mockKeyRelease(KeyCode.A)
1029
+
assertThat(calls, `is`(0))
1030
+
1031
+
// within threshold, so 2nd press triggers it
1032
+
input.mockKeyPress(KeyCode.A)
1033
+
input.mockKeyRelease(KeyCode.A)
1034
+
assertThat(calls, `is`(1))
1035
+
1036
+
// 3rd press should not trigger since action should be reset
1037
+
input.mockKeyPress(KeyCode.A)
1038
+
input.mockKeyRelease(KeyCode.A)
1039
+
assertThat(calls, `is`(1))
1040
+
1041
+
// 4th press should trigger as normal
1042
+
input.mockKeyPress(KeyCode.A)
1043
+
input.mockKeyRelease(KeyCode.A)
1044
+
assertThat(calls, `is`(2))
1045
+
1046
+
input.doublePressTimeThreshold = 0.1
1047
+
1048
+
input.mockKeyPress(KeyCode.A)
1049
+
input.mockKeyRelease(KeyCode.A)
1050
+
assertThat(calls, `is`(2))
1051
+
1052
+
input.update(0.2)
1053
+
1054
+
// does not trigger it since more time has passed than threshold
1055
+
input.mockKeyPress(KeyCode.A)
1056
+
input.mockKeyRelease(KeyCode.A)
1057
+
assertThat(calls, `is`(2))
1058
+
}
1059
+
1015
1060
// @Test
1016
1061
// fun `Serialization`() {
1017
1062
// val action = object : UserAction("Action") {}
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4