    001/*
    002 * Forge Mod Loader
    003 * Copyright (c) 2012-2013 cpw.
    004 * All rights reserved. This program and the accompanying materials
    005 * are made available under the terms of the GNU Lesser Public License v2.1
    006 * which accompanies this distribution, and is available at
    007 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
    008 *
    009 * Contributors:
    010 *     cpw - implementation
    011 */
    012
    013package cpw.mods.fml.client.modloader;
    014
    015import java.util.Arrays;
    016import java.util.EnumSet;
    017import java.util.List;
    018
    019import org.lwjgl.input.Mouse;
    020
    021import com.google.common.collect.ObjectArrays;
    022import com.google.common.primitives.Booleans;
    023
    024import net.minecraft.client.settings.KeyBinding;
    025import cpw.mods.fml.client.registry.KeyBindingRegistry;
    026import cpw.mods.fml.common.TickType;
    027import cpw.mods.fml.common.modloader.ModLoaderModContainer;
    028
    029/**
    030 * @author cpw
    031 *
    032 */
    033public class ModLoaderKeyBindingHandler extends KeyBindingRegistry.KeyHandler
    034{
    035    private ModLoaderModContainer modContainer;
    036    private List<KeyBinding> helper;
    037    private boolean[] active = new boolean[0];
    038    private boolean[] mlRepeats = new boolean[0];
    039    private boolean[] armed = new boolean[0];
    040
    041    public ModLoaderKeyBindingHandler()
    042    {
    043        super(new KeyBinding[0], new boolean[0]);
    044    }
    045
    046    void setModContainer(ModLoaderModContainer modContainer)
    047    {
    048        this.modContainer = modContainer;
    049    }
    050
    051    public void fireKeyEvent(KeyBinding kb)
    052    {
    053        ((net.minecraft.src.BaseMod)modContainer.getMod()).keyboardEvent(kb);
    054    }
    055
    056    @Override
    057    public void keyDown(EnumSet<TickType> type, KeyBinding kb, boolean end, boolean repeats)
    058    {
    059        if (!end)
    060        {
    061            return;
    062        }
    063        int idx = helper.indexOf(kb);
    064        if (type.contains(TickType.CLIENT))
    065        {
    066            armed[idx] = true;
    067        }
    068        if (armed[idx] && type.contains(TickType.RENDER) && (!active[idx] || mlRepeats[idx]))
    069        {
    070            fireKeyEvent(kb);
    071            active[idx] = true;
    072            armed[idx] = false;
    073        }
    074    }
    075
    076    @Override
    077    public void keyUp(EnumSet<TickType> type, KeyBinding kb, boolean end)
    078    {
    079        if (!end)
    080        {
    081            return;
    082        }
    083        int idx = helper.indexOf(kb);
    084        active[idx] = false;
    085    }
    086
    087    @Override
    088    public EnumSet<TickType> ticks()
    089    {
    090        return EnumSet.of(TickType.CLIENT, TickType.RENDER);
    091    }
    092
    093    @Override
    094    public String getLabel()
    095    {
    096        return modContainer.getModId() +" KB "+keyBindings[0].keyCode;
    097    }
    098
    099    void addKeyBinding(KeyBinding binding, boolean repeats)
    100    {
    101        this.keyBindings = ObjectArrays.concat(this.keyBindings, binding);
    102        this.repeatings = new boolean[this.keyBindings.length];
    103        Arrays.fill(this.repeatings, true);
    104        this.active = new boolean[this.keyBindings.length];
    105        this.armed = new boolean[this.keyBindings.length];
    106        this.mlRepeats = Booleans.concat(this.mlRepeats, new boolean[] { repeats });
    107        this.keyDown = new boolean[this.keyBindings.length];
    108        this.helper = Arrays.asList(this.keyBindings);
    109    }
    110}