Skip to content

Commit

Permalink
Fixed reordering
Browse files Browse the repository at this point in the history
Removed capture focus. Keyboard focus now gained after dragging
  • Loading branch information
foulston committed Apr 18, 2017
1 parent 2f4e078 commit c5997f2
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 32 deletions.
Binary file modified Assets/ReorderableList/Example/Example.prefab
Binary file not shown.
Binary file modified Assets/ReorderableList/Example/GameObjects.prefab
Binary file not shown.
91 changes: 59 additions & 32 deletions Assets/ReorderableList/List/Editor/ReorderableList.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using UnityEditor;
using UnityEngine;

Expand Down Expand Up @@ -56,7 +58,6 @@ public enum ElementDisplayType {
public float footerHeight;
public float slideEasing;
public bool showDefaultBackground;
public bool captureFocus;
public ElementDisplayType elementDisplayType;
public string elementNameProperty;
public string elementNameOverride;
Expand Down Expand Up @@ -134,7 +135,6 @@ public ReorderableList(SerializedProperty list, bool canAdd, bool canRemove, boo
slideEasing = 0.15f;
expandable = true;
showDefaultBackground = true;
captureFocus = true;
multipleSelection = true;
elementLabel = new GUIContent();

Expand Down Expand Up @@ -894,7 +894,7 @@ private void HandleContextClick(Event evt, SerializedProperty element) {

if (element.isInstantiatedPrefab) {

menu.AddItem(new GUIContent("Revert Value to Prefab"), false, selection.RevertValues, list);
menu.AddItem(new GUIContent("Revert " + GetElementLabel(element).text + " to Prefab"), false, selection.RevertValues, list);
menu.AddSeparator(string.Empty);
}

Expand Down Expand Up @@ -1045,7 +1045,7 @@ private void HandlePreSelection(Rect rect, Event evt) {

if (elementHeaderRect.Contains(evt.mousePosition) && !elementExpandRect.Contains(evt.mousePosition)) {

DoSelection(index, evt);
DoSelection(index, true, evt);
HandleUtility.Repaint();
}
}
Expand All @@ -1054,8 +1054,9 @@ private void HandlePreSelection(Rect rect, Event evt) {
}
else if (evt.type == EventType.MouseDrag && draggable && GUIUtility.hotControl == controlID) {

if (UpdateDragPosition(evt.mousePosition, rect, dragList)) {
if (selection.Length > 0 && UpdateDragPosition(evt.mousePosition, rect, dragList)) {

GUIUtility.keyboardControl = controlID;
dragging = true;
}

Expand All @@ -1069,13 +1070,13 @@ private void HandlePostSelection(Rect rect, Event evt) {

case EventType.MouseDown:

if (rect.Contains(evt.mousePosition) && IsSelectionButton(evt) && ((captureFocus || evt.button == 2) || GUIUtility.keyboardControl == 0 || GUIUtility.keyboardControl == controlID)) {
if (rect.Contains(evt.mousePosition) && IsSelectionButton(evt)) {

int index = GetSelectionIndex(evt.mousePosition);

if (CanSelect(index)) {

DoSelection(index, evt);
DoSelection(index, GUIUtility.keyboardControl == 0 || GUIUtility.keyboardControl == controlID || evt.button == 2, evt);
}
else {

Expand Down Expand Up @@ -1181,7 +1182,7 @@ private bool IsSelectionButton(Event evt) {
return evt.button == 0 || evt.button == 2;
}

private void DoSelection(int index, Event evt) {
private void DoSelection(int index, bool setKeyboardControl, Event evt) {

//append selections based on action, this may be a additive (ctrl) or range (shift) selection

Expand Down Expand Up @@ -1210,7 +1211,11 @@ private void DoSelection(int index, Event evt) {
GUIUtility.hotControl = controlID;
}

GUIUtility.keyboardControl = controlID;
if (setKeyboardControl) {

GUIUtility.keyboardControl = controlID;
}

evt.Use();
}

Expand Down Expand Up @@ -1257,10 +1262,8 @@ private DragElement[] GetDragList(float dragPosition) {
return b.selected ? b.startIndex.CompareTo(a.startIndex) : -1;
}
else {
return a.startIndex.CompareTo(b.startIndex);
}
return a.startIndex.CompareTo(b.startIndex);
});

return dragList;
Expand All @@ -1278,7 +1281,7 @@ private bool UpdateDragPosition(Vector2 position, Rect bounds, DragElement[] dra

dragPosition = Mathf.Clamp(position.y, bounds.yMin + minOffset, bounds.yMax - maxOffset);

if (dragPosition != pressPosition) {
if (Mathf.Abs(dragPosition - pressPosition) > 1) {

dragDirection = (int)Mathf.Sign(dragPosition - pressPosition);
return true;
Expand All @@ -1301,19 +1304,23 @@ private void ReorderDraggedElements(DragElement[] dragList) {

System.Array.Sort(dragList, (a, b) => a.desiredRect.center.y.CompareTo(b.desiredRect.center.y));

int start = dragDirection == 1 ? 0 : selection.Length - 1;
int end = dragDirection == 1 ? selection.Length : -1;
selection.Sort((a, b) => {
int d1 = GetDragIndexFromSelection(a);
int d2 = GetDragIndexFromSelection(b);
return dragDirection > 0 ? d1.CompareTo(d2) : d2.CompareTo(d1);
});

//swap the selected elements in the List
//TODO There's a reorderable bug here. Fix

for (int i = start; i != end; i += dragDirection) {
int s = selection.Length;

int newIndex = GetDragIndexFromSelection(selection[i]);
while (--s > -1) {

selection[i] = newIndex;
int newIndex = GetDragIndexFromSelection(selection[s]);

Debug.Log("Moving " + dragList[newIndex].startIndex + " to " + newIndex);
selection[s] = newIndex;

list.MoveArrayElement(dragList[newIndex].startIndex, newIndex);
}
Expand All @@ -1328,17 +1335,7 @@ private void ReorderDraggedElements(DragElement[] dragList) {

private int GetDragIndexFromSelection(int index) {

int i, len = dragList.Length;

for (i = 0; i < len; i++) {

if (dragList[i].startIndex == index) {

return i;
}
}

return -1;
return System.Array.FindIndex(dragList, t => t.startIndex == index);
}

private int GetSelectionIndex(Vector2 position) {
Expand All @@ -1360,6 +1357,9 @@ private int GetSelectionIndex(Vector2 position) {

private bool CanSelect(ListSelection selection) {

return selection.Length > 0 ? selection.All(s => CanSelect(s)) : false;

/*
int i, len = selection.Length;
if (len == 0) {
Expand All @@ -1376,6 +1376,7 @@ private bool CanSelect(ListSelection selection) {
}
return true;
*/
}

private bool CanSelect(int index) {
Expand All @@ -1385,6 +1386,9 @@ private bool CanSelect(int index) {

private bool CanSelect(Vector2 position) {

return selection.Length > 0 ? selection.Any(s => IsPositionWithinElement(position, s)) : false;

/*
int i, len = selection.Length;
if (len == 0) {
Expand All @@ -1401,10 +1405,14 @@ private bool CanSelect(Vector2 position) {
}
return false;
*/
}

private bool IsPositionWithinElement(Vector2 position, int index) {

return CanSelect(index) ? elementRects[index].Contains(position) : false;

/*
if (CanSelect(index)) {
Rect elementRect = elementRects[index];
Expand All @@ -1415,6 +1423,7 @@ private bool IsPositionWithinElement(Vector2 position, int index) {
return false;
}
*/
}

private bool IsElementExpandable(SerializedProperty element) {
Expand Down Expand Up @@ -1612,7 +1621,7 @@ public Rect SetRect(int id, Rect rect) {
// -- SELECTION --
//

class ListSelection {
class ListSelection : IEnumerable<int> {

private List<int> indexes;

Expand Down Expand Up @@ -1728,6 +1737,14 @@ public void Sort() {
}
}

public void Sort(System.Comparison<int> comparison) {

if (indexes.Count > 0) {

indexes.Sort(comparison);
}
}

public int[] ToArray() {

return indexes.ToArray();
Expand Down Expand Up @@ -1836,6 +1853,16 @@ private void AppendRange(int from, int to) {

Append(to);
}

public IEnumerator<int> GetEnumerator() {

return ((IEnumerable<int>)indexes).GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator() {

return ((IEnumerable<int>)indexes).GetEnumerator();
}
}

//
Expand Down

0 comments on commit c5997f2

Please sign in to comment.