2014-04-20 04:44:06 +02:00
|
|
|
package mekanism.client;
|
|
|
|
|
|
|
|
import net.minecraft.client.settings.KeyBinding;
|
|
|
|
import cpw.mods.fml.common.gameevent.TickEvent.Type;
|
|
|
|
|
|
|
|
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
|
|
|
|
*
|
2014-08-27 02:16:32 +02:00
|
|
|
* @param bindings
|
|
|
|
* @param rep
|
2014-04-20 04:44:06 +02:00
|
|
|
*/
|
2014-06-02 20:04:10 +02:00
|
|
|
public MekKeyHandler(KeyBinding[] bindings, boolean[] rep)
|
2014-04-20 04:44:06 +02:00
|
|
|
{
|
|
|
|
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];
|
2014-04-20 04:44:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register the keys into the system. You will do your own keyboard
|
|
|
|
* management elsewhere. No events will fire if you use this method
|
|
|
|
*
|
2014-08-27 02:16:32 +02:00
|
|
|
* @param bindings
|
2014-04-20 04:44:06 +02:00
|
|
|
*/
|
2014-06-02 20:04:10 +02:00
|
|
|
public MekKeyHandler(KeyBinding[] bindings)
|
2014-04-20 04:44:06 +02:00
|
|
|
{
|
2014-06-02 20:04:10 +02:00
|
|
|
keyBindings = bindings;
|
|
|
|
isDummy = true;
|
2014-04-20 04:44:06 +02:00
|
|
|
}
|
|
|
|
|
2014-09-11 05:05:45 +02:00
|
|
|
public static boolean getIsKeyPressed(KeyBinding keyBinding)
|
|
|
|
{
|
|
|
|
int keyCode = keyBinding.getKeyCode();
|
|
|
|
return keyCode < 0 ? Mouse.isButtonDown(keyCode + 100) : Keyboard.isKeyDown(keyCode);
|
|
|
|
}
|
|
|
|
|
2014-04-20 04:44:06 +02:00
|
|
|
public KeyBinding[] getKeyBindings ()
|
|
|
|
{
|
2014-06-02 20:04:10 +02:00
|
|
|
return keyBindings;
|
2014-04-20 04:44:06 +02:00
|
|
|
}
|
|
|
|
|
2014-05-29 20:01:43 +02:00
|
|
|
public void keyTick(Type type, boolean tickEnd)
|
2014-04-20 04:44:06 +02:00
|
|
|
{
|
2014-06-02 20:04:10 +02:00
|
|
|
for(int i = 0; i < keyBindings.length; i++)
|
2014-04-20 04:44:06 +02:00
|
|
|
{
|
|
|
|
KeyBinding keyBinding = keyBindings[i];
|
2014-08-27 02:16:32 +02:00
|
|
|
boolean state = keyBinding.getIsKeyPressed();
|
|
|
|
|
2014-06-02 20:04:10 +02:00
|
|
|
if(state != keyDown[i] || (state && repeatings[i]))
|
2014-04-20 04:44:06 +02:00
|
|
|
{
|
2014-06-02 20:04:10 +02:00
|
|
|
if(state)
|
2014-04-20 04:44:06 +02:00
|
|
|
{
|
2014-08-27 02:16:32 +02:00
|
|
|
keyDown(type, keyBinding, tickEnd, state == keyDown[i]);
|
2014-04-20 04:44:06 +02:00
|
|
|
}
|
2014-06-02 20:04:10 +02:00
|
|
|
else {
|
2014-04-20 04:44:06 +02:00
|
|
|
keyUp(type, keyBinding, tickEnd);
|
|
|
|
}
|
2014-06-02 20:04:10 +02:00
|
|
|
|
|
|
|
if(tickEnd)
|
2014-04-20 04:44:06 +02:00
|
|
|
{
|
|
|
|
keyDown[i] = state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-08-27 02:16:32 +02:00
|
|
|
* Called when the key is first in the down position. Will be called
|
|
|
|
* subsequently with isRepeat set to true
|
2014-04-20 04:44:06 +02:00
|
|
|
*
|
2014-08-27 02:16:32 +02:00
|
|
|
* @see #keyUp(Type, KeyBinding, boolean)
|
2014-04-20 04:44:06 +02:00
|
|
|
*
|
|
|
|
* @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);
|
2014-04-20 04:44:06 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fired once when the key changes state from down to up
|
|
|
|
*
|
2014-08-27 02:16:32 +02:00
|
|
|
* @see #keyDown(Type, KeyBinding, boolean, boolean)
|
2014-04-20 04:44:06 +02:00
|
|
|
*
|
|
|
|
* @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);
|
2014-04-20 04:44:06 +02:00
|
|
|
}
|