This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
/
WorldSelector.cs
78 lines (65 loc) · 2.4 KB
/
WorldSelector.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using System.Collections.Generic;
using System.Linq;
using Unity.Entities;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine.UIElements;
namespace Improbable.Gdk.Core.Editor.UIElements
{
public class WorldSelector : VisualElement
{
public delegate void WorldChanged(World world);
private const string UxmlPath = "Packages/io.improbable.gdk.core/Editor/UIElements/WorldSelector.uxml";
public World ActiveWorld { get; private set; }
public WorldChanged OnWorldChanged;
private readonly ToolbarMenu menuElement;
public WorldSelector()
{
var template = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(UxmlPath);
template.CloneTree(this);
menuElement = this.Q<ToolbarMenu>("worldSelector");
}
public void UpdateWorldSelection()
{
var worldMenu = menuElement.menu;
var spatialWorlds = new List<World>();
foreach (var world in World.All)
{
if (world.GetExistingSystem<WorkerSystem>() != null)
{
spatialWorlds.Add(world);
}
}
for (var i = 0; i < spatialWorlds.Count; i++)
{
var spatialWorld = spatialWorlds[i];
worldMenu.InsertAction(i, spatialWorld.Name,
action => SelectWorld((World) action.userData),
action => ActiveWorld == (World) action.userData
? DropdownMenuAction.Status.Checked
: DropdownMenuAction.Status.Normal,
spatialWorld);
}
// Trim excess items
var menuSize = worldMenu.MenuItems().Count;
for (var i = spatialWorlds.Count; i < menuSize; i++)
{
worldMenu.RemoveItemAt(spatialWorlds.Count);
}
// Update selected item if needed
if (ActiveWorld == null || !ActiveWorld.IsCreated)
{
SelectWorld(spatialWorlds.FirstOrDefault());
}
}
private void SelectWorld(World world)
{
ActiveWorld = world;
menuElement.text = ActiveWorld?.Name ?? "No SpatialOS worlds active";
OnWorldChanged?.Invoke(world);
}
public new class UxmlFactory : UxmlFactory<WorldSelector>
{
}
}
}