Login  Register

Re: MouseTranslate behavior not working

Posted by runiter on Feb 11, 2022; 1:02pm
URL: https://forum.jogamp.org/MouseTranslate-behavior-not-working-tp4041599p4041641.html

At first it seemed like it was due to Java version because it used to work, but after looking at Java3D code it seemed like it was intentional.

Anyways I fixed it by changing its code to this:

import java.awt.AWTEvent;
import java.awt.event.MouseEvent;
import java.util.Iterator;
import org.jogamp.java3d.TransformGroup;
import org.jogamp.java3d.WakeupCriterion;
import org.jogamp.java3d.WakeupOnAWTEvent;
import org.jogamp.java3d.WakeupOnBehaviorPost;
import org.jogamp.java3d.utils.behaviors.mouse.MouseBehavior;
import org.jogamp.java3d.utils.behaviors.mouse.MouseBehaviorCallback;
import org.jogamp.vecmath.Vector3d;

public class MouseTranslate extends MouseBehavior {
        double x_factor = 0.02D;
        double y_factor = 0.02D;
        Vector3d translation = new Vector3d();
        private MouseBehaviorCallback callback = null;

        public MouseTranslate(TransformGroup transformGroup) {
                super(transformGroup);
        }

        public void initialize() {
                super.initialize();
                if ((this.flags & 2) == 2) {
                        this.invert = true;
                        this.x_factor *= -1.0D;
                        this.y_factor *= -1.0D;
                }

        }

        public void setFactor(double factor) {
                this.x_factor = this.y_factor = factor;
        }

        public void processStimulus(Iterator<WakeupCriterion> criteria) {
                while(criteria.hasNext()) {
                        WakeupCriterion wakeup = criteria.next();
                        MouseEvent evt;
                        if (wakeup instanceof WakeupOnAWTEvent) {
                                AWTEvent[] events = ((WakeupOnAWTEvent)wakeup).getAWTEvent();
                                if (events.length > 0) {
                                        evt = (MouseEvent)events[events.length - 1];
                                        this.doProcess(evt);
                                }
                        } else if (wakeup instanceof WakeupOnBehaviorPost) {
                                while(true) {
                                        synchronized(this.mouseq) {
                                                if (this.mouseq.isEmpty()) {
                                                        break;
                                                }

                                                for(evt = (MouseEvent)this.mouseq.remove(0); evt.getID() == 506 && !this.mouseq.isEmpty() && ((MouseEvent)this.mouseq.get(0)).getID() == 506; evt = (MouseEvent)this.mouseq.remove(0)) {
                                                }
                                        }

                                        this.doProcess(evt);
                                }
                        }
                }

                this.wakeupOn(this.mouseCriterion);
        }

        public void processMouseEvent(MouseEvent evt) {
                if (evt.getButton() == 3) {
                        super.processMouseEvent(evt);
                }
        }

        void doProcess(MouseEvent evt) {
                this.processMouseEvent(evt);
                if (this.buttonPress && (this.flags & 1) == 0 || this.wakeUp && (this.flags & 1) != 0) {
                        int id = evt.getID();
                        if (id == 506 && !evt.isAltDown()) {
                                this.x = evt.getX();
                                this.y = evt.getY();
                                int dx = this.x - this.x_last;
                                int dy = this.y - this.y_last;
                                if (!this.reset && Math.abs(dy) < 50 && Math.abs(dx) < 50) {
                                        this.transformGroup.getTransform(this.currXform);
                                        this.translation.x = (double)dx * this.x_factor;
                                        this.translation.y = (double)(-dy) * this.y_factor;
                                        this.transformX.set(this.translation);
                                        if (this.invert) {
                                                this.currXform.mul(this.currXform, this.transformX);
                                        } else {
                                                this.currXform.mul(this.transformX, this.currXform);
                                        }

                                        this.transformGroup.setTransform(this.currXform);
                                        if (this.callback != null) {
                                                this.callback.transformChanged(1, this.currXform);
                                        }
                                } else {
                                        this.reset = false;
                                }

                                this.x_last = this.x;
                                this.y_last = this.y;
                        } else if (id == 501) {
                                this.x_last = evt.getX();
                                this.y_last = evt.getY();
                        }
                }

        }

        public void setupCallback(MouseBehaviorCallback callback) {
                this.callback = callback;
        }
}
Saeid Nourian, Ph.D. Eng. | Graphing Calculator 3D