From 3ceda4daf8ae343e7fe90e048109e0adb508f0d8 Mon Sep 17 00:00:00 2001 From: Matteo Hausner Date: Sat, 24 Feb 2024 16:33:15 +0100 Subject: [PATCH] ToButtonAction, ToKeyAction, ToMouseButtonAction: added maximum activation interval analogous to already implemented minimum activation interval --- ...ion.java => ActivationIntervalAction.java} | 46 +++++++++++++++---- .../input/action/AxisToButtonAction.java | 2 +- .../input/action/ButtonToButtonAction.java | 2 +- .../input/action/ToButtonAction.java | 2 +- .../input/action/ToKeyAction.java | 4 +- .../input/action/ToMouseButtonAction.java | 4 +- ...a => ActivationIntervalEditorBuilder.java} | 4 +- src/main/resources/strings.properties | 1 + src/main/resources/strings_de_DE.properties | 1 + 9 files changed, 49 insertions(+), 17 deletions(-) rename src/main/java/de/bwravencl/controllerbuddy/input/action/{MinActivationIntervalAction.java => ActivationIntervalAction.java} (52%) rename src/main/java/de/bwravencl/controllerbuddy/input/action/gui/{MinActivationIntervalEditorBuilder.java => ActivationIntervalEditorBuilder.java} (93%) diff --git a/src/main/java/de/bwravencl/controllerbuddy/input/action/MinActivationIntervalAction.java b/src/main/java/de/bwravencl/controllerbuddy/input/action/ActivationIntervalAction.java similarity index 52% rename from src/main/java/de/bwravencl/controllerbuddy/input/action/MinActivationIntervalAction.java rename to src/main/java/de/bwravencl/controllerbuddy/input/action/ActivationIntervalAction.java index 3f177b52..06e0ce37 100644 --- a/src/main/java/de/bwravencl/controllerbuddy/input/action/MinActivationIntervalAction.java +++ b/src/main/java/de/bwravencl/controllerbuddy/input/action/ActivationIntervalAction.java @@ -18,42 +18,67 @@ import de.bwravencl.controllerbuddy.input.Input; import de.bwravencl.controllerbuddy.input.action.annotation.ActionProperty; -import de.bwravencl.controllerbuddy.input.action.gui.MinActivationIntervalEditorBuilder; +import de.bwravencl.controllerbuddy.input.action.gui.ActivationIntervalEditorBuilder; -abstract class MinActivationIntervalAction extends DescribableAction +abstract class ActivationIntervalAction extends DescribableAction implements IInitializationAction { @ActionProperty( label = "MIN_ACTIVATION_INTERVAL", - editorBuilder = MinActivationIntervalEditorBuilder.class, + editorBuilder = ActivationIntervalEditorBuilder.class, order = 500) private int minActivationInterval = 0; + @ActionProperty( + label = "MAX_ACTIVATION_INTERVAL", + editorBuilder = ActivationIntervalEditorBuilder.class, + order = 501) + private int maxActivationInterval = 0; + private transient boolean wasUp = true; private transient long minActivationTime = 0L; + private transient long maxActivationTime = Integer.MAX_VALUE; + + public int getMaxActivationInterval() { + return maxActivationInterval; + } + public int getMinActivationInterval() { return minActivationInterval; } - boolean handleMinActivationInterval(final boolean hot) { - if (minActivationInterval > 0L) { + boolean handleActivationInterval(final boolean hot) { + final var hasMinActivationInterval = minActivationInterval > 0L; + final var hasMaxActivationInterval = maxActivationInterval > 0L; + + if (hasMinActivationInterval || hasMaxActivationInterval) { final var currentTime = System.currentTimeMillis(); if (hot) { + if (hasMaxActivationInterval && currentTime > maxActivationTime) { + return false; + } + if (wasUp) { wasUp = false; - minActivationTime = currentTime + minActivationInterval; + if (hasMinActivationInterval) { + minActivationTime = currentTime + minActivationInterval; + } + if (hasMaxActivationInterval) { + maxActivationTime = currentTime + maxActivationInterval; + } } } else { wasUp = true; - if (currentTime <= minActivationTime) { + if (hasMinActivationInterval && currentTime <= minActivationTime) { return true; } minActivationTime = 0L; + maxActivationTime = Long.MAX_VALUE; } } @@ -63,7 +88,12 @@ boolean handleMinActivationInterval(final boolean hot) { @Override public void init(final Input input) { wasUp = true; - minActivationTime = 0L; + minActivationTime = 0; + maxActivationTime = Long.MAX_VALUE; + } + + public void setMaxActivationInterval(final int maxActivationInterval) { + this.maxActivationInterval = maxActivationInterval; } public void setMinActivationInterval(final int minActivationInterval) { diff --git a/src/main/java/de/bwravencl/controllerbuddy/input/action/AxisToButtonAction.java b/src/main/java/de/bwravencl/controllerbuddy/input/action/AxisToButtonAction.java index f673ba27..f1be87c5 100644 --- a/src/main/java/de/bwravencl/controllerbuddy/input/action/AxisToButtonAction.java +++ b/src/main/java/de/bwravencl/controllerbuddy/input/action/AxisToButtonAction.java @@ -43,7 +43,7 @@ public void doAction(final Input input, final int component, Float value) { value = handleLongPress(input, component, value); var hot = !input.isAxisSuspended(component) && value >= minAxisValue && value <= maxAxisValue; - hot = handleMinActivationInterval(hot); + hot = handleActivationInterval(hot); if (isAlreadyPressed(input)) { return; diff --git a/src/main/java/de/bwravencl/controllerbuddy/input/action/ButtonToButtonAction.java b/src/main/java/de/bwravencl/controllerbuddy/input/action/ButtonToButtonAction.java index 67787515..69a36f00 100644 --- a/src/main/java/de/bwravencl/controllerbuddy/input/action/ButtonToButtonAction.java +++ b/src/main/java/de/bwravencl/controllerbuddy/input/action/ButtonToButtonAction.java @@ -32,7 +32,7 @@ public final class ButtonToButtonAction extends ToButtonAction implements public void doAction(final Input input, final int component, Byte value) { value = handleLongPress(input, component, value); - final var hot = handleMinActivationInterval(value != 0); + final var hot = handleActivationInterval(value != 0); if (isAlreadyPressed(input)) { return; diff --git a/src/main/java/de/bwravencl/controllerbuddy/input/action/ToButtonAction.java b/src/main/java/de/bwravencl/controllerbuddy/input/action/ToButtonAction.java index 2ef3589a..5a035eaf 100644 --- a/src/main/java/de/bwravencl/controllerbuddy/input/action/ToButtonAction.java +++ b/src/main/java/de/bwravencl/controllerbuddy/input/action/ToButtonAction.java @@ -22,7 +22,7 @@ import de.bwravencl.controllerbuddy.input.action.gui.ButtonEditorBuilder; import java.text.MessageFormat; -public abstract class ToButtonAction extends MinActivationIntervalAction { +public abstract class ToButtonAction extends ActivationIntervalAction { @ActionProperty(label = "BUTTON_ID", editorBuilder = ButtonEditorBuilder.class, order = 10) int buttonId = 0; diff --git a/src/main/java/de/bwravencl/controllerbuddy/input/action/ToKeyAction.java b/src/main/java/de/bwravencl/controllerbuddy/input/action/ToKeyAction.java index 9b2e9b28..90620e29 100644 --- a/src/main/java/de/bwravencl/controllerbuddy/input/action/ToKeyAction.java +++ b/src/main/java/de/bwravencl/controllerbuddy/input/action/ToKeyAction.java @@ -25,7 +25,7 @@ import de.bwravencl.controllerbuddy.input.action.gui.LongPressEditorBuilder; import java.text.MessageFormat; -abstract class ToKeyAction extends MinActivationIntervalAction +abstract class ToKeyAction extends ActivationIntervalAction implements IActivatableAction, ILongPressAction, IResetableAction { @ActionProperty(label = "ACTIVATION", editorBuilder = ActivationEditorBuilder.class, order = 11) @@ -73,7 +73,7 @@ public KeyStroke getKeystroke() { } void handleAction(boolean hot, final Input input) { - hot = handleMinActivationInterval(hot); + hot = handleActivationInterval(hot); if (activatable == Activatable.ALWAYS) { input.getDownUpKeyStrokes().add(keystroke); diff --git a/src/main/java/de/bwravencl/controllerbuddy/input/action/ToMouseButtonAction.java b/src/main/java/de/bwravencl/controllerbuddy/input/action/ToMouseButtonAction.java index 94e93ffb..e01efb32 100644 --- a/src/main/java/de/bwravencl/controllerbuddy/input/action/ToMouseButtonAction.java +++ b/src/main/java/de/bwravencl/controllerbuddy/input/action/ToMouseButtonAction.java @@ -24,7 +24,7 @@ import de.bwravencl.controllerbuddy.input.action.gui.MouseButtonEditorBuilder; import java.text.MessageFormat; -abstract class ToMouseButtonAction extends MinActivationIntervalAction +abstract class ToMouseButtonAction extends ActivationIntervalAction implements IActivatableAction, ILongPressAction { private static final int DEFAULT_MOUSE_BUTTON = 1; @@ -66,7 +66,7 @@ public int getMouseButton() { } void handleAction(boolean hot, final Input input) { - hot = handleMinActivationInterval(hot); + hot = handleActivationInterval(hot); if (activatable == Activatable.ALWAYS) { input.getDownUpMouseButtons().add(mouseButton); diff --git a/src/main/java/de/bwravencl/controllerbuddy/input/action/gui/MinActivationIntervalEditorBuilder.java b/src/main/java/de/bwravencl/controllerbuddy/input/action/gui/ActivationIntervalEditorBuilder.java similarity index 93% rename from src/main/java/de/bwravencl/controllerbuddy/input/action/gui/MinActivationIntervalEditorBuilder.java rename to src/main/java/de/bwravencl/controllerbuddy/input/action/gui/ActivationIntervalEditorBuilder.java index e141dfeb..d4532430 100644 --- a/src/main/java/de/bwravencl/controllerbuddy/input/action/gui/MinActivationIntervalEditorBuilder.java +++ b/src/main/java/de/bwravencl/controllerbuddy/input/action/gui/ActivationIntervalEditorBuilder.java @@ -24,9 +24,9 @@ import javax.swing.JSpinner; import javax.swing.text.DefaultFormatter; -public final class MinActivationIntervalEditorBuilder extends NumberEditorBuilder { +public final class ActivationIntervalEditorBuilder extends NumberEditorBuilder { - public MinActivationIntervalEditorBuilder( + public ActivationIntervalEditorBuilder( final EditActionsDialog editActionsDialog, final IAction action, final String fieldName, diff --git a/src/main/resources/strings.properties b/src/main/resources/strings.properties index 3ad2eb63..ab9da3d7 100644 --- a/src/main/resources/strings.properties +++ b/src/main/resources/strings.properties @@ -350,6 +350,7 @@ INVERT = Invert VIRTUAL_AXIS = Virtual Axis BUTTON_ID = Virtual Button MIN_ACTIVATION_INTERVAL = Min. Activation Interval +MAX_ACTIVATION_INTERVAL = Max. Activation Interval ACTIVATION = Activation KEYSTROKE = Keystroke Combination MOUSE_BUTTON = Mouse Button diff --git a/src/main/resources/strings_de_DE.properties b/src/main/resources/strings_de_DE.properties index 3c212c0e..fa2f7ad9 100644 --- a/src/main/resources/strings_de_DE.properties +++ b/src/main/resources/strings_de_DE.properties @@ -323,6 +323,7 @@ INVERT = Invertieren VIRTUAL_AXIS = Virtuelle Achse BUTTON_ID = Virtueller Button MIN_ACTIVATION_INTERVAL = Min. Aktivierungsintervall +MAX_ACTIVATION_INTERVAL = Max. Aktivierungsintervall ACTIVATION = Aktivierung KEYSTROKE = Tastenkombination MOUSE_BUTTON = Maustaste