mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-13 00:37:52 +00:00
Re-Add key binding modifiers and contexts.
This commit is contained in:
1 parent
9baabf9249
commit
21d39f6558
10 files changed
+630
-2
No files matched your search
@@ -0,0 +1,20 @@
|
||||
package net.minecraftforge.client.settings;
|
||||
|
||||
import net.minecraft.client.settings.KeyBinding;
|
||||
|
||||
/**
|
||||
* Defines the context that a {@link KeyBinding} is used.
|
||||
* Key conflicts occur when a {@link KeyBinding} has the same {@link IKeyConflictContext} and has conflicting modifiers and keyCodes.
|
||||
*/
|
||||
public interface IKeyConflictContext {
|
||||
/**
|
||||
* @return true if conditions are met to activate {@link KeyBinding}s with this context
|
||||
*/
|
||||
boolean isActive();
|
||||
|
||||
/**
|
||||
* @return true if the other context can have {@link KeyBinding} conflicts with this one.
|
||||
* This will be called on both contexts to check for conflicts.
|
||||
*/
|
||||
boolean conflicts(IKeyConflictContext other);
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package net.minecraftforge.client.settings;
|
||||
|
||||
import net.minecraft.client.settings.KeyBinding;
|
||||
import net.minecraft.util.IntHashMap;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.EnumMap;
|
||||
import java.util.List;
|
||||
|
||||
public class KeyBindingMap
|
||||
{
|
||||
private static final EnumMap<KeyModifier, IntHashMap<Collection<KeyBinding>>> map = new java.util.EnumMap<KeyModifier, IntHashMap<Collection<KeyBinding>>>(KeyModifier.class);
|
||||
static
|
||||
{
|
||||
for (KeyModifier modifier : KeyModifier.values())
|
||||
{
|
||||
map.put(modifier, new IntHashMap<Collection<KeyBinding>>());
|
||||
}
|
||||
}
|
||||
|
||||
public KeyBinding lookupActive(int keyCode)
|
||||
{
|
||||
KeyModifier activeModifier = KeyModifier.isKeyCodeModifier(keyCode) ? KeyModifier.NONE : KeyModifier.getActiveModifier();
|
||||
Collection<KeyBinding> bindings = map.get(activeModifier).lookup(keyCode);
|
||||
if (bindings != null)
|
||||
{
|
||||
for (KeyBinding binding : bindings)
|
||||
{
|
||||
if (binding.isActiveAndMatches(keyCode))
|
||||
{
|
||||
return binding;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<KeyBinding> lookupAll(int keyCode)
|
||||
{
|
||||
List<KeyBinding> matchingBindings = new ArrayList<KeyBinding>();
|
||||
for (IntHashMap<Collection<KeyBinding>> bindingsMap : map.values())
|
||||
{
|
||||
Collection<KeyBinding> bindings = bindingsMap.lookup(keyCode);
|
||||
if (bindings != null)
|
||||
{
|
||||
matchingBindings.addAll(bindings);
|
||||
}
|
||||
}
|
||||
return matchingBindings;
|
||||
}
|
||||
|
||||
public void addKey(int keyCode, KeyBinding keyBinding)
|
||||
{
|
||||
KeyModifier keyModifier = keyBinding.getKeyModifier();
|
||||
IntHashMap<Collection<KeyBinding>> bindingsMap = map.get(keyModifier);
|
||||
Collection<KeyBinding> bindingsForKey = bindingsMap.lookup(keyCode);
|
||||
if (bindingsForKey == null)
|
||||
{
|
||||
bindingsForKey = new ArrayList<KeyBinding>();
|
||||
bindingsMap.addKey(keyCode, bindingsForKey);
|
||||
}
|
||||
bindingsForKey.add(keyBinding);
|
||||
}
|
||||
|
||||
public void removeKey(KeyBinding keyBinding)
|
||||
{
|
||||
KeyModifier keyModifier = keyBinding.getKeyModifier();
|
||||
int keyCode = keyBinding.getKeyCode();
|
||||
IntHashMap<Collection<KeyBinding>> bindingsMap = map.get(keyModifier);
|
||||
Collection<KeyBinding> bindingsForKey = bindingsMap.lookup(keyCode);
|
||||
if (bindingsForKey != null)
|
||||
{
|
||||
bindingsForKey.remove(keyBinding);
|
||||
if (bindingsForKey.isEmpty())
|
||||
{
|
||||
bindingsMap.removeObject(keyCode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void clearMap()
|
||||
{
|
||||
for (IntHashMap<Collection<KeyBinding>> bindings : map.values())
|
||||
{
|
||||
bindings.clearMap();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package net.minecraftforge.client.settings;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.GuiScreen;
|
||||
|
||||
public enum KeyConflictContext implements IKeyConflictContext
|
||||
{
|
||||
/**
|
||||
* Universal key bindings are used in every context and will conflict with any other context.
|
||||
* Key Bindings are universal by default.
|
||||
*/
|
||||
UNIVERSAL {
|
||||
@Override
|
||||
public boolean isActive()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean conflicts(IKeyConflictContext other)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Gui key bindings are only used when a {@link GuiScreen} is open.
|
||||
*/
|
||||
GUI {
|
||||
@Override
|
||||
public boolean isActive()
|
||||
{
|
||||
return Minecraft.getMinecraft().currentScreen != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean conflicts(IKeyConflictContext other)
|
||||
{
|
||||
return this == other;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* In-game key bindings are only used when a {@link GuiScreen} is not open.
|
||||
*/
|
||||
IN_GAME {
|
||||
@Override
|
||||
public boolean isActive()
|
||||
{
|
||||
return !GUI.isActive();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean conflicts(IKeyConflictContext other)
|
||||
{
|
||||
return this == other;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
package net.minecraftforge.client.settings;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.GuiScreen;
|
||||
import net.minecraft.client.resources.I18n;
|
||||
import org.lwjgl.input.Keyboard;
|
||||
|
||||
public enum KeyModifier {
|
||||
CONTROL {
|
||||
@Override
|
||||
protected boolean matches(int keyCode)
|
||||
{
|
||||
if (Minecraft.isRunningOnMac)
|
||||
{
|
||||
return keyCode == Keyboard.KEY_LMETA || keyCode == Keyboard.KEY_RMETA;
|
||||
}
|
||||
else
|
||||
{
|
||||
return keyCode == Keyboard.KEY_LCONTROL || keyCode == Keyboard.KEY_RCONTROL;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isActive()
|
||||
{
|
||||
return GuiScreen.isCtrlKeyDown();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLocalizedComboName(String keyName)
|
||||
{
|
||||
return I18n.format("forge.controlsgui.control", keyName);
|
||||
}
|
||||
},
|
||||
SHIFT {
|
||||
@Override
|
||||
protected boolean matches(int keyCode)
|
||||
{
|
||||
return keyCode == Keyboard.KEY_LSHIFT || keyCode == Keyboard.KEY_RSHIFT;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isActive()
|
||||
{
|
||||
return GuiScreen.isShiftKeyDown();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLocalizedComboName(String keyName)
|
||||
{
|
||||
return I18n.format("forge.controlsgui.shift", keyName);
|
||||
}
|
||||
},
|
||||
ALT {
|
||||
@Override
|
||||
protected boolean matches(int keyCode)
|
||||
{
|
||||
return keyCode == Keyboard.KEY_LMENU || keyCode == Keyboard.KEY_RMENU;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isActive()
|
||||
{
|
||||
return GuiScreen.isAltKeyDown();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLocalizedComboName(String keyName)
|
||||
{
|
||||
return I18n.format("forge.controlsgui.alt", keyName);
|
||||
}
|
||||
},
|
||||
NONE {
|
||||
@Override
|
||||
protected boolean matches(int keyCode)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isActive()
|
||||
{
|
||||
return !SHIFT.isActive() && !CONTROL.isActive() && !ALT.isActive();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLocalizedComboName(String keyName)
|
||||
{
|
||||
return keyName;
|
||||
}
|
||||
};
|
||||
|
||||
public static final KeyModifier[] MODIFIER_VALUES = {SHIFT, CONTROL, ALT};
|
||||
|
||||
public static KeyModifier getActiveModifier()
|
||||
{
|
||||
for (KeyModifier keyModifier : MODIFIER_VALUES)
|
||||
{
|
||||
if (keyModifier.isActive())
|
||||
{
|
||||
return keyModifier;
|
||||
}
|
||||
}
|
||||
return NONE;
|
||||
}
|
||||
|
||||
public static boolean isKeyCodeModifier(int keyCode)
|
||||
{
|
||||
for (KeyModifier keyModifier : MODIFIER_VALUES)
|
||||
{
|
||||
if (keyModifier.matches(keyCode))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected abstract boolean matches(int keyCode);
|
||||
|
||||
public abstract boolean isActive();
|
||||
|
||||
public abstract String getLocalizedComboName(String keyName);
|
||||
}
|
||||
Reference in new issue
Block a user