void DrawOnlineFormula(FormulaData formula) { var niceName = ObjectNames.NicifyVariableName(formula.name); //Button is disabled until formula is downloaded var guiEnabled = GUI.enabled; GUI.enabled = false; GUILayout.BeginHorizontal(); GUILayout.Button(new GUIContent(niceName, niceName), GUILayout.MaxWidth(this.position.width - 60)); GUI.enabled = guiEnabled; DrawDownloadButton(downloadButtonGUIContent, formula); DrawOptionsButton(formula); GUILayout.EndHorizontal(); }
void LoadLocalFormulas() { var methodList = new List <MethodInfo>(Utils.GetAllFormulaMethodsWithAttribute()); foreach (var method in methodList) { //Process only if valid method was found if (method != null) { var formulaName = method.DeclaringType.Name; var formula = formulaDataStore.FormulaData.Find(x => x.name == formulaName); //If formula doesn't exist in formulaDataStore if (formula == null) { formula = new FormulaData(); formula.name = formulaName; formula.projectFilePath = Constants.formulasFolderUnityPath + formulaName + ".cs"; formulaDataStore.FormulaData.Add(formula); EditorUtility.SetDirty(formulaDataStore); } //Always write these values, even if formula already exists formula.localFileExists = new FileInfo(Utils.GetFullPathFromAssetsPath(formula.projectFilePath)).Exists; formula.methodInfo = method; var formulaAttribute = Utils.GetFormulaAttributeForMethodInfo(method); if (formulaAttribute != null) { formula.niceName = formulaAttribute.name; formula.tooltip = formulaAttribute.tooltip; formula.author = formulaAttribute.author; } } } //If there are local formulas in data store that point to local files that don't exist //and also don't have downloadURLs, remove them for (int i = formulaDataStore.FormulaData.Count - 1; i >= 0; i--) { var formulaData = formulaDataStore.FormulaData[i]; var fullPath = Utils.GetFullPathFromAssetsPath(formulaData.projectFilePath); var fi = new FileInfo(fullPath); if (!fi.Exists && string.IsNullOrEmpty(formulaData.downloadURL)) { formulaDataStore.FormulaData.RemoveAt(i); EditorUtility.SetDirty(formulaDataStore); } } }
void LoadLocalFormulas() { var editorFormulasDirectory = new DirectoryInfo(Utils.GetFullPathFromAssetsPath(Constants.formulasFolderUnityPath)); var files = new List <FileInfo>(editorFormulasDirectory.GetFiles()); //Remove all files that don't have a .cs extension files.RemoveAll(x => !x.Extension.Equals(".cs", StringComparison.InvariantCultureIgnoreCase)); foreach (var file in files) { var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file.FullName); var formula = formulaDataStore.FormulaData.Find(x => x.name == fileNameWithoutExtension); var methodInfo = Utils.GetFormulaMethod(fileNameWithoutExtension); //Process only if valid method was found if (methodInfo != null) { //If formula doesn't exist in formulaDataStore if (formula == null) { formula = new FormulaData(); formula.name = fileNameWithoutExtension; formula.projectFilePath = Constants.formulasFolderUnityPath + file.Name; formulaDataStore.FormulaData.Add(formula); EditorUtility.SetDirty(formulaDataStore); } formula.localFileExists = new FileInfo(Utils.GetFullPathFromAssetsPath(formula.projectFilePath)).Exists; formula.methodInfo = methodInfo; } } //If there are local formulas in data store that point to local files that don't exist //and also don't have downloadURLs, remove them for (int i = formulaDataStore.FormulaData.Count - 1; i >= 0; i--) { var formulaData = formulaDataStore.FormulaData[i]; var fullPath = Utils.GetFullPathFromAssetsPath(formulaData.projectFilePath); var fi = new FileInfo(fullPath); if (!fi.Exists && string.IsNullOrEmpty(formulaData.downloadURL)) { formulaDataStore.FormulaData.RemoveAt(i); EditorUtility.SetDirty(formulaDataStore); } } }
void DrawOptionsButton(FormulaData formula) { if (GUILayout.Button(optionsButtonGUIContent, GUILayout.MaxWidth(20), GUILayout.MaxHeight(18))) { var menu = new GenericMenu(); if (formula.localFileExists) { menu.AddItem(new GUIContent("Open in External Script Editor"), false, OpenFormulaInExternalScriptEditor, formula); } menu.AddItem(new GUIContent("Go to GitHub page"), false, GoToFormulaDownloadURL, formula); menu.AddItem(new GUIContent("Hide"), formula.hidden, ToggleFormulaHidden, formula); if (formula.localFileExists) { menu.AddItem(new GUIContent("Delete"), false, DeleteFormula, formula); } menu.ShowAsContext(); } }
public void DownloadFormula(FormulaData formulaData, bool isUpdateCheck) { DebugLog("Download Formula: " + formulaData.name + " isUpdateCheck: " + isUpdateCheck); if (downloadFormulaActions.Any(x => x.formulaData == formulaData)) { Debug.Log("Formula already being downloaded"); return; } var urlToUse = isUpdateCheck ? formulaData.apiURL : formulaData.downloadURL; if (string.IsNullOrEmpty(urlToUse)) { formulaData.UpdateCheckTimeUTC = DateTime.UtcNow; EditorUtility.SetDirty(formulaDataStore); DebugLog("Formula's url to use is null or empty, not downloading."); return; } var downloadFormulaAction = new DownloadFormulaAction(); downloadFormulaAction.formulaData = formulaData; downloadFormulaAction.isUpdateCheck = isUpdateCheck; if (isUpdateCheck) { formulaData.UpdateCheckTimeUTC = DateTime.UtcNow; EditorUtility.SetDirty(formulaDataStore); } var request = WebRequest.Create(new Uri(urlToUse)) as HttpWebRequest; downloadFormulaAction.request = request; request.UserAgent = "EditorFormulas"; request.Method = "GET"; request.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore); request.IfModifiedSince = formulaData.DownloadTimeUTC; request.BeginGetResponse(HandleAsync_DownloadFormula, downloadFormulaAction); downloadFormulaActions.Add(downloadFormulaAction); }
void DrawDownloadButton(GUIContent defaultContent, FormulaData formula) { var guiContent = defaultContent; var diffInMilliseconds = DateTime.UtcNow.Subtract(formula.DownloadTimeUTC).TotalMilliseconds; bool compilingOrDownloadingFormula = (EditorApplication.isCompiling && diffInMilliseconds < 20000) || webHelper.IsDownloadingFormula(formula); //If the formula is in WebHelper's download queue or //the editor is compiling and download was less than 20 seconds ago, show spinner if (compilingOrDownloadingFormula) { int waitSpinIndex = Mathf.FloorToInt(((float)(diffInMilliseconds % 2000d) / 2000f) * 12f); guiContent = waitSpinGUIContents[waitSpinIndex]; doRepaint = true; } if (GUILayout.Button(guiContent, GUILayout.MaxWidth(24), GUILayout.MaxHeight(18))) { //Button should do nothing if compiling or downloading formula if (!compilingOrDownloadingFormula) { webHelper.DownloadFormula(formula, false); } } }
public bool IsDownloadingFormula(FormulaData formulaData) { return(downloadFormulaActions.Any(x => x.formulaData == formulaData)); }
void ProcessGetOnlineFormulasResponse() { if (getOnlineFormulasResponse != null || useLastGetOnlineFormulasResponse) { string responseStreamString = null; DebugLog("Get online formulas response"); if (useLastGetOnlineFormulasResponse) { DebugLog("Using last response"); useLastGetOnlineFormulasResponse = false; responseStreamString = formulaDataStore.lastGetOnlineFormulasResponse; } else if (getOnlineFormulasResponse.StatusCode == HttpStatusCode.OK) { DebugLog("Got response with OK status code"); responseStreamString = new StreamReader(getOnlineFormulasResponse.GetResponseStream()).ReadToEnd(); } //Not modified is handled in try/catch so what's the issue here? else { Debug.Log("Something went wrong in get online formulas response: " + getOnlineFormulasResponse.StatusCode); EditorUtility.SetDirty(formulaDataStore); } DebugLog("Response in next line\n" + responseStreamString); if (getOnlineFormulasResponse != null) { getOnlineFormulasResponse.Close(); getOnlineFormulasResponse = null; } if (responseStreamString != null) { var repositoryContentsList = MiniJSON.Json.Deserialize(responseStreamString) as List <object>; //If json deserialized into a proper object if (repositoryContentsList != null) { //Set formulaDataStore.lastGetOnlineFormulasResponse to the response string formulaDataStore.lastGetOnlineFormulasResponse = responseStreamString; } foreach (Dictionary <string, object> content in repositoryContentsList) { var apiURL = content["url"] as string; var htmlURL = content["html_url"] as string; var downloadURL = content ["download_url"] as string; var extension = Path.GetExtension(downloadURL); //If it's a .cs file if (string.Equals(extension, ".cs", StringComparison.InvariantCultureIgnoreCase)) { var fileName = Path.GetFileName(downloadURL); var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(downloadURL); //Check to see if it exists in formulaDataStore var formula = formulaDataStore.FormulaData.Find(x => x.name == fileNameWithoutExtension); if (formula == null) { formula = new FormulaData(); formula.name = fileNameWithoutExtension; formula.projectFilePath = Constants.formulasFolderUnityPath + fileName; formula.localFileExists = new FileInfo(Utils.GetFullPathFromAssetsPath(formula.projectFilePath)).Exists; formula.downloadURL = downloadURL; formula.htmlURL = htmlURL; formula.apiURL = apiURL; formulaDataStore.FormulaData.Add(formula); } else { //If download URL doesn't match (can happen if file was copied to project, instead of downloaded) if (!string.Equals(formula.downloadURL, downloadURL)) { //Update download url formula.downloadURL = downloadURL; } } } } //Update lastUpdateTime if we didn't use last response if (!useLastGetOnlineFormulasResponse) { formulaDataStore.LastUpdateTime = DateTime.UtcNow; } EditorUtility.SetDirty(formulaDataStore); if (FormulaDataUpdated != null) { FormulaDataUpdated(); } } } }
void DrawUsableFormula(FormulaData formula) { var niceName = ObjectNames.NicifyVariableName(formula.name); var method = formula.methodInfo; if (method == null) { return; } var parameters = parametersDictionary[method]; var parameterValuesArray = parameterValuesDictionary[method]; var hasParameters = parameters.Length > 0; if (hasParameters) { GUILayout.BeginVertical(GUI.skin.box, GUILayout.MaxWidth(this.position.width)); } GUILayout.BeginHorizontal(); var formulaButtonWidth = this.position.width - 32; if (hasParameters) { formulaButtonWidth -= 4; } if (formula.updateAvailable) { formulaButtonWidth -= 32; } //Commented out for now, not sure if necessary - Button is only enabled if parameters have been initialized // GUI.enabled = parameters.Length == 0 || parameterValuesArray.All(x => x != null); if (GUILayout.Button(new GUIContent(niceName, niceName), GUILayout.Width(formulaButtonWidth))) { method.Invoke(null, parameterValuesArray); } // GUI.enabled = true; DrawOptionsButton(formula); if (formula.updateAvailable) { DrawDownloadButton(updateButtonGUIContent, formula); } GUILayout.EndHorizontal(); if (hasParameters) { //Draw parameter fields for (int p = 0; p < parameters.Length; p++) { var parameter = parameters[p]; var parameterType = parameter.ParameterType; var niceParameterName = ObjectNames.NicifyVariableName(parameter.Name); var valueObj = parameterValuesArray[p]; GUILayout.BeginHorizontal(); object newValue = null; // if(parameterType.IsClass && parameterType.IsSerializable) // { // var fieldInfos = parameterType.GetFields(BindingFlags.Instance | BindingFlags.Public); // //TODO: Draw a field for each public instance field of class // } EditorGUI.BeginChangeCheck(); if (parameterType == typeof(int)) { newValue = EditorGUILayout.IntField(new GUIContent(niceParameterName, niceParameterName), valueObj != null ? ((int)valueObj) : 0); } else if (parameterType == typeof(float)) { newValue = EditorGUILayout.FloatField(new GUIContent(niceParameterName, niceParameterName), valueObj != null ? ((float)valueObj) : 0f); } else if (parameterType == typeof(string)) { newValue = EditorGUILayout.TextField(new GUIContent(niceParameterName, niceParameterName), valueObj != null ? ((string)valueObj) : string.Empty); } else if (parameterType == typeof(bool)) { newValue = EditorGUILayout.Toggle(new GUIContent(niceParameterName, niceParameterName), valueObj != null ? ((bool)valueObj) : false); } else if (parameterType == typeof(Rect)) { newValue = EditorGUILayout.RectField(new GUIContent(niceParameterName, niceParameterName), valueObj != null ? ((Rect)valueObj) : new Rect()); } //TODO: Don't do this, instead use RectOffset as a class else if (parameterType == typeof(RectOffset)) { //We use a Vector4Field for RectOffset type because there isn't an Editor GUI drawer for rect offset var rectOffset = (RectOffset)valueObj; var vec4 = EditorGUILayout.Vector4Field(niceParameterName, valueObj != null ? new Vector4(rectOffset.left, rectOffset.right, rectOffset.top, rectOffset.bottom) : Vector4.zero); newValue = new RectOffset((int)vec4.x, (int)vec4.y, (int)vec4.z, (int)vec4.w); } else if (parameterType == typeof(Vector2)) { var fieldWidth = EditorGUIUtility.fieldWidth; EditorGUIUtility.fieldWidth = 1f; newValue = EditorGUILayout.Vector2Field(new GUIContent(niceParameterName, niceParameterName), valueObj != null ? ((Vector2)valueObj) : Vector2.zero); EditorGUIUtility.fieldWidth = fieldWidth; } else if (parameterType == typeof(Vector3)) { newValue = EditorGUILayout.Vector3Field(new GUIContent(niceParameterName, niceParameterName), valueObj != null ? ((Vector3)valueObj) : Vector3.zero); } else if (parameterType == typeof(Vector4)) { newValue = EditorGUILayout.Vector4Field(niceParameterName, valueObj != null ? ((Vector4)valueObj) : Vector4.zero); } else if (parameterType == typeof(Color)) { newValue = EditorGUILayout.ColorField(new GUIContent(niceParameterName, niceParameterName), valueObj != null ? ((Color)valueObj) : Color.white); } else if (parameterType == typeof(UnityEngine.Object)) { newValue = EditorGUILayout.ObjectField(new GUIContent(niceParameterName, niceParameterName), valueObj != null ? ((UnityEngine.Object)valueObj) : null, parameterType, true); } else if (parameterType.IsEnum) { newValue = EditorGUILayout.EnumPopup(new GUIContent(niceParameterName, niceParameterName), valueObj != null ? ((System.Enum)valueObj) : default(System.Enum)); } else if (parameterType == typeof(LayerMask)) { newValue = Utils.LayerMaskField(niceParameterName, valueObj != null ? ((LayerMask)valueObj) : default(LayerMask)); } if (EditorGUI.EndChangeCheck()) { parameterValuesArray[p] = newValue; } GUILayout.EndHorizontal(); } } if (hasParameters) { GUILayout.EndVertical(); } }