import vtk.vtkProp;
-public class TranslateAction extends vtkSwtAction{
-
+public class TranslateAction extends vtkSwtAction {
+
public static final int X = 0;
public static final int Y = 1;
public static final int Z = 2;
public static final int P = 6;
private VTKNodeMap<?, ? extends INode> nodeMap;
- //private TranslateGizmo gizmo = new TranslateGizmo();
+ // private TranslateGizmo gizmo = new TranslateGizmo();
private TranslateAxisGizmo gizmo = new TranslateAxisGizmo();
protected IG3DNode node;
-
-
-
+
private Cursor activeCursor;// = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
private Cursor dragCursor;// = Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR);
-
+
protected ToolComposite toolComposite;
protected Combo axisCombo;
-
+
public void setNode(IG3DNode node) {
this.node = node;
- if ((node instanceof IStructuralNode) && ((IStructuralNode)node).isPartOfInstantiatedModel() && !((IStructuralNode)node).isInstantiatedModelRoot()) {
+ if ((node instanceof IStructuralNode) && ((IStructuralNode) node).isPartOfInstantiatedModel()
+ && !((IStructuralNode) node).isInstantiatedModelRoot()) {
setEnabled(false);
} else {
setEnabled(true);
}
}
-
+
public IG3DNode getNode() {
return node;
}
-
- public TranslateAction(InteractiveVtkComposite panel, VTKNodeMap<?, ? extends INode> nodeMap, ToolComposite toolComposite) {
+
+ public TranslateAction(InteractiveVtkComposite panel, VTKNodeMap<?, ? extends INode> nodeMap,
+ ToolComposite toolComposite) {
super(panel);
setImageDescriptor(Activator.imageDescriptorFromPlugin("com.famfamfam.silk", "icons/arrow_out.png"));
setText("Translate");
this.nodeMap = nodeMap;
-
+
activeCursor = Display.getCurrent().getSystemCursor(SWT.CURSOR_HAND);
dragCursor = Display.getCurrent().getSystemCursor(SWT.CURSOR_SIZEALL);
this.toolComposite = toolComposite;
}
-
+
protected void createTools(ToolComposite toolComposite) {
- Label label = new Label(toolComposite, SWT.READ_ONLY);
- label.setText("Translate direction:");
- axisCombo = new Combo(toolComposite, SWT.READ_ONLY);
- axisCombo.add("X");
- axisCombo.add("Y");
- axisCombo.add("Z");
- axisCombo.add("XY");
- axisCombo.add("XZ");
- axisCombo.add("YZ");
- axisCombo.add("Camera");
- axisCombo.addSelectionListener(new SelectionAdapter() {
- @Override
- public void widgetSelected(SelectionEvent e) {
- Combo c = (Combo)e.getSource();
- index = c.getSelectionIndex();
- updateLock();
- panel.getComponent().setFocus();
- }
- });
- axisCombo.select(index);
- Button close = new Button(toolComposite, SWT.PUSH);
- close.setText("Close");
- close.addSelectionListener(new SelectionAdapter() {
- public void widgetSelected(SelectionEvent e) {
- panel.useDefaultAction();
- };
- });
- toolComposite.relayout();
+ createMessage(toolComposite);
+ createAxisSelection(toolComposite);
+ createCloseButton(toolComposite);
+
+ toolComposite.relayout();
}
-
+
+ protected void createMessage(ToolComposite toolComposite) {
+ new Label(toolComposite, SWT.READ_ONLY).setText("Press ESC to close translation. ");
+ }
+
+ protected void createAxisSelection(ToolComposite toolComposite) {
+ Label label = new Label(toolComposite, SWT.READ_ONLY);
+ label.setText("Translate direction:");
+ axisCombo = new Combo(toolComposite, SWT.READ_ONLY);
+ axisCombo.add("X");
+ axisCombo.add("Y");
+ axisCombo.add("Z");
+ axisCombo.add("XY");
+ axisCombo.add("XZ");
+ axisCombo.add("YZ");
+ axisCombo.add("Camera");
+ axisCombo.addSelectionListener(new SelectionAdapter() {
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ Combo c = (Combo) e.getSource();
+ index = c.getSelectionIndex();
+ updateLock();
+ panel.getComponent().setFocus();
+ }
+ });
+ axisCombo.select(index);
+ }
+
+ protected void createCloseButton(ToolComposite toolComposite) {
+ Button close = new Button(toolComposite, SWT.PUSH);
+ close.setText("Close");
+ close.addSelectionListener(new SelectionAdapter() {
+ public void widgetSelected(SelectionEvent e) {
+ panel.useDefaultAction();
+ };
+ });
+ }
+
public void attach() {
if (node == null)
return;
if (toolComposite != null) {
- createTools(toolComposite);
+ createTools(toolComposite);
}
setDBUndo(false);
super.attach();
}
});
}
-
+
public void deattach() {
- if (toolComposite != null) {
- toolComposite.clear();
- axisCombo = null;
- }
- setDBUndo(true);
+ if (toolComposite != null) {
+ toolComposite.clear();
+ axisCombo = null;
+ }
+ setDBUndo(true);
node = null;
nodeMap.commit("Translate");
deattachUI();
super.deattach();
panel.refresh();
}
-
+
private void attachUI() {
panel.getComponent().setCursor(activeCursor);
gizmo.attach(panel);
}
-
+
private void deattachUI() {
panel.getComponent().setCursor(Display.getCurrent().getSystemCursor(SWT.CURSOR_ARROW));
gizmo.deattach();
}
-
+
@Override
public boolean keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
if (e.getKeyCode() == KeyEvent.VK_G) {
worldCoord = !worldCoord;
}
-
+
updateLock();
update();
- //panel.repaint();
+ // panel.repaint();
return true;
}
-
+
private void updateLock() {
- gizmo.setType(index);
- if (axisCombo != null)
- axisCombo.select(index);
- panel.refresh();
+ gizmo.setType(index);
+ if (axisCombo != null)
+ axisCombo.select(index);
+ panel.refresh();
}
-
@Override
public boolean mouseClicked(MouseEvent e) {
if (e.getClickCount() > 1) {
} else {
panel.useDefaultAction();
}
- //if(!gizmo.isPartOf(actor))
- // panel.useDefaultAction();
+ // if(!gizmo.isPartOf(actor))
+ // panel.useDefaultAction();
return true;
}
return false;
}
-
+
private boolean isOverNode(MouseEvent e) {
vtkProp picked[] = panel.pick(e.getX(), e.getY());
- if (picked !=null) {
+ if (picked != null) {
for (int i = 0; i < picked.length; i++) {
if (node.equals(nodeMap.getNode(picked[i])))
return true;
}
return false;
}
-
-
+
int index = P;
protected boolean valid = false;
private boolean worldCoord = true;
private AxisAngle4d aa = null;
private Quat4d q = null;
-
-
+
public void setWorldCoord(boolean b) {
if (worldCoord == b)
return;
worldCoord = b;
update();
-
+
}
-
-
+
protected void update() {
if (node == null)
return;
q = null;
} else {
aa = new AxisAngle4d();
- aa.set(((IG3DNode)node.getParent()).getWorldOrientation());
+ aa.set(((IG3DNode) node.getParent()).getWorldOrientation());
gizmo.setRotation(aa);
q = new Quat4d();
MathTools.getQuat(aa, q);
}
-
+
Vector3d nodePos = node.getWorldPosition();
- //System.out.println(nodePos);
+ // System.out.println(nodePos);
gizmo.setPosition(nodePos);
-
Point3d camPos = new Point3d(panel.getRenderer().GetActiveCamera().GetPosition());
Vector3d p = new Vector3d(nodePos);
p.sub(camPos);
-
+
if (q != null) {
Quat4d qi = new Quat4d(q);
qi.inverse();
double distance = p.length();
p.negate();
double fov = panel.getRenderer().GetActiveCamera().GetViewAngle();
- float s = (float) (Math.sin(fov) * distance * 0.1);
+ float s = (float) (Math.sin(fov) * distance * 0.1);
Vector3d scale = new Vector3d(1., 1., 1.);
-
+
// if (p.x > 0.f)
// scale.x = -1.;
// if (p.y > 0.f)
// scale.z = -1.;
scale.scale(s);
gizmo.setScale(scale);
-
+
} else {
Vector3d scale = new Vector3d(1.f, 1.f, 1.f);
double s = panel.getRenderer().GetActiveCamera().GetParallelScale() / 5.;
scale.scale(s);
gizmo.setScale(scale);
}
-
- //panel.Render();
+
+ // panel.Render();
panel.refresh();
}
-
+
protected Vector3d prevTranslate = null;
-
+
@Override
public boolean mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
getDefaultAction().mousePressed(e);
}
return true;
- //index = gizmo.getTranslateAxis(actor);
- //if (index == -1) {
- // valid = false;
- // panel.getDefaultAction().mousePressed(e);
- // return;
- //}
- //valid = true;
- //prevTranslate = getTranslate(e.getX(), e.getY());
- //System.out.println("start translate " + prevTranslate);
+ // index = gizmo.getTranslateAxis(actor);
+ // if (index == -1) {
+ // valid = false;
+ // panel.getDefaultAction().mousePressed(e);
+ // return;
+ // }
+ // valid = true;
+ // prevTranslate = getTranslate(e.getX(), e.getY());
+ // System.out.println("start translate " + prevTranslate);
}
-
-
-
+
@Override
public boolean mouseReleased(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
}
return true;
}
-
+
@Override
public boolean mouseDragged(MouseEvent e) {
- //if ((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) > 0 && valid) {
- if (e.getButton() == MouseEvent.BUTTON1 && valid) {
-
+ // if ((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) > 0 && valid) {
+ if (e.getButton() == MouseEvent.BUTTON1 && valid) {
+
Vector3d translate = getTranslate(e.getX(), e.getY(), prevTranslate);
- //System.out.println("translate " + translate);
+ // System.out.println("translate " + translate);
if (translate == null)
return true;
boolean step = ((e.getModifiers() & MouseEvent.CTRL_MASK) > 0);
pos = constaints(pos, step);
setPos(pos);
}
- //mapping.rangeModified(node);
-
- //nodeMap.modified(node);
+ // mapping.rangeModified(node);
+
+ // nodeMap.modified(node);
update();
} else {
getDefaultAction().mouseDragged(e);
}
return true;
}
-
+
@Override
public boolean mouseWheelMoved(MouseWheelEvent e) {
return getDefaultAction().mouseWheelMoved(e);
}
-
+
protected void setPos(Vector3d pos) {
node.setPosition(pos);
}
-
+
protected void setWorldPos(Vector3d pos) {
node.setWorldPosition(pos);
}
-
+
private double istep = 10.0;
private int decimals = 2;
-
+
protected Vector3d constaints(Vector3d p, boolean step) {
- if(!step)
+ if (!step)
return p;
switch (index) {
case X:
}
return p;
}
-
+
@Override
public boolean mouseMoved(MouseEvent e) {
getDefaultAction().mouseMoved(e);
return true;
}
-
+
protected Vector3d getTranslate(double x, double y) {
return getTranslate(x, y, new Vector3d());
}
-
+
protected Vector3d getTranslate(double x, double y, Vector3d offset) {
Vector3d translate = new Vector3d();
-
- Ray ray = vtkUtil.createMouseRay(panel.getRenderer(),x, y);
-
+
+ Ray ray = vtkUtil.createMouseRay(panel.getRenderer(), x, y);
+
Vector3d p = node.getWorldPosition();
Vector3d dir = null;
-
+
switch (index) {
case P:
Vector3d normal = new Vector3d(panel.getRenderer().GetActiveCamera().GetDirectionOfProjection());
}
break;
- case X :
- dir = new Vector3d(1.0,0.0,0.0);
- if(!worldCoord)
- MathTools.rotate(q, dir, dir);
- Vector3d i1 = new Vector3d();
- Vector3d i2 = new Vector3d();
- s = new double[2];
- MathTools.intersectStraightStraight( p, dir,ray.pos, ray.dir, i2, i1,s);
- translate.x = s[0];
-
- break;
- case Y :
- dir = new Vector3d(0.0,1.0,0.0);
- if(!worldCoord)
+ case X:
+ dir = new Vector3d(1.0, 0.0, 0.0);
+ if (!worldCoord)
+ MathTools.rotate(q, dir, dir);
+ Vector3d i1 = new Vector3d();
+ Vector3d i2 = new Vector3d();
+ s = new double[2];
+ MathTools.intersectStraightStraight(p, dir, ray.pos, ray.dir, i2, i1, s);
+ translate.x = s[0];
+
+ break;
+ case Y:
+ dir = new Vector3d(0.0, 1.0, 0.0);
+ if (!worldCoord)
MathTools.rotate(q, dir, dir);
i1 = new Vector3d();
i2 = new Vector3d();
s = new double[2];
- MathTools.intersectStraightStraight( p, dir,ray.pos, ray.dir, i2, i1,s);
+ MathTools.intersectStraightStraight(p, dir, ray.pos, ray.dir, i2, i1, s);
translate.y = s[0];
break;
- case Z :
- dir = new Vector3d(0.0,0.0,1.0);
- if(!worldCoord)
+ case Z:
+ dir = new Vector3d(0.0, 0.0, 1.0);
+ if (!worldCoord)
MathTools.rotate(q, dir, dir);
i1 = new Vector3d();
i2 = new Vector3d();
s = new double[2];
- MathTools.intersectStraightStraight( p, dir,ray.pos, ray.dir, i2, i1,s);
+ MathTools.intersectStraightStraight(p, dir, ray.pos, ray.dir, i2, i1, s);
translate.z = s[0];
break;
- case XY :
- normal = new Vector3d(0.0,0.0,1.0);
- if(!worldCoord)
+ case XY:
+ normal = new Vector3d(0.0, 0.0, 1.0);
+ if (!worldCoord)
MathTools.rotate(q, normal, normal);
r = new Vector3d();
if (MathTools.intersectStraightPlane(ray.pos, ray.dir, p, normal, r)) {
translate.y = r.y;
}
break;
- case XZ :
- normal = new Vector3d(0.0,1.0,0.0);
- if(!worldCoord)
+ case XZ:
+ normal = new Vector3d(0.0, 1.0, 0.0);
+ if (!worldCoord)
MathTools.rotate(q, normal, normal);
r = new Vector3d();
if (MathTools.intersectStraightPlane(ray.pos, ray.dir, p, normal, r)) {
translate.z = r.z;
}
break;
- case YZ :
- normal = new Vector3d(1.0,0.0,0.0);
- if(!worldCoord)
+ case YZ:
+ normal = new Vector3d(1.0, 0.0, 0.0);
+ if (!worldCoord)
MathTools.rotate(q, normal, normal);
r = new Vector3d();
if (MathTools.intersectStraightPlane(ray.pos, ray.dir, p, normal, r)) {
translate.z = r.z;
}
break;
- default :
-
+ default:
+
return null;
}
translate.sub(offset);
return translate;
}
-
+
}
import org.simantics.plant3d.Activator;
import org.simantics.plant3d.scenegraph.InlineComponent;
import org.simantics.plant3d.scenegraph.PipelineComponent;
-import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.Direction;
-public class TranslateInlineAction extends TranslateAction{
-
- private boolean inline;
-
+public class TranslateInlineAction extends TranslateAction {
+
+ private boolean inline;
+
private Vector3d s;
private Vector3d e;
private Vector3d dir;
- public TranslateInlineAction(InteractiveVtkComposite panel, VTKNodeMap<?, ? extends INode> nodeMap, ToolComposite toolComposite) {
+ public TranslateInlineAction(InteractiveVtkComposite panel, VTKNodeMap<?, ? extends INode> nodeMap,
+ ToolComposite toolComposite) {
super(panel, nodeMap, toolComposite);
setImageDescriptor(Activator.imageDescriptorFromPlugin("com.famfamfam.silk", "icons/arrow_refresh.png"));
}
-
+
@Override
public void setNode(IG3DNode node) {
super.setNode(node);
if (node instanceof InlineComponent) {
- InlineComponent comp = (InlineComponent)node;
+ InlineComponent comp = (InlineComponent) node;
if (comp.isVariableLength()) {
- setEnabled(false);
+ setEnabled(false);
} else if (comp.getNext() == null || comp.getPrevious() == null) {
- setEnabled(true);
- inline = false;
- dir = comp.getControlPoint().getInlineDir();
- dir.normalize();
- } else {
+ setEnabled(true);
+ inline = false;
+ dir = comp.getControlPoint().getInlineDir();
+ dir.normalize();
+ } else {
setEnabled(true);
inline = true;
PipelineComponent prev = comp.getPrevious();
PipelineComponent next = comp.getNext();
if (prev == null || next == null) {
- // TODO : we should support open ended translation (when translated component is the first or last of the run).
+ // TODO : we should support open ended translation (when translated component is
+ // the first or last of the run).
setEnabled(false);
return;
}
- if (prev.getControlPoint().isInline() && !prev.getControlPoint().isFixedLength() && prev.getPrevious() != null)
+ if (prev.getControlPoint().isInline() && !prev.getControlPoint().isFixedLength()
+ && prev.getPrevious() != null)
prev = prev.getPrevious();
- if (next.getControlPoint().isInline() && !next.getControlPoint().isFixedLength() && next.getNext() != null) {
+ if (next.getControlPoint().isInline() && !next.getControlPoint().isFixedLength()
+ && next.getNext() != null) {
next = next.getNext();
}
Point3d ns = new Point3d();
next.getEnds(ns, ne);
prev.getEnds(ps, pe);
dir = comp.getControlPoint().getInlineDir();
- // We may have offsets in the path leg, hence we have to project the coordinates.
+ // We may have offsets in the path leg, hence we have to project the
+ // coordinates.
Vector3d wp = node.getWorldPosition();
if (prev.getControlPoint().isVariableLength())
s = MathTools.closestPointOnStraight(ps, wp, dir);
setEnabled(false);
}
}
-
+
@Override
- protected void createTools(ToolComposite toolComposite) {
-
+ protected void createAxisSelection(ToolComposite toolComposite) {
+ // No axis selection for inline components
}
-
+
@Override
public boolean keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
panel.useDefaultAction();
if (!inline) {
- return super.keyPressed(e);
+ return super.keyPressed(e);
} else {
- if (valid)
- return true;
-
- update();
- return true;
+ if (valid)
+ return true;
+
+ update();
+ return true;
}
}
-
+
@Override
public void setWorldCoord(boolean b) {
-
+
}
-
+
@Override
public boolean mouseDragged(MouseEvent e) {
- //if ((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) > 0 && valid) {
- if (e.getButton() == MouseEvent.BUTTON1 && valid) {
+ // if ((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) > 0 && valid) {
+ if (e.getButton() == MouseEvent.BUTTON1 && valid) {
if (inline) {
- Vector3d translate = getTranslate(e.getX(), e.getY(), prevTranslate);
- //System.out.println("translate " + translate);
- if (translate == null)
- return true;
- //boolean step = ((e.getModifiers() & MouseEvent.CTRL_MASK) > 0);
- Vector3d pos = new Vector3d(node.getWorldPosition());
-
+ Vector3d translate = getTranslate(e.getX(), e.getY(), prevTranslate);
+ // System.out.println("translate " + translate);
+ if (translate == null)
+ return true;
+ // boolean step = ((e.getModifiers() & MouseEvent.CTRL_MASK) > 0);
+ Vector3d pos = new Vector3d(node.getWorldPosition());
+
// System.out.println(pos + " " + translate);
- //pos.add(translate);
- pos.set(translate);
- //pos = constaints(pos, step);
- setWorldPos(pos);
- update();
+ // pos.add(translate);
+ pos.set(translate);
+ // pos = constaints(pos, step);
+ setWorldPos(pos);
+ update();
} else {
- super.mouseDragged(e);
+ super.mouseDragged(e);
}
} else {
getDefaultAction().mouseDragged(e);
update();
- }
+ }
return true;
}
-
+
protected Vector3d getTranslate(double x, double y, Vector3d offset) {
-
- Ray ray = vtkUtil.createMouseRay(panel.getRenderer(),x, y);
-
+
+ Ray ray = vtkUtil.createMouseRay(panel.getRenderer(), x, y);
+
Vector3d p = node.getWorldPosition();
-
+
if (inline) {
- Vector3d i1 = new Vector3d();
- Vector3d i2 = new Vector3d();
-
- double mu[] = new double[2];
- MathTools.intersectStraightStraight( p, dir,ray.pos, ray.dir, i2, i1,mu);
-
- Vector3d t = MathTools.closestPointOnEdge(i1, s, e);
- return t;
+ Vector3d i1 = new Vector3d();
+ Vector3d i2 = new Vector3d();
+
+ double mu[] = new double[2];
+ MathTools.intersectStraightStraight(p, dir, ray.pos, ray.dir, i2, i1, mu);
+
+ Vector3d t = MathTools.closestPointOnEdge(i1, s, e);
+ return t;
} else {
- return super.getTranslate(x, y, offset);
+ return super.getTranslate(x, y, offset);
}
-
- }
+ }
}