I ended up having nifty gui work perfectly with marte engine.
What I did is create a NiftyWorld class which extends World and have all the nasty nifty gui code in there. I got most of the code from the NiftyBasicGame class and altered it a bit to work with marte. I also created a class for the MouseEvents which I got the code from here:
viewtopic.php?f=3&t=3436 The nifty libraries I used are nifty-1.3.jar, nifty-lwjgl-renderer-1.3,jar (not the slick renderer), nifty-default-control-1.3.jar, and nifty-default-controls-1.3.jar. The only thing that doesn't really work is when using a textfield, you can't hold, for an example, backspace to delete all the characters. You have to repeatedly press it to delete them all instead of just holding it in. I am not sure how to fix that. Its a problem for any key.
Code:
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.NiftyInputConsumer;
import de.lessvoid.nifty.input.keyboard.KeyboardInputEvent;
import de.lessvoid.nifty.nulldevice.NullSoundDevice;
import de.lessvoid.nifty.renderer.lwjgl.input.LwjglKeyboardInputEventCreator;
import de.lessvoid.nifty.renderer.lwjgl.render.LwjglRenderDevice;
import de.lessvoid.nifty.spi.input.InputSystem;
import de.lessvoid.nifty.tools.TimeProvider;
import it.randomtower.engine.World;
import java.util.ArrayList;
import java.util.List;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.opengl.SlickCallable;
import org.newdawn.slick.state.StateBasedGame;
/**
* A class for using Nifty-gui with Marte Engine's world class.
* @author Colt
* @version Nov 24, 2011
*/
public class NiftyWorld extends World {
/** The maximum number of controllers supported by the basic game */
private static final int MAX_CONTROLLERS = 20;
/** The maximum number of controller buttons supported by the basic game */
private static final int MAX_CONTROLLER_BUTTONS = 100;
/** The state of the left control */
protected boolean[] controllerLeft = new boolean[MAX_CONTROLLERS];
/** The state of the right control */
protected boolean[] controllerRight = new boolean[MAX_CONTROLLERS];
/** The state of the up control */
protected boolean[] controllerUp = new boolean[MAX_CONTROLLERS];
/** The state of the down control */
protected boolean[] controllerDown = new boolean[MAX_CONTROLLERS];
/** The state of the button controlls */
protected boolean[][] controllerButton = new boolean[MAX_CONTROLLERS][MAX_CONTROLLER_BUTTONS];
protected Nifty nifty;
protected int mouseX;
protected int mouseY;
private List<MouseEvent> mouseEvents = new ArrayList<MouseEvent>();
private List<KeyboardInputEvent> keyEvents = new ArrayList<KeyboardInputEvent>();
private LwjglKeyboardInputEventCreator inputEventCreator = new LwjglKeyboardInputEventCreator();
private boolean mouseDown;
/**
* Creates a world with the use of Nifty - GUI.
* @param id
* @param gc
*/
public NiftyWorld(int id, GameContainer gc) {
super(id, gc);
}
/**
* @see org.newdawn.slick.InputListener#setInput(org.newdawn.slick.Input)
*/
@Override
public void setInput(Input input) {
}
/**
* @see org.newdawn.slick.Game#closeRequested()
*/
public boolean closeRequested() {
return true;
}
@Override
public void init(GameContainer container, StateBasedGame sbg) throws SlickException {
nifty = new Nifty(new LwjglRenderDevice(), new NullSoundDevice(), new InputSystem() {
@Override
public void forwardEvents(final NiftyInputConsumer inputEventConsumer) {
for (MouseEvent event : mouseEvents) {
event.processMouseEvents(inputEventConsumer);
}
mouseEvents.clear();
for (KeyboardInputEvent event : keyEvents) {
inputEventConsumer.processKeyboardEvent(event);
}
keyEvents.clear();
}
@Override
public void setMousePosition(int x, int y) {
}
}, new TimeProvider());
}
@Override
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
nifty.update();
}
@Override
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
SlickCallable.enterSafeBlock();
nifty.render(true);
SlickCallable.leaveSafeBlock();
}
private void forwardMouseEventToNifty(final int mouseX, final int mouseY, final boolean mouseDown) {
mouseEvents.add(new MouseEvent(mouseX, mouseY, mouseDown, 0));
}
/**
* @see org.newdawn.slick.InputListener#mouseMoved(int, int, int, int)
*/
@Override
public void mouseMoved(final int oldx, final int oldy, final int newx, final int newy) {
mouseX = newx;
mouseY = newy;
forwardMouseEventToNifty(mouseX, mouseY, mouseDown);
}
/**
* @see org.newdawn.slick.InputListener#mousePressed(int, int, int)
*/
@Override
public void mousePressed(final int button, final int x, final int y) {
mouseX = x;
mouseY = y;
mouseDown = true;
forwardMouseEventToNifty(mouseX, mouseY, mouseDown);
}
/**
* @see org.newdawn.slick.InputListener#mouseReleased(int, int, int)
*/
@Override
public void mouseReleased(final int button, final int x, final int y) {
mouseX = x;
mouseY = y;
mouseDown = false;
forwardMouseEventToNifty(mouseX, mouseY, mouseDown);
}
/**
* @see org.newdawn.slick.InputListener#keyPressed(int, char)
*/
@Override
public void keyPressed(final int key, final char c) {
keyEvents.add(inputEventCreator.createEvent(key, c, true));
}
/**
* @see org.newdawn.slick.InputListener#keyReleased(int, char)
*/
@Override
public void keyReleased(final int key, final char c) {
keyEvents.add(inputEventCreator.createEvent(key, c, false));
}
/**
* @see org.newdawn.slick.InputListener#mouseDragged(int, int, int, int)
*/
@Override
public void mouseDragged(int oldx, int oldy, int newx, int newy) {
}
/**
* @see org.newdawn.slick.InputListener#mouseClicked(int, int, int, int)
*/
@Override
public void mouseClicked(int button, int x, int y, int clickCount) {
}
/**
* @see org.newdawn.slick.InputListener#controllerButtonPressed(int, int)
*/
@Override
public void controllerButtonPressed(int controller, int button) {
controllerButton[controller][button] = true;
}
/**
* @see org.newdawn.slick.InputListener#controllerButtonReleased(int, int)
*/
@Override
public void controllerButtonReleased(int controller, int button) {
controllerButton[controller][button] = false;
}
/**
* @see org.newdawn.slick.InputListener#controllerDownPressed(int)
*/
@Override
public void controllerDownPressed(int controller) {
controllerDown[controller] = true;
}
/**
* @see org.newdawn.slick.InputListener#controllerDownReleased(int)
*/
@Override
public void controllerDownReleased(int controller) {
controllerDown[controller] = false;
}
/**
* @see org.newdawn.slick.InputListener#controllerLeftPressed(int)
*/
@Override
public void controllerLeftPressed(int controller) {
controllerLeft[controller] = true;
}
/**
* @see org.newdawn.slick.InputListener#controllerLeftReleased(int)
*/
@Override
public void controllerLeftReleased(int controller) {
controllerLeft[controller] = false;
}
/**
* @see org.newdawn.slick.InputListener#controllerRightPressed(int)
*/
@Override
public void controllerRightPressed(int controller) {
controllerRight[controller] = true;
}
/**
* @see org.newdawn.slick.InputListener#controllerRightReleased(int)
*/
@Override
public void controllerRightReleased(int controller) {
controllerRight[controller] = false;
}
/**
* @see org.newdawn.slick.InputListener#controllerUpPressed(int)
*/
@Override
public void controllerUpPressed(int controller) {
controllerUp[controller] = true;
}
/**
* @see org.newdawn.slick.InputListener#controllerUpReleased(int)
*/
@Override
public void controllerUpReleased(int controller) {
controllerUp[controller] = false;
}
/**
* @see org.newdawn.slick.InputListener#mouseWheelMoved(int)
*/
@Override
public void mouseWheelMoved(int change) {
}
/**
* @see org.newdawn.slick.InputListener#isAcceptingInput()
*/
@Override
public boolean isAcceptingInput() {
return true;
}
/**
* @see org.newdawn.slick.InputListener#inputEnded()
*/
@Override
public void inputEnded() {
}
/**
* @see org.newdawn.slick.ControlledInputReciever#inputStarted()
*/
@Override
public void inputStarted() {
}
}
Code:
import de.lessvoid.nifty.NiftyInputConsumer;
/**
*
* @author Colt
* @version Nov 24, 2011
*/
public class MouseEvent {
private int mouseX;
private int mouseY;
private int mouseWheel;
private int button;
private boolean buttonDown;
public MouseEvent(final int mouseX, final int mouseY, final boolean mouseDown, final int mouseButton) {
this.mouseX = mouseX;
this.mouseY = mouseY;
this.buttonDown = mouseDown;
this.button = mouseButton;
this.mouseWheel = 0;
}
public void processMouseEvents(final NiftyInputConsumer inputEventConsumer) {
inputEventConsumer.processMouseEvent(mouseX, mouseY, mouseWheel, button, buttonDown);
}
}
So if anyone wants to use Nifty-gui with marte engine and they don't wanna go through the hassle of getting it to work correctly, just put this two classes in your project. When you create a new state just extend NiftyWorld and then the rest is up to you! Basic example:
Code:
import de.lessvoid.nifty.builder.ElementBuilder.Align;
import de.lessvoid.nifty.builder.ElementBuilder.VAlign;
import de.lessvoid.nifty.builder.LayerBuilder;
import de.lessvoid.nifty.builder.PanelBuilder;
import de.lessvoid.nifty.builder.ScreenBuilder;
import de.lessvoid.nifty.controls.button.builder.ButtonBuilder;
import de.lessvoid.nifty.controls.label.builder.LabelBuilder;
import de.lessvoid.nifty.controls.textfield.builder.TextFieldBuilder;
import de.lessvoid.nifty.screen.Screen;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;
/**
*
* @author Colt
* @version Nov 24, 2011
*/
public class WorldExample extends NiftyWorld {
public WorldExample(int id, GameContainer gc) {
super(id, gc);
}
@Override
public void init(GameContainer gc, final StateBasedGame sbg) throws SlickException {
super.init(gc, sbg);
//you still have to lood the controller and style files
//you don't have to create a new nifty object. you can use the nifty object from NiftyWorld
nifty.loadStyleFile("nifty-default-styles.xml");
nifty.loadControlFile("nifty-default-controls.xml");
Screen mainScreen = new ScreenBuilder("main") {
{
//you have to create a worldexamplecontroller class
controller(new WorldExampleController(sbg));
layer(new LayerBuilder("foreground") {
{
childLayoutCenter();
backgroundColor("#0000");
panel(new PanelBuilder("namePanel") {
{
height("130px");
width("200px");
style("nifty-panel");
align(Align.Center);
valign(VAlign.Center);
childLayoutVertical();
control(new LabelBuilder("nameLbl") {
{
label("Enter infection name");
alignCenter();
}
});
control(new TextFieldBuilder("name") {
{
width("120px");
height("20px");
alignCenter();
paddingBottom("40px");
}
});
control(new ButtonBuilder("next") {
{
label("Next");
width("120px");
height("40px");
alignCenter();
}
});
}
});
}
});
}
}.build(nifty);
nifty.addScreen("mainScreen", mainScreen);
nifty.gotoScreen("mainScreen");
}
@Override
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
super.update(gc, sbg, delta);
}
@Override
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
super.render(gc, sbg, g);
}
}
Hope this helps, because I had a lot of trouble with this for some reason
