Jump to content
LaunchBox Community Forums

Hiding Custom Menu Option Depending on Game


Recommended Posts

Hi, I have mentioned this a couple of times to no response, so after spending a long time trying to figure it out, I thought I would make a new topic about it as it's starting to annoy me. Essentially what I am trying to do is have a custom menu item (IGameMenuItem) be hidden or removed when a game that does not meet a criteria is required. I am aware that there is no built in method in the plugin system that will conveniently do this (with Show In Launch Box, only appearing to trigger on startup) and I am also aware this may be deliberate. But I feel like this should be able to work somehow, without modifying launchbox, so I attempted to access the selected games context menus directly, using the built in form features. My problem was I couldn't find where the controls were for the games, I was able to access the view that the games appear in, but this control had no relevant child controls. This has left me a little baffled as to how the primary form window in Launchbox is actually structured, and how to manipulate it. Adding a reference to the control or context menu for each game to the plugin system would be extremely useful for more advanced users, but I feel that there is probably a way to solve my problem under the current system and I'm just missing it. Knowing if this can be achieved and how to achieve it would be much appreciated. And if manipulation of the context menu has indeed been deliberately left out, and not just overlooked, I would like to know why. Launchbox is really really great software, but I have always felt it has always had these small, minor but annoying limitations, that usually make doing certain things cumbersome, and it appears this also extends to coding with the plugin system.

Link to comment
Share on other sites

Sorry that I cannot help you with any answers, I simply do not know anything about plugin coding.

The best I can offer is a link to the Launchbox Discord channel where we have a sub channel for plugin development and there are usually a couple of very knowledgeable people in such matters that might be able to help you out with it.

https://discord.gg/X3Cwsyw

Link to comment
Share on other sites

The initial post I made does seem a bit unclear reading it back, so basically all I am trying to do is hide the IGameMenuItem when it is not valid (i.e. greyed out). I want to do something like shown below, however am unsure how to actually go about hiding/showing the Menu Item. I will also check out the discord channel.

 

        public bool GetIsValidForGame(IGame selectedGame)
        {
            var emulator = PluginHelper.DataManager.GetEmulatorById(selectedGame.EmulatorId);
            if (emulator != null && emulator.Title == "PCSX2") {
              	// Show Menu Item 
                return true;
            }
           
          	// Hide Menu Item
            return false;
            
        }

 

Edited by alec100_94
Link to comment
Share on other sites

Looks like that should work. When your method returns false, the menu item will be hidden in BigBox and disabled (grayed out) in LaunchBox. I don't think you can actually hide the menu item in LaunchBox; only disable it. Yeah check out the discord channel to see if someone has a fix, if what you're trying to do is actually hide the menu item in LB.

Link to comment
Share on other sites

I know the above code works, I am actually trying to hide the item (not just disable). I was trying to do it by accessing the children of the main form, I was able to access the container window (image view), but not the individual games (hence not the context menu). To me no way of manipulating the context menus, if the developer desires, seems like a pretty big omission and could lead to some much cleaner plugins if used properly.

I have joined the discord channel and posted in the plugin development section, which seems like an awesome community of LB developers. Hopefully someone on there will at least be able to clarify if it is even possible or not with the current system.

Link to comment
Share on other sites

Yes, I agree -- ideally the plugin system could be updated to allow manipulation of the context menus in LB and the menus in BB. Perhaps at the very least, IGameMenuItem could allow the developer to specify whether they'd want the menu item disabled or just hidden altogether. I hope you find the answer on discord. If you do, please make a quick reply here saying so.

Link to comment
Share on other sites

Miraculously after spending about six hours trying to figure this out I was able to come up with a solution that appears to mostly work, with the small caveat that you can see the item being hidden/displayed very briefly (if used like described above), this may be possible to fix in the future, but for now this seems to work fine. The solution is quite complex (making use of reflection), but is wrapped up into an easy to use function that can be copy + pasted into virtually any IGameMenuItemPlugin. If there is a bug that I've not discovered yet, report it and it will get fixed. It seems most things with the context menu (and probably other things) can be done if you know what your doing. Though better built in support for this would be nice, I kind of understand why it's not in there.

        private void HideContextMenuItem(bool hide)
        {
            // Gets the context menu from Launchbox Main Form
            var contextMenuStripField = PluginHelper.LaunchBoxMainForm.GetType().GetField("contextMenuStrip", BindingFlags.NonPublic | BindingFlags.Instance);
            var contextMenuStrip = (ContextMenuStrip)contextMenuStripField.GetValue(PluginHelper.LaunchBoxMainForm);

            // Hides or shows the menu item with this plugins caption
            for (int i = 0; i < contextMenuStrip.Items.Count; ++i)
            {
                if (contextMenuStrip.Items[i].Text == Caption)
                {
                    contextMenuStrip.Items[i].Visible = !hide;
                    break;
                }
            }


            // If all plugins in context menu are hidden, also hide the last seperator
            for (int i = contextMenuStrip.Items.Count - 1, itemsHidden = 0; i > -1; --i)
            {
                if (contextMenuStrip.Items[i].GetType() == typeof(ToolStripSeparator))
                {
                    contextMenuStrip.Items[i].Visible = (itemsHidden == contextMenuStrip.Items.Count - i - 1) ? false : true;
                    break;
                }

                if (!contextMenuStrip.Items[i].Visible)
                    itemsHidden++;
            }

            // Write the changes to the context menu back to Launchbox Main Form
            contextMenuStripField.SetValue(PluginHelper.LaunchBoxMainForm, contextMenuStrip);
        }

 

Edited by alec100_94
  • Like 1
Link to comment
Share on other sites

I fixed a bug in the above code, where it may not have hidden the separator properly. It should be pretty much ready for plug and play use (with the minor annoyance of sometimes being able to see items hide, if anyone knows a solution that would be great). This got me thinking that there are a lot of really cool things Launchbox has the potential to do, but not really particularly easily through the exposed plugin system. If @Jason Carr is not planning on extending the plugin system any time soon, then I was thinking of starting a community created helper library, primarily focused on reflection (to access non-exposed methods/fields), allowing a less experienced programmer to do some of the more complicated tasks in Launchbox, and ultimately make the plugins they want.

Link to comment
Share on other sites

@UnderwoodNC, you could definitely try and implement it like that if you wanted and see what results you get, and the current method isn't as visually jarring as it sounds. I'll mess around with it more when I'm developing my next plugin, as the plugin I am currently working on (PCSX2 Configurator) just has the item hidden all the time with no action. If your confused to how that is useful look for my next release of said plugin, which will be released with full source code.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...