Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Commit 5cb31ac

Browse files
SnakefangoxTheGlitch76
andauthored
Add support for keybindings (#154)
* Added patchwork-key-bindings * Added patchwork keybinding for Patcher Cleaning up remaining absolute imports * Fix missing import * Fix archive base name * Fix folders, paths and project names * Fix KeyBinding names and add @unique * Should probably respect keybind context * Remove now incorrect comment Co-authored-by: Glitch <[email protected]> Co-authored-by: Glitch <[email protected]>
1 parent 9d5cca0 commit 5cb31ac

File tree

12 files changed

+557
-0
lines changed

12 files changed

+557
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
archivesBaseName = "patchwork-key-bindings"
2+
version = getSubprojectVersion(project, "0.1.0")
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.minecraftforge.client.extensions;
21+
22+
import javax.annotation.Nonnull;
23+
24+
import net.minecraftforge.client.settings.IKeyConflictContext;
25+
import net.minecraftforge.client.settings.KeyModifier;
26+
27+
import net.minecraft.client.options.KeyBinding;
28+
import net.minecraft.client.util.InputUtil;
29+
30+
public interface IForgeKeybinding {
31+
default KeyBinding getKeyBinding() {
32+
return (KeyBinding) this;
33+
}
34+
35+
@Nonnull
36+
InputUtil.KeyCode getKey();
37+
38+
/**
39+
* Checks that the key conflict context and modifier are active, and that the keyCode matches this binding.
40+
*/
41+
default boolean isActiveAndMatches(InputUtil.KeyCode keyCode) {
42+
return keyCode.getKeyCode() != 0 && keyCode.equals(getKey()) && getKeyConflictContext().isActive() && getKeyModifier().isActive(getKeyConflictContext());
43+
}
44+
45+
default void setToDefault() {
46+
setKeyModifierAndCode(getKeyModifierDefault(), getKeyBinding().getDefaultKeyCode());
47+
}
48+
49+
IKeyConflictContext getKeyConflictContext();
50+
51+
void setKeyConflictContext(IKeyConflictContext keyConflictContext);
52+
53+
KeyModifier getKeyModifierDefault();
54+
55+
KeyModifier getKeyModifier();
56+
57+
void setKeyModifierAndCode(KeyModifier keyModifier, InputUtil.KeyCode keyCode);
58+
59+
/**
60+
* Returns true when one of the bindings' key codes conflicts with the other's modifier.
61+
*/
62+
default boolean hasKeyCodeModifierConflict(KeyBinding other) {
63+
if (getKeyConflictContext().conflicts(((IForgeKeybinding) other).getKeyConflictContext()) || ((IForgeKeybinding) other).getKeyConflictContext().conflicts(getKeyConflictContext())) {
64+
return getKeyModifier().matches(((IForgeKeybinding) other).getKey()) || ((IForgeKeybinding) other).getKeyModifier().matches(getKey());
65+
}
66+
67+
return false;
68+
}
69+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.minecraftforge.client.settings;
21+
22+
/**
23+
* Defines the context that a {@link KeyBinding} is used.
24+
* Key conflicts occur when a {@link KeyBinding} has the same {@link IKeyConflictContext} and has conflicting modifiers and keyCodes.
25+
*/
26+
public interface IKeyConflictContext {
27+
/**
28+
* @return true if conditions are met to activate {@link KeyBinding}s with this context
29+
*/
30+
boolean isActive();
31+
32+
/**
33+
* @return true if the other context can have {@link KeyBinding} conflicts with this one.
34+
* This will be called on both contexts to check for conflicts.
35+
*/
36+
boolean conflicts(IKeyConflictContext other);
37+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.minecraftforge.client.settings;
21+
22+
import net.minecraft.client.MinecraftClient;
23+
24+
public enum KeyConflictContext implements IKeyConflictContext {
25+
/**
26+
* Universal key bindings are used in every context and will conflict with any other context.
27+
* Key Bindings are universal by default.
28+
*/
29+
UNIVERSAL {
30+
@Override
31+
public boolean isActive() {
32+
return true;
33+
}
34+
35+
@Override
36+
public boolean conflicts(IKeyConflictContext other) {
37+
return true;
38+
}
39+
},
40+
41+
/**
42+
* Gui key bindings are only used when a {@link GuiScreen} is open.
43+
*/
44+
GUI {
45+
@Override
46+
public boolean isActive() {
47+
return MinecraftClient.getInstance().currentScreen != null;
48+
}
49+
50+
@Override
51+
public boolean conflicts(IKeyConflictContext other) {
52+
return this == other;
53+
}
54+
},
55+
56+
/**
57+
* In-game key bindings are only used when a {@link GuiScreen} is not open.
58+
*/
59+
IN_GAME {
60+
@Override
61+
public boolean isActive() {
62+
return !GUI.isActive();
63+
}
64+
65+
@Override
66+
public boolean conflicts(IKeyConflictContext other) {
67+
return this == other;
68+
}
69+
}
70+
}
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.minecraftforge.client.settings;
21+
22+
import java.util.function.Supplier;
23+
24+
import javax.annotation.Nullable;
25+
26+
import org.lwjgl.glfw.GLFW;
27+
28+
import net.minecraft.client.MinecraftClient;
29+
import net.minecraft.client.gui.screen.Screen;
30+
import net.minecraft.client.resource.language.I18n;
31+
import net.minecraft.client.util.InputUtil;
32+
33+
public enum KeyModifier {
34+
CONTROL {
35+
@Override
36+
public boolean matches(InputUtil.KeyCode key) {
37+
int keyCode = key.getKeyCode();
38+
39+
if (MinecraftClient.IS_SYSTEM_MAC) {
40+
return keyCode == GLFW.GLFW_KEY_LEFT_ALT || keyCode == GLFW.GLFW_KEY_RIGHT_ALT;
41+
} else {
42+
return keyCode == GLFW.GLFW_KEY_LEFT_CONTROL || keyCode == GLFW.GLFW_KEY_RIGHT_CONTROL;
43+
}
44+
}
45+
46+
@Override
47+
public boolean isActive(@Nullable IKeyConflictContext conflictContext) {
48+
return Screen.hasControlDown();
49+
}
50+
51+
@Override
52+
public String getLocalizedComboName(InputUtil.KeyCode key, Supplier<String> defaultLogic) {
53+
String keyName = defaultLogic.get();
54+
String localizationFormatKey = MinecraftClient.IS_SYSTEM_MAC ? "forge.controlsgui.control.mac" : "forge.controlsgui.control";
55+
return I18n.translate(localizationFormatKey, keyName);
56+
}
57+
},
58+
SHIFT {
59+
@Override
60+
public boolean matches(InputUtil.KeyCode key) {
61+
return key.getKeyCode() == GLFW.GLFW_KEY_LEFT_SHIFT || key.getKeyCode() == GLFW.GLFW_KEY_RIGHT_SHIFT;
62+
}
63+
64+
@Override
65+
public boolean isActive(@Nullable IKeyConflictContext conflictContext) {
66+
return Screen.hasShiftDown();
67+
}
68+
69+
@Override
70+
public String getLocalizedComboName(InputUtil.KeyCode key, Supplier<String> defaultLogic) {
71+
return I18n.translate("forge.controlsgui.shift", defaultLogic.get());
72+
}
73+
},
74+
ALT {
75+
@Override
76+
public boolean matches(InputUtil.KeyCode key) {
77+
return key.getKeyCode() == GLFW.GLFW_KEY_LEFT_ALT || key.getKeyCode() == GLFW.GLFW_KEY_RIGHT_ALT;
78+
}
79+
80+
@Override
81+
public boolean isActive(@Nullable IKeyConflictContext conflictContext) {
82+
return Screen.hasAltDown();
83+
}
84+
85+
@Override
86+
public String getLocalizedComboName(InputUtil.KeyCode keyCode, Supplier<String> defaultLogic) {
87+
return I18n.translate("forge.controlsgui.alt", defaultLogic.get());
88+
}
89+
},
90+
NONE {
91+
@Override
92+
public boolean matches(InputUtil.KeyCode key) {
93+
return false;
94+
}
95+
96+
@Override
97+
public boolean isActive(@Nullable IKeyConflictContext conflictContext) {
98+
if (conflictContext != null && !conflictContext.conflicts(KeyConflictContext.IN_GAME)) {
99+
for (KeyModifier keyModifier : MODIFIER_VALUES) {
100+
if (keyModifier.isActive(conflictContext)) {
101+
return false;
102+
}
103+
}
104+
}
105+
106+
return true;
107+
}
108+
109+
@Override
110+
public String getLocalizedComboName(InputUtil.KeyCode key, Supplier<String> defaultLogic) {
111+
return defaultLogic.get();
112+
}
113+
};
114+
115+
public static final KeyModifier[] MODIFIER_VALUES = {SHIFT, CONTROL, ALT};
116+
117+
public static KeyModifier getActiveModifier() {
118+
for (KeyModifier keyModifier : MODIFIER_VALUES) {
119+
if (keyModifier.isActive(null)) {
120+
return keyModifier;
121+
}
122+
}
123+
124+
return NONE;
125+
}
126+
127+
public static boolean isKeyCodeModifier(InputUtil.KeyCode key) {
128+
for (KeyModifier keyModifier : MODIFIER_VALUES) {
129+
if (keyModifier.matches(key)) {
130+
return true;
131+
}
132+
}
133+
134+
return false;
135+
}
136+
137+
public static KeyModifier valueFromString(String stringValue) {
138+
try {
139+
return valueOf(stringValue);
140+
} catch (NullPointerException | IllegalArgumentException ignored) {
141+
return NONE;
142+
}
143+
}
144+
145+
public abstract boolean matches(InputUtil.KeyCode key);
146+
147+
public abstract boolean isActive(@Nullable IKeyConflictContext conflictContext);
148+
149+
public abstract String getLocalizedComboName(InputUtil.KeyCode key, Supplier<String> defaultLogic);
150+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.patchworkmc.impl.keybindings;
21+
22+
import net.minecraftforge.client.settings.IKeyConflictContext;
23+
import net.minecraftforge.client.settings.KeyModifier;
24+
25+
public interface ForgeKeyBindingConstruct {
26+
void patchwork$constructForgeKeyBindingOptions(IKeyConflictContext keyConflictContext, KeyModifier keyModifier);
27+
}

0 commit comments

Comments
 (0)