package net.mkonrad.synes.ctrl;

import javax.media.j3d.Node;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;

import net.mkonrad.synes.gui.elements.AbstractGraphLine;
import net.mkonrad.synes.util.DbgTools;

abstract public class AbstractLinePickingCtrl extends AbstractPickableObj
{
  protected Vector3f _selectionClickPoint = null;
  protected Vector3f _selectionGraphPoint = null;
  protected AbstractGraphLine _selectedGraphLine = null;
  
  public AbstractLinePickingCtrl(CentralPickingCtrl pickCtrl)
  {
    super(pickCtrl);
  }
  
  abstract public void removeGraph();
  
  abstract public boolean clicked(Node pickedObj, Point3f where, int mouseState);
  abstract public boolean dragged(Point3f where, int mouseState);
  abstract public boolean released(Point3f where, int mouseState);
  
  public boolean moved(Node pickedObj, Point3f where, int mouseState)
  {
    return true;
  }
  
  protected void moveGraphLine(Vector3f dest)
  {
    if (_selectedGraphLine == null || _selectionClickPoint == null
        || _selectionGraphPoint == null)
      return;
    
    dest.sub(_selectionClickPoint);
    Vector3f moveTo = new Vector3f();
    moveTo.add(_selectionGraphPoint, dest);
    
    _selectedGraphLine.setPos(moveTo);
  }
  
  protected void clearSelection()
  {
    _selectedGraphLine = null;
    _selectionClickPoint = null;
    _selectionGraphPoint = null;
    
    _pickCtrl.setState(CentralPickingCtrl.STATE_READY);
  }
  
  protected void makeSelection(AbstractGraphLine pickedObj, Point3f where)
  {
    _selectedGraphLine = pickedObj;
    _selectionGraphPoint = _selectedGraphLine.getPos();
    
    _selectionClickPoint = new Vector3f(where);
    
    _pickCtrl.setState(CentralPickingCtrl.STATE_GRAPH_MOVE);
  }
}
