Skip to content

Commit

Permalink
Reworked (and working) server selector.
Browse files Browse the repository at this point in the history
  • Loading branch information
kring committed Nov 30, 2023
1 parent 837f0e6 commit 321ba6a
Show file tree
Hide file tree
Showing 10 changed files with 271 additions and 31 deletions.
13 changes: 10 additions & 3 deletions Editor/CesiumEditorWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,25 @@ public static void ShowWindow()

void OnEnable()
{
this._serverSelector = new CesiumIonServerSelector(this);

// Load the icon separately from the other resources.
Texture2D icon = Resources.Load<Texture2D>("Cesium-64x64");
icon.wrapMode = TextureWrapMode.Clamp;
this.titleContent = new GUIContent("Cesium", icon);
}

CesiumIonSession.Ion().Resume();
private void OnDisable()
{
this._serverSelector.Dispose();
this._serverSelector = null;
}

private bool _isIonConnected = false;
private bool _isIonConnecting = false;
private bool _isIonProfileLoaded = false;
private bool _isIonLoadingProfile = false;
private CesiumIonServerSelector _serverSelector;

private Vector2 _scrollPosition = Vector2.zero;

Expand All @@ -53,14 +60,14 @@ void OnGUI()
// OnGUI is invoked in the Layout event.
if (Event.current.type == EventType.Layout)
{
CesiumIonSession ion = CesiumIonSession.Ion();
CesiumIonSession ion = CesiumIonServerManager.instance.CurrentSession;
this._isIonConnected = ion.IsConnected();
this._isIonConnecting = ion.IsConnecting();
this._isIonProfileLoaded = ion.IsProfileLoaded();
this._isIonLoadingProfile = ion.IsLoadingProfile();
}

CesiumIonServerUI.Selector();
this._serverSelector.OnGUI();
this.DrawCesiumToolbar();

this._scrollPosition = EditorGUILayout.BeginScrollView(this._scrollPosition);
Expand Down
10 changes: 8 additions & 2 deletions Editor/CesiumIonAssetsWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ public static void ShowWindow()
private TreeViewState _assetsTreeState;
private IonAssetsTreeView _assetsTreeView;
private SearchField _searchField;
private CesiumIonServerSelector _serverSelector;

