This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add list support to Worker Inspector (#1396)
- Loading branch information
Jamie Brynes
authored
Jun 17, 2020
1 parent
8adf7d4
commit 352b85a
Showing
15 changed files
with
479 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
workers/unity/Packages/io.improbable.gdk.debug/WorkerInspector/Codegen/AssemblyInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
using System.Runtime.CompilerServices; | ||
|
||
[assembly: InternalsVisibleTo("Improbable.Gdk.Debug.WorkerInspector.Codegen.EditmodeTests")] |
3 changes: 3 additions & 0 deletions
3
workers/unity/Packages/io.improbable.gdk.debug/WorkerInspector/Codegen/AssemblyInfo.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
149 changes: 149 additions & 0 deletions
149
workers/unity/Packages/io.improbable.gdk.debug/WorkerInspector/Codegen/PaginatedListView.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using UnityEditor; | ||
using UnityEngine; | ||
using UnityEngine.UIElements; | ||
|
||
namespace Improbable.Gdk.Debug.WorkerInspector.Codegen | ||
{ | ||
public class PaginatedListView<TElement, TData> : VisualElement | ||
where TElement : VisualElement | ||
{ | ||
private const string UxmlPath = | ||
"Packages/io.improbable.gdk.debug/WorkerInspector/Templates/PaginatedListView.uxml"; | ||
|
||
private List<TData> data; | ||
|
||
private readonly Action<int, TData, TElement> bindElement; | ||
private readonly VisualElement container; | ||
private readonly ElementPool<TElement> elementPool; | ||
private readonly int elementsPerPage; | ||
|
||
private readonly VisualElement controlsContainer; | ||
private readonly Button forwardButton; | ||
private readonly Button backButton; | ||
private readonly Label pageCounter; | ||
|
||
private int currentPage = 0; | ||
private int numPages = 0; | ||
|
||
public PaginatedListView(string label, Func<TElement> makeElement, Action<int, TData, TElement> bindElement, int elementsPerPage = 5) | ||
{ | ||
this.bindElement = bindElement; | ||
this.elementsPerPage = elementsPerPage; | ||
|
||
var template = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(UxmlPath); | ||
template.CloneTree(this); | ||
|
||
this.Q<Label>(name: "list-name").text = label; | ||
container = this.Q<VisualElement>(className: "user-defined-type-container-data"); | ||
|
||
controlsContainer = this.Q<VisualElement>(className: "paginated-list-controls"); | ||
pageCounter = this.Q<Label>(name: "page-counter"); | ||
|
||
backButton = this.Q<Button>(name: "back-button"); | ||
backButton.clickable.clicked += () => ChangePageCount(-1); | ||
|
||
forwardButton = this.Q<Button>(name: "forward-button"); | ||
forwardButton.clickable.clicked += () => ChangePageCount(1); | ||
|
||
elementPool = new ElementPool<TElement>(makeElement); | ||
} | ||
|
||
public void Update(List<TData> newData) | ||
{ | ||
data = newData; | ||
|
||
if (data.Count == 0) | ||
{ | ||
controlsContainer.AddToClassList("hidden"); | ||
} | ||
else | ||
{ | ||
controlsContainer.RemoveFromClassList("hidden"); | ||
} | ||
|
||
CalculatePages(); | ||
RefreshView(); | ||
} | ||
|
||
internal void ChangePageCount(int diff) | ||
{ | ||
currentPage += diff; | ||
currentPage = Mathf.Clamp(currentPage, 0, numPages - 1); | ||
CalculatePages(); | ||
RefreshView(); | ||
} | ||
|
||
private void CalculatePages() | ||
{ | ||
numPages = Mathf.CeilToInt((float) data.Count / elementsPerPage); | ||
numPages = Mathf.Clamp(numPages, 1, numPages); | ||
currentPage = Mathf.Clamp(currentPage, 0, numPages - 1); | ||
|
||
pageCounter.text = $"{currentPage + 1}/{numPages}"; | ||
} | ||
|
||
private void RefreshView() | ||
{ | ||
// Calculate slice of list to be rendered. | ||
var firstIndex = currentPage * elementsPerPage; | ||
var length = Math.Min(elementsPerPage, data.Count - firstIndex); | ||
|
||
// If the child count is the same, don't adjust it. | ||
// If the child count is less, add the requisite number. | ||
// If the child count is more, pop elements off the end. | ||
|
||
var diff = container.childCount - length; | ||
|
||
if (diff > 0) | ||
{ | ||
for (var i = 0; i < diff; i++) | ||
{ | ||
var element = container.ElementAt(container.childCount - 1); | ||
container.RemoveAt(container.childCount - 1); | ||
elementPool.Return((TElement) element); | ||
} | ||
} | ||
else if (diff < 0) | ||
{ | ||
for (var i = diff; i < 0; i++) | ||
{ | ||
container.Add(elementPool.GetOrCreate()); | ||
} | ||
} | ||
|
||
// At this point, container.Children() has the same length as the slice. | ||
var elementIndex = firstIndex; | ||
foreach (var child in container.Children()) | ||
{ | ||
bindElement(elementIndex, data[elementIndex], (TElement) child); | ||
elementIndex++; | ||
} | ||
|
||
backButton.SetEnabled(currentPage != 0); | ||
forwardButton.SetEnabled(currentPage != numPages - 1); | ||
} | ||
} | ||
|
||
internal class ElementPool<TElement> where TElement : VisualElement | ||
{ | ||
private readonly Stack<TElement> pool = new Stack<TElement>(); | ||
private readonly Func<TElement> makeElement; | ||
|
||
public ElementPool(Func<TElement> makeElement) | ||
{ | ||
this.makeElement = makeElement; | ||
} | ||
|
||
public TElement GetOrCreate() | ||
{ | ||
return pool.Count == 0 ? makeElement() : pool.Pop(); | ||
} | ||
|
||
public void Return(TElement element) | ||
{ | ||
pool.Push(element); | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
.../unity/Packages/io.improbable.gdk.debug/WorkerInspector/Codegen/PaginatedListView.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
workers/unity/Packages/io.improbable.gdk.debug/WorkerInspector/Codegen/Tests.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
19 changes: 19 additions & 0 deletions
19
...Inspector/Codegen/Tests/Improbable.Gdk.Debug.WorkerInspector.Codegen.EditmodeTests.asmdef
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "Improbable.Gdk.Debug.WorkerInspector.Codegen.EditmodeTests", | ||
"references": [ | ||
"Improbable.Gdk.Debug.WorkerInspector.Codegen" | ||
], | ||
"includePlatforms": [ | ||
"Editor" | ||
], | ||
"excludePlatforms": [], | ||
"allowUnsafeCode": false, | ||
"overrideReferences": true, | ||
"precompiledReferences": [ | ||
"nunit.framework.dll" | ||
], | ||
"autoReferenced": false, | ||
"defineConstraints": [], | ||
"versionDefines": [], | ||
"noEngineReferences": false | ||
} |
7 changes: 7 additions & 0 deletions
7
...ctor/Codegen/Tests/Improbable.Gdk.Debug.WorkerInspector.Codegen.EditmodeTests.asmdef.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.