Skip to content
This repository has been archived by the owner on Jan 18, 2022. It is now read-only.

Commit

Permalink
Add worker details to worker inspector (#1382)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie Brynes authored Jun 4, 2020
1 parent 6e75771 commit 738f0a7
Show file tree
Hide file tree
Showing 13 changed files with 244 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
- Added a "C# Editor Output Directory" field to the GDK Tools Configuration. [#1376](https://github.com/spatialos/gdk-for-unity/pull/1376)
- This specifies where Editor-only code is generated to.
- This defaults to `Assets/Generated/Editor`.
- `EntityId` now implements `IComparable<EntityId>`. [#1375](https://github.com/spatialos/gdk-for-unity/pull/1375)
- The `ComponentDatabase` now exposes a non-generic `GetComponentId(Type type)` static method. [#1379](https://github.com/spatialos/gdk-for-unity/pull/1379)
- Added a new "Worker Inspector" Editor window. [#1375](https://github.com/spatialos/gdk-for-unity/pull/1375) [#1379](https://github.com/spatialos/gdk-for-unity/pull/1379) [#1382](https://github.com/spatialos/gdk-for-unity/pull/1382)
- This window displays worker information like: worker flags, worker ID, and worker type.
- This window also displays the entities that a worker has checked out.
- For each entity checked out, you can view the components on that entity and whether the worker is authoritative over that component.

### Fixed

Expand Down
2 changes: 2 additions & 0 deletions workers/unity/Packages/io.improbable.gdk.core/View/View.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace Improbable.Gdk.Core
{
public class View
{
public IReadOnlyDictionary<string, string> WorkerFlags => workerFlags;

private readonly Dictionary<Type, IViewStorage> typeToViewStorage = new Dictionary<Type, IViewStorage>();
private readonly Dictionary<uint, IViewStorage> componentIdToViewStorage = new Dictionary<uint, IViewStorage>();
private readonly List<IViewStorage> viewStorages = new List<IViewStorage>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<UXML xmlns="UnityEngine.UIElements">
<VisualElement name="worker-details-container">
<VisualElement name="worker-info">
<TextField name="worker-type" label="Worker Type"/>
<TextField name="worker-id" label="Worker ID"/>
</VisualElement>
<VisualElement name="worker-flags">
<Label text="Worker Flags"/>
<ListView name="worker-flags-list">
</ListView>
</VisualElement>
</VisualElement>
</UXML>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.columns {
#entities {
flex-direction: row;
flex-grow: 1;
}
Expand All @@ -9,7 +9,7 @@
--unity-item-height: 24;
}

.unity-list-view__item {
#entity-list-view .unity-list-view__item {
padding-left: 8px;
-unity-text-align: middle-left;
}
Expand Down Expand Up @@ -74,6 +74,61 @@
padding-bottom: 4px;
}

.is-auth-toggle .unity-label:disabled {
color: white;
#worker-details {
padding: 8px 4px;
border-top-color: rgba(0, 0, 0, 0.20);
border-top-width: 1px;
height: 15%;
}

#worker-details-container {
flex-direction: row;
flex-grow: 1;
}

#worker-details-label {
-unity-font-style: bold;
font-size: 18px;
margin-bottom: 8px;
}

#worker-info {
max-width: 40%;
flex-grow: 1;
padding-left: 4px;
}

#worker-type .unity-label {
min-width: 100px;
}

#worker-id .unity-label {
min-width: 100px;
}

#worker-flags {
max-width: 60%;
flex-grow: 2;
padding-left: 20px;
}

#worker-flags-list {
margin-top: 6px;
flex-grow: 1;
flex-direction: column;
--unity-item-height: 24;
-unity-text-align: middle-left;
}

#worker-flags-list .unity-list-view__item {
padding-left: 2px;
}

.flag-element-container {
flex-direction: row;
}

.flag-element {
flex-grow: 1;
max-width: 50%;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
xmlns:wi="Improbable.Gdk.Debug.WorkerInspector"
>
<gdk:WorldSelector />
<VisualElement class="columns">
<wi:WorkerDetail name="worker-details"/>
<VisualElement name="entities">
<wi:EntityList name="entity-panel"/>
<wi:EntityDetail name="entity-detail" />
</VisualElement>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.is-auth-toggle .unity-label:disabled {
color: white;
}

#worker-info .unity-label:disabled {
color: white;
}

#worker-flags .unity-label:disabled {
color: white;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.is-auth-toggle .unity-label:disabled {
color: black;
}

#worker-info .unity-label:disabled {
color: black;
}

#worker-flags .unity-label:disabled {
color: black;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using System.Collections.Generic;
using Improbable.Gdk.Core;
using Unity.Entities;
using UnityEditor;
using UnityEngine.UIElements;