private void OnEnable()
{
{
this._serverSelector = new CesiumIonServerSelector(this);

// Load the icon separately from the other resources.
Texture2D icon = Resources.Load<Texture2D>("Cesium-64x64");
icon.wrapMode = TextureWrapMode.Clamp;
Expand All @@ -53,6 +56,9 @@ private void OnDisable()
{
CesiumIonSession.OnConnectionUpdated -= this._assetsTreeView.Refresh;
CesiumIonSession.OnAssetsUpdated -= this._assetsTreeView.Refresh;

this._serverSelector.Dispose();
this._serverSelector = null;
}

void BuildTreeView()
Expand Down Expand Up @@ -95,7 +101,7 @@ void DrawRefreshButtonAndSearchBar()

EditorGUILayout.BeginVertical();
EditorGUILayout.Space(15.0f);
CesiumIonServerUI.Selector();
this._serverSelector.OnGUI();
EditorGUILayout.EndVertical();

if (GUILayout.Button(
Expand Down
55 changes: 50 additions & 5 deletions Editor/CesiumIonServerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
using System;
using UnityEditor;
using UnityEngine;
using UnityEditor.Scripting;
using System.Diagnostics;

namespace CesiumForUnity
{
[FilePath("UserSettings/CesiumIonServerManager.asset", FilePathAttribute.Location.ProjectFolder)]
public class CesiumIonServerManager : ScriptableSingleton<CesiumIonServerManager>
{
public event Action<CesiumIonServerManager> CurrentChanged;
public event Action<CesiumIonServerManager> ServerListChanged;

public CesiumIonServer Current
{
get
Expand All @@ -22,10 +23,8 @@ public CesiumIonServer Current
// and move it to the user access token map.
if (string.IsNullOrEmpty(this.GetUserAccessToken(this._currentCesiumIonServer)))
{
UnityEngine.Debug.Log("Checking for backward compatible access token.");
const string editorPrefKey = "CesiumUserAccessToken";
string userAccessToken = EditorPrefs.GetString(editorPrefKey);
UnityEngine.Debug.Log("Old token: " + userAccessToken);
if (!string.IsNullOrEmpty(userAccessToken))
{
this.SetUserAccessToken(this._currentCesiumIonServer, userAccessToken);
Expand All @@ -39,6 +38,8 @@ public CesiumIonServer Current
set
{
this._currentCesiumIonServer = value;
// TODO: set "current for new objects" in Runtime library
CurrentChanged?.Invoke(this);
}
}

Expand All @@ -61,6 +62,41 @@ public CesiumIonSession GetSession(CesiumIonServer server)
return session;
}

public void ResumeAll()
{
foreach (CesiumIonServer server in this._servers)
{
CesiumIonSession session = this.GetSession(server);
if (session != null)
{
session.Resume();
session.GetProfileUsername();
}
}
}

public IReadOnlyList<CesiumIonServer> Servers
{
get
{
this.RefreshServerList();
return this._servers;
}
}

public void RefreshServerList()
{
this._servers = new List<CesiumIonServer>();

string[] guids = AssetDatabase.FindAssets("t:" + typeof(CesiumIonServer).FullName);
foreach (string guid in guids)
{
string path = AssetDatabase.GUIDToAssetPath(guid);
this._servers.Add(AssetDatabase.LoadAssetAtPath<CesiumIonServer>(path));
}
ServerListChanged?.Invoke(this);
}

internal string GetUserAccessToken(CesiumIonServer server)
{
int index = this._userAccessTokenMap.FindIndex(record => record.server == server);
Expand All @@ -81,10 +117,18 @@ internal void SetUserAccessToken(CesiumIonServer server, string token)
record.token = token;
this._userAccessTokenMap.Add(record);
}

//
this.Save(true);
}

class RefreshServers : AssetPostprocessor
{
static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths, bool didDomainReload)
{
CesiumIonServerManager.instance.ResumeAll();
}
}

[Serializable]
private class UserAccessTokenRecord
{
Expand All @@ -99,5 +143,6 @@ private class UserAccessTokenRecord
private CesiumIonServer _currentCesiumIonServer;

private Dictionary<CesiumIonServer, CesiumIonSession> _sessions = new Dictionary<CesiumIonServer, CesiumIonSession>();
private List<CesiumIonServer> _servers = new List<CesiumIonServer>();
}
}
112 changes: 112 additions & 0 deletions Editor/CesiumIonServerSelector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using System;
using UnityEditor;
using UnityEditor.IMGUI.Controls;
using UnityEngine;

namespace CesiumForUnity
{
public class CesiumIonServerSelector : IDisposable
{
private EditorWindow _parent;
private AdvancedDropdownState _dropDownState = new AdvancedDropdownState();

public CesiumIonServerSelector(EditorWindow parent)
{
this._parent = parent;
CesiumIonServerManager.instance.CurrentChanged += OnCurrentChanged;
}

public void Dispose()
{
CesiumIonServerManager.instance.CurrentChanged -= OnCurrentChanged;
}

private void OnCurrentChanged(CesiumIonServerManager manager)
{
this._parent.Repaint();
}

public void OnGUI()
{
CesiumIonServer server = CesiumIonServerManager.instance.Current;
GUIContent content = new GUIContent(GetLabelFromCesiumIonServer(server), "The current Cesium ion server");
Rect rect = EditorGUILayout.BeginHorizontal();
if (EditorGUILayout.DropdownButton(content, FocusType.Keyboard))
{
var serverDropDown = new ServerDropDown(/*this._dropDownState*/ new AdvancedDropdownState(), rect);
serverDropDown.Show(rect);
}
EditorGUILayout.EndHorizontal();


//CesiumIonServer newServer = (CesiumIonServer)EditorGUILayout.ObjectField(server, typeof(CesiumIonServer), false);
//if (newServer != CesiumIonServerManager.instance.Current)
//{
// Debug.Log("Changing server");
// CesiumIonServerManager.instance.Current = newServer;
//}
}

public static string GetLabelFromCesiumIonServer(CesiumIonServer server)
{
if (server == null)
return "Error: No Cesium ion server configured.";

CesiumIonSession session = CesiumIonServerManager.instance.GetSession(server);

string profileName = session.GetProfileUsername();

string prefix = "";
string suffix = "";

if (session.IsConnecting() || session.IsResuming())
suffix = " (connecting...)";
else if (session.IsLoadingProfile())
suffix = " (loading profile...)";
else if (session.IsConnected() && session.IsProfileLoaded())
prefix = profileName + " @ ";
else
suffix = " (not connected)";

return prefix + server.name + suffix;
}

private class ServerDropDownItem : AdvancedDropdownItem
{
public CesiumIonServer server;

public ServerDropDownItem(CesiumIonServer server)
: base(GetLabelFromCesiumIonServer(server))
{
this.server = server;
}
}

private class ServerDropDown : AdvancedDropdown
{
public ServerDropDown(AdvancedDropdownState state, Rect rect)
: base(state)
{
int itemCount = Math.Min(10, CesiumIonServerManager.instance.Servers.Count);
this.minimumSize = new Vector2(50.0f, (itemCount + 3) * EditorGUIUtility.singleLineHeight);
}

protected override AdvancedDropdownItem BuildRoot()
{
AdvancedDropdownItem root = new AdvancedDropdownItem("Cesium ion Servers");

foreach (CesiumIonServer server in CesiumIonServerManager.instance.Servers)
{
root.AddChild(new ServerDropDownItem(server));
}

return root;
}

protected override void ItemSelected(AdvancedDropdownItem item)
{
CesiumIonServerManager.instance.Current = ((ServerDropDownItem)item).server;
}
}
}
}
File renamed without changes.
18 changes: 0 additions & 18 deletions Editor/CesiumIonServerUI.cs

This file was deleted.

4 changes: 4 additions & 0 deletions Editor/ConfigureReinterop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public void ExposeToCPP()
EditorPrefs.DeleteKey("Key");

Application.OpenURL("URL");
string temporaryCachePath = Application.temporaryCachePath;
string applicationVersion = Application.version;
string unityVersion = Application.unityVersion;
string applicationPlatform = Helpers.ToString(Application.platform);
Expand Down Expand Up @@ -149,6 +150,9 @@ public void ExposeToCPP()
string tokenID = CesiumRuntimeSettings.defaultIonAccessTokenID;
CesiumRuntimeSettings.defaultIonAccessTokenID = "tokenID";

int requestsPerCachePrune = CesiumRuntimeSettings.requestsPerCachePrune;
ulong maxItems = CesiumRuntimeSettings.maxItems;

Cesium3DTileset[] tilesets = UnityEngine.Object.FindObjectsOfType<Cesium3DTileset>();
Cesium3DTileset tileset = tilesets[0];
for (int i = 0; i < tilesets.Length; i++)
Expand Down
6 changes: 3 additions & 3 deletions native~/Editor/src/CesiumIonSessionImpl.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "CesiumIonSessionImpl.h"

#include "UnityAssetAccessor.h"
#include "UnityExternals.h"
#include "UnityTaskProcessor.h"

#include <DotNet/CesiumForUnity/CesiumIonServer.h>
Expand All @@ -19,9 +20,8 @@ CesiumIonSessionImpl& CesiumIonSessionImpl::ion() {

CesiumIonSessionImpl::CesiumIonSessionImpl(
const DotNet::CesiumForUnity::CesiumIonSession& session)
: _asyncSystem(
CesiumAsync::AsyncSystem(std::make_shared<UnityTaskProcessor>())),
_pAssetAccessor(std::make_shared<UnityAssetAccessor>()),
: _asyncSystem(CesiumForUnityNative::getAsyncSystem()),
_pAssetAccessor(CesiumForUnityNative::getAssetAccessor()),
_connection(std::nullopt),
_profile(std::nullopt),
_assets(std::nullopt),
Expand Down
Loading

0 comments on commit 321ba6a

Please sign in to comment.