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    
015    package cpw.mods.fml.client.modloader;
016    
017    import java.util.Arrays;
018    import java.util.EnumSet;
019    import java.util.List;
020    
021    import org.lwjgl.input.Mouse;
022    
023    import com.google.common.collect.ObjectArrays;
024    import com.google.common.primitives.Booleans;
025    
026    import net.minecraft.src.KeyBinding;
027    import cpw.mods.fml.client.registry.KeyBindingRegistry;
028    import cpw.mods.fml.common.TickType;
029    import cpw.mods.fml.common.modloader.ModLoaderModContainer;
030    
031    /**
032     * @author cpw
033     *
034     */
035    public 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        /**
044         * @param keyHandler
045         */
046        public ModLoaderKeyBindingHandler()
047        {
048            super(new KeyBinding[0], new boolean[0]);
049        }
050    
051        void setModContainer(ModLoaderModContainer modContainer)
052        {
053            this.modContainer = modContainer;
054        }
055    
056        public void fireKeyEvent(KeyBinding kb)
057        {
058            ((net.minecraft.src.BaseMod)modContainer.getMod()).keyboardEvent(kb);
059        }
060    
061        @Override
062        public void keyDown(EnumSet<TickType> type, KeyBinding kb, boolean end, boolean repeats)
063        {
064            if (!end)
065            {
066                return;
067            }
068            int idx = helper.indexOf(kb);
069            if (type.contains(TickType.CLIENT))
070            {
071                armed[idx] = true;
072            }
073            if (armed[idx] && type.contains(TickType.RENDER) && (!active[idx] || mlRepeats[idx]))
074            {
075                fireKeyEvent(kb);
076                active[idx] = true;
077                armed[idx] = false;
078            }
079        }
080    
081        @Override
082        public void keyUp(EnumSet<TickType> type, KeyBinding kb, boolean end)
083        {
084            if (!end)
085            {
086                return;
087            }
088            int idx = helper.indexOf(kb);
089            active[idx] = false;
090        }
091    
092        @Override
093        public EnumSet<TickType> ticks()
094        {
095            return EnumSet.of(TickType.CLIENT, TickType.RENDER);
096        }
097    
098        @Override
099        public String getLabel()
100        {
101            return modContainer.getModId() +" KB "+keyBindings[0].keyCode;
102        }
103    
104        void addKeyBinding(KeyBinding binding, boolean repeats)
105        {
106            this.keyBindings = ObjectArrays.concat(this.keyBindings, binding);
107            this.repeatings = new boolean[this.keyBindings.length];
108            Arrays.fill(this.repeatings, true);
109            this.active = new boolean[this.keyBindings.length];
110            this.armed = new boolean[this.keyBindings.length];
111            this.mlRepeats = Booleans.concat(this.mlRepeats, new boolean[] { repeats });
112            this.keyDown = new boolean[this.keyBindings.length];
113            this.helper = Arrays.asList(this.keyBindings);
114        }
115    }