Files
RebootKit/Editor/RebootWindow/GameServicesView.cs
2025-04-14 23:22:38 +02:00

103 lines
3.4 KiB
C#

using System.Collections.Generic;
using RebootKit.Engine;
using RebootKit.Engine.Foundation;
using RebootKit.Engine.UI;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;
namespace RebootKitEditor.RebootWindow {
public class GameServicesView : IView {
VisualElement m_RootElement;
readonly List<Editor> m_ServiceEditors = new();
[Inject] EngineConfigAsset m_EngineConfigAsset;
public void Dispose() {
foreach (Editor editor in m_ServiceEditors) {
if (editor != null) {
Object.DestroyImmediate(editor);
}
}
m_ServiceEditors.Clear();
}
public VisualElement Build() {
m_RootElement = new ScrollView();
Label servicesAmountLabel = new($"Game services: {m_EngineConfigAsset.services.Length}") {
style = {
color = new Color(0.7f, 0.9f, 0.9f),
unityFontStyleAndWeight = FontStyle.Bold
}
};
m_RootElement.Add(servicesAmountLabel);
for (int i = 0; i < m_EngineConfigAsset.services.Length; i++) {
ServiceAsset serviceAsset = m_EngineConfigAsset.services[i];
VisualElement serviceView = CreateServicesView(serviceAsset);
serviceView.style.backgroundColor = i % 2 == 0 ? new Color(0.1f, 0.1f, 0.1f) : new Color(0.2f, 0.2f, 0.2f);
m_RootElement.Add(serviceView);
}
return m_RootElement;
}
VisualElement CreateServicesView<T>(ServiceAsset<T> serviceAsset) where T : class, IService {
VisualElement root = new() {
style = {
paddingBottom = 4,
paddingTop = 4,
paddingLeft = 4,
paddingRight = 4,
borderBottomLeftRadius = 4,
borderBottomRightRadius = 4
}
};
VisualElement header = new() {
style = {
backgroundColor = new Color(0.2f, 0.2f, 0.2f),
paddingLeft = 8,
paddingRight = 8,
paddingTop = 4,
paddingBottom = 4,
borderTopLeftRadius = 4,
borderTopRightRadius = 4,
}
};
root.Add(header);
Label nameLabel = new(serviceAsset.name) {
style = {
color = new Color(0.7f, 0.9f, 0.9f),
unityFontStyleAndWeight = FontStyle.Bold
}
};
header.Add(nameLabel);
VisualElement editorView = new() {
style = {
backgroundColor = new Color(0.3f, 0.3f, 0.3f),
paddingLeft = 10,
paddingRight = 10,
paddingTop = 5,
paddingBottom = 5,
minHeight = 50
}
};
root.Add(editorView);
Editor editor = Editor.CreateEditor(serviceAsset);
m_ServiceEditors.Add(editor);
InspectorElement inspectorElement = new(editor);
editorView.Add(inspectorElement);
return root;
}
}
}