-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve & generalise keybind code (#152)
- Loading branch information
Showing
7 changed files
with
241 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
common/src/main/java/net/xolt/freecam/config/keys/FreecamComboKeyMapping.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package net.xolt.freecam.config.keys; | ||
|
||
import com.mojang.blaze3d.platform.InputConstants; | ||
|
||
|
||
class FreecamComboKeyMapping extends FreecamKeyMapping { | ||
|
||
private final Runnable action; | ||
private final HoldAction holdAction; | ||
private final long maxHoldTicks; | ||
|
||
private boolean usedWhileHeld = false; | ||
private long ticksHeld = 0; | ||
|
||
FreecamComboKeyMapping(String translationKey, InputConstants.Type type, int code, Runnable action, HoldAction holdAction, long maxHoldTicks) { | ||
super(translationKey, type, code); | ||
this.action = action; | ||
this.holdAction = holdAction; | ||
this.maxHoldTicks = maxHoldTicks; | ||
} | ||
|
||
@Override | ||
public void tick() { | ||
if (isDown()) { | ||
// Count held ticks, so we can run action on release | ||
ticksHeld++; | ||
reset(); | ||
|
||
// Handle combo actions | ||
if (holdAction.run()) { | ||
usedWhileHeld = true; | ||
} | ||
} | ||
// Check if pressed, but now released | ||
else if (consumeClick() || ticksHeld > 0) { | ||
// Only run action if the key wasn't used (or held too long) | ||
if (!usedWhileHeld && ticksHeld < maxHoldTicks) { | ||
action.run(); | ||
} | ||
// Reset state | ||
reset(); | ||
ticksHeld = 0; | ||
usedWhileHeld = false; | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
common/src/main/java/net/xolt/freecam/config/keys/FreecamKeyMapping.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package net.xolt.freecam.config.keys; | ||
|
||
import com.mojang.blaze3d.platform.InputConstants; | ||
import net.minecraft.client.KeyMapping; | ||
import net.minecraft.client.renderer.texture.Tickable; | ||
|
||
import java.util.function.Consumer; | ||
|
||
public class FreecamKeyMapping extends KeyMapping implements Tickable { | ||
|
||
private final Consumer<FreecamKeyMapping> onTick; | ||
|
||
/** | ||
* @apiNote should only be used if overriding {@link #tick()} | ||
*/ | ||
protected FreecamKeyMapping(String translationKey, InputConstants.Type type, int code) { | ||
this(translationKey, type, code, null); | ||
} | ||
|
||
FreecamKeyMapping(String translationKey, InputConstants.Type type, int code, Consumer<FreecamKeyMapping> onTick) { | ||
super("key.freecam." + translationKey, type, code, "category.freecam.freecam"); | ||
this.onTick = onTick; | ||
} | ||
|
||
@Override | ||
public void tick() { | ||
onTick.accept(this); | ||
} | ||
|
||
/** | ||
* Reset whether the key was pressed. | ||
* <p> | ||
* @implNote Cannot use {@link KeyMapping#release()} because it doesn't work as expected. | ||
*/ | ||
@SuppressWarnings("StatementWithEmptyBody") | ||
public void reset() { | ||
while (consumeClick()) {} | ||
} | ||
} |
Oops, something went wrong.