I need to remove a part of the code from the button in the lemur


I need to remove this code

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package GUI;

import com.jme3.input.event.MouseButtonEvent;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.Control;
import com.simsilica.lemur.Button;
import com.simsilica.lemur.event.DefaultMouseListener;
import com.simsilica.lemur.event.FocusMouseListener;
import com.simsilica.lemur.event.MouseEventControl;
import com.simsilica.lemur.style.ElementId;

/**
 *
 * @author 
 */
public class myButton extends Button {
    public myButton( String s ) {
        super(s, true, new ElementId(ELEMENT_ID), null);
        addControl(new MouseEventControl(FocusMouseListener.INSTANCE, new ButtonMouseHandler()));
    }

    public myButton( String s, String style ) {
        super(s, true, new ElementId(ELEMENT_ID), style);
        addControl(new MouseEventControl(FocusMouseListener.INSTANCE, new ButtonMouseHandler()));
    }

    public myButton( String s, ElementId elementId ) {
        super(s, true, elementId, null);
        addControl(new MouseEventControl(FocusMouseListener.INSTANCE, new ButtonMouseHandler()));
    }
    
    public myButton( String s, ElementId elementId, String style ) {
        super(s, true, elementId, style);
        addControl(new MouseEventControl(FocusMouseListener.INSTANCE, new ButtonMouseHandler()));
    }


    protected class ButtonMouseHandler extends DefaultMouseListener {

        @Override
        public void mouseButtonEvent(MouseButtonEvent event, Spatial target, Spatial capture) {
            // Do our own better handling of 'click' now
            // super.mouseButtonEvent(event, target, capture);
            if (!isEnabled())
                return;

            if (event.isPressed()) {
                setPressed(event.isPressed());
                System.err.println(event.isPressed());
            } else if (isPressed()) {
                // Only run the up processing if we were already pressed
                // This also handles the case where we weren't enabled before
                // but are now, etc.

                if (target == capture) {
                    // Then we are still over the button and we should run the
                    // click
                    runClick();
                }
                // If we run the up without checking properly then we
                // potentially get up events with no down event.  This messes
                // up listeners that are (correctly) expecting an up for every
                // down and no ups without downs.
                // So, any time the capture is us then we will run, else not
                if (capture == myButton.this) {
                    setPressed(false);
                }
            }
        }
    }
}
   

I tried to do that but it didn’t work.
Should I just copy the entire button code and then just delete this code?
What are the ways to modify this portion of code without using invasive programming?

To ask the wider question. Why do you want that? What are you trying to achieve

1 Like

If you need to completely break your user interface in this way then there is no standard or easy way to do it.

As richtea mentions, it would be useful to know why you want to completely break event processing for your buttons. Maybe there is a different way to do what you want to do without totally breaking all event processing for your buttons.

1 Like

This goes back to a topic from a long, long time ago


event.setConsumed();

event.setConsumed(); causes this blue checkbox to not reset when the left mouse button is raised

This is my minimap part Because minimap is using a custom CursorEventControl, there is no event.setConsumed(); in the cursorButtonEvent, so it won’t conflict with the multiselect box.
Ignore this paragraph here

          else {
                    event.setConsumed();
                }
public class CamAndMouseCur extends BaseAppState implements ActionListener, AnalogListener,

