Skip to content

Commit

Permalink
Added an experimental Attribute for
Browse files Browse the repository at this point in the history
conditional property fields
  • Loading branch information
omegaleo committed Feb 16, 2024
1 parent 7c02181 commit eab0935
Show file tree
Hide file tree
Showing 60 changed files with 3,825 additions and 3,703 deletions.
26 changes: 13 additions & 13 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# These are supported funding model platforms

github: omegaleo
patreon: # Replace with a single Patreon username
open_collective: # Replace with a single Open Collective username
ko_fi: omegaleo
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
otechie: # Replace with a single Otechie username
lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
# These are supported funding model platforms

github: omegaleo
patreon: # Replace with a single Patreon username
open_collective: # Replace with a single Open Collective username
ko_fi: omegaleo
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
otechie: # Replace with a single Otechie username
lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
120 changes: 60 additions & 60 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,60 +1,60 @@
# This .gitignore file should be placed at the root of your Unity project directory
#
# Get latest from https://github.com/github/gitignore/blob/master/Unity.gitignore
#
/[Ll]ibrary/
/[Tt]emp/
/[Oo]bj/
/[Bb]uild/
/[Bb]uilds/
/[Ll]ogs/
/[Mm]emoryCaptures/

# Asset meta data should only be ignored when the corresponding asset is also ignored
!/[Aa]ssets/**/*.meta

# Uncomment this line if you wish to ignore the asset store tools plugin
# /[Aa]ssets/AssetStoreTools*

# Autogenerated Jetbrains Rider plugin
[Aa]ssets/Plugins/Editor/JetBrains*

# Visual Studio cache directory
.vs/

# Gradle cache directory
.gradle/

# Autogenerated VS/MD/Consulo solution and project files
ExportedObj/
.consulo/
*.csproj
*.unityproj
*.sln
*.suo
*.tmp
*.user
*.userprefs
*.pidb
*.booproj
*.svd
*.pdb
*.mdb
*.opendb
*.VC.db

# Unity3D generated meta files
*.pidb.meta
*.pdb.meta
*.mdb.meta

# Unity3D generated file on crash reports
sysinfo.txt

# Builds
*.apk
*.unitypackage

# Crashlytics generated file
crashlytics-build.properties

# This .gitignore file should be placed at the root of your Unity project directory
#
# Get latest from https://github.com/github/gitignore/blob/master/Unity.gitignore
#
/[Ll]ibrary/
/[Tt]emp/
/[Oo]bj/
/[Bb]uild/
/[Bb]uilds/
/[Ll]ogs/
/[Mm]emoryCaptures/

# Asset meta data should only be ignored when the corresponding asset is also ignored
!/[Aa]ssets/**/*.meta

# Uncomment this line if you wish to ignore the asset store tools plugin
# /[Aa]ssets/AssetStoreTools*

# Autogenerated Jetbrains Rider plugin
[Aa]ssets/Plugins/Editor/JetBrains*

# Visual Studio cache directory
.vs/

# Gradle cache directory
.gradle/

# Autogenerated VS/MD/Consulo solution and project files
ExportedObj/
.consulo/
*.csproj
*.unityproj
*.sln
*.suo
*.tmp
*.user
*.userprefs
*.pidb
*.booproj
*.svd
*.pdb
*.mdb
*.opendb
*.VC.db

# Unity3D generated meta files
*.pidb.meta
*.pdb.meta
*.mdb.meta

# Unity3D generated file on crash reports
sysinfo.txt

# Builds
*.apk
*.unitypackage

# Crashlytics generated file
crashlytics-build.properties

4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"git.ignoreLimitWarning": true
{
"git.ignoreLimitWarning": true
}
52 changes: 26 additions & 26 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
## Contributing

