Example usage for com.badlogic.gdx InputProcessor InputProcessor

List of usage examples for com.badlogic.gdx InputProcessor InputProcessor

Introduction

In this page you can find the example usage for com.badlogic.gdx InputProcessor InputProcessor.

Prototype

InputProcessor

Source Link

Usage

From source file:br.cefetmg.games.minigames.AngryTooths.java

@Override
public void onUpdate(float dt) {
    if (trigger_velocidade) {
        velocidade_inicial = posicao_inicial.sub(posicao_final);
        velocidade_inicial.scl((float) 0.005);
        tooth.inicia_velocidade(velocidade_inicial);
        trigger_velocidade = false;// ww w.ja v a  2s  . c  o m
    }
    if (trigger) {
        tooth.atua_gravidade();
        tooth.integra(dt);
        tooth.update(dt);
    }
    if (mouth.getBoundingRectangle().contains(tooth.getBoundingRectangle())
            && tooth.getX() > mouth.getX() + 40) {
        super.challengeSolved();
    }
    if (tooth.getY() < LIMITE_ERRO) {
        super.challengeFailed();
    }
    if (trigger_velocidade_boca) {
        mouth.atua_dificuldade_velocidade_inicial(difficulty);
        trigger_velocidade_boca = false;
    }
    mouth.movimento_alternado();
    mouth.integra(dt);
    mouth.update(dt);
    tooth.troca_sprite();
    Gdx.input.setInputProcessor(new InputProcessor() {
        @Override
        public boolean keyDown(int keycode) {
            return false;
        }

        @Override
        public boolean keyUp(int keycode) {
            return false;
        }

        @Override
        public boolean keyTyped(char character) {
            return false;
        }

        @Override
        public boolean touchDown(int screenX, int screenY, int pointer, int button) {
            if (trigger_click) {
                click = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
                screen.viewport.unproject(click);
                posicao_inicial = new Vector3(click.x, click.y, 0);
            }
            return false;
        }

        @Override
        public boolean touchUp(int screenX, int screenY, int pointer, int button) {
            if (posicao_inicial != null && trigger_click == true) {
                click = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
                screen.viewport.unproject(click);
                posicao_final = new Vector3(click.x, click.y, 0);
                trigger = true;
                trigger_velocidade = true;
                if (trigger_click) {
                    toothSound.play();
                    trigger_click = false;
                }
            }
            return false;
        }

        @Override
        public boolean touchDragged(int screenX, int screenY, int pointer) {
            return false;
        }

        @Override
        public boolean mouseMoved(int screenX, int screenY) {
            return false;
        }

        @Override
        public boolean scrolled(int amount) {
            return false;
        }
    });
}

From source file:com.badlogic.gdx.spriter.demo.SpriterDemoApp.java

