Slick Forums

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

All times are UTC




Post new topic Reply to topic  [ 9 posts ] 
Author Message
PostPosted: Wed May 19, 2010 2:34 pm 
Offline

Joined: Wed Mar 31, 2010 6:22 pm
Posts: 55
The Eforen theme isn't applying correctly for some reason, here is what it looks like:

Image


There is no point in posting the theme files etc because it's just the test folder in the TWL examples so it's 100% identical. Here is the class, I'm sure I'm just loading it the wrong way:

Code:
package com.adrian.spanish;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import de.matthiasmann.twl.Button;
import de.matthiasmann.twl.GUI;
import de.matthiasmann.twl.Widget;
import de.matthiasmann.twl.renderer.lwjgl.LWJGLRenderer;
import de.matthiasmann.twl.theme.ThemeManager;
import de.matthiasmann.twl.ToggleButton;


public class MatchingStateOne extends BasicGameState{

   public Button country = new Button();
   public MatchingStateOne(int stateID) {
      this.stateID = stateID;
   }

   @Override
   public int getID() {
      // TODO Auto-generated method stub
      return stateID;
   }
   public void loadStuff() throws IOException {
      country.setTheme("button");
      country.setPosition(40, 50);
   }
   public void init(GameContainer container, StateBasedGame game)
   throws SlickException {
      try {
         loadStuff();
      } catch (IOException e1) {
         // TODO Auto-generated catch block
         e1.printStackTrace();
      }
      // construct & configure root widget
      if (root == null) { //make sure the widget hasn't already been created
         root = new Widget();
         root.setTheme("");
         root.add(country);
         return; }
      // save Slick's GL state while loading the theme
      GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS);
      try {
         lwjglRenderer = new LWJGLRenderer();
         //theme = ThemeManager.createThemeManager(Thread.currentThread().getContextClassLoader()
         //              .getResource("./gui/simple.xml"), lwjglRenderer);
         File file = new File("theme/guiTheme.xml");
         theme = ThemeManager.createThemeManager(file.toURL(),lwjglRenderer);
         gui = new GUI(root, lwjglRenderer);
         gui.applyTheme(theme);
         gui.setBackground(theme.getImageNoWarning("image"));


      } catch (LWJGLException e) {
         e.printStackTrace();
      } catch(IOException e){
         e.printStackTrace();
      } finally {
         // restore Slick's GL state
         GL11.glPopAttrib();
      }

      // connect input
      twlInputAdapter = new TWLInputAdapter(gui, container.getInput());
      container.getInput().addPrimaryListener(twlInputAdapter);

   }

   public void render(GameContainer container, StateBasedGame game, Graphics g)
   throws SlickException {
      twlInputAdapter.render();

   }

   public void update(GameContainer container, StateBasedGame game, int delta)
   throws SlickException {
      twlInputAdapter.update();

   }

   public Rectangle newRect(float x, float y, float width, float height) {
      return new Rectangle(x, y, width, height);
   }

   public void addToMap() {
      for(int i = 0; i < countries.length; i++) {
         map.put(countries[i], capitals[i]);
      }
   }

   /*instance variables*/
   private Map<String, String> map = new HashMap<String, String>();
   private String[] countries = {"Spain", "Equatorial","Mexico",
         "Guatemala","Honduras","El Salvador","Nicaragua","Costa Rica",
         "Panama", "Cuba", "Dominican Republic",
         "Puerto Rico", "Venezuela", "Colombia", "Ecuador",
         "Peru", "Paraguay", "Chile", "Argentina", "Uruguay"};
   private String[] capitals = {"Madrid", "Malabo", "Mexico City",
         "Guatemala City", "Tegucigalpa",
         "San Salvador", "Managua", "San José",
         "Panama City", "Havana", "Santo Domingo",
         "San Juan", "Caracas", "Bogotá", "Quito",
         "Lima", "Asunción", "Santiago", "Buenos Aires", "Montevideo"};


   /*Instance variables*/
   private int stateID;
   /* Max of 20 buttons or add more to the arrays */
   private LWJGLRenderer lwjglRenderer;
   private ThemeManager theme;
   private GUI gui;
   private Widget root;
   private TWLInputAdapter twlInputAdapter;

}


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 19, 2010 5:20 pm 
Offline
Slick Zombie

