Problem with custom InputHandler

hi everybody,



I have written a Custom Input Handler which essentially switches between various action modes depending upon

which button is active.



Essentially the problem is that this is not getting switched on…



ChessBoardhandler.java


public class ChessBoardHandler extends InputHandler {
    private ChessGameBoard game = null;
    private AbsoluteMouse mouse = null;
    private Node rootNode = null;
    private DisplaySystem display = null;
    private ChessBoardPick pick = null;
    private ChessBoardRotateAction mouseRotate =null;


    public ChessBoardHandler() {
        super();
        game = ChessGameBoard.getCurrentGame();
        rootNode = game.getRenderer().getBoardRootNode();
        display = DisplaySystem.getDisplaySystem();

        initCursor();

        //Now the movements
        pick = new ChessBoardPick(mouse);
        mouseRotate =  new ChessBoardRotateAction(mouse,rootNode);
       
    }


    private void initCursor() {
        MouseInput.get().setCursorVisible(false);
        mouse = new AbsoluteMouse("Mouse Input", display.getWidth(), display.getHeight());
        mouse.registerWithInputHandler(this);
       
        AlphaState alpha = DisplaySystem.getDisplaySystem().getRenderer().createAlphaState();
        alpha.setBlendEnabled(false);
        alpha.setSrcFunction(AlphaState.SB_SRC_ALPHA);
        alpha.setDstFunction(AlphaState.DB_ONE);
        alpha.setTestEnabled(true);
        alpha.setTestFunction(AlphaState.TF_GREATER);
        alpha.setEnabled(true);

        mouse.setRenderState(SkinManager.getDefaultManager().getTextureInfo(SkinEnum.Tex_DefaultCursor).getTextureState(Texture.MM_LINEAR_LINEAR, Texture.FM_LINEAR));
        mouse.setRenderState(alpha);
        mouse.setLocalScale(new Vector3f(1, 1, 1));
        mouse.setLocalTranslation(new Vector3f(display.getWidth() / 2, display.getHeight() / 2, 0.0F));

        rootNode.attachChild(mouse);
      }

    public void ActivateBoardRotationMode() {
         this.removeAllActions();
         this.addAction(mouseRotate);
    }

    public void ActivateZoomMode() {

    }

    public void ActivateTranslateMode() {

    }

    public void ActivatePickMode() {
        this.removeAllActions();
        this.addAction(pick);
       // setActionSpeed(100f);
    }
}



and below is the mouseinput action which should get activated


public class ChessBoardPick extends MouseInputAction {
    private float shotTime = 0;
    private ChessGameBoard game = null;
    private DisplaySystem display;
    private PickResults results;

    public ChessBoardPick(AbsoluteMouse mouse) {
        this.mouse = mouse;
        this.game = ChessGameBoard.getCurrentGame();
        display = DisplaySystem.getDisplaySystem();
        results = new BoundingPickResults();
    }

    public void performAction(InputActionEvent inputActionEvent) {
        shotTime += inputActionEvent.getTime();
        // MouseInput.get().isButtonDown(0)
        if( shotTime > 0.1f) {
        Vector2f screenPos = new Vector2f();
        // Get the position that the mouse is pointing to
        screenPos.set(mouse.getHotSpotPosition().x, mouse.getHotSpotPosition().y);
        // Get the world location of that X,Y value
        Vector3f worldCoords = display.getWorldCoordinates(screenPos, 0);
        Vector3f worldCoords2 = display.getWorldCoordinates(screenPos, 1);
        // Create a ray starting from the camera, and going in the direction of the mouse's location
        Ray mouseRay = new Ray(worldCoords, worldCoords2.subtractLocal(worldCoords).normalizeLocal());
        // Does the mouse's ray intersect the box's world bounds?
        results.setCheckDistance(true);
        game.getRenderer().getBoardRootNode().findPick(mouseRay, results);
        if (results.getNumber() > 0) {
            if (results.getPickData(0).getTargetMesh().getParentGeom() instanceof SimpleGameBoardRendererCell) {
                if (MouseInput.get().isButtonDown(0)) {
                    game.GamePositionActive(((SimpleGameBoardRendererCell) results.getPickData(0).getTargetMesh().getParentGeom()).getPos());
                } else {
                    game.GamePositionHover(((SimpleGameBoardRendererCell) results.getPickData(0).getTargetMesh().getParentGeom()).getPos());
                }
            }
        }
        results.clear();
        shotTime = 0f;
        }
    }
   
}



I have tested the ChessBoardPick class individually and it works perfectly when it is individually used on a InputHandler class

Can you pls tell me what is wrong????


also on a side note ... is this the best method of doing this ...?