@Override
public void create() {
    // Initialize object
    batch = new SpriteBatch();
    camera = new OrthographicCamera();

    FileHandleResolver resolver = new InternalFileHandleResolver();
    assetManager = new AssetManager(resolver);
    assetManager.setLoader(SpriterData.class, new SpriterDataLoader(resolver));

    resolver = new AbsoluteFileHandleResolver();
    externalAssetManager = new AssetManager(resolver);
    externalAssetManager.setLoader(SpriterData.class, new SpriterDataLoader(resolver));

    assetManager.load("uiskin.json", Skin.class);
    assetManager.finishLoading();//from   w w  w .java 2s  .c  o  m

    skin = assetManager.get("uiskin.json");

    // Create widgets
    stage = new Stage(new ScreenViewport(camera), batch);

    Label titleLabel = new Label("gdx-spriter", skin);
    titleLabel.setFontScale(3f);

    Label fpsLabel = new Label("FPS", skin) {
        @Override
        public void act(float delta) {
            this.setText(Gdx.graphics.getFramesPerSecond() + " FPS");
            super.act(delta);
        }
    };

    fileChooser = new SelectBox<SpriterDemoFileHandle>(skin);
    fileChooser.setName("Files");
    fileChooser.addListener(new ChangeListener() {
        @Override
        public void changed(ChangeEvent event, Actor actor) {
            changeSpriterFile(fileChooser.getSelected());
            lastUsedSelectBox = fileChooser;
        }
    });

    Button fileFinder = new TextButton("Browse", skin);
    fileFinder.addListener(new ChangeListener() {
        @Override
        public void changed(ChangeEvent event, Actor actor) {
            FileDialog fileDialog = new FileDialog((java.awt.Frame) null, "Choose Spriter file",
                    FileDialog.LOAD);
            fileDialog.setDirectory(lastFolderBrowsed);
            fileDialog.setResizable(true);
            fileDialog.setVisible(true);
            String file = fileDialog.getFile();
            String directory = fileDialog.getDirectory();
            if (directory != null) {
                lastFolderBrowsed = directory;
            }
            if (file != null) {
                String path = directory + file;
                addFile(Gdx.files.absolute(path), externalAssetManager);
                fileChooser.setItems(files);
                fileChooser.setSelectedIndex(fileChooser.getItems().size - 1);
            }
        }
    });

    entityChooser = new SelectBox<SpriterAnimator>(skin);
    entityChooser.setName("Entities");
    entityChooser.addListener(new ChangeListener() {
        @Override
        public void changed(ChangeEvent event, Actor actor) {
            changeAnimator(entityChooser.getSelected());
            lastUsedSelectBox = entityChooser;
        }
    });

    animationChooser = new SelectBox<String>(skin);
    animationChooser.setName("Animations");
    animationChooser.addListener(new ChangeListener() {
        @Override
        public void changed(ChangeEvent event, Actor actor) {
            changeAnimation(animationChooser.getSelected());
            lastUsedSelectBox = animationChooser;
        }
    });

    final CheckBox transitionCheckbox = new CheckBox("Transition", skin);
    transitionCheckbox.setChecked(transition);
    transitionCheckbox.addListener(new ChangeListener() {
        @Override
        public void changed(ChangeEvent event, Actor actor) {
            transition = transitionCheckbox.isChecked();
        }
    });

    charmapChooser = new List<SpriterCharacterMap>(skin);
    ArraySelection<SpriterCharacterMap> selection = charmapChooser.getSelection();
    selection.setMultiple(true);
    selection.setRangeSelect(false);
    selection.setToggle(false);
    selection.setRequired(false);
    charmapChooser.setName("Charmaps");
    charmapChooser.addListener(new ChangeListener() {
        @Override
        public void changed(ChangeEvent event, Actor actor) {
            changeCharacterMaps(charmapChooser.getSelection().items().orderedItems());
        }
    });

    angleSlider = new SpriterDemoAnimatorSlider(0, 360, 1f, skin, "%.0f") {
        @Override
        public void setValue(SpriterAnimator animator, float value) {
            spriterAnimator.setRotation(value);
        }

        @Override
        protected float getValue(SpriterAnimator animator) {
            return spriterAnimator.getRotation();
        }
    };

    pivotXSlider = new SpriterDemoAnimatorSlider(-1000f, 1000f, 1f, skin, "%.0f") {
        @Override
        public void setValue(SpriterAnimator animator, float value) {
            animator.setPivotX(value);
        }

        @Override
        protected float getValue(SpriterAnimator animator) {
            return animator.getPivotX();
        }
    };

    pivotYSlider = new SpriterDemoAnimatorSlider(-1000f, 1000f, 1f, skin, "%.0f") {
        @Override
        public void setValue(SpriterAnimator animator, float value) {
            animator.setPivotY(value);
        }

        @Override
        protected float getValue(SpriterAnimator animator) {
            return animator.getPivotY();
        }
    };

    scaleXSlider = new SpriterDemoAnimatorSlider(-10f, 10f, 0.1f, skin) {
        @Override
        public void setValue(SpriterAnimator animator, float value) {
            spriterAnimator.setScaleX(value);
        }

        @Override
        protected float getValue(SpriterAnimator animator) {
            return spriterAnimator.getScaleX();
        }
    };

    scaleYSlider = new SpriterDemoAnimatorSlider(-10f, 10f, 0.1f, skin) {
        @Override
        public void setValue(SpriterAnimator animator, float value) {
            spriterAnimator.setScaleY(value);
        }

        @Override
        protected float getValue(SpriterAnimator animator) {
            return spriterAnimator.getScaleY();
        }
    };

    alphaSlider = new SpriterDemoAnimatorSlider(0f, 1f, 0.01f, skin, "%.2f") {
        @Override
        public void setValue(SpriterAnimator animator, float value) {
            animator.setAlpha(value);
        }

        @Override
        protected float getValue(SpriterAnimator animator) {
            return animator.getAlpha();
        }
    };

    speedSlider = new SpriterDemoAnimatorSlider(0f, 10f, 0.1f, skin) {
        @Override
        public void setValue(SpriterAnimator animator, float value) {
            animator.setSpeed(value);
        }

        @Override
        protected float getValue(SpriterAnimator animator) {
            return animator.getSpeed();
        }
    };

    allAnimatorSliders = new SpriterDemoAnimatorSlider[] { scaleXSlider, scaleYSlider, pivotXSlider,
            pivotYSlider, angleSlider, alphaSlider, speedSlider };

    metaLabel = new Label("Meta: ", skin);
    metaLabel.setWrap(true);
    metaLabel.setAlignment(Align.topLeft);

    spriterPlaceholder = new Label("No animator", skin);
    spriterPlaceholder.setAlignment(Align.center);

    spriterAnimator = new SpriterAnimatorActor(animator);
    spriterAnimator.debug();

    playPauseButton = new ImageButton(skin, "play");
    playPauseButton.setChecked(true);
    playPauseButton.addListener(new ChangeListener() {
        @Override
        public void changed(ChangeEvent event, Actor actor) {
            boolean playing = playPauseButton.isChecked();
            spriterAnimator.setDisabled(!playing);
        }
    });

    timeSlider = new Slider(0f, 2000f, 1, false, skin);
    timeSlider.addListener(timeSliderListener = new ChangeListener() {
        @Override
        public void changed(ChangeEvent event, Actor actor) {
            if (animator == null)
                return;
            animator.setTime(timeSlider.getValue());
            animator.update(0f);
        }
    });

    timeLabel = new Label("---", skin);

    // Put everything in place

    Table titleTable = new Table(skin);
    titleTable.add(titleLabel).pad(6f);
    titleTable.add().expandX();
    titleTable.add(fpsLabel).padRight(10f);

    Table selectionTable = new Table(skin);
    selectionTable.defaults().pad(3f);

    Table filesTable = new Table(skin);
    filesTable.row();
    filesTable.add(fileChooser).expand().fillX();
    filesTable.add(fileFinder).padLeft(2f).padRight(1f);

    Table animationsTable = new Table(skin);
    animationsTable.row();
    animationsTable.add(animationChooser).expand().fill();
    animationsTable.add(transitionCheckbox).fillX().padLeft(2f);

    ScrollPane scrollableCharmapChooser = new ScrollPane(charmapChooser);

    Table menuTable = new Table(skin);
    menuTable.defaults().pad(3f).expandX().fillX();
    menuTable.row();
    menuTable.add(titleTable).colspan(2);
    menuTable.row();
    menuTable.add("File");
    menuTable.add(filesTable).pad(4f);
    menuTable.row();
    menuTable.add("Entity");
    menuTable.add(entityChooser).pad(4f);
    menuTable.row();
    menuTable.add("Animation");
    menuTable.add(animationsTable).pad(4f);
    menuTable.row();
    menuTable.add("Maps");
    menuTable.add(scrollableCharmapChooser).pad(4f);
    menuTable.row();
    menuTable.add("Angle");
    menuTable.add(angleSlider);
    menuTable.row();
    menuTable.add("Pivot X");
    menuTable.add(pivotXSlider);
    menuTable.row();
    menuTable.add("Pivot Y");
    menuTable.add(pivotYSlider);
    menuTable.row();
    menuTable.add("Scale X");
    menuTable.add(scaleXSlider);
    menuTable.row();
    menuTable.add("Scale Y");
    menuTable.add(scaleYSlider);
    menuTable.row();
    menuTable.add("Alpha");
    menuTable.add(alphaSlider);
    menuTable.row();
    menuTable.add("Speed");
    menuTable.add(speedSlider);
    menuTable.row();
    menuTable.add().expandY();

    Table contentTable = new Table(skin);
    contentTable.row();
    contentTable.add(metaLabel).left().expandX().maxHeight(1f);
    contentTable.row();
    contentTable.stack(spriterPlaceholder, spriterAnimator).expand();

    Table timelineTable = new Table(skin);
    timelineTable.row();
    timelineTable.add("Timeline").expandX().align(Align.bottom);
    timelineTable.row();
    timelineTable.add(timeSlider).expandX().fillX();
    timelineTable.row();
    timelineTable.add(timeLabel).expandX().align(Align.top);

    Table controlTable = new Table(skin);
    controlTable.add(playPauseButton).space(5f).expandY().fillY();
    controlTable.add(timelineTable).expandX().fillX();

    rootTable = new Table(skin);
    rootTable.setFillParent(true);
    rootTable.row();
    rootTable.add(menuTable).expandY().fill();
    rootTable.add(contentTable).expand().fill();
    rootTable.row();
    rootTable.add(controlTable).colspan(2).expandX().fillX();

    stage.addActor(rootTable);

    // Bring input processing to the party

    InputProcessor globalInput = new InputProcessor() {
        @Override
        public boolean keyDown(int keycode) {
            switch (keycode) {
            case Keys.UP:
            case Keys.Z:
            case Keys.W:
                if (lastUsedSelectBox != null)
                    lastUsedSelectBox.setSelectedIndex(Math.max(lastUsedSelectBox.getSelectedIndex() - 1, 0));
                break;
            case Keys.DOWN:
            case Keys.S:
                if (lastUsedSelectBox != null)
                    lastUsedSelectBox.setSelectedIndex(Math.min(lastUsedSelectBox.getSelectedIndex() + 1,
                            lastUsedSelectBox.getItems().size - 1));
                break;

            default:
                break;
            }
            return true;
        }

        @Override
        public boolean keyUp(int keycode) {
            return false;
        }

        @Override
        public boolean keyTyped(char character) {
            return false;
        }

        @Override
        public boolean touchDown(int screenX, int screenY, int pointer, int button) {
            return false;
        }

        @Override
        public boolean touchUp(int screenX, int screenY, int pointer, int button) {
            return false;
        }

        @Override
        public boolean touchDragged(int screenX, int screenY, int pointer) {
            return false;
        }

        @Override
        public boolean mouseMoved(int screenX, int screenY) {
            return false;
        }

        @Override
        public boolean scrolled(int amount) {
            amount *= 0.05f; // Zoom coefficient
            scaleXSlider.setValue(scaleXSlider.getValue() + amount);
            scaleYSlider.setValue(scaleYSlider.getValue() + amount);
            return false;
        }
    };
    Gdx.input.setInputProcessor(new InputMultiplexer(stage, globalInput));

    // Add default test resources
    Array<String> testResources = new Array<String>(SpriterTestData.scml);
    testResources.addAll(SpriterTestData.scon);
    testResources.sort();
    for (String path : testResources)
        addFile(Gdx.files.internal(path.replaceFirst("/", "")), assetManager);

    // Also go discover some unknown exotic resources! (won't work in jar though)
    Array<FileHandle> spriterFiles = SpriterDemoUtils.findFiles(new String[] { "scml", "scon" });
    for (FileHandle f : spriterFiles)
        addFile(f, assetManager);

    fileChooser.setItems(files);

    lastUsedSelectBox = fileChooser;

    if (playPauseButton.isChecked())
        animator.play(animator.getAnimations().iterator().next());
}

