-
Notifications
You must be signed in to change notification settings - Fork 101
/
Way.cs
422 lines (420 loc) · 16.3 KB
/
Way.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#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.Collections.Generic;
using System.Linq;
using System.Xml;
using UnityEngine;
using UnityEngine.Events;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace Packages.MapToolbox
{
[ExecuteInEditMode]
public class Way : Member
{
[SerializeField] List<Node> nodes = new List<Node>();
public List<Node> Nodes => nodes;
public UnityAction<Node> OnNodeMoved { get; set; }
public UnityAction<Node> OnAddNode { get; set; }
public UnityAction<Node> OnRemoveNode { get; set; }
public List<Tag> extermTags = new List<Tag>();
Lanelet2Map Lanelet2Map => GetComponentInParent<Lanelet2Map>();
protected void Start()
{
nodes.RemoveNull();
foreach (var item in nodes)
{
item.Ref.RemoveNull();
item.Ref.TryAdd(this);
UnRegistNode(item);
RegistNode(item);
}
}
private void OnDestroy()
{
nodes.RemoveNull();
foreach (var item in nodes)
{
item.Ref.TryRemove(this);
}
}
internal void Load(XmlNode xmlNode)
{
name = xmlNode.Attributes["id"].Value;
nodes.RemoveNull();
foreach (XmlNode nd in xmlNode.SelectNodes("nd"))
{
nodes.Add(Lanelet2Map.transform.Find(nd.Attributes["ref"].Value).GetComponent<Node>());
}
foreach (XmlNode tag in xmlNode.SelectNodes("tag"))
{
switch (tag.Attributes["k"].Value)
{
case "type":
switch (tag.Attributes["v"].Value)
{
case "line_thin":
gameObject.AddComponent<LineThin>();
break;
case "stop_line":
gameObject.AddComponent<StopLine>();
break;
case "traffic_sign":
gameObject.AddComponent<TrafficSign>();
break;
case "traffic_light":
gameObject.AddComponent<TrafficLight>();
break;
case "light_bulbs":
gameObject.AddComponent<LightBulbs>();
break;
case "parking_space":
gameObject.AddComponent<ParkingSpace>();
break;
case "parking_lot":
gameObject.AddComponent<ParkingLot>();
break;
case "pedestrian_marking":
gameObject.AddComponent<PedestrianMarking>();
break;
case "":
extermTags.Add(new Tag(tag));
break;
default:
extermTags.Add(new Tag(tag));
Debug.LogWarning($"Unsupported Way type {tag.Attributes["v"].Value} on {name}");
break;
}
break;
case "subtype":
switch (tag.Attributes["v"].Value)
{
case "solid":
var lineThin = gameObject.GetComponent<LineThin>();
if (lineThin)
{
lineThin.subType = LineThin.SubType.solid;
}
break;
case "dashed":
lineThin = gameObject.GetComponent<LineThin>();
if (lineThin)
{
lineThin.subType = LineThin.SubType.dashed;
}
break;
case "stop_sign":
break;
case "":
extermTags.Add(new Tag(tag));
break;
default:
extermTags.Add(new Tag(tag));
Debug.LogWarning($"Unsupported Way subtype {tag.Attributes["v"].Value} on {name}");
break;
}
break;
case "height":
var traffic_light = gameObject.GetComponent<TrafficLight>();
if (traffic_light)
{
traffic_light.height = float.Parse(tag.Attributes["v"].Value);
}
break;
case "width":
var parking_space = gameObject.GetComponent<ParkingSpace>();
if (parking_space)
{
parking_space.Width = float.Parse(tag.Attributes["v"].Value);
}
break;
case "area":
extermTags.Add(new Tag(tag));
break;
case "traffic_light_id":
extermTags.Add(new Tag(tag));
break;
default:
Debug.LogWarning($"Unsupported Way tag {tag.Attributes["k"].Value} on {name}");
break;
}
}
}
internal XmlElement Save(XmlDocument doc)
{
if(Valide)
{
XmlElement way = doc.CreateElement("way");
way.SetAttribute("id", name);
foreach (var item in nodes)
{
XmlElement nd = doc.CreateElement("nd");
nd.SetAttribute("ref", item.name);
way.AppendChild(nd);
}
var line_thin = GetComponent<LineThin>();
if (line_thin)
{
way.AppendChild(doc.AddTag("type", "line_thin"));
way.AppendChild(doc.AddTag("subtype", line_thin.subType.ToString()));
}
var stop_line = GetComponent<StopLine>();
if (stop_line)
{
way.AppendChild(doc.AddTag("type", "stop_line"));
way.AppendChild(doc.AddTag("subtype", "solid"));
}
var traffic_sign = GetComponent<TrafficSign>();
if (traffic_sign)
{
way.AppendChild(doc.AddTag("type", "traffic_sign"));
way.AppendChild(doc.AddTag("subtype", "stop_sign"));
}
var traffic_light = GetComponent<TrafficLight>();
if (traffic_light)
{
way.AppendChild(doc.AddTag("type", "traffic_light"));
way.AppendChild(doc.AddTag("height", traffic_light.height.ToString()));
}
var parking_lot = GetComponent<ParkingLot>();
if (parking_lot)
{
way.AppendChild(doc.AddTag("type", "parking_lot"));
way.AppendChild(doc.AddTag("area", "yes"));
}
var parking_space = GetComponent<ParkingSpace>();
if (parking_space)
{
way.AppendChild(doc.AddTag("type", "parking_space"));
way.AppendChild(doc.AddTag("width", parking_space.width.ToString()));
}
var pedestrian_marking = GetComponent<PedestrianMarking>();
if (pedestrian_marking)
{
way.AppendChild(doc.AddTag("type", "pedestrian_marking"));
}
foreach (var item in extermTags)
{
way.AppendChild(doc.AddTag(item.k, item.v));
}
return way;
}
return null;
}
internal void EditPoints()
{
if (nodes.Count > 0)
{
var originPositions = nodes.Select(_ => _.transform.position);
for (int i = 0; i < originPositions.Count(); i++)
{
var oldPosition = originPositions.ElementAt(i);
var newPosition = Handles.PositionHandle(oldPosition, Quaternion.identity);
if (!newPosition.Equals(oldPosition))
{
nodes[i].Position = newPosition;
}
}
}
}
internal Node InsertNode(Node node, int index = int.MaxValue)
{
RegistNode(node);
node.Ref.Add(this);
Undo.RegisterCreatedObjectUndo(node, "add node");
if (index < 0)
{
index = 0;
}
else if (index >= Nodes.Count)
{
index = nodes.Count;
}
nodes.Insert(index, node);
OnAddNode?.Invoke(node);
return node;
}
internal Node InsertNode(Vector3 position, int index = int.MaxValue)
{
var node = Node.AddNew(Lanelet2Map, position);
return InsertNode(node, index);
}
internal void RemoveNode(int index)
{
if (nodes.Count == 0)
{
return;
}
if (index < 0)
{
index = 0;
}
else if (index >= nodes.Count)
{
index = nodes.Count - 1;
}
var tmp = nodes[index];
UnRegistNode(tmp);
OnRemoveNode?.Invoke(tmp);
nodes.RemoveAt(index);
Undo.DestroyObjectImmediate(tmp.gameObject);
}
internal void RemoveDuplicatedNodes()
{
for (int i = 1; i < nodes.Count;)
{
if (nodes[i].Equals(nodes[i - 1]))
{
nodes.RemoveAt(i);
}
i++;
}
}
internal bool Valide => NodeCount() > 1;
internal int NodeCount()
{
nodes.RemoveNull();
RemoveDuplicatedNodes();
return nodes.Count;
}
private void RegistNode(Node node)
{
node.OnMoved += OnNodeMovedAction;
node.OnMerged += OnNodeMerged;
node.OnDestroyed += OnNodeDestroyed;
}
private void UnRegistNode(Node node)
{
node.OnMoved -= OnNodeMovedAction;
node.OnMerged -= OnNodeMerged;
node.OnDestroyed -= OnNodeDestroyed;
}
private void OnNodeMovedAction(Node node) => OnNodeMoved?.Invoke(node);
private void OnNodeMerged(Node oldNode, Node newNode)
{
UnRegistNode(oldNode);
RegistNode(newNode);
var index = nodes.IndexOf(oldNode);
nodes.Remove(oldNode);
nodes.Insert(index, newNode);
RemoveDuplicatedNodes();
}
private void OnNodeDestroyed(Node node)
{
UnRegistNode(node);
OnRemoveNode?.Invoke(node);
nodes.Remove(node);
}
}
#if UNITY_EDITOR
[CustomEditor(typeof(Way))]
[CanEditMultipleObjects]
class WayEditor : Editor
{
const int maxMultiEditorCount = 10;
List<Way> Targets = new List<Way>();
Way Target { get; set; }
private void OnEnable()
{
if (targets.Length > maxMultiEditorCount)
{
return;
}
if (targets.Length > 0)
{
if (targets.Length == 1)
{
Target = target as Way;
}
foreach (Way item in targets)
{
Targets.Add(item);
}
}
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
if (Targets.Count > 1)
{
bool contains_stop_line = Targets.Any(_ => _.GetComponent<StopLine>() != null);
if (contains_stop_line)
{
bool contains_traffic_light = Targets.Any(_ => _.GetComponent<TrafficLight>() != null);
bool contains_traffic_sign = Targets.Any(_ => _.GetComponent<TrafficSign>() != null);
if (contains_traffic_light)
{
if (GUILayout.Button("Add traffic light regulatory element"))
{
var stop_line = Targets.Select(_ => _.GetComponent<StopLine>()).Where(_ => _ != null).First();
var re = RegulatoryElement.AddNew(Targets.First().GetComponentInParent<Lanelet2Map>());
re.subType = RegulatoryElement.SubType.traffic_light;
re.ref_line = Targets.Where(_ => _.GetComponent<StopLine>() != null).First();
re.refers = Targets.Where(_ => _.GetComponent<TrafficLight>() != null).First();
re.Relation.Members.Add(re.ref_line);
re.Relation.Members.Add(re.refers);
var relation = stop_line.GetLanelet();
if (relation)
{
relation.Relation.Members.Add(re.Relation);
}
}
}
if (contains_traffic_sign)
{
if (GUILayout.Button("Add traffic sign regulatory element"))
{
var stop_line = Targets.Select(_ => _.GetComponent<StopLine>()).Where(_ => _ != null).First();
var re = RegulatoryElement.AddNew(Targets.First().GetComponentInParent<Lanelet2Map>());
re.subType = RegulatoryElement.SubType.traffic_sign;
re.ref_line = Targets.Where(_ => _.GetComponent<StopLine>() != null).First();
re.refers = Targets.Where(_ => _.GetComponent<TrafficSign>() != null).First();
re.Relation.Members.Add(re.ref_line);
re.Relation.Members.Add(re.refers);
var relation = stop_line.GetLanelet();
if (relation)
{
relation.Relation.Members.Add(re.Relation);
}
}
}
}
}
else
{
if (GUILayout.Button("Remove Duplicated Nodes"))
{
Target.RemoveDuplicatedNodes();
}
}
}
private void OnSceneGUI()
{
if (Targets.Count > 0)
{
Tools.current = Tool.None;
foreach (Way item in Targets)
{
item.EditPoints();
}
}
}
}
#endif
}