Slick Forums

Discuss the Slick 2D Library
It is currently Mon May 20, 2013 3:13 am

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Popup window issues
PostPosted: Tue Jul 31, 2012 2:23 pm 
Offline

Joined: Tue Jul 31, 2012 1:17 pm
Posts: 3
Hey, so as the title suggests I’m having a little trouble with the popup window. What I want out of the popup window is for it to appear after I click my start button. The popup window will ask the user if they want to carry on with their old game or not, and the user will click yes, no or cancel.

My project: I’m using the http://wiki.l33tlabs.org/bin/view/TWL/Integrating+TWL+into+StateBasedGame guide to use TWL with my slick2d game. This includes using the createRootPane() method to setup the GUI as I couldn’t get getRootPane() to work within my init() method. That’s an issue for another day :P My project consists of a class which extends TWLStateBasedGame and includes my main method, and initiates several other classes extending BasicTWLGameState. The first class it initiates and enters is my MainMenuState which is the class I’m currently trying to edit for the popup window.
I’ve been messing around with different objects and solutions other people have posted, but they haven’t given me my desired effect. So far I just get text and buttons, either using Buttons & Labels + DialogLayout + PopupWindow or DesktopArea + PopupWindow + SimpleDialog.

Also having errors with the themes, as I constantly get 1 or these 2 System errors, depending on what I put in my code: Selected fallback theme for missing theme. Or Selected fallback theme for missing theme "state1". I’m using the simple.xml file.

I can show you what I’ve got so far.. but it just seems a bit of a mess at the moment with trying so many different ways to get something rendered. Tried to cut out code I'm not currently using.

Code:
    @Override
    protected RootPane createRootPane() {
       rp = super.createRootPane();
        rp.setTheme("mainMenu");
       
        deskArea = new DesktopArea();
        resFrame = new ResizableFrame();
       
        deskArea.add(resFrame);
        pW = new PopupWindow(deskArea);
        pW.openPopup();
        pW.setTheme("popupwindow"); //popupwindow
      
        sD = new SimpleDialog();
        //sD.setTitle("");
        sD.setMessage("Would you like to carry on with your old game?");
        sD.setTheme("");
        sD.setOkCallback(new Runnable() {
            public void run() {
               try {
               sL.loadGame();
            } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
            }
              loadGame = true;
              counter = 5000;
               
            }
        });
        sD.setCancelCallback(new Runnable() {
            public void run() {
               pW.closePopup();
               pW.setCloseOnEscape(true);
               //pW.setRequestCloseCallback(requestCloseCallback);
            }
        });

        rp.add(sD.showDialog(pW));
       
        return rp;
    }


Code:
    @Override
    protected void layoutRootPane() {
        pW.adjustSize();
        //pW.centerPopup();
        pW.setPosition(50, 50);
    }


With this code I get 3 strings, the message, "OK" and "Cancel" which don't act as buttons which I'm guessing they're suppose to?

Any help would be greatly appreciated!


Top
 Profile  
 
 Post subject: Re: Popup window issues
PostPosted: Thu Aug 02, 2012 7:12 am 
Offline
Slick Zombie

Joined: Fri Jan 29, 2010 7:02 pm
Posts: 1171
This code should crash here:
Code:
rp.add(sD.showDialog(pW));
Never add a PopupWIndow to another widget.

The point of SimpleDialog is that it will already construct a PopupWindow and show it so that you don't need to do anything. The return value is only provided so that you can close the dialog from code when it is no longer needed (eg a timer, network message etc).

Also passing a PopupWindow as a parent is possible but probably no the best approach.
I suggest to remove the DesktopArea and the PopupWindow and just use SimpleDialog and the root pane.

As for the missing theme message - if you create your own widget or specify your own theme name (setTheme) you will need to add them to the theme XML.
The fallback theme message means that it selected the fallback theme
Code:
<theme name="*" ref="-defaults"/>
so that the widget gets a theme at all (basically an empty one), but it allows the children go get their theme.

_________________
TWL - The Themable Widget Library


Top
 Profile  
 
 Post subject: Re: Popup window issues
PostPosted: Thu Aug 02, 2012 2:51 pm 
Offline

Joined: Tue Jul 31, 2012 1:17 pm
Posts: 3
Thanks for the reply. I messed around with the themes and seem to have found that viewtopic.php?f=18&t=4859 theme works just fine. Maybe I wasn’t using the correct theme names in the other XML files, but at least this one works.

Right so as I was writing my reply with my new bugs, I seem to have fixed them.

Your advice works just fine, I got a SimpleDialog window to appear. I think I’m going to try get the PopupWindow to work (update its working – correctly? We shall see). This is because it seems more customisable, and I’ll need to use it again in another state asking the user for a name.

When you say you can’t add the PopupWindow as a child to any widget, you’re saying the only way to view the PopupWindow is through the method popupWindow.openPopup();. Not by rootpane.add(popupWindow);, because at the moment the only way I can see anything to do with the PopupWindow is by adding it to the rootpane. Here’s my code for the PopupWindow:

Code:
    @Override
    protected RootPane createRootPane() {
       rp = super.createRootPane();
       createPopupWindow();
        return rp;
    }
   
    @Override
    protected void layoutRootPane() {
       if(pW.isOpen() == true){
            pW.adjustSize();
            pW.centerPopup();
            //pW.setPosition(50, 50);
       }
    }

