public static bool Toggle( bool src, string label, float size = -1, Cell cell = null, //should be horizontal bool isLeft = false, Action <object> onChange = null, string tooltip = null) { if (size < 0) { size = active.autoLayout == Layout.Vertical? lineHeight : 1; } if (cell == null) { cell = active.Add(Layout.Horizontal, size); } Cell toggleCell; Cell labelCell; if (isLeft) { toggleCell = cell.Add(Layout.Full, 20); labelCell = cell.Add(Layout.Full); } else { labelCell = cell.Add(Layout.Full); toggleCell = cell.Add(Layout.Full, 20); } Label(label, cell: labelCell); Rect rect = toggleCell.ToDisplay(defaultPadding); #if UNITY_EDITOR bool dst = UnityEditor.EditorGUI.Toggle(rect, src); #else bool dst = src; #endif if (src != dst) { if (onChange != null) { onChange(dst); } //RecordUndo(); } return(dst); }
public static Cell FoldoutCell( ref bool expanded, string label, float size = 1, Cell cell = null, //vertical object backgroundPaddingBox = null, Action <object> onChange = null, string tooltip = null) { if (size < 0) { size = active.autoLayout == Layout.Vertical? lineHeight : 1; } if (cell == null) { cell = active.Add(Layout.Vertical, size); } //background if (expanded) { Padding backgroundPadding = backgroundPaddingBox != null ? (Padding)backgroundPaddingBox : new Padding(-3, -3, -3, -3); Element(Icons.GetElementStyle("DPLayout_FoldoutBackground"), cell: cell, padding: backgroundPadding); } //foldout Cell foldCell = cell.Add(Layout.Full, lineHeight); expanded = Foldout(expanded, label, cell: foldCell, onChange: onChange, tooltip: tooltip); //field Cell hCell = cell.Add(Layout.Horizontal); hCell.Add(Layout.Full, 10); //margin Cell fieldCell = hCell.Add(Layout.Vertical); fieldCell.prevActive = active; active = fieldCell; return(fieldCell); }
public static Cell Padded(Layout layout, Padding padding, float size = 1, string name = null, bool background = false) { Cell h = active.Add(Layout.Horizontal, size); if (background) { Element(Icons.GetElementStyle("DPLayout_FoldoutBackground"), cell: h); } h.Add(Layout.Full, padding.left); Cell v = h.Add(Layout.Vertical, size); v.Add(Layout.Full, padding.top); Cell cell = v.Add(Layout.Vertical, size, name); v.Add(Layout.Full, padding.bottom); h.Add(Layout.Full, padding.right); cell.prevActive = active; active = cell; return(cell); }
public static void Class( object obj, string category = null, float size = -1, Cell cell = null, //should be vertical Action <object> onChange = null) { Type objType = obj.GetType(); if (size < 0) { size = active.autoLayout == Layout.Vertical? lineHeight : 1; } //loading attributes if (!Val.attributesCaches.ContainsKey(objType)) { Val.Cache(objType); } Val[] attributes = Val.attributesCaches[objType]; //drawing if (cell == null) { cell = active.Add(Layout.Vertical, size, "Class " + objType.Name); } for (int a = 0; a < attributes.Length; a++) { if (category != null && attributes[a].cat != category) { continue; } Cell fieldCell = cell.Add(Layout.Horizontal, lineHeight); FieldInfo field = attributes[a].field; object val = field.GetValue(obj); Field( val, attributes[a].name, cell: fieldCell, isLeft: attributes[a].isLeft, onChange: newVal => { field.SetValue(obj, newVal); if (onChange != null) { onChange(newVal); } }); } }
public static T ScriptableAsset <T> ( T asset, string label, System.Func <T> construct = null, Action <object> onChange = null, //if asset re-assigned, created or reset string tooltip = null) where T : ScriptableObject, ISerializationCallbackReceiver { Cell back = UI.active.Add(Layout.Horizontal); UI.Background(back); Cell labelBack = back.Add(Layout.Vertical, size: 0.5f); Cell labelCell = labelBack.Add(Layout.Full, size: UI.lineHeight); UI.Label(label, cell: labelCell); Cell fieldBack = back.Add(Layout.Vertical); Cell fieldCell = fieldBack.Add(Layout.Full, size: UI.lineHeight); T newAsset = (T)UI.Field(asset, cell: fieldCell); Cell createCell = fieldBack.Add(Layout.Full, size: UI.lineHeight); if (asset == null) { if (UI.Button(false, "Create", cell: createCell)) { if (construct == null) { newAsset = ScriptableObject.CreateInstance <T>(); } else { newAsset = construct(); } } } else { if (UI.Button(false, "Reset", cell: createCell)) { #if UNITY_EDITOR if (UnityEditor.EditorUtility.DisplayDialog("Reset to Default", "This will remove all of the data and create a default one. Are you sure you wsih to continue?", "Reset to Default", "Cancel")) #endif { if (construct == null) { newAsset = ScriptableObject.CreateInstance <T>(); } else { newAsset = construct(); } } } } Cell storeCell = fieldBack.Add(Layout.Full, size: UI.lineHeight); #if UNITY_EDITOR if (asset == null || !UnityEditor.AssetDatabase.Contains(asset)) { if (UI.Button(false, "Store to Assets", cell: storeCell, disabled: asset == null)) { newAsset = SaveAsset(asset); } } else { if (UI.Button(false, "Release", cell: storeCell, disabled: asset == null)) { newAsset = ReleaseAsset(asset); } } #endif Cell saveCopyCell = fieldBack.Add(Layout.Full, size: UI.lineHeight); if (UI.Button(false, "Save as Copy", cell: saveCopyCell, disabled: asset == null)) { T copyAsset = ScriptableObject.Instantiate <T>(asset); SaveAsset(copyAsset); } if (newAsset != asset) { if (onChange != null) { onChange(newAsset); } //RecordUndo(); } return(newAsset); }
/*public static T Field<T> ( * T val, * string label = null, * float size=-1, * Cell cell = null, //should be horizontal * float fieldWidth = -1, * bool isLeft = false, //nof for toggles, later mabe for other fields * bool disabled = false, * Action<object> onChange=null, * string tooltip = null) * where T : struct * { * return (T)Field( * (object) val, * label, * size:size, * cell:cell, * fieldWidth:fieldWidth, * isLeft:isLeft, * disabled:disabled, * onChange:onChange, * tooltip:tooltip); * }*/ public static object Field( object val, string label = null, float size = -1, Cell cell = null, //should be horizontal float fieldWidth = -1, bool isLeft = false, //nof for toggles, later mabe for other fields bool disabled = false, bool allowSceneObject = false, Func <object, Rect, object> drawField = null, Func <object, object, bool> changeFunc = null, Action <object> onChange = null, string tooltip = null) { //if (Event.current.type == EventType.Layout || Event.current.type == EventType.MouseDrag) return val; if (val is bool) //special case for toggle { val = Toggle((bool)val, label, size: size, cell: cell, isLeft: isLeft, onChange: tmp => { if (onChange != null) { onChange(tmp); } }, tooltip: tooltip); return(val); } if (size < 0) { size = active.autoLayout == Layout.Vertical? lineHeight : 1; } if (fieldWidth < 0) { fieldWidth = UI.fieldWidth; } if (cell == null) { cell = active.Add(Layout.Horizontal, size, "FieldCell"); } #if UNITY_EDITOR if (disabled) { UnityEditor.EditorGUI.BeginDisabledGroup(true); } #endif Cell labelCell = cell; Cell fieldCell = cell; //label if (label != null) { labelCell = cell.Add(Layout.Full, 1 - fieldWidth, "Label"); fieldCell = cell.Add(Layout.Full, 1 - fieldWidth, "Field"); } Rect labelRect = labelCell.ToDisplay(defaultPadding); //for drag change if (label != null) { //Label(label, cell:labelCell, padding:padding); #if UNITY_EDITOR UnityEditor.EditorGUI.LabelField(labelRect, label, style: Styles.label); //faster #endif } //field bool change = false; Rect fieldRect = fieldCell.ToDisplay(defaultPadding); if (drawField != null) { object newVal = drawField(val, fieldRect); if (changeFunc != null) { change = changeFunc(val, newVal); } else { change = val != newVal; } val = newVal; } else if (val is float) { #if UNITY_EDITOR float oldVal = (float)val; float newVal = UnityEditor.EditorGUI.DelayedFloatField(fieldRect, oldVal, style: Styles.field); if (label != null) { newVal = DragChange.DragChangeField(oldVal, labelRect); } change = Math.Abs(newVal - oldVal) > Mathf.Epsilon; val = newVal; #endif } else if (val is int) { #if UNITY_EDITOR int oldVal = (int)val; int newVal = UnityEditor.EditorGUI.DelayedIntField(fieldRect, oldVal, style: Styles.field); if (label != null) { newVal = Mathf.RoundToInt(DragChange.DragChangeField(oldVal, labelRect, minStep: 1)); } change = oldVal != newVal; val = newVal; #endif } else if (val is UnityEngine.Object) //else if (type.IsSubclassOf(typeof(UnityEngine.Object))) { Type type = val.GetType(); #if UNITY_EDITOR UnityEngine.Object oldVal = (UnityEngine.Object)val; UnityEngine.Object newVal = UnityEditor.EditorGUI.ObjectField(fieldRect, oldVal, type, allowSceneObject); change = oldVal != newVal; val = newVal; #endif } if (change) { if (onChange != null) { onChange(val); } //RecordUndo(); } #if UNITY_EDITOR if (disabled) { UnityEditor.EditorGUI.EndDisabledGroup(); } #endif return(val); }
//starting group public static Cell Vertical(float size = 1, string name = null) { Cell cell = active.Add(Layout.Vertical, size, name); //if (background) Element(Icons.GetElementStyle("DPLayout_FoldoutBackground"), cell:cell); cell.prevActive = active; active = cell; return(cell); }
public static bool Button( bool src, string label = null, Texture2D icon = null, float size = -1, Cell cell = null, //should be horizontal bool disabled = false, Action <bool> onChange = null, string tooltip = null) { //if (Event.current.type == EventType.Layout || Event.current.type == EventType.MouseDrag) return src; if (size < 0) { size = active.autoLayout == Layout.Vertical? lineHeight : 1; } if (cell == null) { cell = active.Add(Layout.Horizontal, size, "Button"); //using horizontal for icon+label } #if UNITY_EDITOR if (disabled) { UnityEditor.EditorGUI.BeginDisabledGroup(true); } #endif //draw button Rect buttonRect = cell.ToDisplay(defaultPadding); GUIContent content = new GUIContent(icon == null? label : "", tooltip); //drawing button text separately if icon is used bool dst = GUI.Toggle(buttonRect, src, content, Styles.button); //draw icon if (icon != null && label == null) { Icon(icon, cell: cell); } //draw icon + label if (icon != null && label != null) { cell.Add(Layout.Full, 5); // empty offset Cell iconCell = cell.Add(Layout.Full, size: icon.width, name: "Icon"); Cell labelCell = cell.Add(Layout.Full); Icon(icon, cell: iconCell); Rect labelRect = labelCell.ToDisplay(); #if UNITY_EDITOR UnityEditor.EditorGUI.LabelField(labelRect, label, style: Styles.label); #endif } #if UNITY_EDITOR if (disabled) { UnityEditor.EditorGUI.EndDisabledGroup(); } #endif if (src != dst) { if (onChange != null) { onChange(dst); } //RecordUndo(); } return(dst); }