Skip to content

Commit

Permalink
Add StateMachine and AttributePropertyDrawer
Browse files Browse the repository at this point in the history
Refactoring, restructure project
  • Loading branch information
Bodix committed Dec 22, 2020
1 parent 81465aa commit 83665e5
Show file tree
Hide file tree
Showing 41 changed files with 330 additions and 82 deletions.
45 changes: 45 additions & 0 deletions Scripts/Editor/Drawers/AttributePropertyDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Evolunity for Unity
// Copyright © 2020 Bogdan Nikolayev <bodix321@gmail.com>
// All Rights Reserved

using System.Linq;
using Evolutex.Evolunity.Extensions;
using UnityEditor;
using UnityEngine;

namespace Evolutex.Evolunity.Editor.Drawers
{
public abstract class AttributePropertyDrawer<T> : PropertyDrawer where T : PropertyAttribute
{
protected abstract SerializedPropertyType[] SupportedTypes { get; }

public override float GetPropertyHeight(SerializedProperty property, GUIContent label) => -2f;

protected T Attribute => (T) attribute;

public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
if (IsSupported(property))
{
OnValidatedGUI(position, property, label);
}
else
{
string typeErrorText = "Type " + property.propertyType
+ " is not supported with this property attribute.\n\n"
+ "Supported property types:\n"
+ SupportedTypes.AsString(x => "- " + x, "\n");

EditorGUI.HelpBox(GUILayoutUtility.GetRect(new GUIContent(typeErrorText), EditorStyles.helpBox),
typeErrorText, MessageType.Error);
}
}

protected virtual bool IsSupported(SerializedProperty property)
{
return SupportedTypes.Any(x => x == property.propertyType);
}

protected virtual void OnValidatedGUI(Rect position, SerializedProperty property, GUIContent label) { }
}
}
3 changes: 3 additions & 0 deletions Scripts/Editor/Drawers/AttributePropertyDrawer.cs.meta

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

41 changes: 17 additions & 24 deletions Scripts/Editor/Drawers/LayerDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,37 @@
// Copyright © 2020 Bogdan Nikolayev <bodix321@gmail.com>
// All Rights Reserved

using Evolunity.Attributes;
using Evolutex.Evolunity.Attributes;
using UnityEditor;
using UnityEngine;
using UnityEngine.Assertions;

namespace Evolutex.Evolunity.Editor.Drawers
{
[CustomPropertyDrawer(typeof(LayerAttribute))]
public class LayerDrawer : PropertyDrawer
public class LayerDrawer : AttributePropertyDrawer<LayerAttribute>
{
private const string TypeErrorMessage = "Use " + nameof(LayerAttribute) + " with int";

public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
protected override SerializedPropertyType[] SupportedTypes => new[]
{
Assert.IsTrue(property.propertyType == SerializedPropertyType.Integer, TypeErrorMessage);
SerializedPropertyType.Integer
};

if (property.propertyType == SerializedPropertyType.Integer)
protected override void OnValidatedGUI(Rect position, SerializedProperty property, GUIContent label)
{
int index = property.intValue;
if (index < 0)
{
int index = property.intValue;
if (index < 0)
{
Debug.LogError("Layer index is too low '" + index + "', was set to '0'");

index = 0;
}
else if (index > 31)
{
Debug.LogError("Layer index is too high '" + index + "', was set to '31'");
Debug.LogError("Layer index is too low '" + index + "', was set to '0'");

index = 31;
}

property.intValue = EditorGUI.LayerField(position, label, index);
index = 0;
}
else
else if (index > 31)
{
EditorGUI.LabelField(position, TypeErrorMessage);
Debug.LogError("Layer index is too high '" + index + "', was set to '31'");

index = 31;
}

property.intValue = EditorGUI.LayerField(position, label, index);
}
}
}
4 changes: 2 additions & 2 deletions Scripts/Editor/Editors/CommentEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
// All Rights Reserved

using System.Linq;
using Evolunity.Components;
using Evolutex.Evolunity.Extensions;
using UnityEditor;
using UnityEngine;
using Evolutex.Evolunity.Components;
using Evolutex.Evolunity.Extensions;

