Skip to content

Commit

Permalink
Added some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
david-paperseven committed Oct 1, 2020
1 parent 5420636 commit 1f168fb
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 29 deletions.
18 changes: 12 additions & 6 deletions Assets/Code/Editor/SlickViewLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,34 @@ public SlickViewLayout(int totalRows, int rowHeight, Action<Rect, int, bool> dra
}


public bool Draw(Rect rect, int totalRows, GUIStyle style)
/// <summary>
/// Draw
/// Called during the OnGUI of the client code
/// for SlickView to render its view
/// </summary>
/// <param name="rect"></param>
/// <param name="totalRows"></param>
/// <param name="style"></param>
public void Draw(Rect rect, int totalRows, GUIStyle style)
{
Event e = Event.current;

bool selectionChanged = false;
_slickViewState.visRect = rect;
_slickViewState.totalRows = totalRows;

// iterate through all the rows that are _visible_ in the view
// and call the draw delegate
foreach (SlickViewElement el in SlickView.ListView(_slickViewState, style))
{
if (e.type == EventType.MouseDown && e.button == 0 && el.position.Contains(e.mousePosition))
{
_selectedRow = _slickViewState.row;
selectionChanged = true;
_selectedRow = _slickViewState.row;
}
else if (e.type == EventType.Repaint)
{
_draw(el.position, el.row, _selectedRow == el.row);
}
}

return selectionChanged;
}
}
}
106 changes: 83 additions & 23 deletions Assets/Code/Editor/SlickViewWindow.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
Expand All @@ -9,17 +10,20 @@ public class SlickViewWindow : EditorWindow
private SlickViewLayout _slickViewLayout;

private static SlickViewWindow _instance;
public static GUIStyle Box;
public static GUIStyle EvenBackground;
public static GUIStyle OddBackground;
public static GUIStyle LogStyle;
private static GUIStyle Box;
private static GUIStyle EvenBackground;
private static GUIStyle OddBackground;
private static GUIStyle LogStyle;
private bool _initialisedGUIStyles;
Texture2D[] _textures;
private Vector2 _scrollPos;
private bool _slickview = true;
private Rect[] _columnRects;
GUIContent _tempContent = new GUIContent();

/// <summary>
/// An example list view element for the demo window
/// </summary>
public struct ExampleListElement
{
public ExampleListElement(int index, Texture2D texture2D)
Expand All @@ -28,19 +32,21 @@ public ExampleListElement(int index, Texture2D texture2D)
name = texture2D.name;
width = texture2D.width;
height = texture2D.height;
format = texture2D.format;
texture = texture2D;
columnWidth = new[] {60, 240, 60, 60, 50};
columnWidth = new[] {60, 240, 60, 60, 100, 21};
}

public string elementNumber;
public string name;
public int width;
public int height;
public Texture2D texture;
public int[] columnWidth;
public string elementNumber;
public string name;
public int width;
public int height;
public TextureFormat format;
public Texture2D texture;
public int[] columnWidth;
}

private List<ExampleListElement> _listElements = new List<ExampleListElement>(1000);
private List<ExampleListElement> _listElements = new List<ExampleListElement>(10000);

[MenuItem("SlickView/Open Demo Window", false, 0)]
public static void CreateDeviceWindow()
Expand All @@ -55,7 +61,11 @@ static void AfterAssembliesLoaded()
_instance.InitVars();
}

void Init()
/// <summary>
/// InitStyles
/// This has to be called from OnGUI rather than a initialisation step
/// </summary>
void InitStyles()
{
if (!_initialisedGUIStyles)
{
Expand All @@ -67,11 +77,21 @@ void Init()
}
}

/// <summary>
/// InitVars
/// Set up all the elements for the demo window
/// This happens whenever the window is opened or the assemblies have been loaded
/// </summary>
void InitVars()
{
_listElements.Clear();
_slickViewLayout = new SlickViewLayout(32, 24, DrawElement);
_textures = Resources.FindObjectsOfTypeAll<Texture2D>();

// create the slick view class and give it a starting size and delegate for drawing each element
_slickViewLayout = new SlickViewLayout(32, 21, DrawElement);

// the demo window finds all the textures in the project and displays them in order
// over and over until it reaches the specified capacity
_textures = Resources.FindObjectsOfTypeAll<Texture2D>();
for (int i = 0; i < _listElements.Capacity; i++)
{
_listElements.Add(new ExampleListElement(i, _textures[i % _textures.Length]));
Expand All @@ -82,37 +102,70 @@ void InitVars()

private void OnEnable()
{
position = new Rect(200, 200, 300, 400);
wantsLessLayoutEvents = true;
titleContent = new GUIContent("SlickView");
position = new Rect(200, 200, 300, 400);
// wantsLessLayoutEvents = true;
titleContent = new GUIContent("SlickView");
InitVars();
}

private void OnGUI()
{
Init();
InitStyles();

_slickViewLayout.Draw(position, _listElements.Capacity, Box);

// uncomment this to compare performance with SlickView
// ExistingScrollView();
}

/// <summary>
/// ExistingScrollView
/// Example implementation using the existing ScrollView
/// </summary>
void OldScrollView()
{
_scrollPos = EditorGUILayout.BeginScrollView(_scrollPos);
foreach (var el in _listElements)
{
EditorGUILayout.TextField(el.name);
EditorGUILayout.IntField(el.width);
}

EditorGUILayout.EndScrollView();
}


/// <summary>
/// DrawElement
/// Called for each row that is visible
/// </summary>
/// <param name="rect"></param>
/// Area in which to render the elements
/// <param name="row"></param>
/// Row number in the underlying list
/// <param name="selected"></param>
/// Is this row currently selected by the user
void DrawElement(Rect rect, int row, bool selected)
{
ExampleListElement el = _listElements[row];
GUIStyle s = (row & 1) == 0 ? OddBackground : EvenBackground;
s.Draw(rect, false, false, selected, false);
int column = 0;
DrawLabel(ref rect, el.elementNumber, el.columnWidth[column++], false);
DrawLabel(ref rect, el.name, el.columnWidth[column++], false);

// draw some info using the row number as an index into the underlying element array
DrawLabel(ref rect, el.elementNumber, el.columnWidth[column++], selected);
DrawLabel(ref rect, el.name, el.columnWidth[column++], selected);
DrawInt(ref rect, el.width, el.columnWidth[column++]);
DrawInt(ref rect, el.height, el.columnWidth[column++]);
DrawEnum(ref rect, el.format, el.columnWidth[column++]);
DrawTexture(ref rect, el.texture, el.columnWidth[column]);
}

/// Some helper functions for displaying different types
void DrawInt(ref Rect r, int value, int width)
{
r.width = width;
EditorGUI.IntField(r, width);
EditorGUI.IntField(r, value);
r.x += width;
}

Expand All @@ -127,7 +180,14 @@ void DrawLabel(ref Rect r, string value, int width, bool selected)
void DrawTexture(ref Rect r, Texture2D value, int width)
{
r.width = width;
EditorGUI.ObjectField(r, value, typeof(Texture2D), false);
EditorGUI.DrawPreviewTexture(r, value);
r.x += width;
}

void DrawEnum(ref Rect r, Enum value, int width)
{
r.width = width;
EditorGUI.EnumPopup(r, value);
r.x += width;
}
}
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,4 @@ Instead of iterating over the entire list of elements Slick view calculates whic
|:--:|
| *Screenshot of the demo window in SlickView with 10,000 textures* |

To bring up this demo window select Slick View -> Open Demo Window
Binary file modified ScreenshotImage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 1f168fb

Please sign in to comment.