//Title:      camera_mover.java
//Version:    
//Copyright:  Copyright (c) 2000
//Author:     SyGem Software

import java.awt.*;
import java.applet.*;
import sygem.jazz3d2_102.*;
public class camera_mover extends Applet implements Runnable {
    
    Thread m_ce;
    world c_world;
    
    int light1;
    int pid,pid2,pid3,pid4;
    
    //Construct the applet
    public camera_mover() {
    }
    
    //Initialize the applet
    public void init() {
        
        resize(320,240);
        c_world = new world(this);
        
        setLayout(new BorderLayout());
        add("Center", c_world);
        
        renderwf wireframe = new renderwf();
        wireframe.setLineColour(255,255,128);
        wireframe.setHiddenLineRemoval(true);
        renderfs flat_shader = new renderfs();
        rendergs gourard_shader = new rendergs();
        
        loader load_container = new loader(c_world);
        
        load_container.assignLoader("txt", new loaderasc());
        model3d duck = new model3d(0,0,8);
        duck.loadModel("models/duck.txt", load_container);
        
        torus3d torus = new torus3d(0.4,0.2,0.2,15,15,0,0,-8);
        torus.setColour(255,0,128);
        
        cube3d cube = new cube3d(-8,0,0);
        
        sphere3d sphere = new sphere3d(10,10,8,0,0);
        sphere.setColour(0,255,128);
        
        pid =  c_world.addObject(duck,flat_shader);
        pid2 = c_world.addObject(torus,gourard_shader);
        pid3 = c_world.addObject(cube,wireframe);
        pid4 = c_world.addObject(sphere,flat_shader);
        
        light temp_light = new light(-1,0,1);
        light temp_light2 = new light(1,-1,0);
        light1 = c_world.addLight(temp_light);
        light1 = c_world.addLight(temp_light2);
        
    }
    
    //Start the applet
    public void start() {
        if (m_ce == null) {
            m_ce = new Thread(this);
            m_ce.start();
        }
    }
    
    //Stop the applet
    public void stop() {
        if (m_ce != null) {
            m_ce.stop();
            m_ce = null;
        }
    }
    
    //Destroy the applet
    public void destroy() {
    }
    
    public void run() {
        
        c_world.prep();
        
        while (true) {
            
            c_world.rotateObjectLocal(pid,0,3,0);
            c_world.rotateObjectLocal(pid2,1,3,2);
            c_world.rotateObjectLocal(pid3,0,2,5);
            c_world.rotateObjectLocal(pid4,2,1,4);
            c_world.redraw();
        }
    }
    
    public boolean handleEvent(Event e) {
        switch (e.id) {
            case Event.KEY_ACTION:
            case Event.KEY_PRESS:
            switch (e.key) {
                case Event.HOME:
                return true;
                case Event.UP:
                  c_world.rotateCameraLocal(1,0,0);
                  return true;
                case Event.DOWN:
                c_world.rotateCameraLocal(-1,0,0);
                return true;
                case Event.RIGHT:
                if (e.modifiers == Event.SHIFT_MASK) {
                    c_world.rotateCameraLocal(0,-2,0);
                } else {
                    if (e.modifiers == Event.CTRL_MASK) {
                        c_world.moveCameraLocal(0.2,0,0);
                    } else {
                        c_world.rotateCameraLocal(0,-1,0);
                    }
                }
                return true;
                case Event.LEFT:
                if (e.modifiers == Event.SHIFT_MASK) {
                    c_world.rotateCameraLocal(0,2,0);
                } else {
                    if (e.modifiers == Event.CTRL_MASK) {
                        c_world.moveCameraLocal(-0.2,0,0);
                    } else {
                        c_world.rotateCameraLocal(0,1,0);
                    }
                }
                return true;
                case 'x':
                c_world.rotateCameraLocal(0,0,-1);
                return true;
                case 'c':
                c_world.rotateCameraLocal(0,0,1);
                return true;
                case 'a':
                if (e.modifiers == Event.SHIFT_MASK) {
                    c_world.moveCameraLocal(0,0,0.3);
                } else {
                    c_world.moveCameraLocal(0,0,0.1);
                }
                    return true;
                case 'z':
                if (e.modifiers == Event.SHIFT_MASK) {
                    c_world.moveCameraLocal(0,0,-0.3);
                } else {
                    c_world.moveCameraLocal(0,0,-0.1);
                }
                return true;
                default:
                return false;
            }
            
            default:
            return false;
        }
    }
}