Joined: Fri Jan 29, 2010 7:02 pm
Posts: 1171
The buttons in the Eforen theme can't display correctly if the button is too narrow. Testing in the theme editor show that they need a minWidth of 56. I added this to the theme.

_________________
TWL - The Themable Widget Library


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 19, 2010 5:48 pm 
Offline

Joined: Wed Mar 31, 2010 6:22 pm
Posts: 55
Okay Matthias, I made a new class for the widget like you said but it seems to be giving me the child already in tree error and cannot find theme: root.



Code:
package com.adrian.spanish;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import de.matthiasmann.twl.Button;
import de.matthiasmann.twl.GUI;
import de.matthiasmann.twl.Widget;
import de.matthiasmann.twl.renderer.lwjgl.LWJGLRenderer;
import de.matthiasmann.twl.theme.ThemeManager;
import de.matthiasmann.twl.ToggleButton;


public class MatchingStateOne extends BasicGameState{
   public Root root = new Root();

   public MatchingStateOne(int stateID) {
      this.stateID = stateID;
   }

   @Override
   public int getID() {
      // TODO Auto-generated method stub
      return stateID;
   }

   public void init(GameContainer container, StateBasedGame game)
   throws SlickException {
         
               
      // construct & configure root widget
         if (root == null) { //make sure the widget hasn't already been created
             root.setTheme("");
             return; }
      // save Slick's GL state while loading the theme
      GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS);
      try {
         lwjglRenderer = new LWJGLRenderer();
         File file = new File("theme/simple.xml");
         theme = ThemeManager.createThemeManager(file.toURL(),lwjglRenderer);
         gui = new GUI(root, lwjglRenderer);
         gui.applyTheme(theme);
         gui.setBackground(theme.getImageNoWarning("image"));


      } catch (LWJGLException e) {
         e.printStackTrace();
      } catch(IOException e){
         e.printStackTrace();
      } finally {
         // restore Slick's GL state
         GL11.glPopAttrib();
      }

      // connect input
      twlInputAdapter = new TWLInputAdapter(gui, container.getInput());
      container.getInput().addPrimaryListener(twlInputAdapter);

   }

   public void render(GameContainer container, StateBasedGame game, Graphics g)
   throws SlickException {
      twlInputAdapter.render();

   }

   public void update(GameContainer container, StateBasedGame game, int delta)
   throws SlickException {
      twlInputAdapter.update();

   }

   public Rectangle newRect(float x, float y, float width, float height) {
      return new Rectangle(x, y, width, height);
   }

   public void addToMap() {
      for(int i = 0; i < countries.length; i++) {
         map.put(countries[i], capitals[i]);
      }
   }

   /*instance variables*/
   private Map<String, String> map = new HashMap<String, String>();
   private String[] countries = {"Spain", "Equatorial","Mexico",
         "Guatemala","Honduras","El Salvador","Nicaragua","Costa Rica",
         "Panama", "Cuba", "Dominican Republic",
         "Puerto Rico", "Venezuela", "Colombia", "Ecuador",
         "Peru", "Paraguay", "Chile", "Argentina", "Uruguay"};
   private String[] capitals = {"Madrid", "Malabo", "Mexico City",
         "Guatemala City", "Tegucigalpa",
         "San Salvador", "Managua", "San José",
         "Panama City", "Havana", "Santo Domingo",
         "San Juan", "Caracas", "Bogotá", "Quito",
         "Lima", "Asunción", "Santiago", "Buenos Aires", "Montevideo"};


   /*Instance variables*/
   private int stateID;
   /* Max of 20 buttons or add more to the arrays */
   private LWJGLRenderer lwjglRenderer;
   private ThemeManager theme;
   private GUI gui;
   private TWLInputAdapter twlInputAdapter;

}


