public TextMenuComponent(Game game, SpriteFont normalFont, SpriteFont selectFont, Texture2D textureButton)
            : base(game)
        {
            m_textureButton = textureButton;
            m_ButtonRect = new Rectangle(0, 0, m_textureButton.Width, m_textureButton.Height);

            m_regularFont = normalFont;
            m_selectedFont = selectFont;
            this.m_menuItems = new List<string>();

            m_spriteBatch = game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;

            m_inputManager = new InputManager();

            m_SelectedMenuItem = GameMenuItem.None;
        }
Esempio n. 2
0
    private GameObject CreateButton(GameMenuItem gameMenuItem)
    {
        GameObject gameObject = (GameObject)Instantiate(buttonPrefab, this.gameObject.transform);

        gameObject.name = "Button - " + gameMenuItem.Key;
        gameObject.transform.GetComponentInChildren <TextLocalizer>().formatValues = new string[] { LocalizationTable.GetLocalization(gameMenuItem.Key) };

        Button button = gameObject.GetComponent <Button>();

        button.onClick.AddListener(delegate
        {
            if (!GameController.Instance.IsModal)
            {
                DeactivateAll();
                gameMenuItem.Trigger();
            }
        });

        Action localizationFilesChangedHandler = null;

        localizationFilesChangedHandler = delegate
        {
            Transform transform;
            try
            {
                transform = gameObject.transform;
            }
            catch (MissingReferenceException)
            {
                // this sometimes gets called when gameObject doesn't exist
                // if so the gameObject has obviously been destroyed, so deregister
                // the callback
                LocalizationTable.CBLocalizationFilesChanged -= localizationFilesChangedHandler;
                return;
            }

            string menuItemKey = gameObject.name.Replace("Button - ", string.Empty);
            transform.GetComponentInChildren <TextLocalizer>().formatValues = new string[]
            {
                LocalizationTable.GetLocalization(menuItemKey)
            };
        };
        LocalizationTable.CBLocalizationFilesChanged += localizationFilesChangedHandler;

        return(gameObject);
    }
Esempio n. 3
0
    private void OnMenuItemAdded(GameMenuItem gameMenuItem, int position)
    {
        GameObject gameObject = CreateButton(gameMenuItem);

        gameObject.transform.SetSiblingIndex(position);
    }