namespace Evolutex.Evolunity.Editor.Editors
{
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Editor/Editors/PeriodicBehaviourEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
// All Rights Reserved

using System.Collections.Generic;
using Evolunity.Components;
using UnityEditor;
using UnityEngine;
using Evolutex.Evolunity.Components;

namespace Evolutex.Evolunity.Editor.Editors
{
Expand Down
25 changes: 0 additions & 25 deletions Scripts/Editor/ProjectConfig.cs

This file was deleted.

3 changes: 3 additions & 0 deletions Scripts/Editor/Windows.meta

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

26 changes: 26 additions & 0 deletions Scripts/Editor/Windows/Config.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Evolunity for Unity
// Copyright © 2020 Bogdan Nikolayev <bodix321@gmail.com>
// All Rights Reserved

using UnityEditor;
using UnityEngine;

namespace Evolutex.Evolunity.Editor.Windows
{
public class Config : EditorWindow
{
[MenuItem("Window/General/Config")]
private static void ShowWindow()
{
Config window = GetWindow<Config>();
window.titleContent = new GUIContent("Config");
window.Show();
}

private void OnGUI()
{
Application.targetFrameRate =
EditorGUILayout.IntSlider("Target frame rate", Application.targetFrameRate, 0, 300);
}
}
}
File renamed without changes.
2 changes: 1 addition & 1 deletion Scripts/Runtime/Attributes/LayerAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

using UnityEngine;

namespace Evolunity.Attributes
namespace Evolutex.Evolunity.Attributes
{
public class LayerAttribute : PropertyAttribute { }
}
11 changes: 7 additions & 4 deletions Scripts/Runtime/Collections/WeightQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@
using System.Linq;
using UnityEngine;

namespace Evolunity.Collections
namespace Evolutex.Evolunity.Collections
{
public class WeightQueue<T> : IEnumerable<T>
{
private readonly Queue<T> queue;

public WeightQueue(IEnumerable<T> collection, Func<T, float> weightSelector, int count)
/// <summary>
/// Fills the queue with items, with the amount of each item determined by its weight.
/// </summary>
public WeightQueue(IEnumerable<T> items, Func<T, float> weightSelector, int count)
{
queue = new Queue<T>(count);

float weightSum = collection.Select(weightSelector).Sum();
float weightSum = items.Select(weightSelector).Sum();

foreach (T item in collection)
foreach (T item in items)
for (int i = 0; i < Mathf.Round(count * (weightSelector(item) / weightSum)); i++)
queue.Enqueue(item);
}
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Runtime/Components/Comment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

using UnityEngine;

namespace Evolunity.Components
namespace Evolutex.Evolunity.Components
{
[AddComponentMenu("Toolkit/Comment")]
public class Comment : MonoBehaviour
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Runtime/Components/DevelopmentOnly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

using UnityEngine;

namespace Evolunity.Components
namespace Evolutex.Evolunity.Components
{
[AddComponentMenu("Toolkit/Development Only")]
[DisallowMultipleComponent]
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Runtime/Components/DisableMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Copyright © 2020 Bogdan Nikolayev <bodix321@gmail.com>
// All Rights Reserved

namespace Evolunity.Components
namespace Evolutex.Evolunity.Components
{
public enum DisableMethod
{
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Runtime/Components/DontDestroyOnLoad.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

using UnityEngine;

namespace Evolunity.Components
namespace Evolutex.Evolunity.Components
{
[AddComponentMenu("Toolkit/Don't Destroy On Load", 10000)]
[DisallowMultipleComponent]
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Runtime/Components/Input/InputReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace Evolunity.Components.Input
namespace Evolutex.Evolunity.Components
{
[AddComponentMenu("UI/Input Reader")]
[RequireComponent(typeof(Graphic))]
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Runtime/Components/Input/LongPressReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace Evolunity.Components.Input
namespace Evolutex.Evolunity.Components
{
[AddComponentMenu("UI/Long Press Reader")]
[RequireComponent(typeof(Graphic))]
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Runtime/Components/PeriodicBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using UnityEngine;
using UnityEngine.Events;

namespace Evolunity.Components
namespace Evolutex.Evolunity.Components
{
// TO DO:
//
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Runtime/Components/PlatformDependent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

using UnityEngine;

namespace Evolunity.Components
namespace Evolutex.Evolunity.Components
{
[AddComponentMenu("Toolkit/Platform Dependent", 10000)]
[DisallowMultipleComponent]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

using UnityEngine;

namespace Evolunity
namespace Evolutex.Evolunity.Components
{
public abstract class Singleton<T> : MonoBehaviour where T : Singleton<T>
public abstract class SingletonBehaviour<T> : MonoBehaviour where T : SingletonBehaviour<T>
{
[SerializeField]
protected bool dontDestroyOnLoad = true;
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion Scripts/Runtime/Components/Spawners/BaseSpawner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.Collections.Generic;
using UnityEngine;

namespace Evolunity.Components.Spawners
namespace Evolutex.Evolunity.Components
{
public abstract class BaseSpawner<T> : PeriodicBehaviour where T : UnityEngine.Object
{
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Runtime/Components/Spawners/Spawner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

using UnityEngine;

namespace Evolunity.Components.Spawners
namespace Evolutex.Evolunity.Components
{
[AddComponentMenu("Toolkit/Spawner")]
public class Spawner : BaseSpawner<GameObject>
Expand Down
19 changes: 14 additions & 5 deletions Scripts/Runtime/Extensions/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,24 @@ public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable

public static string AsString<T>(this IEnumerable<T> enumerable)
{
return AsString(enumerable, x => x?.ToString());
return AsString(enumerable, x => x?.ToString(), ", ");
}

public static string AsString<T>(this IEnumerable<T> enumerable, Func<T, string> selector)
{
if (enumerable.IsEmpty())
return "[ ]";
return AsString(enumerable, selector, ", ");
}

public static string AsString<T>(this IEnumerable<T> enumerable, string separator)
{
return AsString(enumerable, x => x?.ToString(), separator);
}

return "[ " + string.Join(", ", enumerable.Select(x => selector(x) ?? "null")) + " ]";
public static string AsString<T>(this IEnumerable<T> enumerable, Func<T, string> selector, string separator)
{
return enumerable.IsEmpty()
? string.Empty
: string.Join(separator, enumerable.Select(x => selector(x) ?? "null"));
}
}
}
3 changes: 3 additions & 0 deletions Scripts/Runtime/Patterns.meta

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

8 changes: 8 additions & 0 deletions Scripts/Runtime/Patterns/StateMachine.meta

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

Loading

0 comments on commit 83665e5

Please sign in to comment.