public bool DrawColumns() { string titles = string.Empty; foreach (var pair in columns) { titles += pair.Key + " "; } Rect position = GUILayoutUtility.GetRect(new GUIContent(titles), defaultTitleStyle); position.height += 6; if (Event.current.type == EventType.Repaint) { //Draw background defaultTitleStyle.Draw(position, string.Empty, false, false, false, false); Rect newPosition = position; float fullPositionWidth = position.width; if (!fullListRow) { fullPositionWidth -= 34; } else { fullPositionWidth -= 9; } newPosition.y += 2; newPosition.x += 5; int count = 0; foreach (var pair in columns) { newPosition.width = (fullPositionWidth * (pair.Value - 0.01f)); GUI.Label(newPosition, pair.Key); float x = newPosition.x; if (count < columns.Count - 1) { newPosition.width = (fullPositionWidth * minColumnSize); newPosition.x = x + (fullPositionWidth * (pair.Value - 0.01f)); GUI.Label(newPosition, "|"); } DraggableColumnDivider divider = columnDividers[pair.Key]; divider.position = newPosition; divider.position.x -= divider.position.width * 0.5f; divider.currentPosition = new Vector2(divider.position.x, divider.position.y); //divider.startPosition = divider.currentPosition; divider.newSizeValue = pair.Value; divider.previousWindowSize = position.width; newPosition.x = x + (fullPositionWidth * pair.Value); count++; } } return(CheckDividerDragging(position)); }
bool CheckDividerDragging(Rect fullRect) { Vector2 mousePosition = Event.current.mousePosition; if (Event.current.type == EventType.MouseUp) { isCurrentlyDragging = false; foreach (var pair in columnDividers) { pair.Value.isDragging = false; } } else if (Event.current.type == EventType.MouseDown && !isCurrentlyDragging) { foreach (var pair in columnDividers) { if (pair.Value.position.Contains(mousePosition)) { pair.Value.isDragging = true; isCurrentlyDragging = true; pair.Value.startPosition = mousePosition; pair.Value.startValue = columns[pair.Key]; Event.current.Use(); break; } } } if (!isCurrentlyDragging) { return(false); } List <string> keys = new List <string>(columnDividers.Keys); foreach (string key in keys) { var value = columnDividers[key]; float currentValue = value.startValue; if (value.isDragging) { value.currentPosition = mousePosition; float changeInPixels = (value.currentPosition.x - value.startPosition.x); if (changeInPixels != 0) { bool isNegative = Mathf.Sign(changeInPixels) < 0 ? true : false; changeInPixels = Mathf.Abs(changeInPixels); float relativeChange = (changeInPixels / value.previousWindowSize); if (isNegative) { changeInPixels = currentValue - relativeChange; } else { changeInPixels = currentValue + relativeChange; } value.newSizeValue = Mathf.Clamp(changeInPixels, minColumnSize, MaxColumnSize); } } } if (isCurrentlyDragging) { float fullWidth = 1.0f; bool dragObjectFound = false; for (int i = 0; i < keys.Count; i++) { string key = keys[i]; DraggableColumnDivider divider = columnDividers[key]; if (divider.isDragging || dragObjectFound) { dragObjectFound = true; int objectsLeft = Mathf.Max(0, keys.Count - (i + 1)); if (objectsLeft == 0) { columns[key] = fullWidth; divider.newSizeValue = columns[key]; } else if (fullWidth - (divider.newSizeValue + (objectsLeft * minColumnSize)) < 0) { columns[key] = fullWidth - (objectsLeft * minColumnSize); divider.newSizeValue = columns[key]; } else { columns[key] = divider.newSizeValue; } } else { columns[key] = Mathf.Max(minColumnSize, divider.newSizeValue); } fullWidth -= columns[key]; } RecalculateColumnWidths(); return(true); } return(false); }