jMonkeyEngine-Contributions / Lemur

Lemur is a jMonkeyEngine-based UI toolkit.
http://jmonkeyengine-contributions.github.io/Lemur/
BSD 3-Clause "New" or "Revised" License
116 stars 32 forks source link

the mouse listener has a pre-consumed event, while the cursor listener let us use that boolean to control something #50

Closed AquariusPower closed 5 years ago

AquariusPower commented 7 years ago

topic: https://hub.jmonkeyengine.org/t/cursorlistener-vs-mouselistener-when-to-use-each-do-they-cope/38332/3

test case (click once on the button):

package com.github.devconslejme.tests.temp;

import com.jme3.app.SimpleApplication;
import com.jme3.input.event.MouseButtonEvent;
import com.jme3.input.event.MouseMotionEvent;
import com.jme3.scene.Spatial;
import com.simsilica.lemur.Button;
import com.simsilica.lemur.GuiGlobals;
import com.simsilica.lemur.event.CursorButtonEvent;
import com.simsilica.lemur.event.CursorEventControl;
import com.simsilica.lemur.event.CursorMotionEvent;
import com.simsilica.lemur.event.MouseEventControl;
import com.simsilica.lemur.style.BaseStyles;
import com.simsilica.lemur.event.MouseListener;
import com.simsilica.lemur.event.CursorListener;

public class TestMouseAndCursorListeners extends SimpleApplication{
    public static void main(String[] args) {
        TestMouseAndCursorListeners test = new TestMouseAndCursorListeners();
        test.start();
    }

    private Button  btn;

    @Override
    public void simpleInitApp() {
        GuiGlobals.initialize(this);
        BaseStyles.loadGlassStyle();
        GuiGlobals.getInstance().getStyles().setDefaultStyle(BaseStyles.GLASS);

        initTest();
    }

    private void initTest() {
        btn = new Button("test");
        btn.setLocalTranslation(300, 300, 0);
        guiNode.attachChild(btn);

        MouseEventControl.addListenersToSpatial(btn, new TestMouseListener());
        CursorEventControl.addListenersToSpatial(btn, new TestCursorListener());
    }

    class TestMouseListener implements MouseListener{
        @Override
        public void mouseButtonEvent(MouseButtonEvent event, Spatial target,                Spatial capture) {
            dumpEvent(event.isConsumed(),event);
        }
        @Override       public void mouseEntered(MouseMotionEvent event, Spatial target,                Spatial capture) {      }
        @Override       public void mouseExited(MouseMotionEvent event, Spatial target,             Spatial capture) {      }
        @Override       public void mouseMoved(MouseMotionEvent event, Spatial target,              Spatial capture) {      }
    }
    class TestCursorListener implements CursorListener{
        @Override
        public void cursorButtonEvent(CursorButtonEvent event, Spatial target,              Spatial capture) {
            dumpEvent(event.isConsumed(),event);
        }
        @Override       public void cursorEntered(CursorMotionEvent event, Spatial target,              Spatial capture) {      }
        @Override       public void cursorExited(CursorMotionEvent event, Spatial target,               Spatial capture) {      }
        @Override       public void cursorMoved(CursorMotionEvent event, Spatial target,        Spatial capture) {      }
    }

    public void dumpEvent(boolean b,Object event){
        System.out.println(event.toString()+"\nconsumed="+b+"\nhash="+event.hashCode());
        System.out.println();
    }
}
pspeed42 commented 5 years ago

If I read this and the forum post correctly... if a spatial has both cursor listeners and mouse listeners then the event will be delivered to all of them even if one of the listeners consumes the event. This was to simulate the same behavior from when both listeners were in the same list.

Basically, if an event is already being delivered to a Spatial's listeners because it is not consumed yet... then it will be delivered to ALL of that spatial's listeners. They can choose to look at the isConsumed() or not as needed but the thinking is that multiple listeners on the same spatial must be coordinating something.

...and otherwise, listener order would become critical and that's not a pleasant thing for an application to manage.

I'm going to close the issue assuming that's the case... but if it's not and I'm just being dumb then reopen it with additional explanation.