Interact onClick=”” problem ( With custom controls )

Hi,

After the post about multiple Nifty screens problem, I am now trying to convert my ScreenControllers into Controller.

I think I have grasped how to do it.



3 have already been converted :slight_smile:

But I have issues with the fourth one :



This one is populated dynamicly.

It’s the map that display the available locations for the player depending on how far in the game he is.

Each location is a custom control and I add them like that :



[java]

CustomControlCreator controlCreator = new CustomControlCreator( “ae-imgButton” );

controlCreator.setStyle( “ae-mapElt-style” );

controlCreator.set( “firstEffect”, icon );

controlCreator.setInteractOnClick( “clickOnMapElt(” + name + “)” );

[/java]

( I may have to witch to ControlBuilder instead of CustomControlCreator )



When my map was a screenController it didn’t cause any problem and it sorks fine.

But now it’s control, It doesn’t work anymore : ( And it’s normal )



Now I have this :



Toolbar ScreenController

|__Map Controller



When I add a new Control inside an element of my Map controller, the setInteractOnClick() is bind to my Toolbar ScreenController and not to my Map Controller.



That means my method won’t work if I put it inside my Map class :

[java]

public class Map implements Controller

{

[…]

public void clickOnMapElt( String elt ) {[…]}

[…]

}

[/java]

But it will work if I put it my Toolbar class :

[java]

public class Toolbar implements ScreenController

{

[…]

public void clickOnMapElt( String elt ) {[…]}

[…]

}

[/java]

But I absolutly don’t want that!



Is there a way to link interaction of dynamicly created buttons to a Controller instead of the ScreenController ?



Thanks in advance.



Edit : For now I have done this ( Since all my custom controls are registered inside my toolbar )

[java]

public class Toolbar implements ScreenController

{

[…]



public void sheetClick( String sheetName, String methodName, String argument )

{

//


// 1 : Find the corresponding control
//
AbstractSheetController control = findControl( sheetName );
if( control == null )
{
LogManager.warn( this, "Can't launch " + methodName + " for sheet " + sheetName );
return;
}

//
// 2 : Launch the related method
//
try
{
Method[] methods = control.getClass().getMethods();
for( int i = 0; i < methods.length; i++ )
{
//For convenience we make sure that we have only one method with the correct name. No polymorphism here
Method method = methods;
if( ! methodName.equals( method.getName() ) )
continue;

//Only String parameters are authorized here
Class<?>[] parameters = method.getParameterTypes();
Object[] args = new String[parameters.length];
if( args.length > 0 )
args[0] = argument;
method.invoke( control, args );

return;
}
throw new Exception( "Can't find method " + methodName + " inside " + control.getClass().toString() );
}
catch( Exception e )
{
LogManager.error( this, e.getMessage() );
}
}
[/java]
And I change my onClick interaction :
[java]
//controlCreator.setInteractOnClick( "clickOnMapElt(" + name + ")" );
controlCreator.setInteractOnClick( "sheetClick(" + retrieveSheetName() + ",clickOnMapElt," + name + ")" );
[/java]

It works, but I wish do to do something cleaner :)

oh yeah! :smiley:



I’ve just fixed that bug some days ago and commited it to svn today! :smiley: The problem was that when you add a control to another control then the interact methods where not updated correctly (they didn’t know about that parent control).



That has been fixed now and should soon be in the nightly JME3 build too!

Great !



Thanks.