Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add functionally to load preferences/override from a toml file #77

Conversation

n1ght-hunter
Copy link
Contributor

@n1ght-hunter n1ght-hunter commented Oct 10, 2024

Example data structure

    #[derive(Debug, Clone, PartialEq, Eq, Reflect)]
    enum AwesomeOptions {
        One,
        Two,
        Three,
    }

    #[derive(Debug, Clone, PartialEq, Eq, Reflect, Resource)]
    struct AwesomePeople {
        pub name: String,
        pub age: u32,
    }

    #[derive(Debug, Clone, PartialEq, Eq, Reflect, Resource)]
    #[reflect(@SettingsType::Project, @SettingsTags(vec!["basic", "settings", "awesome"]))]
    struct AwesomeSettings {
        #[reflect(@MergeStrategy::Append)]
        pub options_list: Vec<AwseomeOptions>,
        pub people: Vec<AwesomePeople>,
    }

Equivalent toml

# struct with the key being the snake case variant of the type
[awesome_settings]
# array of unit enums
options_list = ["One", "Two", "Three"]
# ether this for vec of structs
people = [
    { name = "bob", age = 31},
]
# or this
[[awesome_settings.people]]
name = "joe"
age = 42

what is currently de serializable

# basic struct
[basic_settings]
name = "bevy_editor_settings"


# list with replace
[list_testing]
list = ["three", "four"]

# list with append
[list_testing_append]
list = [3, 4]

# top level enum
[enum_testing]
variant = "Two"

# enum in a struct
# and enum tuple
[enum_settings]
test1 = {Tuple = ["hello", 42]}

#  a struct enum on a struct
[enum_settings.test2.Struct]
name = "four"
age = 4

# a struct with a list of enums
[enum_settings_list]
settings = ["Three"]

# top level tuple struct
# adds the fields field to the tuple sturct. this is ONLY done for a top level tuple struct
[tuple_struct]
fields = [2, "two"]

# tuple struct in a struct
[struct_with_tuple]
tuple = [3, "three"]

Settings types

/// Annotation for a type to show which type of settings it belongs to.
#[derive(Debug, Clone, PartialEq, Eq, Reflect)]
pub enum SettingsType {
    /// These are settings that are saved in the os user's configuration directory. \
    /// These settings are global to the user and are not tied to a specific project. \
    /// Settings are along the lines of hotkeys etc.
    Global,
    /// Workspace preferences use the global preferences by default. End users can modify them, customizing their layout, theming and hotkeys. \
    /// The file is created when the user applies changes to their workspace preferences within the editor. \
    /// Workspace preferences can be shared between multiple projects and are not isolated to project folders.*
    Workspace,
    /// Project preference overrides are empty and stored within the project settings. \
    ///  When a project overrides a global/workspace preference, it is no longer possible to change them. \
    ///  In order to modify the preference, users must modify the project settings instead.
    /// There are two states that overrides can be in:
    /// - Inheriting - No override is set. Users can freely change the preference. Users can use what they have set within the global/workspace preferences.
    /// - Modified - When an override has been set, users can no longer change the preference without modifying the project settings. You can switch between inheriting and modified at any time without consequence.
    Project,
}

List merging strategy

#[derive(Debug, Clone, Reflect, Default)]
/// Annotation for a type to show how to merge lists when loading settings.
/// if not set, the default is to replace the existing list.
pub enum MergeStrategy {
    #[default]
    /// When Mergeing the list, the new list will replace the existing list.
    Replace,
    /// When Mergeing the list, the new list will be appended to the existing list.
    Append,
}

Tags

#[derive(Debug, Clone, Reflect)]
/// Annotation for a type to add tags to the settings. these tags can be used to filter settings in the editor.
pub struct SettingsTags(pub Vec<&'static str>);

@n1ght-hunter n1ght-hunter changed the title Dev/add plugin functionalty to settings add functionally to save and load preferences/settings Oct 10, 2024
@alice-i-cecile alice-i-cecile added C-Feature Make something new possible S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged A-Preferences Setting and saving user and project preferences labels Oct 16, 2024
@n1ght-hunter n1ght-hunter changed the title add functionally to save and load preferences/settings add functionally to load preferences/override from a toml file Oct 26, 2024
@hcabel
Copy link
Contributor

hcabel commented Nov 1, 2024

This is super exiting, I really hope we can use this soon 🤞(doesn't have to be perfect)
Let me know if you need help :)

@n1ght-hunter
Copy link
Contributor Author

update on this pr i am currently working on rewriting the code to match closer to the bevy reflect serde directory structure
https://github.com/bevyengine/bevy/tree/main/crates/bevy_reflect/src/serde

@n1ght-hunter
Copy link
Contributor Author

@hcabel @MiniaczQ other then a code cleanup this pr should be pretty close to completion. so feel free to have a look through the code and review.

@n1ght-hunter n1ght-hunter marked this pull request as ready for review December 22, 2024 05:24
@n1ght-hunter n1ght-hunter removed the S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged label Dec 22, 2024
@n1ght-hunter n1ght-hunter added this pull request to the merge queue Dec 25, 2024
Merged via the queue into bevyengine:main with commit 78d2081 Dec 25, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Preferences Setting and saving user and project preferences C-Feature Make something new possible
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants