-
Notifications
You must be signed in to change notification settings - Fork 0
Settings.md
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.
this is a generic class that can be used for main primitives including :
stringbooleanintegerfloatdoubleColorVector3
other types could be added later if needed
public Setting(string key, T default_value)
- the
keyis 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);the value is a property named V to be really short to write
ex:
bool_item.V = true;
if (bool_item.V) ...
the default value can be recovered using the Reset function
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
It is used like a standard settings but are limited to double and float. The value is always clamped between min and max values
just min and max are added
public ClampSetting(string path, T default_value, T min, T max)
use it to clamp int values. It should be integrated with ClampSetting but the comparaison is slightly different from float and double
a setting that can be binded with an Enum
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
Vlike other settings - but also with the
int_valueproperty
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)