Mekanism-tilera-Edition/src/main/java/mekanism/client/MekKeyHandler.java

107 lines
2.7 KiB
Java
Raw Normal View History

package mekanism.client;
import java.util.EnumSet;
import net.minecraft.client.settings.KeyBinding;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.TickEvent.ClientTickEvent;
import cpw.mods.fml.common.gameevent.TickEvent.Phase;
import cpw.mods.fml.common.gameevent.TickEvent.Type;
import cpw.mods.fml.relauncher.Side;
public abstract class MekKeyHandler
{
public KeyBinding[] keyBindings;
public boolean[] keyDown;
public boolean[] repeatings;
public boolean isDummy;
/**
* Pass an array of keybindings and a repeat flag for each one
*
* @param keyBindings
* @param repeatings
*/
2014-06-02 20:04:10 +02:00
public MekKeyHandler(KeyBinding[] bindings, boolean[] rep)
{
assert keyBindings.length == repeatings.length : "You need to pass two arrays of identical length";
2014-06-02 20:04:10 +02:00
keyBindings = bindings;
repeatings = rep;
keyDown = new boolean[keyBindings.length];
}
/**
* Register the keys into the system. You will do your own keyboard
* management elsewhere. No events will fire if you use this method
*
* @param keyBindings
*/
2014-06-02 20:04:10 +02:00
public MekKeyHandler(KeyBinding[] bindings)
{
2014-06-02 20:04:10 +02:00
keyBindings = bindings;
isDummy = true;
}
public KeyBinding[] getKeyBindings ()
{
2014-06-02 20:04:10 +02:00
return keyBindings;
}
2014-05-29 20:01:43 +02:00
public void keyTick(Type type, boolean tickEnd)
{
2014-06-02 20:04:10 +02:00
for(int i = 0; i < keyBindings.length; i++)
{
KeyBinding keyBinding = keyBindings[i];
int keyCode = keyBinding.getKeyCode();
boolean state = (keyCode < 0 ? Mouse.isButtonDown(keyCode + 100) : Keyboard.isKeyDown(keyCode));
2014-06-02 20:04:10 +02:00
if(state != keyDown[i] || (state && repeatings[i]))
{
2014-06-02 20:04:10 +02:00
if(state)
{
keyDown(type, keyBinding, tickEnd, state != keyDown[i]);
}
2014-06-02 20:04:10 +02:00
else {
keyUp(type, keyBinding, tickEnd);
}
2014-06-02 20:04:10 +02:00
if(tickEnd)
{
keyDown[i] = state;
}
}
}
}
/**
* Called when the key is first in the down position on any tick from the
* {@link #ticks()} set. Will be called subsequently with isRepeat set to
* true
*
* @see #keyUp(EnumSet, KeyBinding, boolean)
*
* @param types
* the type(s) of tick that fired when this key was first down
* @param tickEnd
* was it an end or start tick which fired the key
* @param isRepeat
* is it a repeat key event
*/
2014-06-02 20:04:10 +02:00
public abstract void keyDown(Type types, KeyBinding kb, boolean tickEnd, boolean isRepeat);
/**
* Fired once when the key changes state from down to up
*
* @see #keyDown(EnumSet, KeyBinding, boolean, boolean)
*
* @param types
* the type(s) of tick that fired when this key was first down
* @param tickEnd
* was it an end or start tick which fired the key
*/
2014-06-02 20:04:10 +02:00
public abstract void keyUp(Type types, KeyBinding kb, boolean tickEnd);
}