Regds
AgentX


AgentX said:

also on a side note ... is this the best method of doing this ...?

No. Don't extend InputHandler. It is meant as a final class. It is not final because there are old implementations of handlers extending it. But it really for backwards compatibility only.
In your case it does not even make sense, as you do not override any methods from InputHandler as far as I can see from your posted code.
What about using an InputHandler for each mode and activating and deactivating them?

Actually that is exactly what i want to do.



the reason i have encapsulated them into a single class is for ease of use.

heres the new version which adds or removes actions depending on the mode



public class ChessBoardHandler{
    private InputHandler input;
    private ChessGameBoard game = null;
    private AbsoluteMouse mouse = null;
    private Node rootNode = null;
    private DisplaySystem display = null;
    private ChessBoardPick pick = null;
    private ChessBoardRotateAction mouseRotate =null;


    public ChessBoardHandler() {

        game = ChessGameBoard.getCurrentGame();
        rootNode = game.getRenderer().getBoardRootNode();
        display = DisplaySystem.getDisplaySystem();
        input = new InputHandler();
        initCursor();

        //Now key novements
        pick = new ChessBoardPick(mouse);
        mouseRotate =  new ChessBoardRotateAction(mouse,rootNode);
       
    }


    private void initCursor() {
        MouseInput.get().setCursorVisible(false);
        mouse = new AbsoluteMouse("Mouse Input", display.getWidth(), display.getHeight());
        mouse.registerWithInputHandler(input);
       
        AlphaState alpha = DisplaySystem.getDisplaySystem().getRenderer().createAlphaState();
        alpha.setBlendEnabled(false);
        alpha.setSrcFunction(AlphaState.SB_SRC_ALPHA);
        alpha.setDstFunction(AlphaState.DB_ONE);
        alpha.setTestEnabled(true);
        alpha.setTestFunction(AlphaState.TF_GREATER);
        alpha.setEnabled(true);

        mouse.setRenderState(SkinManager.getDefaultManager().getTextureInfo(SkinEnum.Tex_DefaultCursor).getTextureState(Texture.MM_LINEAR_LINEAR, Texture.FM_LINEAR));
        mouse.setRenderState(alpha);
        mouse.setLocalScale(new Vector3f(1, 1, 1));
        mouse.setLocalTranslation(new Vector3f(display.getWidth() / 2, display.getHeight() / 2, 0.0F));

        rootNode.attachChild(mouse);
      }

    public InputHandler getHandler(){
        return input;
    }

    public void ActivateBoardRotationMode() {
         input.removeAllActions();
         input.addAction(mouseRotate);
    }

    public void ActivateZoomMode() {

    }

    public void ActivateTranslateMode() {

    }

    public void ActivatePickMode() {
        input.removeAllActions();
        input.addAction(pick);
    }
 
}



to test this i even created a simple test chessboard renderer


public class TestChessBoardRenderer extends SimpleGame {
    private ChessGameBoard board;
  
       public static void main(String args[]){
       TestChessBoardRenderer app = new TestChessBoardRenderer();
       app.start();
    }

   protected void simpleInitGame() {
        Node tmp;
        display.setTitle("TestChessBoard Renderer Test");
       
        SkinManager.getDefaultManager().LoadDefaultData();
        SkinManager.getDefaultManager().LoadSkinData("WoodenSkin",true);
       
       board = new ChessGameBoard();
        board.initBoard();
        board.getRenderer().updateBoardItems();
        tmp = board.getRenderer().getBoardRootNode();
        tmp.setModelBound(new BoundingBox());
        tmp.updateModelBound();

        CullState cs = display.getRenderer().createCullState();
        cs.setCullMode(CullState.CS_BACK);
        rootNode.setRenderState(cs);
        ChessBoardHandler chesshandler = new ChessBoardHandler();
        input =chesshandler.getHandler();
        chesshandler.ActivatePickMode();
       // input =  new InputHandler();// new FirstPersonHandler(cam,50f,1f );
        cam.setLocation(new Vector3f(0f,-90,70f));
        cam.lookAt(tmp.getWorldTranslation(),new Vector3f(0,0,1));
        cam.update();
        rootNode.attachChild(tmp);
        rootNode.updateRenderState();
        rootNode.updateModelBound();
        lightState.setEnabled(false);
    
       lightState.setTwoSidedLighting(false);
    }
}



this just shows me the board , does not even show the cursor...

where am i going wrong?




but it still doesnt work.....


I would suggest adding all the input handlers to the InputHandler at once, then set each one enabled or disabled as needed. (As irrisior said)



Also, I don't see you pick and rotate actions, this probably where the problem lies…