Lemur – Removing mouse listeners?

Hello everyone,
I’ve starrted to restructure my lemur GUI, and ran into a problem:
I have one container that is supposed to always have 9 buttons in it (even if I’m not using all of them).
Now, I want to redo the buttons with a method because the container is a popup. So I have this:
private void toggleDisplayBuildGroup(int group)
{
clearBuildGroupButtons();

        if(!guiNode.hasChild(buildGroupContainer))
        {
            guiNode.attachChild(buildGroupContainer);
            ResourceBundle langFile = program.getSettingVariables().LANG_FILE;
            
            //I can use the multiplier differently!
            float groupOffset = (group + 1) * createFontMultiplierFromWidth(program.getSettingVariables().WIDTH) * 64;
            
            buildGroupContainer.setLocalTranslation(groupOffset, program.getSettingVariables().HEIGHT - program.getSettingVariables().HEIGHT / 20, 0);
            
            Button bg01 = (Button) buildGroupContainer.getChild("buildGroup01");
            Button bg02 = (Button) buildGroupContainer.getChild("buildGroup02");
            Button bg03 = (Button) buildGroupContainer.getChild("buildGroup03");
            Button bg04 = (Button) buildGroupContainer.getChild("buildGroup04");
            Button bg05 = (Button) buildGroupContainer.getChild("buildGroup05");
            Button bg06 = (Button) buildGroupContainer.getChild("buildGroup06");
            Button bg07 = (Button) buildGroupContainer.getChild("buildGroup07");
            Button bg08 = (Button) buildGroupContainer.getChild("buildGroup08");
            Button bg09 = (Button) buildGroupContainer.getChild("buildGroup09");
            
            switch(group)
            {
                //Base group
                case 0:
                {
                    bg01.setBackground(new QuadBackgroundComponent(program.getAssetManager().loadTexture("Interface/Game/house.png")));
                    bg01.addClickCommands(new Command<Button>()
                    {
                        @Override
                        public void execute(Button source)
                        {
                            program.getStateManager().getState(GameState.class).changeItem(GameState.ITEM_HOUSE_01);
                        }
                    });
                    bg01.addMouseListener(new DefaultMouseListener()
                    {
                        @Override
                        public void mouseEntered(MouseMotionEvent event, Spatial target, Spatial capture)
                        {
                            buildingTooltip(true, langFile.getString("lumberHouse"), langFile.getString("lumberHouseStats"),
                                    langFile.getString("money") + ": 200\n5 " + langFile.getString("wood"), groupOffset);
                        }

                        @Override
                        public void mouseExited(MouseMotionEvent event, Spatial target, Spatial capture)
                        {
                            buildingTooltip(false, "", "", "", 0);
                        }         
                    });

                    bg02.setBackground(new QuadBackgroundComponent(program.getAssetManager().loadTexture("Interface/Game/warehouse.png")));
                    bg02.addClickCommands(new Command<Button>()
                    {
                        @Override
                        public void execute(Button source)
                        {
                            program.getStateManager().getState(GameState.class).changeItem(GameState.ITEM_WAREHOUSE_01);
                        }
                    });
                    bg02.addMouseListener(new DefaultMouseListener()
                    {
                        @Override
                        public void mouseEntered(MouseMotionEvent event, Spatial target, Spatial capture)
                        {
                            buildingTooltip(true, langFile.getString("warehouse"), langFile.getString("warehouseStats"),
                                    langFile.getString("money") + ": 1000\n8 " + langFile.getString("wood"), groupOffset);
                        }

                        @Override
                        public void mouseExited(MouseMotionEvent event, Spatial target, Spatial capture)
                        {
                            buildingTooltip(false, "", "", "", 0);
                        }         
                    });

                    bg03.setBackground(new QuadBackgroundComponent(program.getAssetManager().loadTexture("Interface/Game/office.png")));
                    bg03.addClickCommands(new Command<Button>()
                    {
                        @Override
                        public void execute(Button source)
                        {
                            program.getStateManager().getState(GameState.class).changeItem(GameState.ITEM_OFFICE);
                        }
                    });
                    bg03.addMouseListener(new DefaultMouseListener()
                    {
                        @Override
                        public void mouseEntered(MouseMotionEvent event, Spatial target, Spatial capture)
                        {
                            buildingTooltip(true, langFile.getString("office"), langFile.getString("officeStats"), "", groupOffset);
                        }

                        @Override
                        public void mouseExited(MouseMotionEvent event, Spatial target, Spatial capture)
                        {
                            buildingTooltip(false, "", "", "", 0);
                        }         
                    });

                    bg04.setBackground(new QuadBackgroundComponent(program.getAssetManager().loadTexture("Interface/Game/ironMine.png")));
                    bg04.addClickCommands(new Command<Button>()
                    {
                        @Override
                        public void execute(Button source)
                        {
                            program.getStateManager().getState(GameState.class).changeItem(GameState.ITEM_IRON_MINE);
                        }
                    });
                    bg04.addMouseListener(new DefaultMouseListener()
                    {
                        @Override
                        public void mouseEntered(MouseMotionEvent event, Spatial target, Spatial capture)
                        {
                            buildingTooltip(true, langFile.getString("ironMine"), langFile.getString("ironMineStats"),
                                    langFile.getString("money") + ": 600\n5 " + langFile.getString("wood"), groupOffset);
                        }

                        @Override
                        public void mouseExited(MouseMotionEvent event, Spatial target, Spatial capture)
                        {
                            buildingTooltip(false, "", "", "", 0);
                        }         
                    });
                    
                    bg05.setBackground(new QuadBackgroundComponent(program.getAssetManager().loadTexture("Interface/Game/researchCenter.png")));
                    bg05.addClickCommands(new Command<Button>()
                    {
                        @Override
                        public void execute(Button source)
                        {
                            program.getStateManager().getState(GameState.class).changeItem(GameState.ITEM_RESEARCH_CENTER);
                        }
                    });
                    bg05.addMouseListener(new DefaultMouseListener()
                    {
                        @Override
                        public void mouseEntered(MouseMotionEvent event, Spatial target, Spatial capture)
                        {
                            buildingTooltip(true, langFile.getString("researchCenter"), langFile.getString("researchCenterStats"),
                                    langFile.getString("money") + ": 800\n5 " + langFile.getString("iron"), groupOffset);
                        }

                        @Override
                        public void mouseExited(MouseMotionEvent event, Spatial target, Spatial capture)
                        {
                            buildingTooltip(false, "", "", "", 0);
                        }         
                    });
                    
                    break;
                }
                //Farm group
                case 1:
                {
                    bg01.setBackground(new QuadBackgroundComponent(program.getAssetManager().loadTexture("Interface/Game/farm.png")));
                    bg01.addClickCommands(new Command<Button>()
                    {
                        @Override
                        public void execute(Button source)
                        {
                            program.getStateManager().getState(GameState.class).changeItem(GameState.ITEM_FARM);
                        }
                    });
                    bg01.addMouseListener(new DefaultMouseListener()
                    {
                        @Override
                        public void mouseEntered(MouseMotionEvent event, Spatial target, Spatial capture)
                        {
                            buildingTooltip(true, langFile.getString("farm"), langFile.getString("farmStats"),
                                    langFile.getString("money") + ": 500\n5 " + langFile.getString("wood"), groupOffset);
                        }
            
                        @Override
                        public void mouseExited(MouseMotionEvent event, Spatial target, Spatial capture)
                        {
                            buildingTooltip(false, "", "", "", 0);
                        }         
                    });
                    
                    break;
                }
                default:
                {
                    logger.log(Level.WARNING, "Incorrect number for build group entered: {0}. Ignoring...", group);
                    break;
                }
            }
        }
        else
        {
            guiNode.detachChild(buildGroupContainer);
        }
    }
    
    //TODO: Remove mouse listeners
    private void clearBuildGroupButtons()
    {
        Button bg01 = (Button) buildGroupContainer.getChild("buildGroup01");
        Button bg02 = (Button) buildGroupContainer.getChild("buildGroup02");
        Button bg03 = (Button) buildGroupContainer.getChild("buildGroup03");
        Button bg04 = (Button) buildGroupContainer.getChild("buildGroup04");
        Button bg05 = (Button) buildGroupContainer.getChild("buildGroup05");
        Button bg06 = (Button) buildGroupContainer.getChild("buildGroup06");
        Button bg07 = (Button) buildGroupContainer.getChild("buildGroup07");
        Button bg08 = (Button) buildGroupContainer.getChild("buildGroup08");
        Button bg09 = (Button) buildGroupContainer.getChild("buildGroup09");
        
        bg01.setBackground(null);
        bg02.setBackground(null);
        bg03.setBackground(null);
        bg04.setBackground(null);
        bg05.setBackground(null);
        bg06.setBackground(null);
        bg07.setBackground(null);
        bg08.setBackground(null);
        bg09.setBackground(null);
        
        try
        {
            bg01.getClickCommands().clear();
            bg02.getClickCommands().clear();
            bg03.getClickCommands().clear();
            bg04.getClickCommands().clear();
            bg05.getClickCommands().clear();
            bg06.getClickCommands().clear();
            bg07.getClickCommands().clear();
            bg08.getClickCommands().clear();
            bg09.getClickCommands().clear();
        }
        catch(Exception e)
        {
            
        }
    }

