private void Import(string path) { string[] lines = File.ReadAllLines(path); for (int i = 0; i < lines.Length; i++) { string line = lines[i]; bool complete = false; string text = line; if (line.StartsWith("[Complete]")) { text = line.Substring(10); complete = true; } text = text.Replace(@"\n", "\n"); ToDoElement element = new ToDoElement() { text = text, completed = complete }; todoList.list.Add(element); } }
private void DrawElementBackground(Rect rect, int index, bool active, bool focus) { if (index < 0) { return; } ToDoElement element = todoList.list[index]; Rect elementRect = new Rect(rect.x, rect.y + 1, rect.width, rect.height - 1); #if !UNITY_2020_1_OR_NEWER // rect.height is wrong for older versions (tested: 2018.3, 2019.3), so needs recalculating elementRect.height = ElementHeight(index); #else // Handling the completed/green background for older versions causes issues while reordering if (element.completed) { EditorGUI.DrawRect(elementRect, completedColor); } #endif if (focus) { EditorGUI.DrawRect(elementRect, focusColor); } else if (active) { EditorGUI.DrawRect(elementRect, activeColor); } }
private float ElementHeight(int index) { ToDoElement element = todoList.list[index]; float width = GetWidth(); if (style_textArea == null) { style_textArea = new GUIStyle(GUI.skin.label); style_textArea.alignment = TextAnchor.UpperLeft; style_textArea.wordWrap = true; } if (element.editing) { style_textArea.richText = false; } // Height float height = style_textArea.CalcHeight(new GUIContent(element.text), width) + 5; style_textArea.richText = true; if (element.objectReference != null) { height += EditorGUIUtility.singleLineHeight + 4; } height = Mathf.Max(EditorGUIUtility.singleLineHeight + 4, height); return(height); }
public void RemoveCompleted() { for (int i = list.Count - 1; i >= 0; i--) { ToDoElement element = list[i]; if (element.completed) { list.RemoveAt(i); } } }
private void Export(string path) { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < todoList.list.Count; i++) { ToDoElement element = todoList.list[i]; string line = ""; if (element.completed) { line += "[Complete]"; } line += element.text.Replace("\n", @"\n"); stringBuilder.AppendLine(line); } File.WriteAllText(path, stringBuilder.ToString()); }
private void DrawElement(Rect rect, int index, bool active, bool focus) { ToDoElement element = todoList.list[index]; #if !UNITY_2020_1_OR_NEWER // rect.height is wrong for older versions (tested: 2018.3, 2019.3), so needs recalculating rect.height = ElementHeight(index); // Versions older than 2020 can't handle this in DrawElementBackground Rect elementRect = new Rect(rect.x, rect.y + 1, rect.width, rect.height - 1); if (element.completed) { EditorGUI.DrawRect(elementRect, completedColor); } if (focus) { EditorGUI.DrawRect(elementRect, focusColor); } else if (active) { EditorGUI.DrawRect(elementRect, activeColor); } #endif // Toggle float h = EditorGUIUtility.singleLineHeight; EditorGUI.BeginChangeCheck(); bool completed = EditorGUI.Toggle( new Rect(rect.x + 2, rect.y + 2, h, h), element.completed); if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(todoList, "Toggled Task Completion"); element.completed = completed; } // This prevents text area from highlighting all text on focus bool preventSelection = (UnityEngine.Event.current.type == EventType.MouseDown); Color cursorColor = GUI.skin.settings.cursorColor; if (preventSelection) { GUI.skin.settings.cursorColor = new Color(0, 0, 0, 0); } // Text Colours if (completed) { style_textArea.normal.textColor = completedTextColor; style_textArea.focused.textColor = completedTextColor; style_textArea.hover.textColor = completedTextColor; } else { style_textArea.normal.textColor = textColor; style_textArea.focused.textColor = textColor; style_textArea.hover.textColor = textColor; } // If editing, turn off richText if (element.editing) { style_textArea.richText = false; } // Text Area float x = h + 5; float textHeight = rect.height; if (element.objectReference) { textHeight -= 25; } EditorGUI.BeginChangeCheck(); GUI.SetNextControlName("TextArea"); string text = EditorGUI.TextArea( new Rect(rect.x + x, rect.y + 2, rect.width - x, textHeight), element.text, style_textArea); element.editing = (GUI.GetNameOfFocusedControl() == "TextArea"); style_textArea.richText = true; if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(todoList, "Edited Task Text"); element.text = text; } // Reset Cursor Color if (preventSelection) { GUI.skin.settings.cursorColor = cursorColor; } // Object Field if (element.objectReference) { EditorGUI.BeginChangeCheck(); EditorGUI.LabelField( new Rect(rect.x + x, rect.y + rect.height + 5 - 25, rect.width - 27, h), "Linked Object : ", style_textArea); x += EditorGUIUtility.labelWidth; Object obj = EditorGUI.ObjectField( new Rect(rect.x + x, rect.y + rect.height + 5 - 25, rect.width - x, h), element.objectReference, typeof(Object), true); if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(todoList, "Changed Task Object"); element.objectReference = obj; } } // Handle Drag & Drop Object onto Element UnityEngine.Event currentEvent = UnityEngine.Event.current; if (rect.Contains(currentEvent.mousePosition)) { if (currentEvent.type == EventType.DragUpdated) { DragAndDrop.visualMode = DragAndDropVisualMode.Link; currentEvent.Use(); } else if (currentEvent.type == EventType.DragPerform) { DragAndDrop.AcceptDrag(); if (DragAndDrop.objectReferences.Length > 0) { Object obj = DragAndDrop.objectReferences[0]; Undo.RecordObject(todoList, "Changed Task Object"); element.objectReference = obj; } currentEvent.Use(); } } }