namespace Improbable.Gdk.Debug.WorkerInspector
{
internal class WorkerDetail : VisualElement
{
private const string FlagKeyName = "key";
private const string FlagValueName = "value";

private readonly TextField workerType;
private readonly TextField workerId;
private readonly ListView workerFlags;

private readonly List<KeyValuePair<string, string>> workerFlagData = new List<KeyValuePair<string, string>>();

private View view;

public WorkerDetail()
{
const string uxmlPath = "Packages/io.improbable.gdk.debug/WorkerInspector/Templates/WorkerDetail.uxml";
var template = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(uxmlPath);
template.CloneTree(this);

workerType = this.Q<TextField>("worker-type");
workerType.SetEnabled(false);

workerId = this.Q<TextField>("worker-id");
workerId.SetEnabled(false);

workerFlags = this.Q<ListView>("worker-flags-list");
workerFlags.makeItem = MakeItem;
workerFlags.bindItem = BindElement;
workerFlags.itemsSource = workerFlagData;
workerFlags.SetEnabled(false);
}

public void SetWorld(World world)
{
if (world == null)
{
workerType.value = "";
workerId.value = "";
workerFlagData.Clear();
workerFlags.Refresh();
view = null;
return;
}

var workerSystem = world.GetExistingSystem<WorkerSystem>();

workerType.value = workerSystem.WorkerType;
workerId.value = workerSystem.WorkerId;

view = workerSystem.View;
Update();
}

public void Update()
{
if (view == null)
{
return;
}

workerFlagData.Clear();

foreach (var pair in view.WorkerFlags)
{
workerFlagData.Add(pair);
}

workerFlags.Refresh();
}

private static VisualElement MakeItem()
{
var container = new VisualElement();
container.AddToClassList("flag-element-container");

var key = new Label { name = FlagKeyName, };

key.AddToClassList("flag-element");

var value = new Label { name = FlagValueName };

value.AddToClassList("flag-element");

container.Add(key);
container.Add(value);

return container;
}

private void BindElement(VisualElement element, int index)
{
var key = element.Q<Label>(FlagKeyName);
var value = element.Q<Label>(FlagValueName);
var kvp = workerFlagData[index];

key.text = kvp.Key;
value.text = kvp.Value;
}

public new class UxmlFactory : UxmlFactory<WorkerDetail>
{
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ internal class WorkerInspectorWindow : EditorWindow
private WorldSelector worldSelector;
private EntityList entityList;
private EntityDetail entityDetail;
private WorkerDetail workerDetail;

[MenuItem("SpatialOS/Window/Worker Inspector", false)]
public static void ShowWindow()
Expand All @@ -30,32 +31,47 @@ private void OnInspectorUpdate()
worldSelector.UpdateWorldSelection();
entityList.Update();
entityDetail.Update();
workerDetail.Update();
}

private void SetupUI()
{
const string windowUxmlPath = "Packages/io.improbable.gdk.debug/WorkerInspector/Templates/WorkerInspectorWindow.uxml";
const string windowUssPath = "Packages/io.improbable.gdk.debug/WorkerInspector/Templates/WorkerInspectorWindow.uss";

const string darkModeUssPath =
"Packages/io.improbable.gdk.debug/WorkerInspector/Templates/WorkerInspectorWindow_Dark.uss";
const string lightModeUssPath =
"Packages/io.improbable.gdk.debug/WorkerInspector/Templates/WorkerInspectorWindow_Light.uss";

var windowTemplate = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(windowUxmlPath);
windowTemplate.CloneTree(rootVisualElement);

var stylesheet = AssetDatabase.LoadAssetAtPath<StyleSheet>(windowUssPath);
rootVisualElement.styleSheets.Add(stylesheet);

var themedSheet =
AssetDatabase.LoadAssetAtPath<StyleSheet>(EditorGUIUtility.isProSkin
? darkModeUssPath
: lightModeUssPath);
rootVisualElement.styleSheets.Add(themedSheet);

worldSelector = rootVisualElement.Q<WorldSelector>();
worldSelector.OnWorldChanged += OnWorldChanged;

entityList = rootVisualElement.Q<EntityList>();
entityList.OnEntitySelected += OnEntitySelected;

entityDetail = rootVisualElement.Q<EntityDetail>();

workerDetail = rootVisualElement.Q<WorkerDetail>();
}

private void OnWorldChanged(World world)
{
entityList.SetWorld(world);
entityDetail.SetWorld(world);
workerDetail.SetWorld(world);
}

private void OnEntitySelected(EntityData entityData)
Expand Down

0 comments on commit 738f0a7

Please sign in to comment.