-
Notifications
You must be signed in to change notification settings - Fork 0
/
PropertiesViewController.cs
152 lines (121 loc) · 4.76 KB
/
PropertiesViewController.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//
// Cuboid (http://github.com/arjonagelhout/cuboid)
// Copyright (c) 2023 Arjo Nagelhout
//
using System;
using System.Collections;
using System.Collections.Generic;
using Cuboid.Models;
using UnityEngine;
using Cuboid.Utils;
using TMPro;
using System.Linq;
namespace Cuboid.UI
{
/// <summary>
/// Enable the user to edit properties of the selected reality objects.
/// </summary>
public class PropertiesViewController : MonoBehaviour
{
[SerializeField] private TextMeshProUGUI _headerText;
[SerializeField] private TextMeshProUGUI _propertiesHeaderText;
[SerializeField] private GameObject _propertiesHeader;
[SerializeField] private GameObject _propertiesContent;
[SerializeField] private GameObject _noObjectsSelectedNotice;
private SelectionController _selectionController;
private PropertiesController _propertiesController;
private Action<Selection> _onSelectionChanged;
private Action<PropertiesController.RuntimeSerializedPropertiesData> _onActivePropertiesDataChanged;
private List<GameObject> _instantiatedPropertyGameObjects = new List<GameObject>();
private void Start()
{
_selectionController = SelectionController.Instance;
_propertiesController = PropertiesController.Instance;
_setHeaderText = (value) => _headerText.text = value;
_onActivePropertiesDataChanged = OnActivePropertiesDataChanged;
_onSelectionChanged = OnSelectionChanged;
Register();
}
private void SetVisible(bool visible)
{
_propertiesHeader.SetActive(visible);
}
private RealityObjectData _singleSelectedObject;
private Action<string> _setHeaderText;
private void OnSelectionChanged(Selection selection)
{
if (_singleSelectedObject != null)
{
_singleSelectedObject.Name.OnValueChanged -= _setHeaderText;
_singleSelectedObject = null;
}
bool hasObjects = selection.ContainsObjects;
_noObjectsSelectedNotice.SetActive(!hasObjects);
_headerText.text = selection.GetString("Properties");
// This is a bit of an hack, abusing the
if (selection.SelectedRealityObjects.Count == 1)
{
// register for when the name of that one object changes
_singleSelectedObject = selection.SelectedRealityObjects.First();
_singleSelectedObject.Name.OnValueChanged += _setHeaderText;
}
}
private void OnActivePropertiesDataChanged(PropertiesController.RuntimeSerializedPropertiesData propertiesData)
{
// first, destroy the previously instantiated properties
foreach (GameObject instantiatedPropertyGameObject in _instantiatedPropertyGameObjects)
{
Destroy(instantiatedPropertyGameObject);
}
_instantiatedPropertyGameObjects.Clear();
bool hasProperties = propertiesData != null && propertiesData.Properties.Count > 0;
SetVisible(hasProperties);
if (!hasProperties) { return; }
_propertiesHeaderText.text = $"{propertiesData.TypeName} properties";
foreach (PropertiesController.RuntimeSerializedPropertyData propertyData in propertiesData.Properties)
{
GameObject newPropertyGameObject = PropertiesController.InstantiateProperty(propertyData, _propertiesContent.transform);
if (newPropertyGameObject != null)
{
_instantiatedPropertyGameObjects.Add(newPropertyGameObject);
}
}
}
#region Action registration
private void Register()
{
if (_selectionController != null)
{
_selectionController.Selection.Register(_onSelectionChanged);
}
if (_propertiesController != null)
{
_propertiesController.ActivePropertiesData.Register(_onActivePropertiesDataChanged);
}
}
private void Unregister()
{
if (_selectionController != null)
{
_selectionController.Selection.Unregister(_onSelectionChanged);
}
if (_propertiesController != null)
{
_propertiesController.ActivePropertiesData.Unregister(_onActivePropertiesDataChanged);
}
}
private void OnEnable()
{
Register();
}
private void OnDisable()
{
Unregister();
}
private void OnDestroy()
{
Unregister();
}
#endregion
}
}