Esempio n. 4
0
        private void AddExtensionItems()
        {
            var args  = new GetGameMenuItemsArgs();
            var toAdd = new List <GameMenuItem>();

            if (Games != null)
            {
                args.Games = Games;
            }
            else
            {
                args.Games = new List <Game>(1)
                {
                    Game
                };
            }

            foreach (var plugin in model.Extensions.Plugins.Values)
            {
                try
                {
                    var items = plugin.Plugin.GetGameMenuItems(args);
                    if (items.HasItems())
                    {
                        toAdd.AddRange(items);
                    }
                }
                catch (Exception e) when(!PlayniteEnvironment.ThrowAllErrors)
                {
                    logger.Error(e, $"Failed to get menu items from plugin {plugin.Description.Name}");
                }
            }

            foreach (var script in model.Extensions.Scripts)
            {
                if (script.SupportedMenus.Contains(Scripting.SupportedMenuMethods.GameMenu))
                {
                    try
                    {
                        var items = script.GetGameMenuItems(args);
                        if (items.HasItems())
                        {
                            foreach (var item in items)
                            {
                                var newItem = GameMenuItem.FromScriptGameMenuItem(item);
                                newItem.Action = (a) =>
                                {
                                    script.InvokeFunction(item.FunctionName, new List <object>
                                    {
                                        new ScriptGameMenuItemActionArgs
                                        {
                                            Games      = a.Games,
                                            SourceItem = item
                                        }
                                    });
                                };

                                toAdd.Add(newItem);
                            }
                        }
                    }
                    catch (Exception e) when(!PlayniteEnvironment.ThrowAllErrors)
                    {
                        logger.Error(e, $"Failed to get menu items from script {script.Name}");
                    }
                }
            }

            if (toAdd.Count > 0)
            {
                Items.Add(new Separator());
                var menuItems = new Dictionary <string, MenuItem>();
                foreach (var item in toAdd)
                {
                    object newItem = null;
                    if (item.Description == "-")
                    {
                        newItem = new Separator();
                    }
                    else
                    {
                        newItem = new MenuItem()
                        {
                            Header = item.Description,
                            Icon   = MenuHelpers.GetIcon(item.Icon)
                        };

                        if (item.Action != null)
                        {
                            ((MenuItem)newItem).Click += (_, __) =>
                            {
                                try
                                {
                                    item.Action(new GameMenuItemActionArgs
                                    {
                                        Games      = args.Games,
                                        SourceItem = item
                                    });
                                }
                                catch (Exception e) when(!PlayniteEnvironment.ThrowAllErrors)
                                {
                                    logger.Error(e, "Game menu extension action failed.");
                                    Dialogs.ShowErrorMessage(
                                        ResourceProvider.GetString("LOCMenuActionExecError") +
                                        Environment.NewLine + Environment.NewLine +
                                        e.Message, "");
                                }
                            };
                        }
                    }

                    if (item.MenuSection.IsNullOrEmpty())
                    {
                        Items.Add(newItem);
                    }
                    else
                    {
                        var parent = MenuHelpers.GenerateMenuParents(menuItems, item.MenuSection, Items);
                        parent?.Items.Add(newItem);
                    }
                }
            }
        }
        public override void Update(GameTime gameTime)
        {
            m_inputManager.BeginHandler();
            bool down = m_inputManager.IsKeyboardPress(Keys.Down);
            bool up = m_inputManager.IsKeyboardPress(Keys.Up);
            bool enter = m_inputManager.IsKeyboardPress(Keys.Enter);
            bool back = m_inputManager.IsKeyboardPress(Keys.Back);

            m_inputManager.EndHandler();

            if (down || up)
            {
                //SoundManager.m_SelectMenu.Play();
            }

            if (down)
            {
                m_selectedIndex++;
                if (m_selectedIndex == m_menuItems.Count)
                {
                    m_selectedIndex = 0;
                }
            }

            if (up)
            {
                m_selectedIndex--;
                if (m_selectedIndex == -1)
                {
                    m_selectedIndex = m_menuItems.Count - 1;
                }
            }

            if (enter)
            {
                string StrTmp = this.m_menuItems[this.m_selectedIndex];

                XmlNodeList nodeList = this.m_XmlMenu.GetElementsByTagName("Item");
                foreach (XmlNode node in nodeList)
                {
                    if (node.Attributes[0].Value.CompareTo(StrTmp) == 0)
                    {
                        List<string> tmp = this.GetMenuItems(node.ChildNodes);
                        if (tmp != null && tmp.Count > 0)
                        {
                            this.m_menuItems = tmp;
                            this.m_selectedIndex = 0;
                            m_OldSelectedMenu = StrTmp;
                        }
                    }
                }

                //////////////////////
                switch (StrTmp)
                {
                    case "Start Game":
                        m_SelectedMenuItem = GameMenuItem.StartGame;
                        break;
                    case "Edit Map":
                        m_SelectedMenuItem = GameMenuItem.EditMap;
                        break;
                    case "Option":
                        m_SelectedMenuItem = GameMenuItem.Option;
                        break;
                    case "Help":
                        m_SelectedMenuItem = GameMenuItem.Help;
                        break;
                    case "Exit Game":
                        m_SelectedMenuItem = GameMenuItem.Exit;
                        break;
                }

            }

            if (back)
            {
                XmlNodeList nodeList = this.m_XmlMenu.GetElementsByTagName("Item");
                foreach (XmlNode node in nodeList)
                {
                    if (node.Attributes[0].Value.CompareTo(m_OldSelectedMenu) == 0)
                    {
                        List<string> tmp = this.GetMenuItems(node.ParentNode.ChildNodes);
                        if (tmp != null && tmp.Count > 0)
                        {
                            this.m_menuItems = tmp;
                            this.m_selectedIndex = 0;
                            if (node.ParentNode.Attributes.Count > 0)
                            {
                                this.m_OldSelectedMenu = node.ParentNode.Attributes[0].Value;
                            }
                            else
                            {
                                this.m_OldSelectedMenu = string.Empty;
                            }
                        }
                    }
                }
            }

            base.Update(gameTime);
        }
Esempio n. 6
0
        public void ReloadGames()
        {
            gamesMenu.Items.Skip(2).ToList().ForEach(i => gamesMenu.Items.Remove(i));

            foreach (var g in App.Games)
            {
                var menuitem = new GameMenuItem(g) { Label = g.Type.LibraryName, Checked = g == App.CurrentGame };
                menuitem.Clicked += (s, e) =>
                {
                    if (((GameMenuItem)s).Game == App.CurrentGame)
                    {
                        ((GameMenuItem)s).Checked = true;
                    }
                    else
                    {
                        App.SetCurrentGame(((GameMenuItem)s).Game);
                        XSettings.Games.SetAttributeValue("selectedindex", gamesMenu.Items.IndexOf((MenuItem)s) - 2);
                        gamesMenu.Items.Skip(2).Cast<GameMenuItem>().Do(item => item.Checked = false);
                        ((GameMenuItem)s).Checked = true;
                    }
                };
                gamesMenu.Items.Add(menuitem);
            }
        }