Hello everyone !! I have some question to ask.
I want to keep seperate Xml files for each Screen in Nifty. So, I use Nifty.addXml() insteads of Nifty.fromXMl() .
Moreover, I want each ScreenController to be AbstractAppState in order to access jmonkeygame elements and all of them require some
parameter.
The problem is, it seem that the ScreenController isn’t bind to the Nifty Screen so , is it a way to add ScreenController in each Screen.?
Thanks you in advance.
In the XML:
[xml]<screen id="someID" controller="somePackage.someController"></screen>[/xml]
When calling it in Java:
[java]nifty.fromXml("someXML.xml", "someID", someController);[/java]
Nifty.fromXML loads an xml file… /boggle on the Nifty.addXML
Why not just switch screens?
[java]nifty.gotoScreen("someScreen");[/java]
For that matter… why not create all your elements via code and load blank screens. Bypasses the xml parser which seems slow.
I guess you could use AbstractAppState… OR you could pass in a reference to your app and use Callables to update your scene graph.
@rocky: As I remember, Nifty can hold only one XML-File at the same time.
And as t0neg0d mentioned you can let nifty create the screenController-instance (which automatically binds it to the screen). After that you can load this controller by calling nifty.getScreen(“id”).getScreenController(). Than you can attach the screen to your appState-Manager if it inherits from AbstractAppState.
@t0neg0d: The parser is not the fastest one, but I think it’s much easier to create the gui in an html-stylish way using the xml-format.
[xml]<screen id=“something” controller=“visuallibrary.scene.LibrarySceneAppState”>[/xml]
This is my xml. As I understand, define this xml alone doesn’t make nifty to create screenController-instance,
I have to do something such as nifty.fromXml(“XML.xml”, “ID”, someController); right??? ( Sorry if I mis-understand, I’m a newbie here )
And If I let nifty create screenController-instance for me, It will use default constructor.
But what to do if those ScreenController need some parameter for its constructor ?
For the reason that I want to seperate XML because I think it’s more easier to maintain.
However if Nifty can hold only one XML-File at the same time, I will consider the way that your guy told me.
Anyway, Thank yous every one I really appreciate all your help
Oh my mistake. Its seem that to define the controller for screen in xml file is enough for let the nifty create screenController for me.
But the default constructor in the ScreenController class must be provided. ( The addXml() is also work with this method too)
However , I still wonder if there are anyway to tell Nifty to use Constructor that contain some parameter to initial the class.
If there are none. Then maybe I should create some method for setting those parameters
and call those setting before attach the AbstractAppState to AppStateManager.
Thanks you everyone
The bind() method is called before the onStartScreen() when the screen is showed up for the first time, then you use the bind() one to initialize your nifty objects. If your screen extends AbstractScreenController you can initialize your app objects in initialize() method. And yeah, the addXml() is the right method if you wanna keep your screen in separated xml files.
But you have to have some way of attaching the state and that’s the tricky part from bind().
You should be able to provide all of your screen controllers on the fromXml() line, I guess. I would expect them to then be matched up in the addXml() but I don’t know because I’ve never tried that. If could be that addXml() is useless in this (common) jme pattern… but only testing would tell for sure.
But you have to have some way of attaching the state and that’s the tricky part from bind().
Only if he didn't attach the state before using the goToScreen() method. For the first time, I attach the first screen to the state manager, then I use the goToScreen() method. Then When I want to go to other screen, I use a method like this one (or something like this, I don't remember well):
[java]
private void goToScreen(String id) {
AppState state = (AppState)nifty.getScreen(id).getScreenController();
stateManager.attach(state);
stateManager.detach(this);
nifty.goToScreen(id);
}
[/java]
Then when the bind() method of the next screen is called, it will be already added to the state manager. Am I drunk :D?
No, you do everything manually… but that is not obvious to everyone when they are trying to get things to work the easy way.
And by then I think an extra screen controller has already been instantiated which can be confusing and wasteful depending on what it’s doing.
Hmm. I got what you mean. Yeah, the addXml file just add a xml file, it has’t necessarily to be a screen, but it might be a xml file with the sounds / musics / custom effects registered. About the fromXml() one, it does automatically remove the current screen, load a screen, and then go to the screen for you. Unfortunately both addXml() and fromXml() have disadvantages. The addXml() you do the things manually, but it works for multiple xml files. The fromXml() does the things automatically for you, but it doesn’t work for multiple xml files. Then it depends what’s going to be most useful for the user.
The addXml() also overrides existing screens with the same ID, I think. I can’t remember.
Anyway, it’s a good way of having different screens loaded for different reasons. Maybe you have two different options screens one for 16:9 and one for 4:3 ratio, etc. And you can figure out which one you want at runtime and just add the right one while still having your one “set of screens” from Nifty’s perspective.
Wow!!!
All of your comments are very useful for me.
Thanks for information and good tips