InputManager.java :  » Game » cgll » org » chaoticengine » cgll » input » Java Open Source

Java Open Source » Game » cgll 
cgll » org » chaoticengine » cgll » input » InputManager.java
/*
Copyright 2010,2011 Matt Van Der Westhuizen

This file is part of CGLL.

CGLL 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 Software Foundation, either version 3 of the License, or
(at your option) any later version.

CGLL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with CGLL.  If not, see <http://www.gnu.org/licenses/>.
*/

package org.chaoticengine.cgll.input;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.chaoticengine.cgll.entity.Entity;
import org.chaoticengine.cgll.entity.EntityManager;
import org.chaoticengine.cgll.entity.component.Command;
import org.newdawn.slick.Input;
import org.newdawn.slick.KeyListener;
import org.newdawn.slick.MouseListener;

/**
 * An InputManager instance is used to handle all input events for one
 * CGLLGameState, by calling the corresponding Command as configured an
 * XML file loaded by that instance in the for of an InputConfiguration object.
 *
 * @author Matt v.d. Westhuizen
 */
public class InputManager implements MouseListener, KeyListener {
    private Map<Integer, Binding> keyboard = new HashMap<Integer, Binding>();
    private Map<Integer, Binding> mouse = new HashMap<Integer, Binding>();

    private Input input = null;
    private EntityManager em = null;
    private InputConfiguration config = null;
    private boolean enabled = true;

    public InputManager(EntityManager em) {
        this.em = em;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public void setConfiguration(InputConfiguration inputCfg) {
        this.config = inputCfg;

        Iterator<Binding> bIt = config.getBindings().iterator();
        while (bIt.hasNext()) {
            Binding b = bIt.next();
            InputDevice i = b.getInput();

            if (i instanceof KeyboardInputDevice) {
                keyboard.put(i.getKeyCode(), b);
            } else if (i instanceof MouseInputDevice) {
                mouse.put(i.getKeyCode(), b);
            }
        }
    }

    public InputConfiguration getConfiguration() {
        return(config);
    }

    // Mouse support methods
    public void mousePressed(int button, int x, int y) {
        if (!enabled) return;

        Binding b = mouse.get(button);

        if (b != null) {
            Command c = getCommand(b.getCommandName());
            if (c != null) {
                c.enable();
            }
        }
    }

    public void mouseReleased(int button, int x, int y) {
        if (!enabled) return;

        Binding b = mouse.get(button);

        if (b != null) {
            Command c = getCommand(b.getCommandName());
            if (c != null) {
                c.disable();
            }
        }
    }

    public void mouseWheelMoved(int change) {
        if (!enabled) return;

        Binding b = null;
        if (change > 0) {
            b = mouse.get(MouseInputDevice.WHEEL_UP);
        } else if (change < 0) {
            b = mouse.get(MouseInputDevice.WHEEL_DOWN);
        }

        if (b != null) {
            Command c = getCommand(b.getCommandName());

            if (c != null) {
                c.enable();
                c.doAction(1);
                c.disable();
            }
        }
    }

    public void mouseClicked(int button, int x, int y, int clickCount) {
    }

    public void mouseMoved(int oldX, int oldY, int newX, int newY) {
    }

    public void mouseDragged(int i, int i1, int i2, int i3) {
    }

    // Keyboard support methods
    public void keyPressed(int key, char c) {
        if (!enabled) return;

        Binding b = keyboard.get(key);

        if (b != null) {
            Command cmd = getCommand(b.getCommandName());

            if (cmd != null) {
                cmd.enable();
            }
        }
    }

    public void keyReleased(int key, char c) {
        if (!enabled) return;

        Binding b = keyboard.get(key);

        if (b != null) {
            Command cmd = getCommand(b.getCommandName());

            if (cmd != null) {
                cmd.disable();
            }
        }
    }

    // Misc methods
    public void setInput(Input input) {
        this.input = input;
    }

    public boolean isAcceptingInput() {
        return(enabled);
    }

    public void inputStarted() {
    }

    public void inputEnded() {
    }

    // Utility methods
    private Command getCommand(String name) {
        if (name == null) {
            return(null);
        }

        Entity player = em.getPlayer();

        if (player == null) {
            return(null);
        }

        Map<String, Command> commands = player.getComponentManager().getCommands();
        Command cmd = commands.get(name);

        return(cmd);
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.