From source file:com.entermoor.blackandwhiteforest.BlackAndWhiteForest.java

License:Creative Commons License

@Override
public void create() {
    try {/*from   w  ww. j a  v  a2 s.c om*/

        Gdx.graphics.setFullscreenMode(Gdx.graphics.getDisplayMode());

        welcome = new ScreenWelcome();
        main = new ScreenMain();
        settings = new ScreenSettings();
        gaming = new ScreenGaming();

        skin = new Skin();
        while (!assetManager.update())
            ;
        WindowStyle windowStyle = new WindowStyle(new BitmapFont(), Color.BLACK, new TextureRegionDrawable(
                new TextureRegion((Texture) assetManager.get("textures/dialogBackground.png", Texture.class))));
        skin.add("default", windowStyle);
        LabelStyle labelStyle = new LabelStyle(new BitmapFont(), Color.WHITE);
        skin.add("default", labelStyle);

        for (IBAWFPlugin plugin : toInitList)
            plugin.init();

        addProcessor(stage);
        addProcessor(new InputProcessor() {

            @Override
            public boolean touchUp(int screenX, int screenY, int pointer, int button) {
                return false;
            }

            @Override
            public boolean touchDragged(int screenX, int screenY, int pointer) {
                return false;
            }

            @Override
            public boolean touchDown(int screenX, int screenY, int pointer, int button) {
                return false;
            }

            @Override
            public boolean scrolled(int amount) {
                return false;
            }

            @Override
            public boolean mouseMoved(int screenX, int screenY) {
                return false;
            }

            @Override
            public boolean keyUp(int keycode) {
                if (keycode == Keys.ESCAPE) {
                    Gdx.app.exit();
                }
                return false;
            }

            @Override
            public boolean keyTyped(char character) {
                return false;
            }

            @Override
            public boolean keyDown(int keycode) {
                return false;
            }
        });

    } catch (Throwable t) {
        BAWFCrashHandler.handleCrash(t);
    }

    currentBGM = bgm[new Random().nextInt(bgm.length)];
    currentBGM.play();
    setScreen(welcome);
}

