package com.nucleus.scene;

import com.nucleus.common.StringUtils;
import com.nucleus.properties.PropertyManager;
import com.nucleus.properties.PropertyManager.PropertyHandler;
import com.nucleus.vecmath.Transform;

/**
 * This handles the update of the scene View.
 * To register this class to handle property key, call the
 * This class will be registered as handling view properties.
 * 
 * @author Richard Sahlin
 *
 */
public class ViewController implements PropertyHandler {

    /**
     * The key to register in the property handler for this class
     * TODO Do not store as magic string, find some other way.
     */
    public final static String HANDLER_KEY = "view";

    public enum Actions {
        MOVE(),
        SCALE(),
        ROTATE();
    }

    private Transform view;

    /**
     * Creates a new viewcontroller with the specified view.
     * 
     * @param view
     * @throws IllegalArgumentException If view is null
     */
    public ViewController(Transform view) {
        if (view == null) {
            throw new IllegalArgumentException("View transform is null");
        }
        this.view = view;
    }

    /**
     * Registers this class as a propertyhandler for the key, if key is null the {@link #HANDLER_KEY} is used.
     * 
     * @param key The key to register this controller for, or null to use default.
     */
    public void registerPropertyHandler(String key) {
        if (key == null) {
            key = HANDLER_KEY;
        }
        PropertyManager.getInstance().registerKey(key, this);
    }

    /**
     * Returns the value as a number of arrays, the first element contains the action, with any following
     * element containing the data
     * 
     * @param value
     * @return
     */
    private String[] getParts(String value) {
        int index = value.indexOf(":");
        if (index >= 0) {
            return new String[] { value.substring(0, index), value.substring(index + 1) };
        } else {
            return new String[] {value};
        }
    }
    
    @Override
    public boolean handleProperty(String key, String value) {
        String[] parts = getParts(value);
        Actions action = Actions.valueOf(parts[0]);
        handleAction(action, parts[1]);
        return false;
    }

    private void handleAction(Actions action, String data) {
        float[] values = StringUtils.getFloatArray(data);
        switch (action) {
        case MOVE:
            view.setTranslate(values);
        }
    }

    @Override
    public boolean handleObjectProperty(Object obj, String key, String value) {
        // TODO Auto-generated method stub
        return false;
    }

}

@@ -749,16 +749,34 @@
 ROTATE()
+,%0A        MOVETO()
 ;%0A    %7D%0A
@@ -2163,16 +2163,34 @@
 value);%0A
+        try %7B%0A    
         
@@ -2241,16 +2241,20 @@
         
+    
 handleAc
@@ -2277,32 +2277,36 @@
 ts%5B1%5D);%0A        
+ 
+   
 return false;%0A  
@@ -2299,24 +2299,173 @@
 turn false;%0A
+        %7D catch (IllegalArgumentException e) %7B%0A            System.out.println(%22Could not parse action: %22 + parts%5B0%5D);%0A        %7D%0A        return true;%0A
     %7D%0A%0A    p
@@ -2644,27 +2644,211 @@
 iew.
-setTranslate(values
+addTranslation(values);%0A            break;%0A        case MOVETO:%0A            view.setTranslate(values);%0A            break;%0A        default:%0A            throw new IllegalArgumentException(%22Not implemented%22
 );%0A 

Add moveto action to viewcontroller