Esempio n. 1
0
        /// <summary>
        /// Setup the menu
        /// </summary>
        private void InitializeMenu()
        {
            if (editorMenu == null)
            {
                editorMenu = new Menu(ScriptName, "Editor");

                // When the value of a MenuDynamicListItem is changed
                editorMenu.OnDynamicListItemCurrentItemChange += (menu, dynamicListItem, oldValue, newValue) => {
                    string id = dynamicListItem.ItemData as string;
                    MenuPresetValueChanged?.Invoke(id, newValue);
                };

                editorMenu.OnMenuOpen += (menu) => {
                    UpdateWheelValues();
                    UpdateEditorMenu((currentPreset.WheelSize == 0.0f), (currentPreset.WheelWidth == 0.0f));
                };

                // When a MenuItem is selected
                editorMenu.OnItemSelect += (menu, menuItem, itemIndex) => {
                    // If the selected item is the reset button
                    if (menuItem.ItemData as string == ResetID)
                    {
                        MenuResetPresetButtonPressed.Invoke(this, EventArgs.Empty);
                    }
                    else if (menuItem.ItemData as string == SaveID)
                    {
                        SavePreset();
                    }
                    else if (menuItem.ItemData as string == LoadID)
                    {
                        LoadPreset();
                        UpdateWheelValues();
                    }
                };
            }

            UpdateEditorMenu();

            if (menuController == null)
            {
                menuController = new MenuController();
                MenuController.AddMenu(editorMenu);
                MenuController.MenuAlignment = MenuController.MenuAlignmentOption.Right;
                MenuController.MenuToggleKey = (Control)toggleMenu;
                MenuController.EnableMenuToggleKeyOnController = false;
                MenuController.MainMenu = editorMenu;
            }
        }
        /// <summary>
        /// Invoked when the value of a dynamic list item from the main editor menu is changed
        /// </summary>
        /// <param name="menu"></param>
        /// <param name="dynamicListItem"></param>
        /// <param name="oldValue"></param>
        /// <param name="newValue"></param>
        private void EditorMenu_OnDynamicListItemCurrentItemChange(Menu menu, MenuDynamicListItem dynamicListItem, string oldValue, string newValue)
        {
            // If the sender isn't the main editor menu...
            if (menu != EditorMenu)
            {
                return;
            }

            // If item data is not the expected one...
            if (!(dynamicListItem.ItemData is BaseFieldInfo fieldInfo))
            {
                return;
            }

            // Get field name which is controlled by this dynamic list item
            string fieldName = fieldInfo.Name;

            // Notify the value is changed so the preset can update...
            MenuPresetValueChanged?.Invoke(fieldName, newValue, dynamicListItem.Text);
        }
        /// <summary>
        /// Invoked when a <see cref="MenuDynamicListItem"/> from the main editor menu is selected
        /// </summary>
        /// <param name="menu"></param>
        /// <param name="dynamicListItem"></param>
        /// <param name="currentItem"></param>
        private async void EditorMenu_OnDynamicListItemSelect(Menu menu, MenuDynamicListItem dynamicListItem, string currentItem)
        {
            // If the item doesn't control any preset field...
            if (!(dynamicListItem.ItemData is BaseFieldInfo fieldInfo))
            {
                return;
            }

            //var currentItem = dynamicListItem.CurrentItem;
            var    itemText  = dynamicListItem.Text;
            string fieldName = fieldInfo.Name;
            var    fieldType = fieldInfo.Type;

            // Get the user input value
            string text = await GetOnScreenString(currentItem);

            // Check if the value can be accepted
            if (fieldType == FieldType.FloatType)
            {
                var min = (fieldInfo as FieldInfo <float>).Min;
                var max = (fieldInfo as FieldInfo <float>).Max;

                if (float.TryParse(text, out float newvalue))
                {
                    if (newvalue >= min && newvalue <= max)
                    {
                        dynamicListItem.CurrentItem = newvalue.ToString();
                        // Notify the value is changed so the preset can update...
                        MenuPresetValueChanged?.Invoke(fieldName, newvalue.ToString("F3"), itemText);
                    }
                    else
                    {
                        Screen.ShowNotification($"{ScriptName}: Value out of allowed limits for ~b~{fieldName}~w~, Min:{min}, Max:{max}");
                    }
                }
                else
                {
                    Screen.ShowNotification($"{ScriptName}: Invalid value for ~b~{fieldName}~w~");
                }
            }
            else if (fieldType == FieldType.IntType)
            {
                var min = (fieldInfo as FieldInfo <int>).Min;
                var max = (fieldInfo as FieldInfo <int>).Max;

                if (int.TryParse(text, out int newvalue))
                {
                    if (newvalue >= min && newvalue <= max)
                    {
                        dynamicListItem.CurrentItem = newvalue.ToString();
                        // Notify the value is changed so the preset can update...
                        MenuPresetValueChanged?.Invoke(fieldName, newvalue.ToString(), itemText);
                    }
                    else
                    {
                        Screen.ShowNotification($"{ScriptName}: Value out of allowed limits for ~b~{fieldName}~w~, Min:{min}, Max:{max}");
                    }
                }
                else
                {
                    Screen.ShowNotification($"{ScriptName}: Invalid value for ~b~{fieldName}~w~");
                }
            }
            else if (fieldType == FieldType.Vector3Type)
            {
                var min = (fieldInfo as FieldInfo <Vector3>).Min;
                var max = (fieldInfo as FieldInfo <Vector3>).Max;

                var minValueX = min.X;
                var minValueY = min.Y;
                var minValueZ = min.Z;
                var maxValueX = max.X;
                var maxValueY = max.Y;
                var maxValueZ = max.Z;

                if (itemText.EndsWith("_x"))
                {
                    if (float.TryParse(text, out float newvalue))
                    {
                        if (newvalue >= minValueX && newvalue <= maxValueX)
                        {
                            dynamicListItem.CurrentItem = newvalue.ToString("F3");
                            // Notify the value is changed so the preset can update...
                            MenuPresetValueChanged?.Invoke(fieldName, newvalue.ToString("F3"), itemText);
                        }
                        else
                        {
                            Screen.ShowNotification($"{ScriptName}: Value out of allowed limits for ~b~{itemText}~w~, Min:{minValueX}, Max:{maxValueX}");
                        }
                    }
                    else
                    {
                        Screen.ShowNotification($"{ScriptName}: Invalid value for ~b~{itemText}~w~");
                    }
                }
                else if (itemText.EndsWith("_y"))
                {
                    if (float.TryParse(text, out float newvalue))
                    {
                        if (newvalue >= minValueY && newvalue <= maxValueY)
                        {
                            dynamicListItem.CurrentItem = newvalue.ToString("F3");
                            // Notify the value is changed so the preset can update...
                            MenuPresetValueChanged?.Invoke(fieldName, newvalue.ToString("F3"), itemText);
                        }
                        else
                        {
                            Screen.ShowNotification($"{ScriptName}: Value out of allowed limits for ~b~{itemText}~w~, Min:{minValueY}, Max:{maxValueY}");
                        }
                    }
                    else
                    {
                        Screen.ShowNotification($"{ScriptName}: Invalid value for ~b~{itemText}~w~");
                    }
                }
                else if (itemText.EndsWith("_z"))
                {
                    if (float.TryParse(text, out float newvalue))
                    {
                        if (newvalue >= minValueZ && newvalue <= maxValueZ)
                        {
                            dynamicListItem.CurrentItem = newvalue.ToString("F3");
                            // Notify the value is changed so the preset can update...
                            MenuPresetValueChanged?.Invoke(fieldName, newvalue.ToString("F3"), itemText);
                        }
                        else
                        {
                            Screen.ShowNotification($"{ScriptName}: Value out of allowed limits for ~b~{itemText}~w~, Min:{minValueZ}, Max:{maxValueZ}");
                        }
                    }
                    else
                    {
                        Screen.ShowNotification($"{ScriptName}: Invalid value for ~b~{itemText}~w~");
                    }
                }
            }
        }