From 8f4c54f75b3f10a946305b5e919ef8895f9022b6 Mon Sep 17 00:00:00 2001 From: chrisgregan Date: Tue, 2 Sep 2014 14:07:24 +0100 Subject: [PATCH] Custom editor for View object, portrait support. --- Assets/Fungus/Camera/Editor/ViewEditor.cs | 84 ++++++++++++++++++++++- Assets/Fungus/Camera/Scripts/View.cs | 16 ++--- 2 files changed, 90 insertions(+), 10 deletions(-) diff --git a/Assets/Fungus/Camera/Editor/ViewEditor.cs b/Assets/Fungus/Camera/Editor/ViewEditor.cs index 3f78ad67..bdc73f30 100644 --- a/Assets/Fungus/Camera/Editor/ViewEditor.cs +++ b/Assets/Fungus/Camera/Editor/ViewEditor.cs @@ -19,6 +19,86 @@ public class ViewEditor : Editor } } + Vector2 LookupAspectRatio(int index) + { + switch (index) + { + default: + case 1: + return new Vector2(4, 3); + case 2: + return new Vector2(3, 2); + case 3: + return new Vector2(16, 10); + case 4: + return new Vector2(17, 10); + case 5: + return new Vector2(16, 9); + case 6: + return new Vector2(2, 1); + case 7: + return new Vector2(3, 4); + case 8: + return new Vector2(2, 3); + case 9: + return new Vector2(10, 16); + case 10: + return new Vector2(10, 17); + case 11: + return new Vector2(9, 16); + case 12: + return new Vector2(1, 2); + } + } + + public override void OnInspectorGUI() + { + View t = target as View; + + EditorGUI.BeginChangeCheck(); + + string[] ratios = { "", "Landscape / 4:3", "Landscape / 3:2", "Landscape / 16:10", "Landscape / 17:10", "Landscape / 16:9", "Landscape / 2:1", "Portrait / 3:4", "Portrait / 2:3", "Portrait / 10:16", "Portrait / 10:17", "Portrait / 9:16", "Portrait / 1:2" }; + + Vector2 primaryAspectRatio = EditorGUILayout.Vector2Field(new GUIContent("Primary Aspect Ratio", "Width and height values that define the primary aspect ratio (e.g. 4:3)"), t.primaryAspectRatio); + int primaryIndex = EditorGUILayout.Popup("Select Aspect Ratio", 0, ratios); + if (primaryIndex > 0) + { + primaryAspectRatio = LookupAspectRatio(primaryIndex); + } + EditorGUILayout.Separator(); + + Vector2 secondaryAspectRatio = EditorGUILayout.Vector2Field(new GUIContent("Secondary Aspect Ratio", "Width and height values that define the primary aspect ratio (e.g. 4:3)"), t.secondaryAspectRatio); + int secondaryIndex = EditorGUILayout.Popup("Select Aspect Ratio", 0, ratios); + if (secondaryIndex > 0) + { + secondaryAspectRatio = LookupAspectRatio(secondaryIndex); + } + EditorGUILayout.Separator(); + + Color primaryColor = EditorGUILayout.ColorField(new GUIContent("Primary Color", "Color for inner primary aspect ratio rectangle"), t.primaryColor); + Color secondaryColor = EditorGUILayout.ColorField(new GUIContent("Secondary Color", "Color for outer secondary aspect ratio rectangle"), t.secondaryColor); + + if (EditorGUI.EndChangeCheck()) + { + // Avoid divide by zero errors + if (primaryAspectRatio.y == 0) + { + primaryAspectRatio.y = 1; + } + if (secondaryAspectRatio.y == 0) + { + secondaryAspectRatio.y = 1; + } + + t.primaryAspectRatio = primaryAspectRatio; + t.secondaryAspectRatio = secondaryAspectRatio; + t.primaryColor = primaryColor; + t.secondaryColor = secondaryColor; + + SceneView.RepaintAll(); + } + } + void OnSceneGUI () { View t = target as View; @@ -68,8 +148,8 @@ public class ViewEditor : Editor public static void DrawView(View view) { float height = CalculateLocalViewSize(view); - float widthA = height * view.primaryAspectRatio; - float widthB = height * view.secondaryAspectRatio; + float widthA = height * (view.primaryAspectRatio.x / view.primaryAspectRatio.y); + float widthB = height * (view.secondaryAspectRatio.x / view.secondaryAspectRatio.y); // Draw left box { diff --git a/Assets/Fungus/Camera/Scripts/View.cs b/Assets/Fungus/Camera/Scripts/View.cs index 35fed94f..d591b998 100644 --- a/Assets/Fungus/Camera/Scripts/View.cs +++ b/Assets/Fungus/Camera/Scripts/View.cs @@ -22,20 +22,20 @@ namespace Fungus * e.g. a 4:3 aspect ratio = 1.333 */ [Tooltip("Aspect ratio of the primary view rectangle. (e.g. 4:3 aspect ratio = 1.333)")] - public float primaryAspectRatio = (4f / 3f); - - /** - * Color of the primary view rectangle. - */ - [Tooltip("Color of the primary view rectangle.")] - public Color primaryColor = Color.green; + public Vector2 primaryAspectRatio = new Vector2(4, 3); /** * Aspect ratio of the secondary view rectangle. * e.g. a 2:1 aspect ratio = 2/1 = 2.0 */ [Tooltip("Aspect ratio of the secondary view rectangle. (e.g. 2:1 aspect ratio = 2.0)")] - public float secondaryAspectRatio = (2f / 1f); + public Vector2 secondaryAspectRatio = new Vector2(2, 1); + + /** + * Color of the primary view rectangle. + */ + [Tooltip("Color of the primary view rectangle.")] + public Color primaryColor = Color.green; /** * Color of the secondary view rectangle.