    001/*
    002 * The FML Forge Mod Loader suite.
    003 * Copyright (C) 2012 cpw
    004 *
    005 * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free
    006 * Software Foundation; either version 2.1 of the License, or any later version.
    007 *
    008 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
    009 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
    010 *
    011 * You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51
    012 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
    013 */
    014
    015package cpw.mods.fml.client.modloader;
    016
    017import java.util.Arrays;
    018import java.util.EnumSet;
    019import java.util.List;
    020
    021import org.lwjgl.input.Mouse;
    022
    023import com.google.common.collect.ObjectArrays;
    024import com.google.common.primitives.Booleans;
    025
    026import net.minecraft.client.settings.KeyBinding;
    027import cpw.mods.fml.client.registry.KeyBindingRegistry;
    028import cpw.mods.fml.common.TickType;
    029import cpw.mods.fml.common.modloader.ModLoaderModContainer;
    030
    031/**
    032 * @author cpw
    033 *
    034 */
    035public class ModLoaderKeyBindingHandler extends KeyBindingRegistry.KeyHandler
    036{
    037    private ModLoaderModContainer modContainer;
    038    private List<KeyBinding> helper;
    039    private boolean[] active = new boolean[0];
    040    private boolean[] mlRepeats = new boolean[0];
    041    private boolean[] armed = new boolean[0];
    042
    043    public ModLoaderKeyBindingHandler()
    044    {
    045        super(new KeyBinding[0], new boolean[0]);
    046    }
    047
    048    void setModContainer(ModLoaderModContainer modContainer)
    049    {
    050        this.modContainer = modContainer;
    051    }
    052
    053    public void fireKeyEvent(KeyBinding kb)
    054    {
    055        ((net.minecraft.src.BaseMod)modContainer.getMod()).keyboardEvent(kb);
    056    }
    057
    058    @Override
    059    public void keyDown(EnumSet<TickType> type, KeyBinding kb, boolean end, boolean repeats)
    060    {
    061        if (!end)
    062        {
    063            return;
    064        }
    065        int idx = helper.indexOf(kb);
    066        if (type.contains(TickType.CLIENT))
    067        {
    068            armed[idx] = true;
    069        }
    070        if (armed[idx] && type.contains(TickType.RENDER) && (!active[idx] || mlRepeats[idx]))
    071        {
    072            fireKeyEvent(kb);
    073            active[idx] = true;
    074            armed[idx] = false;
    075        }
    076    }
    077
    078    @Override
    079    public void keyUp(EnumSet<TickType> type, KeyBinding kb, boolean end)
    080    {
    081        if (!end)
    082        {
    083            return;
    084        }
    085        int idx = helper.indexOf(kb);
    086        active[idx] = false;
    087    }
    088
    089    @Override
    090    public EnumSet<TickType> ticks()
    091    {
    092        return EnumSet.of(TickType.CLIENT, TickType.RENDER);
    093    }
    094
    095    @Override
    096    public String getLabel()
    097    {
    098        return modContainer.getModId() +" KB "+keyBindings[0].keyCode;
    099    }
    100
    101    void addKeyBinding(KeyBinding binding, boolean repeats)
    102    {
    103        this.keyBindings = ObjectArrays.concat(this.keyBindings, binding);
    104        this.repeatings = new boolean[this.keyBindings.length];
    105        Arrays.fill(this.repeatings, true);
    106        this.active = new boolean[this.keyBindings.length];
    107        this.armed = new boolean[this.keyBindings.length];
    108        this.mlRepeats = Booleans.concat(this.mlRepeats, new boolean[] { repeats });
    109        this.keyDown = new boolean[this.keyBindings.length];
    110        this.helper = Arrays.asList(this.keyBindings);
    111    }
    112}