[Based on MarcDiethelm's Contributing guide](https://gist.github.com/MarcDiethelm/7303312)

If you want to contribute to a project and make it better, your help is very welcome. Contributing is also a great way to learn more about social coding on Github, new technologies and and their ecosystems and how to make constructive, helpful bug reports, feature requests and the noblest of all contributions: a good, clean pull request.

### How to make a clean pull request

- Create a personal fork of the project on Github.
- Clone the fork on your local machine. Your remote repo on Github is called `origin`.
- Add the original repository as a remote called `upstream`.
- If you created your fork a while ago be sure to pull upstream changes into your local repository.
- Create a new branch to work on! Branch from `development` if it exists, else from `main`.
- Implement/fix your feature, comment your code.
- Follow the code style of the project, including indentation.
- If the project has tests run them!
- Write or adapt tests as needed.
- Add or change the documentation as needed.
- Squash your commits into a single commit with git's [interactive rebase](https://help.github.com/articles/interactive-rebase). Create a new branch if necessary.
- Push your branch to your fork on Github, the remote `origin`.
- From your fork open a pull request in the correct branch. Target the project's `development` branch if there is one, else go for `main`!
- ...
- Once the pull request is approved and merged you can pull the changes from `upstream` to your local repo and delete
your extra branch(es).

And last but not least: Always write your commit messages in the present tense. Your commit message should describe what the commit, when applied, does to the code – not what you did to the code.
## Contributing

[Based on MarcDiethelm's Contributing guide](https://gist.github.com/MarcDiethelm/7303312)

If you want to contribute to a project and make it better, your help is very welcome. Contributing is also a great way to learn more about social coding on Github, new technologies and and their ecosystems and how to make constructive, helpful bug reports, feature requests and the noblest of all contributions: a good, clean pull request.

### How to make a clean pull request

- Create a personal fork of the project on Github.
- Clone the fork on your local machine. Your remote repo on Github is called `origin`.
- Add the original repository as a remote called `upstream`.
- If you created your fork a while ago be sure to pull upstream changes into your local repository.
- Create a new branch to work on! Branch from `development` if it exists, else from `main`.
- Implement/fix your feature, comment your code.
- Follow the code style of the project, including indentation.
- If the project has tests run them!
- Write or adapt tests as needed.
- Add or change the documentation as needed.
- Squash your commits into a single commit with git's [interactive rebase](https://help.github.com/articles/interactive-rebase). Create a new branch if necessary.
- Push your branch to your fork on Github, the remote `origin`.
- From your fork open a pull request in the correct branch. Target the project's `development` branch if there is one, else go for `main`!
- ...
- Once the pull request is approved and merged you can pull the changes from `upstream` to your local repo and delete
your extra branch(es).

And last but not least: Always write your commit messages in the present tense. Your commit message should describe what the commit, when applied, does to the code – not what you did to the code.
60 changes: 30 additions & 30 deletions Editor/Drawers/ColoredHeaderDrawer.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
using OmegaLeo.Toolbox.Attributes;
using OmegaLeo.Toolbox.Editor.Helpers;
using UnityEditor;
using UnityEngine;

namespace Omega_Leo_Toolbox.Editor.Drawers
{
[CustomPropertyDrawer(typeof(ColoredHeaderAttribute))]
public class ColoredHeaderDrawer : DecoratorDrawer
{
public override float GetHeight()
{
var attr = attribute as ColoredHeaderAttribute;
return EditorStylesHelper.HeaderStyle().CalcHeight(new GUIContent(attr.Title), Screen.width) + Mathf.Max(attr.Padding, attr.Thickness);
}

public override void OnGUI(Rect position)
{
var attr = attribute as ColoredHeaderAttribute;

position.y += attr.Padding * .5f;
position.height = EditorStylesHelper.HeaderStyle().CalcHeight(new GUIContent(attr.Title), Screen.width);
position.height += attr.Thickness;
position.y += attr.Padding * .5f;

EditorGUI.DrawRect(position, attr.BackgroundColor);
EditorGUI.LabelField(position, attr.Title, EditorStylesHelper.HeaderStyle(attr.TextColor));

}
}
using OmegaLeo.Toolbox.Attributes;
using OmegaLeo.Toolbox.Editor.Helpers;
using UnityEditor;
using UnityEngine;

namespace Omega_Leo_Toolbox.Editor.Drawers
{
[CustomPropertyDrawer(typeof(ColoredHeaderAttribute))]
public class ColoredHeaderDrawer : DecoratorDrawer
{
public override float GetHeight()
{
var attr = attribute as ColoredHeaderAttribute;
return EditorStylesHelper.HeaderStyle().CalcHeight(new GUIContent(attr.Title), Screen.width) + Mathf.Max(attr.Padding, attr.Thickness);
}

public override void OnGUI(Rect position)
{
var attr = attribute as ColoredHeaderAttribute;

position.y += attr.Padding * .5f;
position.height = EditorStylesHelper.HeaderStyle().CalcHeight(new GUIContent(attr.Title), Screen.width);
position.height += attr.Thickness;
position.y += attr.Padding * .5f;

EditorGUI.DrawRect(position, attr.BackgroundColor);
EditorGUI.LabelField(position, attr.Title, EditorStylesHelper.HeaderStyle(attr.TextColor));

}
}
}
94 changes: 94 additions & 0 deletions Editor/Drawers/ConditionalFieldPropertyDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using System;
using OmegaLeo.Toolbox.Attributes;
using UnityEditor;
using UnityEngine;

namespace OmegaLeo.Toolbox.Editor.Drawers
{
[CustomPropertyDrawer(typeof(ConditionalFieldAttribute))]
public class ConditionalFieldPropertyDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
var condHAtt = (ConditionalFieldAttribute)attribute;
bool enabled = GetConditionalFieldAttributeResult(condHAtt, property);

bool wasEnabled = GUI.enabled;
GUI.enabled = enabled;
if (enabled)
{
EditorGUI.PropertyField(position, property, label, true);
}

GUI.enabled = wasEnabled;
}

public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
var condHAtt = (ConditionalFieldAttribute)attribute;
bool enabled = GetConditionalFieldAttributeResult(condHAtt, property);

if (enabled)
{
return EditorGUI.GetPropertyHeight(property, label);
}

// We return a height of 0 when the property is hidden
return 0f;
}

private bool GetConditionalFieldAttributeResult(ConditionalFieldAttribute condField, SerializedProperty property)
{
SerializedProperty sourcePropertyValue = property.serializedObject.FindProperty(condField.ConditionalSourceField);

if (sourcePropertyValue != null)
{
(var sourceType, var sourceValue) = GetPropertyValue(sourcePropertyValue, condField.ConditionalSourceField);

return AreValuesEqual(sourceValue, sourceType, condField.Value, condField.ValueType);
}

return true;
}

private bool AreValuesEqual(object sourceValue, Type sourceType, object condFieldValue, Type condFieldValueType)
{
if (sourceType == condFieldValueType || sourceType == typeof(Enum))
{
return sourceValue.ToString().Equals(condFieldValue.ToString(), StringComparison.OrdinalIgnoreCase);
}

return false;
}

private (Type, object) GetPropertyValue(SerializedProperty property, string propertyName)
{
SerializedProperty prop = property.serializedObject.FindProperty(propertyName);
if (prop != null)
{
switch (prop.propertyType)
{
case SerializedPropertyType.Boolean:
return (typeof(bool), prop.boolValue);
case SerializedPropertyType.Integer:
return (typeof(int), prop.intValue);
case SerializedPropertyType.Float:
return (typeof(float), prop.floatValue);
case SerializedPropertyType.String:
return (typeof(string), prop.stringValue);
case SerializedPropertyType.Enum:
return (typeof(Enum), prop.enumNames[prop.enumValueIndex]);
default:
Debug.LogWarning("ConditionalFieldAttribute: Property type not supported.");
break;
}
}
else
{
Debug.LogWarning("ConditionalFieldAttribute: Property not found.");
}

return (null, null);
}
}
}
3 changes: 3 additions & 0 deletions Editor/Drawers/ConditionalFieldPropertyDrawer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit eab0935

Please sign in to comment.