And here is the widget class:

Code:
package com.adrian.spanish;

import de.matthiasmann.twl.Button;
import de.matthiasmann.twl.Widget;

public class Root extends Widget{

   public Root() {
      
   }
   
   public void layout() {
      setTheme("");
      country.setTheme("countries");
      country.setPosition(40, 50);
      country.setMaxSize(100, 100);
      add(country);
   }
   
   /*Ivars*/
   public Button country = new Button();
   
}



Top
 Profile  
 
 Post subject:
PostPosted: Wed May 19, 2010 5:52 pm 
Offline
Slick Zombie

Joined: Fri Jan 29, 2010 7:02 pm
Posts: 1171
Move the
Code:
      add(country);
out of layout() and into the constructor.

Why do you call setMaxSize? This parameter is set via the theme.
I suggest you call adjustSize() as done in the GameUIDEmo example.

_________________
TWL - The Themable Widget Library


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 19, 2010 6:23 pm 
Offline

Joined: Wed Mar 31, 2010 6:22 pm
Posts: 55
I'm going to write a bug report about this also, but in the wiki it instructs you to init the "states" list re-calling the init method. This is okay in most cases but can be a huge bug when using TWL with Slick, causing it to re-init the rendering codes and objects.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 19, 2010 8:18 pm 
Offline
Game Developer
User avatar

Joined: Wed Feb 17, 2010 12:24 am
Posts: 594
Zombiedevice wrote:
Okay Matthias, I made a new class for the widget like you said but it seems to be giving me the child already in tree error and cannot find theme: root.
Code:
...


I think you got the wrong thread?

Anyways, your code looks like this:
Code:
         if (root == null) { //make sure the widget hasn't already been created
             root.setTheme("");
             return; }


That looks wrong. If it's null you shouldn't be setting a theme on it since it doesn't exist.

Secondary that is also why it's giving you widget/child is already in tree error; since if the root is not null (ie. it exists), it sets everything up again. This part only needs to be called once:
Code:
// save Slick's GL state while loading the theme
      GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS);
      try {
         lwjglRenderer = new LWJGLRenderer();
         File file = new File("theme/simple.xml");
         theme = ThemeManager.createThemeManager(file.toURL(),lwjglRenderer);
         gui = new GUI(root, lwjglRenderer);
         gui.applyTheme(theme);
         gui.setBackground(theme.getImageNoWarning("image"));


      } catch (LWJGLException e) {
         e.printStackTrace();
      } catch(IOException e){
         e.printStackTrace();
      } finally {
         // restore Slick's GL state
         GL11.glPopAttrib();
      }


A Slick gameState's init can get called more than once for various reasons. You want to make sure TWL doesn't get setup more than once though.

Something like this should work.
if (root != null) { return; } // root has already been initialized
// else, setup TWL here.

Edit: I updated the FAQ, hopefully that is clear: http://slick.javaunlimited.net/viewtopi ... 4533#14533


Last edited by dime on Wed May 19, 2010 8:29 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed May 19, 2010 8:28 pm 
Offline

Joined: Wed Mar 31, 2010 6:22 pm
Posts: 55
Thanks Dime for your reply, but me and Matthias worked it out on an irc chat hehe.


The problem was that init() was being inited more than once, all I had to do was remove the init states thing that the wiki tells you to use.


Last edited by Zombiedevice on Wed May 19, 2010 8:54 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed May 19, 2010 8:29 pm 
Offline
Game Developer
User avatar

Joined: Wed Feb 17, 2010 12:24 am
Posts: 594
Zombiedevice wrote:
Thanks Dime for your reply, but me and Matthias worked it out on an irc chat hehe.


The problem was that init() was being inited more than once, all I had to do was remove the init states thing that the wiki tells you to use.


Do you have a link?


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 19, 2010 8:55 pm 
Offline

Joined: Wed Mar 31, 2010 6:22 pm
Posts: 55
http://slick.cokeandcode.com/wiki/doku. ... lickblocks


Search for initStatesList


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 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