+ editor utils

This commit is contained in:
2016-08-29 14:47:28 +07:00
parent a3748a2370
commit 1998739eb3
5 changed files with 69 additions and 51 deletions

View File

@@ -0,0 +1,22 @@
using UnityEngine;
using UnityEditor;
using System;
namespace FlashTools.Internal {
public static class SwfEditorUtils {
public static void DoWithMixedValue(bool mixed, Action act) {
var last_show_mixed_value = EditorGUI.showMixedValue;
EditorGUI.showMixedValue = mixed;
act();
EditorGUI.showMixedValue = last_show_mixed_value;
}
public static void DoWithEnabledGUI(bool enabled, Action act) {
var last_gui_enabled = GUI.enabled;
GUI.enabled = enabled;
act();
GUI.enabled = last_gui_enabled;
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 80bbe5b795df64829b002ddb92fb7a37
timeCreated: 1472455940
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -7,12 +7,11 @@ namespace FlashTools.Internal {
SwfManager _manager = null;
void DrawAnimationCount() {
var last_gui_enabled = GUI.enabled;
GUI.enabled = false;
EditorGUILayout.IntField(
"Animation count",
_manager.AllAnimationCount);
GUI.enabled = last_gui_enabled;
SwfEditorUtils.DoWithEnabledGUI(false, () => {
EditorGUILayout.IntField(
"Animation count",
_manager.AllAnimationCount);
});
}
// ---------------------------------------------------------------------

View File

@@ -110,36 +110,31 @@ namespace FlashTools.Internal.SwfEditorTools {
}
}
static void DoWithMixedValue(bool mixed, Action act) {
var last_show_mixed_value = EditorGUI.showMixedValue;
EditorGUI.showMixedValue = mixed;
act();
EditorGUI.showMixedValue = last_show_mixed_value;
}
public override void OnGUI(
Rect position, SerializedProperty property, GUIContent label)
{
if ( property.propertyType == SerializedPropertyType.String ) {
ValidateProperty(property);
DoWithMixedValue(property.hasMultipleDifferentValues, () => {
var all_sorting_layers = GetAllSortingLayers(true);
var sorting_layer_index = EditorGUI.Popup(
position,
label,
property.hasMultipleDifferentValues
? all_sorting_layers.FindIndex(p => string.IsNullOrEmpty(p))
: all_sorting_layers.FindIndex(p => p == property.stringValue),
all_sorting_layers.Select(p => new GUIContent(p)).ToArray());
var new_value = all_sorting_layers[sorting_layer_index];
if ( !string.IsNullOrEmpty(new_value) ) {
if ( property.hasMultipleDifferentValues ) {
property.stringValue = string.Empty;
SwfEditorUtils.DoWithMixedValue(
property.hasMultipleDifferentValues, () =>
{
var all_sorting_layers = GetAllSortingLayers(true);
var sorting_layer_index = EditorGUI.Popup(
position,
label,
property.hasMultipleDifferentValues
? all_sorting_layers.FindIndex(p => string.IsNullOrEmpty(p))
: all_sorting_layers.FindIndex(p => p == property.stringValue),
all_sorting_layers.Select(p => new GUIContent(p)).ToArray());
var new_value = all_sorting_layers[sorting_layer_index];
if ( !string.IsNullOrEmpty(new_value) ) {
if ( property.hasMultipleDifferentValues ) {
property.stringValue = string.Empty;
}
property.stringValue = new_value;
property.serializedObject.ApplyModifiedProperties();
}
property.stringValue = new_value;
property.serializedObject.ApplyModifiedProperties();
}
});
});
} else {
EditorGUI.LabelField(position, label.text, "Use SwfSortingLayer with string attribute.");
}
@@ -194,13 +189,6 @@ namespace FlashTools.Internal.SwfEditorTools {
}
}
static void DoWithMixedValue(bool mixed, Action act) {
var last_show_mixed_value = EditorGUI.showMixedValue;
EditorGUI.showMixedValue = mixed;
act();
EditorGUI.showMixedValue = last_show_mixed_value;
}
public override void OnGUI(
Rect position, SerializedProperty property, GUIContent label)
{
@@ -209,15 +197,17 @@ namespace FlashTools.Internal.SwfEditorTools {
var bool_prop = FindNextBoolProperty(property, attr.BoolProp);
var need_pow2 = (bool_prop != null && (bool_prop.boolValue || bool_prop.hasMultipleDifferentValues));
ValidateProperty(property, need_pow2, attr.MinPow2, attr.MaxPow2);
DoWithMixedValue(property.hasMultipleDifferentValues, () => {
if ( need_pow2 ) {
var values = GenPowerOfTwoValues(attr.MinPow2, attr.MaxPow2);
var vnames = values.Select(p => new GUIContent(p.ToString())).ToArray();
EditorGUI.IntPopup(position, property, vnames, values, label);
} else {
EditorGUI.PropertyField(position, property, label, true);
}
});
SwfEditorUtils.DoWithMixedValue(
property.hasMultipleDifferentValues, () =>
{
if ( need_pow2 ) {
var values = GenPowerOfTwoValues(attr.MinPow2, attr.MaxPow2);
var vnames = values.Select(p => new GUIContent(p.ToString())).ToArray();
EditorGUI.IntPopup(position, property, vnames, values, label);
} else {
EditorGUI.PropertyField(position, property, label, true);
}
});
} else {
EditorGUI.LabelField(position, label.text, "Use SwfPowerOfTwoIf with integer attribute.");
}

View File

@@ -8,11 +8,6 @@ using UnityEditor;
namespace FlashTools {
[ExecuteInEditMode, DisallowMultipleComponent]
public class SwfManager : MonoBehaviour {
// ---------------------------------------------------------------------
//
// Properties
//
// ---------------------------------------------------------------------
HashSet<SwfAnimation> _animations = new HashSet<SwfAnimation>();
HashSet<SwfAnimationController> _controllers = new HashSet<SwfAnimationController>();