Okay I've continued at this for hours, I can't get it to find the button! My project is due tomorrow and I only have a few hours left to get it finished and I haven't even scratched the surface of the UI part. Any ways, here is the code:
Code:
<textures file="countries.png" format="RGBA">
<texture name="-countries.background.highlight" x="0" y="0" width="68" height="36"/>
</textures>
<textures file="countries.png" format="RGBA">
<select name="countries.background">
<alias ref="-countries.background.highlight" if="armed ^ selected"/>
<texture x="0" y="0" width="68" height="36"/>
</select>
</textures>
For the theme file.
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.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 ToggleButton country = new ToggleButton();
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("countries.background");
}
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
root = new Widget();
root.setTheme("");
root.add(country);
// 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/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 Widget root = new Widget();
private TWLInputAdapter twlInputAdapter;
}
I'm extremely new to TWL and by what I've read, the select tag (more specific the name part of it) is what the method searches for (countries.background) and for some reason it gives me this:
Code:
Could not find theme: countries.background
java.lang.IllegalArgumentException: child widget already in tree
at de.matthiasmann.twl.Widget.insertChild(Widget.java:1088)
at de.matthiasmann.twl.Widget.add(Widget.java:1066)
at com.adrian.spanish.MatchingStateOne.init(MatchingStateOne.java:65)
at org.newdawn.slick.state.StateBasedGame.init(StateBasedGame.java:164)
at org.newdawn.slick.AppGameContainer.setup(AppGameContainer.java:390)
at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:314)
at com.adrian.spanish.SpanishGameMain.main(SpanishGameMain.java:42)
Also, there is nothing wrong with the loading of the xml file because the TWL background loads fine referencing to a select tag.