(nifty) Need explanation methods in making a custom controller class

I can’t really seem to find any information on this… I’m trying to make my own “Controller” (de.lessvoid.nifty.controls.Controller)

my first question is

  1. whats the difference between bind() and init()? I’ve noticed init gets called after bind, but why is there both?

  2. in bind (and in init) it gives both “Properties” and “Attributes” as parameters. As far as I can tell they both hold the same information. why am I given both of these as parameters to the interface methods?

thanks for your help, this has been driving me nuts all day.

Have you seen this?

and this?

http://hub.jmonkeyengine.org/forum/topic/nifty-custom-control/

Yes, amongst many other google searches I’ve done.

I’m not having trouble bassically setting up the custom control. I just dont understand the java Interface class and why it has the methods it does. and I can’t seem to find an explanation anywhere. Theres no javadoc and most guides on the matter are just quick set up guides. Nothing that fully explains the methods/parameters.

I’m not a nifty user but I was able to find this in the javadoc. It made sense to me.
http://nifty-gui.sourceforge.net/projects/1.3.1/nifty/nifty/apidocs/de/lessvoid/nifty/controls/Controller.html

well my only thought about init() is that there might be cases where there’s code that needs to be run that only works once all objects are bound. I can’t think of a scenario where that would be the case but perhaps on really complicated screens it could happen.

What about using “Properties parameter” versus “Attributes controlDefinitionAttributes”. why am i given two objects which seem to hold the same information? Is a reference to the them held back somewhere else? can i safely modify them (if i call set(String,String) on it is that going to impact anything external?). Why do I get these objects twice? (first receive them in bind() then in init()).

If you have a controller that needs to look up other controllers on the screen then I can see how having a separate bind() and init() pass could be very important. Until they are bound the components don’t really exist in the “screen” yet. (I’m guessing.)

I have no idea on the parameters… but the lifecycle was pretty clear to me from the javadocs so I posted the link.

Most likely you receive them twice so that you don’t need to store them. It’s quite a common pattern. I also have no idea why there are separate properties and attributes @void256 will know.

Your best bet may be to take a look at the standard controls and see how they use them.

Somehow I don’t get notifications per email anymore or I am too stupid to configure it right. My apologies for not answering sooner! :frowning:

[java]… Properties parameter, Attributes controlDefinitionAttributes)[/java]

Originally the idea was:

Properties parameter - gives you access to all attributes from the control-Tag. So all the additional parameters you apply to the control tag are available in the Properties instance given.

Here is an example from the ImageSelectControl:

[java]public void bind(
final Nifty niftyParam,
final Screen screenParam,
final Element newElement,
final Properties properties,
final Attributes controlDefinitionAttributes) {

imageWidth = new SizeValue(properties.getProperty(“imageWidth”, “0px”)).getValueAsInt(1.0f);

[/java]

Attributes controlDefinitionAttributes - originally this gave you access to all the default attributes from the controlDefinition Tag. So you could have for instance defaults set directly where you define the control with the controlDefinition XML and you would access these properties with the controlDefinitionAttributes. And if you want to access the actual attributes you would use the properties parameter.

HOWEVER, as you’ve already noticed, you’ll get the same attributes in both parameters. The reason for this is that Nifty will apply all the attributes from the controlDefinition first and then applies the actual attributes from the control internally too AND the controlDefinitionAttributes parameter is not really necessary. In fact I’ve checked and I can’t find any standard control that uses that parameter! So there was never really a use case to access the default values from the controlDefinition separately since you already get them in the properties parameter if the control did not overwrite them.

I think I realized that at some point but couldn’t change the interface anymore without breaking lots of existing code.

So this is:

a) a feature not used
b) an example for bad API design (there should only be one properties parameter and it should be read-only!)
c) an example for lack of API documentation

I’ve created an issue on github for this: https://github.com/void256/nifty-gui/issues/137 so that this can be fixed in Nifty 1.4!