1/******************************************************************************* 2 * Copyright 2011 See AUTHORS file. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 17package com.badlogic.gdx; 18 19import com.badlogic.gdx.utils.Array; 20 21/** An {@link InputProcessor} that delegates to an ordered list of other InputProcessors. Delegation for an event stops if a 22 * processor returns true, which indicates that the event was handled. 23 * @author Nathan Sweet */ 24public class InputMultiplexer implements InputProcessor { 25 private Array<InputProcessor> processors = new Array(4); 26 27 public InputMultiplexer () { 28 } 29 30 public InputMultiplexer (InputProcessor... processors) { 31 for (int i = 0; i < processors.length; i++) 32 this.processors.add(processors[i]); 33 } 34 35 public void addProcessor (int index, InputProcessor processor) { 36 if (processor == null) throw new NullPointerException("processor cannot be null"); 37 processors.insert(index, processor); 38 } 39 40 public void removeProcessor (int index) { 41 processors.removeIndex(index); 42 } 43 44 public void addProcessor (InputProcessor processor) { 45 if (processor == null) throw new NullPointerException("processor cannot be null"); 46 processors.add(processor); 47 } 48 49 public void removeProcessor (InputProcessor processor) { 50 processors.removeValue(processor, true); 51 } 52 53 /** @return the number of processors in this multiplexer */ 54 public int size () { 55 return processors.size; 56 } 57 58 public void clear () { 59 processors.clear(); 60 } 61 62 public void setProcessors (Array<InputProcessor> processors) { 63 this.processors = processors; 64 } 65 66 public Array<InputProcessor> getProcessors () { 67 return processors; 68 } 69 70 public boolean keyDown (int keycode) { 71 for (int i = 0, n = processors.size; i < n; i++) 72 if (processors.get(i).keyDown(keycode)) return true; 73 return false; 74 } 75 76 public boolean keyUp (int keycode) { 77 for (int i = 0, n = processors.size; i < n; i++) 78 if (processors.get(i).keyUp(keycode)) return true; 79 return false; 80 } 81 82 public boolean keyTyped (char character) { 83 for (int i = 0, n = processors.size; i < n; i++) 84 if (processors.get(i).keyTyped(character)) return true; 85 return false; 86 } 87 88 public boolean touchDown (int screenX, int screenY, int pointer, int button) { 89 for (int i = 0, n = processors.size; i < n; i++) 90 if (processors.get(i).touchDown(screenX, screenY, pointer, button)) return true; 91 return false; 92 } 93 94 public boolean touchUp (int screenX, int screenY, int pointer, int button) { 95 for (int i = 0, n = processors.size; i < n; i++) 96 if (processors.get(i).touchUp(screenX, screenY, pointer, button)) return true; 97 return false; 98 } 99 100 public boolean touchDragged (int screenX, int screenY, int pointer) { 101 for (int i = 0, n = processors.size; i < n; i++) 102 if (processors.get(i).touchDragged(screenX, screenY, pointer)) return true; 103 return false; 104 } 105 106 @Override 107 public boolean mouseMoved (int screenX, int screenY) { 108 for (int i = 0, n = processors.size; i < n; i++) 109 if (processors.get(i).mouseMoved(screenX, screenY)) return true; 110 return false; 111 } 112 113 @Override 114 public boolean scrolled (int amount) { 115 for (int i = 0, n = processors.size; i < n; i++) 116 if (processors.get(i).scrolled(amount)) return true; 117 return false; 118 } 119} 120