Tanoda
PackageDefinitionEditor.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using UnityEngine;
10using UnityEditor;
11using System;
12using System.Collections.Generic;
13
14namespace Leap.Unity.Packaging {
15
16 [CustomEditor(typeof(PackageDefinition))]
17 public class PackageDefinitionEditor : DefinitionBaseEditor<PackageDefinition> {
18 private List<PackageDefinition> _childPackages;
19
20 private SerializedProperty _ignoredFiles;
21 private SerializedProperty _ignoredFolders;
22
23 protected override void OnEnable() {
24 base.OnEnable();
25
26 _childPackages = target.GetChildPackages();
27
28 _ignoredFiles = serializedObject.FindProperty("_ignoredFiles");
29 _ignoredFolders = serializedObject.FindProperty("_ignoredFolders");
30
31 hideField("_ignoredFolders");
32 hideField("_ignoredFiles");
33
34 createList("_dependantFolders", drawFolderElement);
35 createList("_dependantFiles", drawFileElement);
36 createList("_dependantPackages", drawPackageElement);
37
38 specifyCustomDecorator("_dependantFolders", prop => drawExportFolder(prop, "Build Package", "Package Export Folder"));
39 }
40
41 public override void OnInspectorGUI() {
42 base.OnInspectorGUI();
43
44 if (_childPackages.Count != 0) {
45 GUILayout.Space(EditorGUIUtility.singleLineHeight * 2);
46
47 EditorGUILayout.BeginHorizontal();
48 EditorGUILayout.LabelField("Packages that depend on this package", EditorStyles.boldLabel);
49 if (GUILayout.Button("Build All")) {
50 target.BuildAllChildPackages();
51 }
52 EditorGUILayout.EndHorizontal();
53
54 EditorGUI.BeginDisabledGroup(true);
55 foreach (var childPackage in _childPackages) {
56 EditorGUILayout.ObjectField(childPackage, typeof(PackageDefinition), false);
57 }
58 EditorGUI.EndDisabledGroup();
59
60 }
61 }
62
63 protected override void OnBuild() {
64 target.BuildPackage(interactive: true);
65 }
66
67 protected override int GetBuildMenuPriority() {
68 return 50;
69 }
70
71 protected override string GetBuildMethodName() {
72 return "BuildPackage";
73 }
74
75 private void drawFolderElement(Rect rect, SerializedProperty property) {
76 drawExplorerElement(rect, property, _ignoredFolders, EditorUtility.OpenFolderPanel);
77 }
78
79 private void drawFileElement(Rect rect, SerializedProperty property) {
80 drawExplorerElement(rect, property, _ignoredFiles, EditorUtility.OpenFilePanel);
81 }
82
83 private void drawPackageElement(Rect rect, SerializedProperty property) {
84 EditorGUI.PropertyField(rect, property, GUIContent.none);
85 }
86
87 private void drawExplorerElement(Rect rect, SerializedProperty property, SerializedProperty ignoredList, Func<string, string, string, string> openAction) {
88 Rect left, middle, right;
89
90 rect.SplitHorizontallyWithRight(out rect, out right, 100);
91 rect.SplitHorizontallyWithRight(out left, out middle, EditorGUIUtility.singleLineHeight);
92
93 EditorGUI.TextField(left, property.stringValue);
94
95 bool isIncluded = true;
96 for (int i = 0; i < ignoredList.arraySize; i++) {
97 if (ignoredList.GetArrayElementAtIndex(i).stringValue == property.stringValue) {
98 isIncluded = false;
99 break;
100 }
101 }
102
103 bool shouldBeIncluded = EditorGUI.Toggle(middle, isIncluded);
104
105 if (shouldBeIncluded != isIncluded) {
106 if (shouldBeIncluded) {
107 for (int i = ignoredList.arraySize; i-- != 0;) {
108 if (ignoredList.GetArrayElementAtIndex(i).stringValue == property.stringValue) {
109 ignoredList.DeleteArrayElementAtIndex(i);
110 }
111 }
112 } else {
113 ignoredList.InsertArrayElementAtIndex(0);
114 ignoredList.GetArrayElementAtIndex(0).stringValue = property.stringValue;
115 }
116 }
117
118 if (GUI.Button(right, "Change")) {
119 string chosenFolder = openAction("Select Folder", Application.dataPath, "");
120 if (!string.IsNullOrEmpty(chosenFolder)) {
121 string relativePath = Utils.MakeRelativePath(Application.dataPath, chosenFolder);
122 if (!string.IsNullOrEmpty(relativePath) && !relativePath.StartsWith("..")) {
123 if (relativePath != property.stringValue) {
124 property.stringValue = relativePath;
125 }
126 }
127 }
128 }
129 }
130 }
131}
ReorderableList createList(string propertyName, Action< Rect, SerializedProperty > drawMethod)
void drawExportFolder(SerializedProperty prop, string buildText, string label)