-
Notifications
You must be signed in to change notification settings - Fork 101
/
Lanelet2Map.cs
199 lines (194 loc) · 7.28 KB
/
Lanelet2Map.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#region License
/******************************************************************************
* Copyright 2018-2021 The AutoCore Authors. All Rights Reserved.
*
* Licensed under the GNU Lesser General Public License, Version 3.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.gnu.org/licenses/lgpl-3.0.html
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*****************************************************************************/
#endregion
using System.Reflection;
using System.Xml;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace Packages.MapToolbox
{
[ExecuteInEditMode]
public class Lanelet2Map : MonoBehaviour
{
private void Awake() => EditorUpdate.Instance.transform.SetAsFirstSibling();
public void Load(string filename)
{
var doc = new XmlDocument();
doc.Load(filename);
var osm = doc.SelectSingleNode("osm");
foreach (XmlNode item in osm.SelectNodes("node"))
{
this.AddChildGameObject<Node>().Load(item);
}
foreach (XmlNode item in osm.SelectNodes("way"))
{
this.AddChildGameObject<Way>().Load(item);
}
foreach (XmlNode item in osm.SelectNodes("relation"))
{
this.AddChildGameObject<Relation>().Load(item);
}
var collectionRelation = GetComponentsInChildren<Relation>();
foreach (var relation in collectionRelation)
{
foreach (var item in relation.RelationId)
{
relation.Members.Add(transform.Find(item).GetComponent<Relation>());
}
}
}
public void Save(string filename)
{
ReIndex();
var info = UnityEditor.PackageManager.PackageInfo.FindForAssembly(Assembly.GetExecutingAssembly());
XmlDocument doc = new XmlDocument();
doc.AppendChild(doc.CreateXmlDeclaration("1.0", "UTF-8", null));
XmlElement osm = doc.CreateElement("osm");
osm.SetAttribute("generator", info.displayName);
osm.SetAttribute("version", info.version);
doc.AppendChild(osm);
foreach (var item in GetComponentsInChildren<Node>())
{
osm.AppendChild(item.Save(doc));
}
foreach (var item in GetComponentsInChildren<Way>())
{
if (item.Valide)
{
var xml = item.Save(doc);
if (xml != null)
{
osm.AppendChild(xml);
}
}
}
foreach (var item in GetComponentsInChildren<Relation>())
{
if (item.Valide)
{
var xml = item.Save(doc);
if (xml != null)
{
osm.AppendChild(xml);
}
}
}
doc.Save(filename);
Undo.RegisterFullObjectHierarchyUndo(gameObject, "Save map");
}
internal static void CreateNew()
{
var go = CreateNewGo();
go.RecordUndoCreateGo();
Selection.activeObject = go;
}
private static GameObject CreateNewGo() => new GameObject(typeof(Lanelet2Map).Name, typeof(Lanelet2Map));
internal void AddLanelet() => Selection.activeObject = Lanelet.AddNew(this);
internal void AddTrafficLight() => Selection.activeObject = TrafficLight.AddNew(this);
internal void AddTrafficSign() => Selection.activeObject = TrafficSign.AddNew(this);
internal void AddParkingLot() => Selection.activeObject = ParkingLot.AddNew(this);
internal void AddParkingSpace() => Selection.activeObject = ParkingSpace.AddNew(this);
internal void AddPedestrianMarking() => Selection.activeObject = PedestrianMarking.AddNew(this);
internal void ReIndex()
{
for (int i = 0; i < transform.childCount; i++)
{
transform.GetChild(i).name = (i + 1).ToString();
}
}
}
[CustomEditor(typeof(Lanelet2Map))]
class Lanelet2MapEditor : Editor
{
[MenuItem("GameObject/Autoware/Lanelet2Map", false, 10)]
static void CreateNew() => Lanelet2Map.CreateNew();
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
if (GUILayout.Button("Save"))
{
string saveFile = EditorUtility.SaveFilePanel(string.Empty, Application.dataPath, "lanelet2_map", "osm");
if (!string.IsNullOrEmpty(saveFile))
{
(target as Lanelet2Map).Save(saveFile);
}
}
if (GUILayout.Button("Add Lanelet"))
{
(target as Lanelet2Map).AddLanelet();
}
if (GUILayout.Button("Add TrafficLight"))
{
(target as Lanelet2Map).AddTrafficLight();
}
if (GUILayout.Button("Add TrafficSign"))
{
(target as Lanelet2Map).AddTrafficSign();
}
if (GUILayout.Button("Add ParkingLot"))
{
(target as Lanelet2Map).AddParkingLot();
}
if (GUILayout.Button("Add ParkingSpace"))
{
(target as Lanelet2Map).AddParkingSpace();
}
if (GUILayout.Button("Add PedestrianMarking"))
{
(target as Lanelet2Map).AddPedestrianMarking();
}
if (GUILayout.Button("ReIndex Entities"))
{
(target as Lanelet2Map).ReIndex();
}
if (GUILayout.Button("Clear Filter"))
{
SceneModeUtility.SearchForType(null);
}
if (GUILayout.Button("Filter Lanelet"))
{
SceneModeUtility.SearchForType(typeof(Lanelet));
}
if (GUILayout.Button("Filter StopLine"))
{
SceneModeUtility.SearchForType(typeof(StopLine));
}
if (GUILayout.Button("Filter TrafficLight"))
{
SceneModeUtility.SearchForType(typeof(TrafficLight));
}
if (GUILayout.Button("Filter TrafficSign"))
{
SceneModeUtility.SearchForType(typeof(TrafficSign));
}
if (GUILayout.Button("Filter ParkingLot"))
{
SceneModeUtility.SearchForType(typeof(ParkingLot));
}
if (GUILayout.Button("Filter ParkingSpace"))
{
SceneModeUtility.SearchForType(typeof(ParkingSpace));
}
if (GUILayout.Button("Filter PedestrianMarking"))
{
SceneModeUtility.SearchForType(typeof(PedestrianMarking));
}
}
}
}