public void RenderField(AIInspectorState state) { var evt = Event.current; var mousePos = evt.mousePosition; var boxStyle = new GUIStyle("Box"); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField(_label); EditorGUILayout.Separator(); if (GUILayout.Button(SharedStyles.addTooltip, SharedStyles.BuiltIn.addButtonSmall)) { AddNew(mousePos, state); } EditorGUILayout.EndHorizontal(); var count = _editorItems.Count; if (count == 0) { return; } EditorGUILayout.BeginVertical(boxStyle); var removeIdx = -1; for (int i = 0; i < count; i++) { EditorGUILayout.BeginVertical(); if (DrawEditorItem(_editorItems[i], state)) { removeIdx = i; } EditorGUILayout.EndVertical(); } EditorGUILayout.EndVertical(); if (removeIdx >= 0 && DisplayHelper.ConfirmDelete("item")) { var item = (IKeyedItem)_editorItems[removeIdx].item; _dictionary.Remove(item.key); _editorItems.RemoveAt(removeIdx); state.MarkDirty(); } }
protected void UpdateValue(T newValue, AIInspectorState state) { state.currentAIUI.undoRedo.Do(new EditorFieldOperation <T>(_owner, _member, _curValue, newValue)); state.MarkDirty(); _curValue = newValue; var prop = _member as PropertyInfo; if (prop != null) { prop.SetValue(_owner, newValue, null); } else { var field = _member as FieldInfo; if (field != null) { field.SetValue(_owner, newValue); } } }
private void AddNew(Vector2 mousePos, AIInspectorState state) { if (_isSimpleType) { if (_list == null) { _setter(CreateList()); } if (_simpleItemCreator == null) { _simpleItemCreator = new SimpleItemConstructor(_list, _itemType); } var item = Activator.CreateInstance(_itemType); DoAdd(item, state); var itemWrapper = _simpleItemCreator.Conctruct(_list.Count - 1, item); _editorItems.Add(ReflectMaster.Reflect(itemWrapper)); return; } Action <Type[]> cb = (selectedTypes) => { if (_list == null) { _setter(CreateList()); } using (state.currentAIUI.undoRedo.bulkOperation) { for (int i = 0; i < selectedTypes.Length; i++) { var selectedType = selectedTypes[i]; if (_itemType.IsGenericType && selectedType.IsGenericType) { var genArgs = _itemType.GetGenericArguments(); selectedType = selectedType.GetGenericTypeDefinition().MakeGenericType(genArgs); } var item = Activator.CreateInstance(selectedType); DoAdd(item, state); _editorItems.Add(ReflectMaster.Reflect(item)); if (Application.isPlaying) { HandleVisualizerAdd(state, item); } } state.MarkDirty(); } }; // We do not want the button click itself to count as a change.. same as above. GUI.changed = false; var screenPos = EditorGUIUtility.GUIToScreenPoint(mousePos); AIEntitySelectorWindow.Get(screenPos, _itemType, cb); }
public void RenderField(AIInspectorState state) { EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField(_label); if (GUILayout.Button(SharedStyles.changeSelectionTooltip, SharedStyles.BuiltIn.changeButtonSmall)) { Action <Type> cb = (selectedType) => { if (_itemType.IsGenericType && selectedType.IsGenericType) { var genArgs = _itemType.GetGenericArguments(); selectedType = selectedType.GetGenericTypeDefinition().MakeGenericType(genArgs); } var old = _item; _item = Activator.CreateInstance(selectedType); _editorItem = ReflectMaster.Reflect(_item); _setter(_item); state.currentAIUI.undoRedo.Do(new CustomEditorFieldOperation(old, _item, _setter)); state.MarkDirty(); }; //We do not want the button click itself to count as a change.. same as above. GUI.changed = false; var screenPos = EditorGUIUtility.GUIToScreenPoint(Event.current.mousePosition); AIEntitySelectorWindow.Get(screenPos, _itemType, cb); } EditorGUILayout.EndHorizontal(); bool doDelete = false; if (_item != null) { EditorGUILayout.BeginVertical("Box"); EditorGUILayout.BeginHorizontal(SharedStyles.BuiltIn.listItemHeader); EditorGUILayout.LabelField(_editorItem.name, SharedStyles.BuiltIn.normalText); if (GUILayout.Button(SharedStyles.deleteTooltip, SharedStyles.BuiltIn.deleteButtonSmall)) { GUI.changed = false; if (DisplayHelper.ConfirmDelete("item")) { doDelete = true; } } EditorGUILayout.EndHorizontal(); _editorItem.Render(state); EditorGUILayout.EndVertical(); } EditorGUILayout.Separator(); //We do the delete outside any layout stuff to ensure we don't get weird warnings. if (doDelete) { state.currentAIUI.undoRedo.Do(new CustomEditorFieldOperation(_item, null, _setter)); _setter(null); _item = null; _editorItem = null; state.MarkDirty(); } }
public void RenderField(AIInspectorState state) { var evt = Event.current; var mousePos = evt.mousePosition; var boxStyle = new GUIStyle("Box"); if (_reorderable && _dragging) { // don't let the mouse affect the X position of the rect being dragged mousePos.x = _draggedRect.x; } EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField(_label); EditorGUILayout.Separator(); if (GUILayout.Button(SharedStyles.addTooltip, SharedStyles.BuiltIn.addButtonSmall)) { AddNew(mousePos, state); } EditorGUILayout.EndHorizontal(); var count = _editorItems.Count; if (count == 0) { return; } EditorGUILayout.BeginVertical(boxStyle); var removeIdx = -1; if (!_reorderable || count <= 1) { for (int i = 0; i < count; i++) { // if list is not reorderable, just draw items EditorGUILayout.BeginVertical(); if (DrawEditorItem(_editorItems[i], state, false)) { removeIdx = i; } EditorGUILayout.EndVertical(); } } else { // Reorderable list int drawIdx = 0; for (int i = 0; i < count; i++) { if (_dragging && _draggingIndex == drawIdx) { drawIdx++; } Rect dragHandle; Rect itemRect; if (i == _dropIndex) { dragHandle = new Rect(); itemRect = EditorGUILayout.BeginVertical(); GUILayout.Space(_dragRectHeight); EditorGUILayout.EndVertical(); } else { itemRect = EditorGUILayout.BeginVertical(); var item = _editorItems[drawIdx++]; if (DrawEditorItem(item, state, true)) { removeIdx = i; } EditorGUILayout.EndVertical(); dragHandle = DrawDragHandle(); } if (evt.button == MouseButton.left && evt.isMouse) { if (!_dragging) { if (dragHandle.Contains(mousePos) && evt.type == EventType.MouseDrag) { _dragging = true; _draggingIndex = _dropIndex = i; _draggedRect = itemRect; _dragOffsetY = itemRect.y - mousePos.y; break; } } else { if (itemRect.Contains(mousePos)) { _dropIndex = i; break; } } } } if (_dragging && evt.type == EventType.MouseUp) { if (_draggingIndex != _dropIndex) { _list.Reorder(_draggingIndex, _dropIndex); _editorItems.Reorder(_draggingIndex, _dropIndex); state.currentAIUI.undoRedo.Do(new ReorderOperation(_draggingIndex, _dropIndex, _list)); state.MarkDirty(); if (Application.isPlaying) { HandleVisualizerReorder(state, _draggingIndex, _dropIndex); } } _dragging = false; _dropIndex = _draggingIndex = -1; } } EditorGUILayout.EndVertical(); if (removeIdx >= 0 && DisplayHelper.ConfirmDelete("item")) { DoRemove(removeIdx, state); _editorItems.RemoveAt(removeIdx); state.MarkDirty(); if (Application.isPlaying) { HandleVisualizerRemove(state, removeIdx); } } if (_reorderable && _dragging) { if (evt.type == EventType.Layout || evt.type == EventType.Repaint) { GUI.Box(new Rect(_draggedRect.x, mousePos.y + _dragOffsetY, _draggedRect.width, _dragRectHeight), _editorItems[_draggingIndex].name, boxStyle); AIInspectorEditor.instance.Repaint(); } } }