Android Open Source - fun-gl Input Manager






From Project

Back to project page fun-gl.

License

The source code is released under:

Apache License

If you think the Android project fun-gl listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.jcxavier.android.opengl.game.manager.input;
//  www  . j av  a2 s  .c o m
import android.view.MotionEvent;
import com.jcxavier.android.opengl.game.type.Touchable;
import com.jcxavier.android.opengl.util.WeakList;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

/**
 * Created on 12/03/2014.
 *
 * @author Joo Xavier <jcxavier@jcxavier.com>
 */
public final class InputManager {

    private final Deque<MotionEvent> motionEventQueue;
    private final List<Touchable> touchableObjects;

    private final Deque<Touchable> incomingTouchables;
    private final Deque<Touchable> outgoingTouchables;

    public InputManager() {
        motionEventQueue = new ArrayDeque<>();
        touchableObjects = new WeakList<>();

        incomingTouchables = new ArrayDeque<>();
        outgoingTouchables = new ArrayDeque<>();
    }

    public void queueEvent(final MotionEvent event) {
        motionEventQueue.offer(event);
    }

    public void onUpdate() {
        handleIncomingOutgoingTouchables();

        while (!motionEventQueue.isEmpty()) {
            MotionEvent event = motionEventQueue.poll();

            // reverse iteration, as we want to get the first element in Z-order
            for (int i = touchableObjects.size() - 1; i >= 0; i--) {
                Touchable touchable = touchableObjects.get(i);

                if (touchable.canBeTouched() && touchable.isTouchedBy(event) && touchable.onTouch(event)) {
                    // touch was handled, don't proceed further
                    break;
                }
            }
        }
    }

    private void handleIncomingOutgoingTouchables() {
        // add all pending touchables first
        while (!incomingTouchables.isEmpty()) {
            Touchable touchable = incomingTouchables.poll();

            if (!touchableObjects.contains(touchable)) {
                touchableObjects.add(touchable);
            }
        }

        // remove all pending touchables afterwards. If an object is added and removed in the same frame, it will
        // be removed
        while (!outgoingTouchables.isEmpty()) {
            Touchable touchable = outgoingTouchables.poll();

            if (!touchableObjects.contains(touchable)) {
                touchableObjects.remove(touchable);
            }
        }
    }

    public void addManagedObject(final Touchable touchable) {
        incomingTouchables.offer(touchable);
    }

    public void removeManagedObject(final Touchable touchable) {
        outgoingTouchables.offer(touchable);
    }

    public List<Touchable> getTouchedObjects(final MotionEvent event) {
        List<Touchable> touchedObjects = new ArrayList<>();

        for (int i = touchableObjects.size() - 1; i >= 0; i--) {
            Touchable touchable = touchableObjects.get(i);

            if (touchable.canBeTouched() && touchable.isTouchedBy(event) && touchable.onTouch(event)) {
                touchedObjects.add(touchable);
            }
        }

        return touchedObjects;
    }
}




Java Source Code List

com.jcxavier.android.opengl.engine.BitmapConfigHelper.java
com.jcxavier.android.opengl.engine.EngineActivity.java
com.jcxavier.android.opengl.engine.EngineRenderer.java
com.jcxavier.android.opengl.engine.EngineView.java
com.jcxavier.android.opengl.engine.RendererOptions.java
com.jcxavier.android.opengl.engine.cache.GLState.java
com.jcxavier.android.opengl.engine.gdx.GdxEglConfigChooser.java
com.jcxavier.android.opengl.engine.shader.ColorShader.java
com.jcxavier.android.opengl.engine.shader.ShaderManager.java
com.jcxavier.android.opengl.engine.shader.Shader.java
com.jcxavier.android.opengl.engine.shader.TextureShader.java
com.jcxavier.android.opengl.engine.texture.TextureFilteringMode.java
com.jcxavier.android.opengl.engine.texture.TextureManager.java
com.jcxavier.android.opengl.engine.texture.TextureWrap.java
com.jcxavier.android.opengl.engine.texture.Texture.java
com.jcxavier.android.opengl.engine.type.RotationMode.java
com.jcxavier.android.opengl.file.FileManager.java
com.jcxavier.android.opengl.game.GameStage.java
com.jcxavier.android.opengl.game.SimpleGameStage.java
com.jcxavier.android.opengl.game.camera.Camera.java
com.jcxavier.android.opengl.game.camera.DefaultCamera.java
com.jcxavier.android.opengl.game.camera.OrthographicCamera.java
com.jcxavier.android.opengl.game.manager.GameManager.java
com.jcxavier.android.opengl.game.manager.ScreenManager.java
com.jcxavier.android.opengl.game.manager.input.InputHandler.java
com.jcxavier.android.opengl.game.manager.input.InputManager.java
com.jcxavier.android.opengl.game.object.DrawableObject.java
com.jcxavier.android.opengl.game.object.GameObject.java
com.jcxavier.android.opengl.game.object.Sprite.java
com.jcxavier.android.opengl.game.type.Resizeable.java
com.jcxavier.android.opengl.game.type.Touchable.java
com.jcxavier.android.opengl.game.type.Transformable.java
com.jcxavier.android.opengl.game.type.Updateable.java
com.jcxavier.android.opengl.math.IVector.java
com.jcxavier.android.opengl.math.Matrix4.java
com.jcxavier.android.opengl.math.Vector2.java
com.jcxavier.android.opengl.math.Vector3.java
com.jcxavier.android.opengl.math.Vector4.java
com.jcxavier.android.opengl.sample.GameActivity.java
com.jcxavier.android.opengl.sample.TestStage.java
com.jcxavier.android.opengl.util.BitmapUtils.java
com.jcxavier.android.opengl.util.Constants.java
com.jcxavier.android.opengl.util.ReflectionUtils.java
com.jcxavier.android.opengl.util.WeakList.java
com.sample.clean.TestActivity.java
com.sample.clean.TestStage.java