| 1 | package net.minecraft.client;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.platform.InputConstants;
|
| 4 | import java.util.function.BooleanSupplier;
|
| 5 | import net.fabricmc.api.EnvType;
|
| 6 | import net.fabricmc.api.Environment;
|
| 7 |
|
| 8 | @Environment(EnvType.CLIENT)
|
| 9 | public class ToggleKeyMapping extends KeyMapping {
|
| 10 | private final BooleanSupplier needsToggle;
|
| 11 | private boolean releasedByScreenWhenDown;
|
| 12 | private final boolean shouldRestore;
|
| 13 |
|
| 14 | public ToggleKeyMapping(final String name, final int value, final KeyMapping.Category category, final BooleanSupplier needsToggle, final boolean shouldRestore) {
|
| 15 | this(name, InputConstants.Type.KEYBOARD, value, category, needsToggle, shouldRestore);
|
| 16 | }
|
| 17 |
|
| 18 | public ToggleKeyMapping(
|
| 19 | final String name,
|
| 20 | final InputConstants.Type type,
|
| 21 | final int value,
|
| 22 | final KeyMapping.Category category,
|
| 23 | final BooleanSupplier needsToggle,
|
| 24 | final boolean shouldRestore
|
| 25 | ) {
|
| 26 | super(name, type, value, category);
|
| 27 | this.needsToggle = needsToggle;
|
| 28 | this.shouldRestore = shouldRestore;
|
| 29 | }
|
| 30 |
|
| 31 | @Override
|
| 32 | protected boolean shouldSetOnIngameFocus() {
|
| 33 | return super.shouldSetOnIngameFocus() && !this.needsToggle.getAsBoolean();
|
| 34 | }
|
| 35 |
|
| 36 | @Override
|
| 37 | public void setDown(final boolean down) {
|
| 38 | if (this.needsToggle.getAsBoolean()) {
|
| 39 | if (down) {
|
| 40 | super.setDown(!this.isDown());
|
| 41 | }
|
| 42 | } else {
|
| 43 | super.setDown(down);
|
| 44 | }
|
| 45 | }
|
| 46 |
|
| 47 | @Override
|
| 48 | protected void release() {
|
| 49 | if (this.needsToggle.getAsBoolean() && this.isDown() || this.releasedByScreenWhenDown) {
|
| 50 | this.releasedByScreenWhenDown = true;
|
| 51 | }
|
| 52 |
|
| 53 | this.reset();
|
| 54 | }
|
| 55 |
|
| 56 | public boolean shouldRestoreStateOnScreenClosed() {
|
| 57 | boolean shouldRestore = this.shouldRestore
|
| 58 | && this.needsToggle.getAsBoolean()
|
| 59 | && this.key.getType() == InputConstants.Type.KEYBOARD
|
| 60 | && this.releasedByScreenWhenDown;
|
| 61 | this.releasedByScreenWhenDown = false;
|
| 62 | return shouldRestore;
|
| 63 | }
|
| 64 |
|
| 65 | protected void reset() {
|
| 66 | super.setDown(false);
|
| 67 | }
|
| 68 | }
|
| 69 |
|