Slick Forums

Discuss the Slick 2D Library
It is currently Thu May 23, 2013 6:57 pm

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Wed Oct 10, 2012 6:23 pm 
Offline

Joined: Tue Sep 18, 2012 12:33 pm
Posts: 10
Hi there,

I started with TWL today and I have to say: It is great. Until now, everything works fine. Much better than AWT and Swing.

But I got one problem now: I need different menus. For example there is the main menu, when the program starts.
If you click on a certain button in this menu, it has to disappear and another menu should appear. First I tried to make this second menu as a submenu of the first, but that did not work, because if you set the parent menu to invisible, then the child menu is as well invisible (which is pretty logically).
So I tried the following: I have one "root" widget. The both described menus are children of this root menu. Then it should be pretty simple to switch between the menus.
That worked so far: The menu shows up correctly when the program starts. But I cannot interact with any elements in this menu.
Not only, that nothing happens if I click on a button, the click is also even not recognized (as you can see because the button does not change it status for hovering etc.).

These are the functions I use to create both menus:
Code:
private void initGUI(){
      initMainMenu();
      root.add(mainMenu);
      
      initStartGameMenu();
      root.add(startGameMenu);
   }
   
   private void initStartGameMenu(){
      startGameMenu = new Widget();
      startGameMenu.setTheme("");
      startGameMenu.setPosition(0, 0);
      startGameMenu.setSize(root.getWidth(), root.getHeight());
      
      //Button things go here.....
      
      startGameMenu.add(backButton);
      startGameMenu.setVisible(false);
   }
   
   private void initMainMenu(){
      mainMenu = new Widget();
      mainMenu.setTheme("");
      mainMenu.setPosition(0, 0);
      mainMenu.setSize(root.getWidth(), root.getHeight());
      
      int posIndex = 0;
      float posDistance = (float)solution.y / 4.0f - 20.0f;
      
      //Button things....
      startGameButton.addCallback(new Runnable() {
         
         @Override
         public void run() {
            startGameMenu.setVisible(true);
            mainMenu.setVisible(false);
         }
      });
      
      //More button things....
      endGameButton.addCallback(new Runnable() {
         
         @Override
         public void run() {
            System.exit(0);
         }
      });
      
      mainMenu.add(startGameButton);
      mainMenu.add(endGameButton);
      mainMenu.add(loadSettingButton);
   }


And here my GUI is created (which is just the code from the TWL example):
Code:
@Override
   public void init(GameContainer gameCon) throws SlickException {
      root = new Widget();
      root.setTheme("");
      
        // save Slick's GL state while loading the theme
        GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS);
        try {
            lwjglRenderer = new LWJGLRenderer();
            theme = ThemeManager.createThemeManager(
                GameOfLife.class.getResource("simple.xml"), lwjglRenderer);
            gui = new GUI(root, lwjglRenderer);
            gui.applyTheme(theme);
        } catch (LWJGLException e) {
            e.printStackTrace();
        } catch(IOException e){
            e.printStackTrace();
        } finally {
            // restore Slick's GL state
            GL11.glPopAttrib();
        }
       
        initGUI();
       
        // connect input
        twlInputAdapter = new TWLInputAdapter(gui, gameCon.getInput());
        gameCon.getInput().addPrimaryListener(twlInputAdapter);
       
      setGameTimer(new GameTimer());
      gameCon.setShowFPS(false);
   }


The menus are in the code like this:
Code:
private Widget root;
    private Widget mainMenu;
    private Widget startGameMenu;


Can anyone please help me?
I have no clue what actually the problem is. And sadly I cannot find any information about somethign like this.

Greetings,
M0rgenstern


Top
 Profile  
 
PostPosted: Wed Oct 10, 2012 6:43 pm 
Offline
Slick Zombie

Joined: Fri Jan 29, 2010 7:02 pm
Posts: 1172
The javadoc says that you should call setSize() and setPosition() only from the direct parent's layout() method.
Also you use a plain Widget as a container - but Widget's layout() method is empty - so your buttons and other widgets don't get a correct size & position.

The reason for all this is that theme data is only available once all elements have been added the to GUI tree - and layout() will be invoked after this has happened.

By default Widget does not clip (see setClip) - so if widget or one of it's children renders outside it is still visible - but the mouse input doesn't work.

_________________
TWL - The Themable Widget Library


Top
 Profile  
 
PostPosted: Wed Oct 10, 2012 7:21 pm 
Offline

Joined: Tue Sep 18, 2012 12:33 pm
Posts: 10
Thank you for your fast reply.

So as I understand you, I have to create a class for every (sub)menu I want to use in my program?
I also see the problem: If I activate clipping for each (sub)menu the buttons are no longer visible.
Can you please tell me how I can manage the problem? Is it enough to make for each (sub)menu an ownclass with its own layout() method?

Greetings,
M0rgenstern


Top
 Profile  
 
PostPosted: Wed Oct 10, 2012 7:55 pm 
Offline
Slick Zombie

Joined: Fri Jan 29, 2010 7:02 pm
Posts: 1172
Yes, either make a class per sub menu and override it's layout() method, or use one of the available container Widgets:
DialogLayout,
BoxLayout,
BorderLayout,
ColumnLayout

The examples use mostly the DialogLayout container which is the most powerful, it can create nearly any layout.

_________________
TWL - The Themable Widget Library


Top
 Profile  
 
PostPosted: Thu Oct 11, 2012 6:46 pm 
Offline

Joined: Tue Sep 18, 2012 12:33 pm
Posts: 10
Thank you very much.
Now everything works fine.
I only have to figure out how the positioning in Layouts do work. But I think that can be found in some examples or by trial and error :-)

Greetings,
M0rgenstern


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group