Skip to content

Commit

Permalink
ToButtonAction, ToKeyAction, ToMouseButtonAction: added maximum activ…
Browse files Browse the repository at this point in the history
…ation interval analogous to already implemented minimum activation interval
  • Loading branch information
bwRavencl committed Feb 24, 2024
1 parent 3407196 commit 3ceda4d
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<V extends Number> extends DescribableAction<V>
abstract class ActivationIntervalAction<V extends Number> extends DescribableAction<V>
implements IInitializationAction<V> {

@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;
}
}

Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public final class ButtonToButtonAction extends ToButtonAction<Byte> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import de.bwravencl.controllerbuddy.input.action.gui.ButtonEditorBuilder;
import java.text.MessageFormat;

public abstract class ToButtonAction<V extends Number> extends MinActivationIntervalAction<V> {
public abstract class ToButtonAction<V extends Number> extends ActivationIntervalAction<V> {

@ActionProperty(label = "BUTTON_ID", editorBuilder = ButtonEditorBuilder.class, order = 10)
int buttonId = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import de.bwravencl.controllerbuddy.input.action.gui.LongPressEditorBuilder;
import java.text.MessageFormat;

abstract class ToKeyAction<V extends Number> extends MinActivationIntervalAction<V>
abstract class ToKeyAction<V extends Number> extends ActivationIntervalAction<V>
implements IActivatableAction<V>, ILongPressAction<V>, IResetableAction<V> {

@ActionProperty(label = "ACTIVATION", editorBuilder = ActivationEditorBuilder.class, order = 11)
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import de.bwravencl.controllerbuddy.input.action.gui.MouseButtonEditorBuilder;
import java.text.MessageFormat;

abstract class ToMouseButtonAction<V extends Number> extends MinActivationIntervalAction<V>
abstract class ToMouseButtonAction<V extends Number> extends ActivationIntervalAction<V>
implements IActivatableAction<V>, ILongPressAction<V> {

private static final int DEFAULT_MOUSE_BUTTON = 1;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
import javax.swing.JSpinner;
import javax.swing.text.DefaultFormatter;

public final class MinActivationIntervalEditorBuilder extends NumberEditorBuilder<Integer> {
public final class ActivationIntervalEditorBuilder extends NumberEditorBuilder<Integer> {

public MinActivationIntervalEditorBuilder(
public ActivationIntervalEditorBuilder(
final EditActionsDialog editActionsDialog,
final IAction<?> action,
final String fieldName,
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/strings.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/strings_de_DE.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3ceda4d

Please sign in to comment.