From source file:com.googlecode.gdxquake2.game.gdxadapter.GdxKbdImpl.java

License:Apache License

@Override
public void Init() {

    Gdx.input.setInputProcessor(new InputProcessor() {
        @Override/*from  w w w  . j  a  v  a2s.c o  m*/
        public boolean keyDown(int keycode) {
            Do_Key_Event(translateKeyCode(keycode), true);
            return true;
        }

        @Override
        public boolean keyUp(int keycode) {
            Do_Key_Event(translateKeyCode(keycode), false);
            return true;
        }

        @Override
        public boolean keyTyped(char character) {
            return false;
        }

        @Override
        public boolean touchDown(int screenX, int screenY, int pointer, int button) {
            return false;
        }

        @Override
        public boolean touchUp(int screenX, int screenY, int pointer, int button) {
            return false;
        }

        @Override
        public boolean touchDragged(int screenX, int screenY, int pointer) {
            return false;
        }

        @Override
        public boolean mouseMoved(int screenX, int screenY) {
            return false;
        }

        @Override
        public boolean scrolled(int amount) {
            return false;
        }
    });

    /*
    PlayN.keyboard().setListener(new Keyboard.Listener() {
              
      @Override
      public void onKeyUp(Event event) {
        Do_Key_Event(translateKeyCode(event), false);
      }
              
      @Override
      public void onKeyTyped(TypedEvent event) {
       // System.out.println("onTypes " + event.typedChar());
      }
              
      @Override
      public void onKeyDown(Event event) {
        Do_Key_Event(translateKeyCode(event), true);
      }
    });
            
            
    PlayN.mouse().setListener(new Mouse.Listener() {
              
      @Override
      public void onMouseWheelScroll(WheelEvent event) {
        int v = (int) event.velocity();
        while (v < 0) {
          Do_Key_Event(IdKeys.K_MWHEELUP, true);
          Do_Key_Event(IdKeys.K_MWHEELUP, false);
          v++;
        } 
        while (v > 0) {
          Do_Key_Event(IdKeys.K_MWHEELDOWN, true);
          Do_Key_Event(IdKeys.K_MWHEELDOWN, false);
          v--;
        }
      }
              
      @Override
      public void onMouseUp(ButtonEvent event) {
        mouseButton(event, false);
      }
              
      @Override
      public void onMouseMove(MotionEvent event) {
        mx = (int) event.dx();
        my = (int) event.dy();
      }
              
      @Override
      public void onMouseDown(ButtonEvent event) {
        mouseButton(event, true);
      }
    });
    */
}

From source file:com.matthewmichelotti.collider.demos.Demos.java

License:Apache License

@Override
public void create() {
    InputProcessor inputProcessor = new InputProcessor() {
        @Override/*from w  w w  . ja  v  a 2 s  . co  m*/
        public boolean keyDown(int keycode) {
            return false;
        }

        @Override
        public boolean keyUp(int keycode) {
            return false;
        }

        @Override
        public boolean keyTyped(char character) {
            return false;
        }

        @Override
        public boolean touchDown(int screenX, int screenY, int pointer, int button) {
            return true;
        }

        @Override
        public boolean touchUp(int screenX, int screenY, int pointer, int button) {
            clicked = true;
            return true;
        }

        @Override
        public boolean touchDragged(int screenX, int screenY, int pointer) {
            return false;
        }

        @Override
        public boolean mouseMoved(int screenX, int screenY) {
            return false;
        }

        @Override
        public boolean scrolled(int amount) {
            return false;
        }
    };
    Gdx.input.setInputProcessor(inputProcessor);

    Scenarios.initScenario(0);
    startTime = TimeUtils.nanoTime();
}

From source file:com.mygdx.java.MyGdxJava.java

License:Apache License