Code:
public void createPopupWindow(){
      
        loadPanel = new DialogLayout();
       
        btnOK = new Button("Yes");
        btnOK.setTheme("button");
        btnOK.addCallback(new Runnable() {
            public void run() {
               try {
               sL.loadGame();
            } catch (IOException e) {
               e.printStackTrace();
            }
              loadGame = true;
              counter = 5000;
            }
        });
       
        btnNo = new Button("No");
        btnNo.setTheme("button");
        btnNo.addCallback(new Runnable() {
            public void run() {
               //Clear everything in the saved file?
               counter = 1000;
               loadGame = true;
            }
        });
       
        btnCancel = new Button("Cancel");
        btnCancel.setTheme("button");
        btnCancel.addCallback(new Runnable() {
            public void run() {
               pW.closePopup();
               pW.setVisible(false);
               pW.destroy();
            }
        });
       
        loadQuestion = new Label();
        loadQuestion.setText("Would you like to carry on with your old game?");
       
        DialogLayout.Group popupLabelH = loadPanel.createSequentialGroup(loadQuestion);
        DialogLayout.Group popupBoxH = loadPanel.createSequentialGroup()
              .addGap()
              .addWidget(btnOK)
              .addGap(20)
              .addWidget(btnNo)
              .addGap(20)
              .addWidget(btnCancel)
              .addGap();
       
        DialogLayout.Group popupLabelV = loadPanel.createParallelGroup(loadQuestion);
        DialogLayout.Group popupBoxV = loadPanel.createParallelGroup(btnOK, btnNo, btnCancel);
       
        loadPanel.setHorizontalGroup(loadPanel.createParallelGroup(popupLabelH, popupBoxH));
        loadPanel.setVerticalGroup(loadPanel.createSequentialGroup(popupLabelV, popupBoxV));
       
        pW = new PopupWindow(loadPanel);
        pW.setTheme("resizableframe"); //panel, resizableframe?
        pW.add(loadPanel);
        pW.setVisible(true);
        pW.openPopup();
        rp.add(pW);
   }


The issue I have at the moment is that the PopupWindow is already initialised and on display. I can’t seem to create the popup when I click my start button. (At the moment my start button is a slick2d image but I’ll be changing that later to a TWL button with the same image I have now.) I've tried moving snippets of code around to create the PopupWindow but only display it when clicking the StartButton, all similar to this but I get the errors:

Thu Aug 02 15:22:17 BST 2012 ERROR:Game.update() failure - check the game code.
org.newdawn.slick.SlickException: Game.update() failure - check the game code.

I have a feeling it's to do with the situation I described above, with openPopup() and add(popupWindow), cause at the moment if I comment out openPopup(), it's still visible. But without .add() I don't see anything.

Any ideas? Thanks.


Top
 Profile  
 
 Post subject: Re: Popup window issues
PostPosted: Thu Aug 02, 2012 9:05 pm 
Offline
Slick Zombie

Joined: Fri Jan 29, 2010 7:02 pm
Posts: 1171
As I said - the PopupWindow can not be used as a child widget - there is a lot of code which assumes how a PopupWindow is displayed. I'll add code which errors out when trying to add to prevent random crashes later on.

When you open a popup with openPopup() you need to set it's size - at best look into SimpleDialog to see how it is done.
And it is generally not a good idea to call setVisible() on a PopupWindow - it will work but not as you expect - better to use openPopup() and closePopup().

Also you should not call destroy() yourself - it is called automatically when a widget is removed from the GUI tree.

_________________
TWL - The Themable Widget Library


Top
 Profile  
 
 Post subject: Re: Popup window issues
PostPosted: Fri Aug 03, 2012 11:32 am 
Offline

Joined: Tue Jul 31, 2012 1:17 pm
Posts: 3
Ok think I got it now, everything works just fine with the just the openPopup() and closePopup(), and it only appears when I click my start button. Thanks for your help! Sure some other people might find this handy later.

Code:
    @Override
    protected RootPane createRootPane() {
       rp = super.createRootPane();
       createPopupWindow();
        return rp;
    }


Code:
   public void createPopupWindow(){
      
        loadPanel = new DialogLayout();
       
        btnOK = new Button("Yes");
        btnOK.setTheme("button");
        btnOK.addCallback(new Runnable() {
            public void run() {
               try {
               sL.loadGame();
            } catch (IOException e) {
               e.printStackTrace();
            }
              loadGame = true;
              counter = 5000;
              pW.closePopup();
            }
        });
       
        btnNo = new Button("No");
        btnNo.setTheme("button");
        btnNo.addCallback(new Runnable() {
            public void run() {
               //Clear everything in the saved file?
               counter = 1000;
               loadGame = true;
               pW.closePopup();
            }
        });
       
        btnCancel = new Button("Cancel");
        btnCancel.setTheme("button");
        btnCancel.addCallback(new Runnable() {
            public void run() {
               pW.closePopup();
            }
        });
       
        loadQuestion = new Label();
        loadQuestion.setText("Would you like to carry on with your old game?");
       
        DialogLayout.Group popupLabelH = loadPanel.createSequentialGroup(loadQuestion);
        DialogLayout.Group popupBoxH = loadPanel.createSequentialGroup()
              .addGap()
              .addWidget(btnOK)
              .addGap(20)
              .addWidget(btnNo)
              .addGap(20)
              .addWidget(btnCancel)
              .addGap();
       
        DialogLayout.Group popupLabelV = loadPanel.createParallelGroup(loadQuestion);
        DialogLayout.Group popupBoxV = loadPanel.createParallelGroup(btnOK, btnNo, btnCancel);
       
        loadPanel.setHorizontalGroup(loadPanel.createParallelGroup(popupLabelH, popupBoxH));
        loadPanel.setVerticalGroup(loadPanel.createSequentialGroup(popupLabelV, popupBoxV));
       
        pW = new PopupWindow(rp);
        pW.setTheme("resizableframe");
        pW.add(loadPanel);
       
   }


and I call pW.openPopupCentered(); when I click my start button.


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 1 guest


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