Skip to content

Settings.md

cfloutier edited this page Apr 27, 2024 · 4 revisions

Settings is used for 2 purposes :

  • Save user preferences
  • Bind the internal mod values with UI controls

All internal values of the mod could used one setting class to keep data saved and shared with UI.

Setting

this is a generic class that can be used for main primitives including :

  • string
  • boolean
  • integer
  • float
  • double
  • Color
  • Vector3

other types could be added later if needed

constructor

public Setting(string key, T default_value)
  • the key is used to define where the value will be saved in the settings file. If key is null or empty it just won't be saved, the default value will be the same on startup
  • the default_value, defines the initial value, it will be overloaded on settings first load.

ex : Define your setting value like that

public Setting<bool> bool_item = new Setting<bool>("my_settings.bool_item", true);

value

the value is a property named V to be really short to write

ex:

   bool_item.V = true;

   if (bool_item.V) ...

Reset

the default value can be recovered using the Reset function

Binding

use the Bind function that can be found on all K2UI controls to bind values with the UI

ex :

  // in your Window's constructor
  // panel is the root VisualElement
  // the K2Toggle is defined with the name `bool_settings` 
  panel.Q<K2Toggle>("bool_settings").Bind(bool_item);
  • The binding update the UI with the current setting value
  • It is a 2 ways binding : settings value is updated when the user use the UI and the Ui is updated when the setting value is changed

ClampSetting

It is used like a standard settings but are limited to double and float. The value is always clamped between min and max values

constructor

just min and max are added

public ClampSetting(string path, T default_value, T min, T max)

ClampSettingInt

use it to clamp int values. It should be integrated with ClampSetting but the comparaison is slightly different from float and double

ClampSettingInt

EnumSetting

a setting that can be binded with an Enum

constructor

ex :

    public enum MyEnum
    {
        Down,
        Center,
        Up,
        Left,
        Right
    }

    public class MySettingsClass
    {
        public EnumSetting<MyEnum> enum_item = new EnumSetting<MyEnum>("my_settings.enum_item", MyEnum.Center);
    }

you can acces the value with

  • the property V like other settings
  • but also with the int_value property

Load and Save to disk

Use the Load and Save function.

When loaded the event onloaded_event is invoked

Use the Init function to load and add an autosave behavior. The full settings will be saved to disc on each second (only if any value have changed)

Clone this wiki locally