I wanted to start developing a new game and I’m currently trying to setup basic classes for everything, including UI stuff. I’m obviously using Nifty GUI as the GUI library (there is no alternative there, is it?). I wanted to use either the Model-View-Controller or Model-View-Presenter design patterns, but I have trouble connecting this with Nifty because you don’t have classes like “Button” or “Textfield” or “Panel” like other GUI libraries have (e.g. Swing). Instead you are using these weird builders which just return Element objects. These elements have no type hierarchy or anything, and it seems impossible to attach some kind of mouse click handler / listener to them. In the tutorials these things are handled very differently using method names and stuff. I don’t like this way of programming where you construct your GUI with these Builders where you put everything together in this weird way, and I also dislike building a GUI using mostly XML. Is there no other way? Because I feel like it’s impossible to follow a proper design pattern here.
Nifty does support MVC. The controller is the ScreenController, the model is your program, the view is the nifty interface.
You can use builders to construct GUIs in code rather than from XML. There are different builders for each of the different things you can build…you can then fetch back the control/renderer/whatever from the constructed scene in order to make changes later on.
This may interest you though: http://hub.jmonkeyengine.org/forum/topic/mythrunas-new-menuing-system/
I get the feeling you didn’t understand my problem. The issue is that I dislike these builders which just return some objects of class Element, not Button, Label or Panel. You can store these as variables and stuff, but you can’t attach click listeners on them. In the examples this is done via this gross anonymous inner class where interactOnClick() is called. I find this very error prone since you can just change the method and no IDE can alert you about this. Because of this Builder thing you can’t write your own reusable components very well. E.g. in Swing or GWT you can just extends from a Button an enhance it. Here creating an inheritance hierarchy is rather tedious because of these Builders.
My question is if there is another way which is not in the JME3 Nifty tutorials.
You should take a look at nifty’s website. They have a REALLY GOOD Manual over there, and then you can start doing the XML way, which IMO is the best feature nifty has.
MVC and inheritance have nothing to do with another whatsoever :?
But XML is nice
Well I guess there is no way around it. I just dislike using XML for UI stuff, it makes it harder to reuse stuff in my eyes. But I never used such a system before, so I guess I’ll have to try it out
The main advantage of XML isn’t for you as the programmer. It’s for team/collaborative working as it allows non-programmers to work on the UI without needing your intervention.
For example there is a composer working on sound in HeroDex. I’ve provided him with a build that has the music playlists etc set up in a simple properties file. He can now swap in and test sound as he likes and then only needs my input when he is ready to show me something. The same thing can apply to user interface, translations, level design, etc. Anything that doesn’t need a programmer is a job that potentially someone else can do.
If you are working on a small 1-person project its not such a big gain, it’s still worth being familiar with the tools and techniques involved though.
Think also to allow other people to modify the UI. Some games (like Heroes of Newerth) did this, and a lot of people loved it (although it died eventually). That’s why I’m using the XML, so you can change the way you want.
After you get your element from using the builder, just call findNiftyControl (not sure about the method name) on this element to get instance of the control that you created.