Nifty Button on Button pressed and on button released implementation

i am working on Android 3D game for tablet. and i want to implement button events (onButtonPressed and onButtonReleased) for Game pad. and i am using Nifty Button. Do you have any idea about how to implement it. Nifty button have only onClick event. so how can i implement onPressed and onReleased.

If you are heavily invested in Nifty for the rest of your application then by all means, hopefully someone will help you with that.

But if all you want is a button on screen that you can get separate press/release events then there are lightweight alternatives that I can recommend. (For example, Lemur could do this pretty easily in only a few lines of code added to your app.)

Are you controlling a cursor with the gamepad or just basic navigation with d-pad / joystick?

Yeah, I also missed the ‘game pad’ thing. So, you are navigating some menus or something with a game pad and want to detect pressed and released on a Nifty button?

(clarification will be useful for those wanting to help and it temporarily rules Lemur out as a solution.)

@ bloodwalker: just basic navigation with d-pad

@pspeed: thanks. i figured that lemur is a good solution

Well, Lemur doesn’t (yet) support navigation with a d-pad. Soon… but I have to get out from under some other stuff first.

…but creating a button and getting pressed/released events is easy, at least. Once the rest is there. :smile:

@pspeed: can you suggest best tutorial site links or pdf resources about lemur, please?

The Lemur wiki is probably the best place to start:

If you search the forum for “Lemur Gems” you will find some examples also.

Edit: note it’s also available as a public dependency if you are using gradle or maven. I should add that to the docs but can provide the info here if you build that way.

Thank you.

Ok, One wasy implementation, while not the best, is what I did when I implemented on the Ouya.

  1. Store the “state” of the menu, meaning which menu/submenu you are in.

  2. Have a listener for the inputs from your gamepad. That way when you press for example, the “A” button and you know which menu, and menu item you are the action will be executed.

     @Override
     public void onKeyDown(int keyCode) {
         System.out.println("currentState: " + currentState);
         System.out.println("keyCode: " + keyCode);
         switch(currentState){
             case INSTRUCTIONS:
                 onHowToPlayClose();
                 currentState = GameMenuState.NONE;
                 break;
             case NONE:
                 if(keyCode == OuyaConstants.BUTTON_MENU){
                     currentState = GameMenuState.PAUSE_MENU;
                     onPauseGame();
                     changePauseOption(-selectedPauseMenuOption);
                 }
                 break;
             case PAUSE_MENU:
                 processPauseMenu(keyCode);
                 break;
             case NEW_GAME:
                 processNewGameMenu(keyCode);
                 break;
             case OPTIONS:
                 processOptionsMenu(keyCode);
                 break;
             case DESTINATION_SELECT:
                 processDestinationSelect(keyCode);
                 break;
             case WINNER:
                 if(keyCode == OuyaConstants.BUTTON_O){
                     currentState = GameMenuState.NONE;
                     onCloseWinnerWindow();
                 }
                 break;
             default:
                 break;
         }
     }
    
  3. for when d-pad/joystick actions happen, you change menu items or indexes and replace images to when the current menu item is selected or deselected

    private void processDestinationSelect(int keyCode){
    switch(keyCode){
    case OuyaConstants.BUTTON_MENU:
    case OuyaConstants.BUTTON_A:

    break;
    case OuyaConstants.BUTTON_DPAD_LEFT:
    changeDestinationSelect(-1);
    break;
    case OuyaConstants.BUTTON_DPAD_RIGHT:
    changeDestinationSelect(1);
    break;
    case OuyaConstants.BUTTON_O:

    default:
    break;
    }
    }

    private void changeDestinationSelect(int diffValue){
    changeImage(“move_” + selectedPossibleMove, deselectedDestinations.get(selectedPossibleMove));

     selectedPossibleMove += diffValue;
     
     if(selectedPossibleMove >= numPossibleMoves){
         selectedPossibleMove = 0;
     }else if(selectedPossibleMove < 0){
         selectedPossibleMove = numPossibleMoves - 1;
     }
     
     changeImage("move_" + selectedPossibleMove, selectedDestinations.get(selectedPossibleMove));
    

    }

You can use any GUI implementation with something like this. It’s an easy approach and a good start, IMO