AND linked Triggers

Hello,



I want to drag a spatial when control left (KEY_LCONTROL) and left mouse button is pressed. Draggin already works with left mouse clicked. I just ask myself how I can link mouse triggers and keyboard triggers.

Thanks.



Regards,

Equi

Register an action listener for both, and then set a boolean, e.g:

[java]

public void onAction(String name, boolean pressed, float tpf){

if (action.equals("LeftControl")) leftCtrl = pressed;

if (action.equals("MouseButton")) mouseBtn = pressed;

}

[/java]



Then in your dragging code, check if both booleans are true and then do the dragging

@Equilibrium

Can you share your code with the dragging? I’m interested in your solution! Thanks

This is my inner class…

[java]

package at.netcrystals.praxiteles.jme3.objects.input;



import at.netcrystals.praxiteles.jme3.objects.PraxitelesApplication;

import at.netcrystals.praxiteles.jme3.objects.PraxitelesCamera;

import at.netcrystals.praxiteles.jme3.objects.PraxitelesSpatial;

import com.jme3.collision.CollisionResult;

import com.jme3.collision.CollisionResults;

import com.jme3.input.InputManager;

import com.jme3.input.controls.ActionListener;

import com.jme3.input.controls.AnalogListener;

import com.jme3.input.controls.InputListener;

import com.jme3.math.Quaternion;

import com.jme3.math.Ray;

import com.jme3.math.Vector2f;

import com.jme3.math.Vector3f;

import com.jme3.renderer.Camera;

import com.jme3.scene.Geometry;



