hey there,
I have a small game in which I use GameStates and a FengGUI-Menu.
When pressing ESC in the InGameState, I switch to MenuState.
While everything in the InGameState is drawn properly,
the Menu lacks its buttons and the functions bound to them.
Maybe someone could help me there?
the “menu” now looks like this:
my code looks the following:
import java.io.IOException;
import org.fenggui.Button;
import org.fenggui.Container;
import org.fenggui.Display;
import org.fenggui.background.PlainBackground;
import org.fenggui.composites.MessageWindow;
import org.fenggui.event.ButtonPressedEvent;
import org.fenggui.event.IButtonPressedListener;
import org.fenggui.event.mouse.IMousePressedListener;
import org.fenggui.event.mouse.IMouseReleasedListener;
import org.fenggui.event.mouse.MousePressedEvent;
import org.fenggui.event.mouse.MouseReleasedEvent;
import org.fenggui.layout.Alignment;
import org.fenggui.layout.RowLayout;
import org.fenggui.layout.StaticLayout;
import org.fenggui.render.Binding;
import org.fenggui.render.Pixmap;
import org.fenggui.util.Color;
import org.fenggui.util.Spacing;
import com.jme.input.AbsoluteMouse;
import com.jme.input.Mouse;
import com.jme.input.MouseInput;
import com.jme.math.Vector3f;
import com.jme.renderer.Renderer;
import com.jme.scene.SceneElement;
import com.jme.scene.state.LightState;
import com.jme.system.DisplaySystem;
import com.jmex.game.state.CameraGameState;
import com.jmex.game.state.GameState;
import com.jmex.game.state.GameStateManager;
public class MyMainMenuState extends CameraGameState implements IMouseReleasedListener, IMousePressedListener{
private Game parent;
private DisplaySystem menuDisplay;
private final Container menuContainer;
private Mouse menuMouse;//used in initInput()
private org.fenggui.Display menuFengguiDisplay;
private FengJMEInputHandler mainMenuInputHandler;
private GameMenuButton newGame, backToGame, options, credits, quit;
private GameMenuButton sound, graphics, back, network;
private org.fenggui.Display dispToDetachWidgetsFrom;
public MyMainMenuState(String name, Game parent) {
super(name);
this.parent=parent;
cam.setLocation(new Vector3f(0,0,20));
cam.setDirection(new Vector3f(0,0,-20));
cam.update();
MouseInput.get().setCursorVisible(true);
menuDisplay=DisplaySystem.getDisplaySystem();
menuDisplay.getRenderer().setBackgroundColor(ColorRGBA.orange);
menuContainer=new Container();
initInput();
rootNode.setLightCombineMode(LightState.OFF);
rootNode.setRenderQueueMode(Renderer.QUEUE_ORTHO);
rootNode.setCullMode(SceneElement.CULL_NEVER);
rootNode.updateRenderState();
rootNode.updateGeometricState(0, true);
}
private void initInput(){
menuMouse=new AbsoluteMouse(
"menuMouse", menuDisplay.getWidth(), menuDisplay.getHeight());
}
public void onActivate() {
menuDisplay.setTitle("House Of Bubbles Menu");
parent.getGameInputHandler().setEnabled(false);
buildMainMenu();
super.onActivate();
}
public void onDeactivate() {
mainMenuInputHandler.setEnabled(false);
parent.getGameInputHandler().setEnabled(true);
super.onDeactivate();
}
private void buildMainMenu(){
menuFengguiDisplay=parent.getGameFengguiDisplay();
menuFengguiDisplay.addWidget(menuContainer);
menuContainer.getAppearance().add(new PlainBackground(Color.BLACK_HALF_OPAQUE));
menuContainer.getAppearance().setPadding(new Spacing(10, 10));
menuContainer.setLayoutManager(new RowLayout(false));
mainMenuInputHandler=new FengJMEInputHandler(menuFengguiDisplay);
mainMenuInputHandler.setEnabled(true);
initMenuButtons(menuContainer, menuFengguiDisplay);
buildMainMenuGUI(menuContainer, menuFengguiDisplay);
}
private void initMenuButtons(
final Container menuContainer, final Display menuFengguiDisplay){
newGame = new GameMenuButton("data/images/play0.png","data/images/play1.png");
backToGame = new GameMenuButton("data/images/btg0.png","data/images/btg1.png");
options = new GameMenuButton("data/images/options0.png", "data/images/options1.png");
credits = new GameMenuButton("data/images/credits0.png", "data/images/credits1.png");
quit = new GameMenuButton("data/images/quit0.png", "data/images/quit1.png");
sound = new GameMenuButton("data/images/sound0.png", "data/images/sound1.png");
graphics = new GameMenuButton("data/images/graphics0.png", "data/images/graphics1.png");
back = new GameMenuButton("data/images/back0.png", "data/images/back1.png");
network = new GameMenuButton("data/images/network0.png", "data/images/network1.png");
newGame.addButtonPressedListener(new IButtonPressedListener(){
public void buttonPressed(ButtonPressedEvent e) {
// TODO create newGame, for now: just go back to game
GameStateManager.getInstance().deactivateChildNamed("mainMenu");
dispToDetachWidgetsFrom = parent.getGameFengguiDisplay();
dispToDetachWidgetsFrom.removeAllWidgets();
GameStateManager.getInstance().activateChildNamed("inGame");
}
});
backToGame.addButtonPressedListener(new IButtonPressedListener(){
public void buttonPressed(ButtonPressedEvent e) {
GameStateManager.getInstance().deactivateChildNamed("mainMenu");
dispToDetachWidgetsFrom = parent.getGameFengguiDisplay();
dispToDetachWidgetsFrom.removeAllWidgets();
GameStateManager.getInstance().activateChildNamed("inGame");
}
});
options.addButtonPressedListener(new IButtonPressedListener(){
public void buttonPressed(ButtonPressedEvent e) {
buildOptionsMenu(menuContainer, menuFengguiDisplay);
}
});
credits.addButtonPressedListener(new IButtonPressedListener(){
public void buttonPressed(ButtonPressedEvent e) {
MessageWindow mw = new MessageWindow("Thank you momie :)");
mw.pack();
menuFengguiDisplay.addWidget(mw);
StaticLayout.center(mw, menuFengguiDisplay);
}
});
quit.addButtonPressedListener(new IButtonPressedListener(){
public void buttonPressed(ButtonPressedEvent e) {
System.out.println("game end");
Game.exit();
}
});
back.addButtonPressedListener(new IButtonPressedListener(){
public void buttonPressed(ButtonPressedEvent e) {
buildMainMenuGUI(menuContainer, menuFengguiDisplay);
}
});
}
private void buildMainMenuGUI(
final Container menuContainer, final Display menuFengguiDisplay){
menuContainer.removeAllWidgets();
menuContainer.addWidget(newGame);
menuContainer.addWidget(backToGame);
menuContainer.addWidget(options);
menuContainer.addWidget(credits);
menuContainer.addWidget(quit);
menuContainer.pack();
StaticLayout.center(menuContainer, menuFengguiDisplay);
}
private void buildOptionsMenu(
final Container menuContainer, final Display menuFengguiDisplay){
menuContainer.removeAllWidgets();
menuContainer.addWidget(sound);
menuContainer.addWidget(graphics);
menuContainer.addWidget(network);
menuContainer.addWidget(back);
menuContainer.pack();
StaticLayout.center(menuContainer, menuFengguiDisplay);
}
class GameMenuButton extends Button{
String s = null;
String h = null;
public GameMenuButton(String lowlightFile, String highlightFile){
super();
s = lowlightFile;
h=highlightFile;
try{
Pixmap lowlight = new Pixmap(Binding.getInstance().getTexture(lowlightFile));
System.out.println("pix are at" + lowlightFile);
setPixmap(lowlight);//getDefaultState().setPixmap(lowlight);
getAppearance().setAlignment(Alignment.MIDDLE);
getAppearance().apply(this);
} catch (IOException e){
e.printStackTrace();
}
}
public String getGameMenuButtonHighlightfile(){
return h;
}
public String getGameMenuButtonLowlightfile(){
return s;
}
}
protected void stateUpdate(float tpf) {
rootNode.updateGeometricState(tpf, true);
menuFengguiDisplay.display();
}
public void mouseReleased(MouseReleasedEvent mouseReleasedEvent) {
GameMenuButton sourceButton=null;
try {
sourceButton=(GameMenuButton) mouseReleasedEvent.getSource();
sourceButton.setPixmap(
new Pixmap(Binding.getInstance().getTexture(
sourceButton.getGameMenuButtonLowlightfile())));
}catch(Exception e){
// TODO: handle exception
}
}
public void mousePressed(MousePressedEvent mousePressedEvent) {
GameMenuButton sourceButton=null;
try{
sourceButton=(GameMenuButton) mousePressedEvent.getSource();
sourceButton.setPixmap(
new Pixmap(Binding.getInstance().getTexture(
sourceButton.getGameMenuButtonHighlightfile())));
}catch (Exception e) {
// TODO: handle exception
}
}
}