The problem here is (as you can probably see) that I need to remove the mouse listeners to have the right hint popup (that’s the buildingTooltip method) display. I tried calling
bg01.removeMouseListener(bg01.getControl(MouseEventControl.class).getListener(DefaultMouseListener.class)),
but that doesn’t do anything. And ideas for this?

Well, removeMouseListener will work if you pass it the right mouse listener to remove. Given that lots of listeners might extend DefaultMouseListener, this call: getListener(DefaultMouseListener.class) is quite ambiguous.

The way your are managing your UI seems overly complicated and strange. It would be interesting to know what you are really trying to do so I might suggest a better way.

1 Like

You could simplify your life a lot just by having one listener instead of 9… then just keep a local field reference to it so it’s easy to remove. You could use user data to store your tooltip text and reuse the same listener for all of the buttons… or even any GUI element that has a tooltip user data string.

1 Like

This is also a very strange thing to do.

And that is not a good way to cleanup your buttons.

The whole approach feels like a bad choice was made early on and then ran with.

Thanks,
I’ll try keeping a reference to each listener and reusing it.
What I want to achieve is basically:
You have 1 button per category, click on it and you get the 3x3 menu with buttons for the actual buildings.
For your last post, I just rewrote everything for this, so I can change it.
I’m basically clearing it because it’s easier than having giant amounts of double code (I may not need all buttons).

Then hide them or don’t attach them or just build the panel when you need it with what it needs.

Setting the background to null only removes the background… which is a strange thing to do.