@Override
public void create() {
    image = null;//from ww  w  .  j ava2s.  c  o  m
    Gdx.app.postRunnable(new Runnable() {

        @Override
        public void run() {
            try {
                start();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    });

    /*
     * FileHandle fileHandle = Gdx.files.internal(Constants.CONFIG);
     * 
     * try { PropertiesUtils.load(Constants.PROPERTIES,
     * fileHandle.reader()); } catch (IOException e) { e.printStackTrace();
     * }
     * 
     * Gdx.app.setLogLevel(Application.LOG_DEBUG);
     * 
     * System.out.println(Constants.PROPERTIES.get("log.level"));
     */
    batch = new SpriteBatch();

    region = new TextureRegion();
    region.setRegion(new Texture("badlogic.jpg"));

    batch = new SpriteBatch();
    getData();
    Gdx.input.setInputProcessor(new InputProcessor() {

        @Override
        public boolean touchUp(int screenX, int screenY, int pointer, int button) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean touchDragged(int screenX, int screenY, int pointer) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean touchDown(int screenX, int screenY, int pointer, int button) {
            getData();
            return true;
        }

        @Override
        public boolean scrolled(int amount) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean mouseMoved(int screenX, int screenY) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean keyUp(int keycode) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean keyTyped(char character) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean keyDown(int keycode) {
            // TODO Auto-generated method stub
            return false;
        }
    });

    // timer = new Timer();
    // Task task = new Task() {
    // @Override
    // public void run() {
    // // getData();
    //
    // }
    // };
    // timer.scheduleTask(task, 0.01f, 0.01f);
    // timer.start();
}

From source file:com.sidereal.dolphinoes.architecture.core.input.Input.java

License:Apache License

/** Method for creating the Core Module, is called after the LibGDX backend has been created, thus LibGDX constructs
 * can be called in this method without errors. Is called in {@link DolphinOES#create()}. */
@Override/*from www .java  2 s.  c om*/
public void create() {

    for (int i = 0; i < this.processorNames.length; i++) {

        // create new input processor
        final InputProcessor inputProcessor = new InputProcessor() {

            @Override
            public boolean touchUp(int screenX, int screenY, int pointer, int button) {

                // update internal input data
                // update button data
                getInputData(button - 10).update(InputStatus.Up);

                // update touch data
                getTouchInputData(pointer - 20).update(InputStatus.Up, screenX, screenY);

                // run listener events

                boolean handled = false;
                // go over all action events and run the ones on up if
                // applicable
                for (int i = 0; i < actionEvents.get(this).size; i++) {
                    ActionEventWrapper currEvent = actionEvents.get(this).get(i);
                    // curr event runs when releasing
                    if (currEvent.inputType.equals(InputEventType.Up)) {

                        if (currEvent.code == pointer - 20 ||
                        // if the event runs for a particular touch
                        currEvent.code == button - 10 ||
                        // or the event runs on a particular button
                        currEvent.code == Keys.ANY_KEY)
                        // or the event runs on every key
                        {
                            handled = handled || currEvent.event.run(getInputData(button - 10));
                        }
                    }
                }

                // go over all touch event and run the ones on up
                for (int i = 0; i < touchEvents.get(this).size; i++) {
                    TouchEventWrapper currEvent = touchEvents.get(this).get(i);
                    // curr event runs when releasing
                    if (currEvent.inputType.equals(InputEventType.Up)) {
                        if (currEvent.code == pointer - 20 ||
                        // if the event runs for a particular touch
                        currEvent.code == Keys.ANY_KEY)
                        // or the event runs on every key
                        {
                            handled = handled || currEvent.event.run(getTouchInputData(pointer - 20));
                        }
                    }
                }

                return handled;
            }

            @Override
            public boolean touchDragged(int screenX, int screenY, int pointer) {

                getTouchInputData(pointer - 20).update(InputStatus.Hold, screenX, screenY);

                boolean handled = false;
                // go over all touch event and run the ones on up

                for (int i = 0; i < touchEvents.get(this).size; i++) {

                    TouchEventWrapper currEvent = touchEvents.get(this).get(i);
                    // curr event runs when releasing
                    if ((currEvent.inputType.equals(InputEventType.Down)
                            && getTouchInputData(pointer - 20).getInputStatus() == InputStatus.Down)
                            || (currEvent.inputType.equals(InputEventType.Hold)
                                    && getTouchInputData(pointer - 20).getInputStatus() == InputStatus.Hold)) {
                        if (currEvent.code == pointer - 20 ||
                        // if an event runs for a particular touch
                        currEvent.code == Keys.ANY_KEY)
                        // if an event runs for all touches
                        {
                            handled = handled || currEvent.event.run(getTouchInputData(pointer - 20));
                        }
                    }
                }

                return true;
            }

            @Override
            public boolean touchDown(int screenX, int screenY, int pointer, int button) {

                // update button data
                if (getInputData(button - 10).getInputStatus().equals(InputStatus.None))
                    getInputData(button - 10).update(InputStatus.Down);

                if (getInputData(pointer - 20).getInputStatus().equals(InputStatus.None))
                    getInputData(pointer - 20).update(InputStatus.Down);

                // update pointer data
                InputStatus targetInputStatus = getTouchInputData(pointer - 20).getInputStatus();
                if (getTouchInputData(pointer - 20).getInputStatus().equals(InputStatus.None))
                    targetInputStatus = InputStatus.Down;
                getTouchInputData(pointer - 20).update(targetInputStatus, screenX, screenY);

                boolean handled = false;
                // update internal input data
                // ...

                // run listener events
                // go over all action events and run the ones on up if
                // applicable
                for (int i = 0; i < actionEvents.get(this).size; i++) {
                    ActionEventWrapper currEvent = actionEvents.get(this).get(i);
                    // curr event runs when releasing
                    if ((currEvent.inputType.equals(InputEventType.Down)
                            && getInputData(button - 10).getInputStatus() == InputStatus.Down)
                            || (currEvent.inputType.equals(InputEventType.Hold)
                                    && getInputData(button - 10).getInputStatus() == InputStatus.Hold)) {

                        if (currEvent.code == pointer - 20 ||
                        // if the event runs for a particular touch
                        currEvent.code == button - 10 ||
                        // or the event runs for a particular button
                        currEvent.code == Keys.ANY_KEY)
                        // or any key is to be handled
                        {
                            handled = handled || currEvent.event.run(getInputData(button - 10));
                        }
                    }
                }

                // go over all touch event and run the ones on up
                for (int i = 0; i < touchEvents.get(this).size; i++) {

                    TouchEventWrapper currEvent = touchEvents.get(this).get(i);
                    // curr event runs when releasing

                    if ((currEvent.inputType.equals(InputEventType.Down)
                            && getTouchInputData(pointer - 20).getInputStatus() == InputStatus.Down)
                            || (currEvent.inputType.equals(InputEventType.Hold)
                                    && getTouchInputData(pointer - 20).getInputStatus() == InputStatus.Hold)) {
                        if (currEvent.code == pointer - 20 ||
                        // if the event runs for a particular touch
                        currEvent.code == Keys.ANY_KEY)
                        // or any key is to be handled

                        {
                            handled = handled || currEvent.event.run(getTouchInputData(pointer - 20));
                        }
                    }
                }

                return handled;
            }

            @Override
            public boolean scrolled(int amount) {

                // run listener events
                boolean handled = false;
                for (int i = 0; i < scrollEvents.get(this).size; i++) {
                    handled = handled || scrollEvents.get(this).get(i).run(amount);
                }
                return handled;
            }

            // ignore, can use Gdx.input for mouse position
            @Override
            public boolean mouseMoved(int screenX, int screenY) {

                return false;
            }

            @Override
            public boolean keyUp(int keycode) {

                // update key press data
                getInputData(keycode).update(InputStatus.Up);

                // run listener events
                boolean handled = false;
                // go over all action events and run the ones on up if
                // applicable
                for (int i = 0; i < actionEvents.get(this).size; i++) {
                    ActionEventWrapper currEvent = actionEvents.get(this).get(i);
                    // curr event runs when releasing
                    if (currEvent.inputType.equals(InputEventType.Up)) {

                        if (currEvent.code == keycode ||
                        // or the event runs on a particular button
                        currEvent.code == InputAction.ANY_ACTION)
                        // or the event runs on every key
                        {
                            handled = handled || currEvent.event.run(getInputData(keycode));
                        }
                    }
                }
                return handled;
            }

            @Override
            public boolean keyTyped(char character) {

                // run listener events
                boolean handled = false;
                for (int i = 0; i < keyTypedEvents.get(this).size; i++) {
                    handled = handled || keyTypedEvents.get(this).get(i).run(character);
                }
                return handled;
            }

            @Override
            public boolean keyDown(int keycode) {

                // update key press data
                if (getInputData(keycode).getInputStatus().equals(InputStatus.None))
                    getInputData(keycode).update(InputStatus.Down);

                // run listener events
                boolean handled = false;

                // go over all action events and run the ones on up if
                // applicable
                for (int i = 0; i < actionEvents.get(this).size; i++) {
                    ActionEventWrapper currEvent = actionEvents.get(this).get(i);
                    // curr event runs when releasing
                    if ((currEvent.inputType.equals(InputEventType.Down)
                            && getInputData(keycode).getInputStatus() == InputStatus.Down)
                            || (currEvent.inputType.equals(InputEventType.Hold)
                                    && getInputData(keycode).getInputStatus() == InputStatus.Hold)) {

                        if (currEvent.code == keycode ||
                        // the event runs for a particular key
                        currEvent.code == InputAction.ANY_ACTION)
                        // or any key is to be handled
                        {
                            handled = handled || currEvent.event.run(getInputData(keycode));
                        }
                    }
                }
                return handled;
            }
        };
        inputProcessors.put(processorNames[i], inputProcessor);
        actionEvents.put(inputProcessor, new Array<ActionEventWrapper>());
        touchEvents.put(inputProcessor, new Array<TouchEventWrapper>());
        scrollEvents.put(inputProcessor, new Array<ScrollEvent>());
        keyTypedEvents.put(inputProcessor, new Array<KeyTypedEvent>());
        inputMultiplexer.addProcessor(inputProcessor);

    }
    // set LibGDX input processor to the one that we are creating
    Gdx.input.setInputProcessor(inputMultiplexer);
}

From source file:dk.sidereal.lumm.architecture.core.Input.java

License:Apache License

/**
 * Method for creating the Core Module, is called after the LibGDX backend
 * has been created, thus LibGDX constructs can be called in this method
 * without errors. Is called in {@link Lumm#create()}.
 *//*from w  w  w. j a v  a 2  s. com*/
@Override
public void onCreate() {

    for (int i = 0; i < this.processorNames.length; i++) {

        // create new input processor
        // TODO input processor with name in same object
        final InputProcessor inputProcessor = new InputProcessor() {

            @Override
            public boolean touchUp(int screenX, int screenY, int pointer, int button) {

                try {

                    // update internal input data
                    // update button data
                    getInputData(button - 10).update(InputStatus.Up);

                    // update touch data
                    getTouchInputData(pointer - 20).update(InputStatus.Up, screenX, screenY);

                    // run listener events

                    boolean handled = false;
                    // go over all action events and run the ones on up if
                    // applicable
                    for (int i = 0; i < actionEvents.get(this).size(); i++) {
                        ActionEventWrapper currEvent = actionEvents.get(this).get(i);
                        // curr event runs when releasing
                        if (currEvent.inputType.equals(ActionType.Up)) {

                            if (currEvent.code == pointer - 20 ||
                            // if the event runs for a particular touch
                            currEvent.code == button - 10 ||
                            // or the event runs on a particular button
                            currEvent.code == Keys.ANY_KEY)
                            // or the event runs on every key
                            {
                                handled = handled || currEvent.event.onClick(getInputData(button - 10));
                            }
                        }
                    }

                    // go over all touch event and run the ones on up
                    for (int i = 0; i < touchEvents.get(this).size(); i++) {
                        TouchEventWrapper currEvent = touchEvents.get(this).get(i);
                        // curr event runs when releasing
                        if (currEvent.inputType.equals(ActionType.Up)) {
                            if (currEvent.code == pointer - 20 ||
                            // if the event runs for a particular touch
                            currEvent.code == Action.ANY_ACTION)
                            // or the event runs on every key
                            {
                                handled = handled || currEvent.event.run(getTouchInputData(pointer - 20));
                            }
                        }
                    }

                    return handled;
                } catch (Exception e) {

                    Lumm.handleException(e);
                    return false;
                }
            }

            @Override
            public boolean touchDragged(int screenX, int screenY, int pointer) {

                try {

                    getTouchInputData(pointer - 20).update(InputStatus.Hold, screenX, screenY);

                    boolean handled = false;
                    // go over all touch event and run the ones on up

                    for (int i = 0; i < touchEvents.get(this).size(); i++) {

                        TouchEventWrapper currEvent = touchEvents.get(this).get(i);
                        // curr event runs when releasing
                        if ((currEvent.inputType.equals(ActionType.Down)
                                && getTouchInputData(pointer - 20).getInputStatus() == InputStatus.Down)
                                || (currEvent.inputType.equals(ActionType.Hold)
                                        && getTouchInputData(pointer - 20)
                                                .getInputStatus() == InputStatus.Hold)) {
                            if (currEvent.code == pointer - 20 ||
                            // if an event runs for a particular touch
                            currEvent.code == Action.ANY_ACTION)
                            // if an event runs for all touches
                            {
                                handled = handled || currEvent.event.run(getTouchInputData(pointer - 20));
                            }
                        }
                    }

                    return true;

                } catch (Exception e) {

                    Lumm.handleException(e);
                    return false;
                }
            }

            @Override
            public boolean touchDown(int screenX, int screenY, int pointer, int button) {

                try {

                    // update button data
                    if (getInputData(button - 10).getInputStatus().equals(InputStatus.None))
                        getInputData(button - 10).update(InputStatus.Down);

                    if (getInputData(pointer - 20).getInputStatus().equals(InputStatus.None))
                        getInputData(pointer - 20).update(InputStatus.Down);

                    // update pointer data
                    InputStatus targetInputStatus = getTouchInputData(pointer - 20).getInputStatus();
                    if (getTouchInputData(pointer - 20).getInputStatus().equals(InputStatus.None))
                        targetInputStatus = InputStatus.Down;
                    getTouchInputData(pointer - 20).update(targetInputStatus, screenX, screenY);

                    boolean handled = false;
                    // update internal input data
                    // ...

                    // run listener events
                    // go over all action events and run the ones on up if
                    // applicable
                    for (int i = 0; i < actionEvents.get(this).size(); i++) {
                        ActionEventWrapper currEvent = actionEvents.get(this).get(i);
                        // curr event runs when releasing
                        if ((currEvent.inputType.equals(ActionType.Down)
                                && getInputData(button - 10).getInputStatus() == InputStatus.Down)
                                || (currEvent.inputType.equals(ActionType.Hold)
                                        && getInputData(button - 10).getInputStatus() == InputStatus.Hold)) {

                            if (currEvent.code == pointer - 20 ||
                            // if the event runs for a particular touch
                            currEvent.code == button - 10 ||
                            // or the event runs for a particular button
                            currEvent.code == Action.ANY_ACTION)
                            // or any key is to be handled
                            {
                                handled = handled || currEvent.event.onClick(getInputData(button - 10));
                            }
                        }
                    }

                    // go over all touch event and run the ones on up
                    for (int i = 0; i < touchEvents.get(this).size(); i++) {

                        TouchEventWrapper currEvent = touchEvents.get(this).get(i);
                        // curr event runs when releasing

                        if ((currEvent.inputType.equals(ActionType.Down)
                                && getTouchInputData(pointer - 20).getInputStatus() == InputStatus.Down)
                                || (currEvent.inputType.equals(ActionType.Hold)
                                        && getTouchInputData(pointer - 20)
                                                .getInputStatus() == InputStatus.Hold)) {
                            if (currEvent.code == pointer - 20 ||
                            // if the event runs for a particular touch
                            currEvent.code == Action.ANY_ACTION)
                            // or any key is to be handled

                            {
                                try {
                                    handled = handled || currEvent.event.run(getTouchInputData(pointer - 20));
                                } catch (Exception e) {
                                    Lumm.net.logThrowable(e);
                                    e.printStackTrace();
                                }
                            }
                        }
                    }

                    return handled;

                } catch (Exception e) {

                    Lumm.handleException(e);
                    return false;
                }
            }

            @Override
            public boolean scrolled(int amount) {

                try {

                    // run listener events
                    boolean handled = false;
                    for (int i = 0; i < scrollEvents.get(this).size(); i++) {
                        handled = handled || scrollEvents.get(this).get(i).run(amount);
                    }
                    return handled;

                } catch (Exception e) {

                    Lumm.handleException(e);
                    return false;
                }
            }

            // ignore, can use Gdx.input for mouse position
            @Override
            public boolean mouseMoved(int screenX, int screenY) {

                return false;
            }

            @Override
            public boolean keyUp(int keycode) {

                try {

                    // update key press data
                    getInputData(keycode).update(InputStatus.Up);

                    // run listener events
                    boolean handled = false;
                    // go over all action events and run the ones on up if
                    // applicable

                    for (int i = 0; i < actionEvents.get(this).size(); i++) {
                        ActionEventWrapper currEvent = actionEvents.get(this).get(i);
                        // curr event runs when releasing
                        if (currEvent.inputType.equals(ActionType.Up)) {

                            if (currEvent.code == keycode ||
                            // or the event runs on a particular button
                            currEvent.code == Action.ANY_ACTION)
                            // or the event runs on every key
                            {
                                try {
                                    handled = handled || currEvent.event.onClick(getInputData(keycode));
                                } catch (Exception e) {
                                    Lumm.net.logThrowable(e);
                                    e.printStackTrace();
                                }
                            }
                        }
                    }
                    return handled;

                } catch (Exception e) {

                    Lumm.handleException(e);
                    return false;
                }
            }

            @Override
            public boolean keyTyped(char character) {

                try {
                    // run listener events
                    boolean handled = false;
                    for (int i = 0; i < keyTypedEvents.get(this).size(); i++) {
                        try {
                            handled = handled || keyTypedEvents.get(this).get(i).run(character);
                        } catch (Exception e) {
                            Lumm.net.logThrowable(e);
                            e.printStackTrace();
                        }
                    }
                    return handled;

                } catch (Exception e) {

                    Lumm.handleException(e);
                    return false;
                }
            }

            @Override
            public boolean keyDown(int keycode) {

                try {
                    // update key press data
                    if (getInputData(keycode).getInputStatus().equals(InputStatus.None))
                        getInputData(keycode).update(InputStatus.Down);

                    // run listener events
                    boolean handled = false;

                    // go over all action events and run the ones on up if
                    // applicable
                    for (int i = 0; i < actionEvents.get(this).size(); i++) {
                        ActionEventWrapper currEvent = actionEvents.get(this).get(i);
                        // curr event runs when releasing
                        if ((currEvent.inputType.equals(ActionType.Down)
                                && getInputData(keycode).getInputStatus() == InputStatus.Down)
                                || (currEvent.inputType.equals(ActionType.Hold)
                                        && getInputData(keycode).getInputStatus() == InputStatus.Hold)) {

                            if (currEvent.code == keycode ||
                            // the event runs for a particular key
                            currEvent.code == Action.ANY_ACTION)
                            // or any key is to be handled
                            {
                                try {
                                    handled = handled || currEvent.event.onClick(getInputData(keycode));
                                } catch (Exception e) {
                                    Lumm.net.logThrowable(e);
                                    e.printStackTrace();
                                }
                            }
                        }
                    }
                    return handled;

                } catch (Exception e) {

                    Lumm.handleException(e);
                    return false;
                }
            }
        };
        inputProcessors.put(processorNames[i], inputProcessor);
        actionEvents.put(inputProcessor, new ArrayList<ActionEventWrapper>());
        touchEvents.put(inputProcessor, new ArrayList<TouchEventWrapper>());
        scrollEvents.put(inputProcessor, new ArrayList<OnScrollListener>());
        keyTypedEvents.put(inputProcessor, new ArrayList<OnKeyTypedListener>());
        inputMultiplexer.addProcessor(inputProcessor);

    }
    // set LibGDX input processor to the one that we are creating
    Gdx.input.setInputProcessor(inputMultiplexer);
}

From source file:rx.libgdx.sources.GdxInputEventSource.java

License:Apache License

/**
 * @see rx.GdxObservable#fromInput//from w w  w.  j a v a  2 s .c om
 */
public static Observable<InputEvent> fromInput() {
    return create(new Observable.OnSubscribe<InputEvent>() {

        @Override
        public void call(final Subscriber<? super InputEvent> subscriber) {
            final InputProcessor processor = new InputProcessor() {

                @Override
                public boolean touchUp(int screenX, int screenY, int pointer, int button) {
                    if (!subscriber.isUnsubscribed()) {
                        subscriber.onNext(new TouchUpEvent(screenX, screenY, pointer, button));
                        return true;
                    }
                    return false;
                }

                @Override
                public boolean touchDown(int screenX, int screenY, int pointer, int button) {
                    if (!subscriber.isUnsubscribed()) {
                        subscriber.onNext(new TouchDownEvent(screenX, screenY, pointer, button));
                        return true;
                    }
                    return false;
                }

                @Override
                public boolean touchDragged(int screenX, int screenY, int pointer) {
                    if (!subscriber.isUnsubscribed()) {
                        subscriber.onNext(new TouchDraggedEvent(screenX, screenY, pointer));
                        return true;
                    }
                    return false;
                }

                @Override
                public boolean keyDown(int keycode) {
                    if (!subscriber.isUnsubscribed()) {
                        subscriber.onNext(new KeyDownEvent(keycode));
                        return true;
                    }
                    return false;
                }

                @Override
                public boolean keyUp(int keycode) {
                    if (!subscriber.isUnsubscribed()) {
                        subscriber.onNext(new KeyUpEvent(keycode));
                        return true;
                    }
                    return false;
                }

                @Override
                public boolean keyTyped(char character) {
                    if (!subscriber.isUnsubscribed()) {
                        subscriber.onNext(new KeyTypedEvent(character));
                        return true;
                    }
                    return false;
                }

                @Override
                public boolean mouseMoved(int screenX, int screenY) {
                    if (!subscriber.isUnsubscribed()) {
                        subscriber.onNext(new MouseMovedEvent(screenX, screenY));
                        return true;
                    }
                    return false;
                }

                @Override
                public boolean scrolled(int amount) {
                    if (!subscriber.isUnsubscribed()) {
                        subscriber.onNext(new ScrolledEvent(amount));
                        return true;
                    }
                    return false;
                }

            };

            final InputProcessor wrapped = Gdx.input.getInputProcessor();
            final InputMultiplexer im;
            if (wrapped instanceof InputMultiplexer) {
                im = (InputMultiplexer) wrapped;
            } else if (wrapped != null) {
                im = new InputMultiplexer(wrapped);
            } else {
                im = new InputMultiplexer();
            }

            im.addProcessor(processor);

            subscriber.add(Subscriptions.create(new Action0() {

                @Override
                public void call() {
                    im.removeProcessor(processor);
                    if (wrapped != null && wrapped != im && im.size() == 1) {
                        Gdx.input.setInputProcessor(wrapped);
                    }
                }

            }));

            Gdx.input.setInputProcessor(im);
        }

    });
}