import com.cult3d.*;
import com.cult3d.world.*;

// This sample demonstrates how to change textures on Cult3D objects using Cult3D Java.
//
// You must have a texture from 3DStudio Max (or other modelling application),
// which you can change in Java later on.
// The Java startup class must implement Cult3DScript
// To connect this class in the Designer, choose the Window->Java Actions and in the new box
// add your classes there and in the Startupclass dialog enter the name of the javaclass which
// implements Cult3DScript for instance JavaTextureChange.class.
// (Note. If you have a designer prior to Cult3D 5.2 you should also write the .class extension
// the first time you add the startup class).
//
// To change image textures you must load the images you want to change to as resources
// in the designer. choose Window->resources and add all your images (resources)
//
// To change back to the original texture you have in you modelling application
// you must either load it as an resource, or use the getTexture method
// in Cult3D's Java API.
// In this code we load it as an resource
//
//
// NOTE: The images you change from Java must be the same size (width and height) as
// the base texture you exported from the Designer.
//
// Please be ware that when you apply textures in your modelling application, those
// textures must have width and height of power of 2, in other words the images
// must be in width and height of 1, 2, 4, 8, 16, 32, 64, 128, 256, 1024 or 2048.
// You must not have the same width and height, you can for instance have an image
// with the width 64 and the height 2048.
//

/**
 * @author Jonatan Andersson 2000-01-01,
 */
public class JavaTextureChange implements Cult3DScript, Runnable
{
    // Name of texture from 3D Studio Max (When exporting with
    // Cult3D Exporter you'll see the texture name, add your texturename here
    private static final String TEXTURE_NAME = "date";

    // Name of a resources in the Designer
    // Add image names in this array for the ability to loop from top to bottom.
    private static final String[] imgNames =
					{
						"designer_texture1.jpg",
						"designer_texture2.jpg",
						"designer_texture3.jpg"
					};

    ///////////////////////////////////////////////////////////////////////
    private Texture        texture;
    private TextureImage[] texImg = new TextureImage[imgNames.length];

    private boolean loaded = false;

    private int textureCounter = 0;

    // Connecting Java to Cult3D, so you can trigger the
    // Manual Event called DoSinus by Java
    private CultEvent doSine = new CultEvent("DoSine");


    public JavaTextureChange()
    {
		// Make a thread which loads the textures, if done by this way you avoid to hang the Cult3D object
		// when loading textures and other timeconsuming resources.
		Thread thread = new Thread(this);
		thread.start();
    }

    public void run()
    {
		System.out.println("loading images");
		// loading images from the designer
		for (int i=0; i<imgNames.length; i++)
		{
			texImg[i] = (TextureImage)Cult.getImage(imgNames[i]);
		}
		System.out.println("Done loading images");

		// constructs a Cult3D Java texture which you can change in many ways
		texture = new Texture(TEXTURE_NAME);

		loaded = true;
		System.out.println("loaded");
    }

    // A method reachable from Cult3D Designer.
    // Methods which should be reachable from the Cult3D Designer  must be public and return void and has a
    // String as its only parameter
    // Only call methods from the Startup class, if it is methods in other classes you want to call, make an
    // method in the startup class which instaniate and calls the real method
    public void changeTexture(String s)
    {
		// if not all texture are loaded yet, do nothing
		if (! loaded) {
			return;
		}

		System.out.println("changing texture to " + textureCounter);
		// A loop for changing textures
        if (textureCounter >= imgNames.length)
        {
			textureCounter = 0;
		}

		texture.setTexture(texImg[textureCounter]);
		textureCounter++;

		// Trigger a Designer event (Manual event named DoSine)
		doSine.trigger();
    }

    // This method resets to the first texture as you have loaded as an resource
    public void resetTexture(String s)
    {
		if (!loaded)
		{
			return;
		}

		System.out.println("resetting texture");
		textureCounter = 0;

		texture.setTexture(texImg[textureCounter]);
    }


    // This method is called when the Cult3DViewer are closed, for instance when you exit you webbrowser
    // or changing site.
    public void cult3dDestroy()
    {
    }
}
