AinaVT / LethalConfig

A mod configuration menu for Lethal Company
https://thunderstore.io/c/lethal-company/p/AinaVT/LethalConfig/
GNU General Public License v3.0
15 stars 6 forks source link

Add RoO style callback to determine if a ConfigEntry can be modified at runtime #8

Closed Rune580 closed 9 months ago

Rune580 commented 9 months ago

This PR aims to implement a similar feature from RoO with some changes.

The idea is to have a callback that is ran when loading the option in the UI. The callback returns a bool and a message, if the result is true then the entry can be modified. If the result is false then it will disallow modifications and display a message provided from the callback to inform the user as to why the entry can't be modified. This allows mod devs to dynamically allow or disallow modifications based on the game state.

Rune580 commented 9 months ago

Got the components to work with the callback

        private void CreateExampleConfigs()
        {
            var intSlider = Config.Bind<int>("Example", "Int Slider", 30, new ConfigDescription("This is an integer slider. You can also type a value in the input field to the right of the slider.", new AcceptableValueRange<int>(0, 100)));
            var floatSlider = Config.Bind<float>("Example", "Float Slider", 0.0f, new ConfigDescription("This is a float slider. You can also type a value in the input field to the right of the slider.", new AcceptableValueRange<float>(-1.0f, 1.0f)));
            var floatStepSlider = Config.Bind<float>("Example", "Float Step Slider", 0.0f, new ConfigDescription("This is a float step slider. It set values in increments. You can also type a value in the input field to the right of the slider.", new AcceptableValueRange<float>(-1.0f, 1.0f)));
            var boolCheckBox = Config.Bind<bool>("Example", "Bool Checkbox", false, new ConfigDescription("This is a bool checkbox."));
            var enumDropDown = Config.Bind<TestEnum>("Example", "Enum Dropdown", TestEnum.None, new ConfigDescription("This is a enum dropdown."));
            var textInput = Config.Bind<string>("Example", "Text Input", "Example", "This is a text input field. It can have a limit of characters too.");
            var intInput = Config.Bind<int>("Example", "Int Input", 50, "This is an integer input field.");
            var floatInput = Config.Bind<float>("Example", "Float Input", 0.5f, "This is a float input field.");

            LethalConfigManager.AddConfigItem(new IntSliderConfigItem(intSlider, requiresRestart: false));
            LethalConfigManager.AddConfigItem(new FloatSliderConfigItem(floatSlider, requiresRestart: false));
            LethalConfigManager.AddConfigItem(new FloatStepSliderConfigItem(floatStepSlider, new FloatStepSliderOptions() { Step = 0.1f, RequiresRestart = false, Min = -1.0f, Max = 1.0f }));
            LethalConfigManager.AddConfigItem(new BoolCheckBoxConfigItem(boolCheckBox, requiresRestart: false));
            LethalConfigManager.AddConfigItem(new EnumDropDownConfigItem<TestEnum>(enumDropDown, false));
            LethalConfigManager.AddConfigItem(new TextInputFieldConfigItem(textInput, false));
            LethalConfigManager.AddConfigItem(new IntInputFieldConfigItem(intInput, new IntInputFieldOptions()
            {
                Max = 150,
                CanModifyCallback = CanModifyCallback
            }));
            LethalConfigManager.AddConfigItem(new FloatInputFieldConfigItem(floatInput, new FloatInputFieldOptions()
            {
                Max = 2.5f
            }));
        }

        private CanModifyResult CanModifyCallback()
        {
            return (false, "This message isn't visible in UI yet...");
        }

image

Rune580 commented 9 months ago

Think I need to improve the visibility of non mutable entries, you can kind of see it in the previous image, but it's not very obvious.

and of course I need to display the reason somewhere

Rune580 commented 9 months ago
        private CanModifyResult CanModifyCallback()
        {
            return (false, "Because I feel like it");
        }

image

Rune580 commented 9 months ago

added reason to the description for now, might need to be a tooltip or something tbh. still think it needs to be more obvious that the entry is immutable

AinaVT commented 9 months ago

added reason to the description for now, might need to be a tooltip or something tbh. still think it needs to be more obvious that the entry is immutable.

Currently there is a tooltip "system", where it'll show tooltips to any component that has a "TooltipTrigger" script to it. It's very basic tho. It doesn't have a max-width, for example, so long phrases would not make the tooltip taller (can probably be fixed through the Tooltip script).

You could add a TooltipTrigger programatically in the ModConfigController base class in its Start or Awake, and only enable it when the config is immutable.

Rune580 commented 9 months ago

image

Rune580 commented 9 months ago

I opted for adding a reference to the TooltipTrigger to each prefab instead, but otherwise it works.

One issue I found is the TooltipTrigger for the reset to default button is still active, and as a result overrides my tooltip. Should be an easy enough fix though.

Rune580 commented 9 months ago

almost ready for review, going to add to the docs before it's ready. If you need/want any changes you should probably request them soon

AinaVT commented 9 months ago

almost ready for review, going to add to the docs before it's ready. If you need/want any changes you should probably request them soon

I think it's good as it is now. Maybe the color doesn't immediatelly indicate it's disabled, but there are things i wanna do later relating to "themes" of the menu to make life easier when it comes to colors in the project, kind of standardizing things. But for now, i think with the tooltip it makes it obvious enough to anyone that tries to edit the settings that it's disabled and why.

Rune580 commented 9 months ago

in that case, this is ready for review then. I've updated the docs as well.