I have registered ActionListener, AnalogListener in CamAndMouseCur.
This code controls the size of the multi-select box when the mouse is dragged.

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

        if (name.equals(CameraInput.CHASECAM_DOWN) || name.equals(CameraInput.CHASECAM_UP) || name.equals(CameraInput.CHASECAM_MOVELEFT) || name.equals(CameraInput.CHASECAM_MOVERIGHT)) {
            //System.err.println();
            if (isSelecting == false) {
                rayUnitSelection.pickUnit();

                if (rayUnitSelection.pick() == null) {
                    inputManager.setMouseCursor(MouseDefault);
                } else {
                    inputManager.setMouseCursor(MouseSelection);
                }
            }
        }

        if (name.equals("MouseButtonLeft") && isSelecting) {

            endClick.set(inputManager.getCursorPosition());
            dragBoxPosition = startClick;
            dragBoxSize = endClick.subtract(startClick);
            for (Node unit : UnitGlobalData.getInstance().getCombatantsUnitList()) {
                //System.err.println(unit.getUserData("CombatantsUnitNode").toString());
                SelectUnit.getSelectUnit().isNodeInDragBox(unit, cam, dragBoxPosition, dragBoxSize);
            }
            System.err.println(isSelecting);
            // 更新选择框大小和位置
            updateSelectionBox();
        }

    }
    @Override
    public void onAction(String name, boolean isPressed, float tpf) {
//        System.out.println(isPressed);
        if (name.equals("MouseButtonLeft") && isPressed) {
            System.err.println(isPressed);
            MouseState(MousesFiniteStateMachine.getInstance().getCurrentState());


            // 鼠标按下,记录起始位置
            xDown = inputManager.getCursorPosition().x;
            yDown = inputManager.getCursorPosition().y;
            startClick.set(inputManager.getCursorPosition());
            isSelecting = true;
        } else if (name.equals("MouseButtonLeft") && !isPressed) {

            isSelecting = false;
            startClick.zero();
            endClick.zero();
            updateSelectionBox();
            UnitGlobalData.getInstance().BeforehandSelectedUnitToSelectedUnitList();
            UnitGlobalData.getInstance().clearBeforehandSelectedUnit();

        }

This controls the reset of the multiselect box when the left mouse button is lifted.

            startClick.zero();
            endClick.zero();
            updateSelectionBox();

But if the mouse overlaps the Lemur’s button then the button will consume the
This causes the onAction to not receive the left mouse button raised.
So am I left with only one option and that is to make a custom button using Container?

I think you are mixing JME listeners with Lemur listeners and so in this case you will have bad time.

But if you remove that setConsumed() then your Lemur button will stop working correctly in some cases.

The best way to fix this is to use Lemur events for your dragging. So a cursor listener on the map (and make the ‘map’ bigger than what is visible, like a giant black quad behind the actual map). Then if the map gets a Lemur button down event then the map is guaranteed to get the Lemur button up event when released… even if the mouse is no longer over the map.

But I’m pretty sure I suggested exactly that before. So maybe something else is wrong.

1 Like

You’re right.

I’ve readjusted my thinking a bit and utilized this code to create a container with the same length and width as the camera. This enables me to box in anywhere on the screen.

        Container checkbox = new Container();
        checkboxUI.attachChild(checkbox);
        simpleApp.getGuiNode().attachChild(checkboxUI);
        checkbox.setPreferredSize(new Vector3f(cam.getWidth(), cam.getHeight(), 0));
        checkbox.setLocalTranslation(0, cam.getHeight(), 0);
        checkbox.setBackground(new QuadBackgroundComponent(ColorRGBA.fromRGBA255(0, 0, 0, 0)));
CursorEventControl.addListenersToSpatial(simpleApp.getGuiNode().getChild("checkboxUI"), new DefaultCursorListener() {

            @Override
            protected void click(CursorButtonEvent event, Spatial target, Spatial capture) {

            }

            @Override
            public void cursorButtonEvent(CursorButtonEvent event, Spatial target, Spatial capture) {
                event.setConsumed();
                
                if (event.getButtonIndex()==0 && event.isPressed()) {
                    xDown = event.getX();
                    yDown = event.getY();
                    //System.err.println(results.getContactPoint());
                   // BOX(scenarioState.mapGrid3D.getPutPlaceBuildingCoordinates(results.getContactPoint(), new Vector2f(3, 3)));//向网格内添加建筑
                    // 鼠标按下,记录起始位置
                    startClick.set(inputManager.getCursorPosition());
                    isSelecting = true;
                } else if(event.getButtonIndex()==0 && !event.isPressed()){
//                    float x = event.getX();
//                    float y = event.getY();
//                    //它检查鼠标在释放按钮时是否移动得很小(小于3个像素)。如果是,它调用 click 方法
//                    if (Math.abs(x - xDown) < 3 && Math.abs(y - yDown) < 3) {
//                        click(event, target, capture);
//                    }
                    isSelecting = false;

                    startClick.zero();
                    endClick.zero();
                    updateSelectionBox();
                    UnitGlobalData.getInstance().BeforehandSelectedUnitToSelectedUnitList();
                    UnitGlobalData.getInstance().clearBeforehandSelectedUnit();
                }

                if (event.getButtonIndex()==1 && event.isPressed()) {
                    RightClickMapLocation = rayMap.pick("MouseButtonRight");

                    MouseButtonCommandTool.getMouseButtonCommandTool().RightMouseButtonCommand(rayMap, RightClickMapLocation);

                } else if (event.getButtonIndex()==1 && !event.isPressed()) {
                    RightClickMapLocation = rayMap.pick("MouseButtonRight");
                }
            }

            @Override
            public void cursorEntered(CursorMotionEvent event, Spatial target, Spatial capture) {
                // System.err.println(event);//.getCollision().getContactPoint()
            }

            @Override
            public void cursorExited(CursorMotionEvent event, Spatial target, Spatial capture) {

            }

            @Override
            public void cursorMoved(CursorMotionEvent event, Spatial target, Spatial capture) {
                results = event.getCollision();
                if (isSelecting) {
                    endClick.set(inputManager.getCursorPosition());
                    dragBoxPosition = startClick;
                    dragBoxSize = endClick.subtract(startClick);

                    //System.err.println(T100.CombatantsUnitList.size());
                    for (Node unit : UnitGlobalData.getInstance().getCombatantsUnitList()) {

                        //System.err.println(unit.getUserData("CombatantsUnitNode").toString());
                        SelectUnit.getSelectUnit().isNodeInDragBox(unit, cam, dragBoxPosition, dragBoxSize);
                    }

                    // 更新选择框大小和位置
                    updateSelectionBox();

                }

            }
        });
    }