/**

*

  • @author

    /

    public abstract class PraxitelesDragListener extends PraxitelesInputListener {



    /


    *

    /

    private PraxitelesMapping dragMapping = null;

    /


    *

    /

    private DragListener dragListener;

    /


    *

    */



    /**
  • Creates a new PraxitelesFlyByAnalogListener to control the given Camera object.

    */

    public PraxitelesDragListener() {

    }



    @Override

    public void initAfterFirstAdd() {

    dragMapping = createMapping();

    addNetcrystalsChildInstance(dragMapping);

    }



    @Override

    public InputListener getInputListener() {

    if (dragListener == null) {

    dragListener = new DragListener();

    }

    return dragListener;

    }



    @Override

    public String getNetcrystalsName() {

    return "Drag Listener";

    }



    public class DragListener implements AnalogListener, ActionListener {



    private PraxitelesSpatial draggedPraxitelesSpatial = null;

    private PraxitelesCamera activePraxitelesCamera = null;

    private Vector2f newMouseCursorPositionOnScreen;

    private Vector2f oldMouseCursorPositionOnScreen;

    private Vector2f mouseCursorDiffOnScreen;

    private float dragDistance = 0.0f;



    private void drag(float value, float tpf) {

    if (!newMouseCursorPositionOnScreen.equals(oldMouseCursorPositionOnScreen)) {

    if (activePraxitelesCamera != null) {

    Camera cam = activePraxitelesCamera.getCamera();



    Vector3f oldOrigin = cam.getWorldCoordinates(oldMouseCursorPositionOnScreen, 0.0f);

    Vector3f oldDirection = cam.getWorldCoordinates(oldMouseCursorPositionOnScreen, 0.3f);

    oldDirection.subtractLocal(oldOrigin).normalizeLocal().multLocal(dragDistance);



    Vector3f newOrigin = cam.getWorldCoordinates(newMouseCursorPositionOnScreen, 0.0f);

    Vector3f newDirection = cam.getWorldCoordinates(newMouseCursorPositionOnScreen, 0.3f);

    newDirection.subtractLocal(newOrigin).normalizeLocal().multLocal(dragDistance);



    newDirection.subtractLocal(oldDirection);

    mouseCursorDiffOnScreen = newMouseCursorPositionOnScreen.subtract(oldMouseCursorPositionOnScreen);

    dragging(value, tpf, mouseCursorDiffOnScreen, draggedPraxitelesSpatial, newDirection, dragDistance, activePraxitelesCamera);

    }

    }

    }



    @Override

    public void onAnalog(String name, float value, float tpf) {

    if (!getEnabled()) {

    return;

    }

    newMouseCursorPositionOnScreen = PraxitelesApplication.findInstance().getApplication().getInputManager().getCursorPosition();

    if (draggedPraxitelesSpatial != null) {

    drag(value, tpf);

    }

    oldMouseCursorPositionOnScreen = newMouseCursorPositionOnScreen.clone();

    }



    /**

    *
  • @param name
  • @param value
  • @param tpf

    /

    @Override

    public void onAction(String name, boolean value, float tpf) {

    if (!getEnabled()) {

    return;

    }

    oldMouseCursorPositionOnScreen = PraxitelesApplication.findInstance().getApplication().getInputManager().getCursorPosition();

    if (name.equals(dragMapping.getMappingName())) {



    if (draggedPraxitelesSpatial == null) {

    //Start

    activePraxitelesCamera = PraxitelesApplication.findInstance().getApplication().getActiveCamera();

    if (activePraxitelesCamera != null) {

    com.jme3.scene.Node rootNode = PraxitelesApplication.findInstance().getApplication().getRootNode();

    rootNode.updateGeometricState();



    Camera cam = activePraxitelesCamera.getCamera();

    InputManager inputManager = PraxitelesApplication.findInstance().getApplication().getInputManager();



    Vector3f origin = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.0f);

    Vector3f direction = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.3f);

    direction.subtractLocal(origin).normalizeLocal();



    Ray ray = new Ray(origin, direction);



    CollisionResults results = new CollisionResults();



    try {

    rootNode.collideWith(ray, results);

    } catch (Exception e) {

    }



    if (results.size() > 0) {

    CollisionResult closest = results.getClosestCollision();

    Geometry geometry = closest.getGeometry();

    if (geometry != null) {

    draggedPraxitelesSpatial = (PraxitelesSpatial) geometry.getUserData(PraxitelesSpatial.PROP_USERDATA_PRAXITELESINSTANCE);

    if (draggedPraxitelesSpatial != null) {

    draggedPraxitelesSpatial = findHighestDraggable(draggedPraxitelesSpatial);

    if (draggedPraxitelesSpatial != null) {

    if (!dragStarted(draggedPraxitelesSpatial)) {

    draggedPraxitelesSpatial = null;

    return;

    }

    dragDistance = draggedPraxitelesSpatial.getWorldTranslation().subtract(activePraxitelesCamera.getWorldTranslation()).length();

    }

    }

    }

    }

    }

    } else {

    dragStopped(draggedPraxitelesSpatial);

    draggedPraxitelesSpatial = null;

    activePraxitelesCamera = null;

    }

    }

    }



    private PraxitelesSpatial findHighestDraggable(PraxitelesSpatial root) {

    if (root.getPickable()) {

    return root;

    } else {

    if (root.getNetcrystalsParent() instanceof PraxitelesSpatial) {

    return findHighestDraggable((PraxitelesSpatial) root.getNetcrystalsParent());

    }

    }

    return null;

    }

    }



    public abstract boolean dragStarted(PraxitelesSpatial praxitelesSpatial);



    public abstract void dragging(float value, float tpf, Vector2f cursorDiff, PraxitelesSpatial praxitelesSpatial, Vector3f direction, float distance, PraxitelesCamera praxitelesCamera);



    public abstract void dragStopped(PraxitelesSpatial praxitelesSpatial);



    public abstract PraxitelesMapping createMapping();

    }

    [/java]





    This is an implementation



    [java]

    /

  • To change this template, choose Tools | Templates
  • and open the template in the editor.

    */

    package at.netcrystals.molecule.objects.input;



    import at.netcrystals.atom.objects.Atom;

    import at.netcrystals.atom.objects.Carbon;

    import at.netcrystals.atom.objects.Hydrogen;

    import at.netcrystals.atom.objects.Oxygen;

    import at.netcrystals.atom.objects.Silicon;

    import at.netcrystals.molecule.swing.MoleculeChoiceComboBox;

    import at.netcrystals.molecule.objects.Atoms;

    import at.netcrystals.molecule.objects.Bonds;

    import at.netcrystals.molecule.objects.Molecule;

    import at.netcrystals.molecule.objects.geometry.SingleBond;

    import at.netcrystals.molecule.windows.MoleculeBuilderTopComponent;

    import at.netcrystals.praxiteles.jme3.objects.PraxitelesCamera;

    import at.netcrystals.praxiteles.jme3.objects.PraxitelesPlane;

    import at.netcrystals.praxiteles.jme3.objects.PraxitelesSpatial;

    import at.netcrystals.praxiteles.jme3.objects.input.PraxitelesDragListener;

    import at.netcrystals.praxiteles.jme3.objects.input.PraxitelesKeyTrigger;

    import at.netcrystals.praxiteles.jme3.objects.input.PraxitelesMapping;

    import at.netcrystals.praxiteles.jme3.objects.input.PraxitelesMouseButtonTrigger;

    import com.jme3.math.Vector2f;

    import com.jme3.math.Vector3f;

    import org.openide.util.Exceptions;



    /**

    *
  • @author

    */

    public class AtomReplicationDragListener extends PraxitelesDragListener {



    //private PraxitelesPlane praxitelesPlane = null;

    private boolean dragging = false;

    private Atom createdAtom;

    private Molecule molecule;



    public AtomReplicationDragListener() {

    }



    @Override

    public String getNetcrystalsName() {

    return “Atom replication drag listener”;

    }



    @Override

    public boolean dragStarted(PraxitelesSpatial praxitelesSpatial) {

    if (praxitelesSpatial instanceof Atom) {

    Atom dragStartAtom = (Atom) praxitelesSpatial;

    molecule = praxitelesSpatial.findFirstNetcrystalsObjectUpwards(Molecule.class);

    if (molecule != null) {

    Atoms atoms = molecule.getNetcrystalsChildInstance(Atoms.class);

    if (atoms != null) {

    if (MoleculeBuilderTopComponent.cOnCDrag() && dragStartAtom instanceof Carbon) {

    createdAtom = new Carbon();

    } else if (MoleculeBuilderTopComponent.hOnCDrag() && dragStartAtom instanceof Carbon) {

    createdAtom = new Hydrogen();

    } else if (MoleculeBuilderTopComponent.siOnSiDrag() && dragStartAtom instanceof Silicon) {

    createdAtom = new Silicon();

    } else if (MoleculeBuilderTopComponent.hOnSiDrag() && dragStartAtom instanceof Silicon) {

    createdAtom = new Hydrogen();

    } else if (MoleculeBuilderTopComponent.oOnSiDrag() && dragStartAtom instanceof Silicon) {

    createdAtom = new Oxygen();

    } else {

    createdAtom = MoleculeChoiceComboBox.findInstance().getSelectedAtom();

    }

    createdAtom.setLocalTranslation(dragStartAtom.getLocalTranslation());

    atoms.addNetcrystalsChildInstance(createdAtom);



    dragging = true;

    return true;

    }

    }

    }

    return false;

    }



    @Override

    public void dragging(float value, float tpf, Vector2f cursorDiff, PraxitelesSpatial praxitelesSpatial, Vector3f direction, float distance, PraxitelesCamera praxitelesCamera) {

    if (dragging && createdAtom != null) {

    //Molecule molecule=createdAtom.findFirstNetcrystalsObjectUpwards(Molecule.class);

    Vector3f differenceVectorInMoleculeCoordinateSystem = createdAtom.getSpatial().getParent().getWorldRotation().inverse().mult(direction);

    createdAtom.setLocalTranslation(createdAtom.getLocalTranslation().add(differenceVectorInMoleculeCoordinateSystem));

    }

    }



    @Override

    public void dragStopped(PraxitelesSpatial praxitelesSpatial) {

    /if (praxitelesPlane != null) {

    praxitelesPlane.removeNetcrystalsChildInstanceFromParent();

    }
    /

    if (praxitelesSpatial instanceof Atom) {

    Atom dragStartAtom = (Atom) praxitelesSpatial;

    if (MoleculeBuilderTopComponent.createBondsWhileDraggingAtoms()) {

    Bonds bonds = molecule.getNetcrystalsChildInstance(Bonds.class);

    if (bonds == null) {

    bonds = new Bonds();

    molecule.addNetcrystalsChildInstance(bonds);

    }

    SingleBond singleBond = new SingleBond();

    bonds.addNetcrystalsChildInstance(singleBond);

    singleBond.setFirstAtomID(dragStartAtom.getNetcrystalsId());

    singleBond.setSecondAtomID(createdAtom.getNetcrystalsId());

    }



    }

    dragging = false;

    }



    @Override

    public PraxitelesMapping createMapping() {

    //CLICK

    PraxitelesMapping dragStartMapping = new PraxitelesMapping();

    dragStartMapping.setMappingName(“Atom Add Drag”);//NOI18N

    addNetcrystalsChildInstance(dragStartMapping);



    PraxitelesMouseButtonTrigger praxitelesClickMouseButtonTrigger = new PraxitelesMouseButtonTrigger();

    praxitelesClickMouseButtonTrigger.setMouseButton(PraxitelesMouseButtonTrigger.MouseButton.LEFT);

    dragStartMapping.addNetcrystalsChildInstance(praxitelesClickMouseButtonTrigger);



    PraxitelesKeyTrigger praxitelesKeyTrigger = new PraxitelesKeyTrigger();

    praxitelesKeyTrigger.setKeyCode(PraxitelesKeyTrigger.KeyCode.KEY_LCONTROL);

    dragStartMapping.addNetcrystalsChildInstance(praxitelesKeyTrigger);



    return dragStartMapping;

    }

    }

    [/java]



    Hope that helps (but I don’t think so…) :slight_smile: If you have questions let me know



    Regards,

    Equi