-
Notifications
You must be signed in to change notification settings - Fork 0
/
Portal.cs
583 lines (519 loc) · 17 KB
/
Portal.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
#region copyright
// BuildR 2.0
// Available on the Unity Asset Store https://www.assetstore.unity3d.com/#!/publisher/412
// Copyright (c) 2017 Jasper Stocker http://support.jasperstocker.com
// Support contact email@jasperstocker.com
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
#endregion
using System;
using System.Collections.Generic;
using UnityEngine;
namespace BuildR2
{
public class Portal : ScriptableObject, ISerializationCallbackReceiver
{
#region Serialisation
public List<SerializableDivision> serializedNodes = new List<SerializableDivision>();
public void OnBeforeSerialize()
{
//ignore calls - serialisation handled internally when data has changed
}
public void OnAfterDeserialize()
{
if (serializedNodes.Count > 0)
_root = ReadNodeFromSerializedNodes(0);
else
_root = new Division();
}
public void Serialise()
{
serializedNodes.Clear();
AddNodeToSerializedNodes(root);
}
void AddNodeToSerializedNodes(Division n)
{
var serializedNode = new SerializableDivision()
{
name = n.name,
frame = n.frame,
recess = n.recess,
size = n.size,
type = n.type,
divisionType = n.divisionType,
surface = n.surface,
identicalChildren = n.identicalChildren,
identicalChildCount = n.identicalChildCount,
expanded = n.expanded,
childCount = n.children.Count,
indexOfFirstChild = serializedNodes.Count + 1
};
serializedNodes.Add(serializedNode);
foreach (var child in n.children)
AddNodeToSerializedNodes(child);
}
Division ReadNodeFromSerializedNodes(int index)
{
var serializedNode = serializedNodes[index];
var children = new List<Division>();
for (int i = 0; i != serializedNode.childCount; i++)
children.Add(ReadNodeFromSerializedNodes(serializedNode.indexOfFirstChild + i));
return new Division()
{
name = serializedNode.name,
frame = serializedNode.frame,
recess = serializedNode.recess,
size = serializedNode.size,
type = serializedNode.type,
divisionType = serializedNode.divisionType,
surface = serializedNode.surface,
expanded = serializedNode.expanded,
identicalChildren = serializedNode.identicalChildren,
identicalChildCount = serializedNode.identicalChildCount,
children = children
};
}
#endregion
public enum ShapeTypes
{
Square,
Circle,
Semicircle,
Arched
}
public enum DivisionTypes
{
Horizontal,
Vertical
}
public enum Types
{
Window,
Door
}
public Types type = Types.Window;
[SerializeField]
private Surface _defaultFrameTexture;
[SerializeField]
private Surface _defaultPanelTexture;
private bool _modified = false;
public Surface defaultFrameTexture
{
get { return _defaultFrameTexture; }
set
{
if (value != _defaultFrameTexture)
{
_defaultFrameTexture = value;
MarkModified();
}
}
}
public Surface defaultPanelTexture
{
get { return _defaultPanelTexture; }
set
{
if (value != _defaultPanelTexture)
{
_defaultPanelTexture = value;
MarkModified();
}
}
}
Division _root = new Division();
public Division root
{
get { return _root; }
}
public void MarkModified()
{
_modified = true;
}
public bool CheckModification()
{
List<Division> treeNodes = new List<Division>();
treeNodes.Add(root);
while (treeNodes.Count > 0)
{
if (!_modified && treeNodes[0].modified) _modified = true;
treeNodes[0].MarkUnmodified();
treeNodes.AddRange(treeNodes[0].children);
treeNodes.RemoveAt(0);
}
bool output = _modified;
_modified = false;
if (output)
{
GenereateData();
SaveData();
}
return output;
}
public void Remove(Division division)
{
if (division == root) return;//cannot delete the root man!!!
List<Division> treeNodes = new List<Division>();
treeNodes.Add(root);
while (treeNodes.Count > 0)
{
// if (!_modified && treeNodes[0].modified) _modified = true;
// treeNodes[0].MarkUnmodified();
int childcount = treeNodes[0].children.Count;
for (int c = 0; c < childcount; c++)
{
if (treeNodes[0].children[c] == division)
{
treeNodes[0].children.RemoveAt(c);
MarkModified();
CheckModification();
return;
}
}
treeNodes.AddRange(treeNodes[0].children);
treeNodes.RemoveAt(0);
}
}
// [SerializeField]
// private byte[] _textureBytes = null;
// private Texture2D _previewTexture;
// private Surface[] _usedSurfaces;
private Mesh _previewMesh;
// private bool _hasBlankSurfaces = false;
// public Texture2D previewTexture
// {
// get { return _previewTexture; }
// }
// public Surface[] usedSurfaces
// {
// get { return _usedSurfaces; }
// }
public Mesh previewMesh
{
get
{
if (_previewMesh == null)
UpdatePreviewMesh();
return _previewMesh;
}
}
private void GenereateData()
{
UpdatePreviewTexture();
LoadPreviewTexture();
UpdatePreviewMesh();
}
private void SaveData()
{
#if UNITY_EDITOR
Serialise();
UnityEditor.EditorUtility.SetDirty(this);
UnityEditor.AssetDatabase.SaveAssets();
UnityEditor.AssetDatabase.Refresh();
#endif
}
public void UpdatePreviewTexture()
{
//TODO
// float max = Mathf.Max(_openingWidthAbs, _openingHeightAbs);
// Vector2 size = new Vector2(max * 2, max * 2);
// if (_wallSurface == null && _ceilingSurface == null && _sillSurface == null && _openingSurface == null)
// _previewTexture = new Texture2D(1, 1);
// else
// WallSectionGenerator.Texture(out _previewTexture, this, size);
//
// _textureBytes = _previewTexture.EncodeToPNG();
}
public void LoadPreviewTexture()
{
// _previewTexture.LoadImage(_textureBytes); TODO
}
public List<Surface> UsedSurfaces()
{
List<Surface> usedSurfaces = new List<Surface>();
List<Division> treeNodes = new List<Division>();
treeNodes.Add(root);
bool blankSurfaces = false;
while (treeNodes.Count > 0)
{
Division current = treeNodes[0];
if (current.surface != null && !usedSurfaces.Contains(current.surface))
usedSurfaces.Add(current.surface);
else
blankSurfaces = true;
treeNodes.AddRange(treeNodes[0].children);
treeNodes.RemoveAt(0);
}
if (blankSurfaces)
{
if (_defaultFrameTexture != null && !usedSurfaces.Contains(_defaultFrameTexture))
usedSurfaces.Insert(0, _defaultFrameTexture);
if (_defaultPanelTexture != null && _defaultPanelTexture != _defaultFrameTexture && !usedSurfaces.Contains(_defaultPanelTexture))
usedSurfaces.Insert(0, _defaultPanelTexture);
}
return usedSurfaces;
}
public void UpdatePreviewMesh(SubmeshLibrary submeshLibrary = null)
{
if (_previewMesh == null)
_previewMesh = new Mesh();
_previewMesh.name = string.Format("{0}_Preview_Mesh", name);
Vector2 size = new Vector2(2, 2);
PortalGenerator.Generate(this, ref _previewMesh, size, submeshLibrary);
}
#region statics
public static Portal CreateWindow()
{
Portal newPortalInstance = CreateInstance<Portal>();
newPortalInstance.type = Types.Window;
#if UNITY_EDITOR
UnityEditor.AssetDatabase.CreateAsset(newPortalInstance, AssetCreator.GeneratePath("newWindow.asset", "Windows and Doors"));
UnityEditor.AssetDatabase.SaveAssets();
UnityEditor.AssetDatabase.Refresh();
#endif
return newPortalInstance;
}
public static Portal CreateDoor()
{
Portal newPortalInstance = CreateInstance<Portal>();
newPortalInstance.type = Types.Door;
#if UNITY_EDITOR
UnityEditor.AssetDatabase.CreateAsset(newPortalInstance, AssetCreator.GeneratePath("newDoor.asset", "Windows and Doors"));
UnityEditor.AssetDatabase.SaveAssets();
UnityEditor.AssetDatabase.Refresh();
#endif
return newPortalInstance;
}
#if UNITY_EDITOR
[UnityEditor.MenuItem("Tools/BuildR/Create New Window", false, ToolsMenuLevels.CREATE_WINDOW)]
private static Portal MenuCreateNewWindow()
{
Portal output = CreateWindow();
UnityEditor.Selection.activeObject = output;
return output;
}
[UnityEditor.MenuItem("Tools/BuildR/Create New Door", false, ToolsMenuLevels.CREATE_DOOR)]
private static Portal MenuCreateNewDoor()
{
Portal output = CreateDoor();
UnityEditor.Selection.activeObject = output;
return output;
}
[UnityEditor.MenuItem("Assets/Create/BuildR/Create New Window", false, ToolsMenuLevels.CREATE_WINDOW)]
private static Portal MenuCreateNewWindowB()
{
Portal output = CreateWindow();
UnityEditor.Selection.activeObject = output;
return output;
}
[UnityEditor.MenuItem("Assets/Create/BuildR/Create New Door", false, ToolsMenuLevels.CREATE_DOOR)]
private static Portal MenuCreateNewDoorB()
{
Portal output = CreateDoor();
UnityEditor.Selection.activeObject = output;
return output;
}
#endif
#endregion
}
public class Division
{
public static string NO_NAME = "Unnamed Division";
public List<Division> children = new List<Division>();
private string _name = NO_NAME;//frame of division (meters)
private float _frame = 0.05f;//frame of division (meters)
private float _recess = 0.01f;//recess of inner (meters)
private float _size = 1.0f;//ratio of division used against sibling divisions
private Portal.ShapeTypes _type;
private Portal.DivisionTypes _divisionType;
private Surface _surface;
private bool _identicalChildren;
private int _identicalChildCount = 1;
private bool _modified;
//editor value
public bool expanded = false;
public string name
{
get
{
return _name;
}
set
{
if (_name != value)
{
_name = value;
MarkModified();
}
}
}
public float frame
{
get
{
return _frame;
}
set
{
if (_frame != value)
{
_frame = value;
MarkModified();
}
}
}
public float recess
{
get
{
return _recess;
}
set
{
if (_recess != value)
{
_recess = value;
MarkModified();
}
}
}
public float size
{
get
{
return _size;
}
set
{
if (_size != value)
{
_size = value;
MarkModified();
}
}
}
public Portal.ShapeTypes type
{
get
{
return _type;
}
set
{
if (_type != value)
{
_type = value;
MarkModified();
}
}
}
public Surface surface
{
get
{
return _surface;
}
set
{
if (_surface != value)
{
_surface = value;
MarkModified();
}
}
}
public bool identicalChildren
{
get
{
return _identicalChildren;
}
set
{
if (_identicalChildren != value)
{
_identicalChildren = value;
if (!hasChildren && _identicalChildren)
children.Add(new Division());
MarkModified();
}
}
}
public int identicalChildCount
{
get { return _identicalChildCount; }
set
{
if (_identicalChildCount != value)
{
_identicalChildCount = Mathf.Max(value, 1);
MarkModified();
}
}
}
public Portal.DivisionTypes divisionType
{
get { return _divisionType; }
set
{
if (_divisionType != value)
{
_divisionType = value;
MarkModified();
}
}
}
public bool hasChildren
{
get { return children.Count > 0; }
}
public List<Division> GetChildren
{
get
{
if (!_identicalChildren)
return children;
else
{
List<Division> output = new List<Division>();
for (int i = 0; i < _identicalChildCount; i++)
output.Add(children[0]);
return output;
}
}
}
public void MarkModified()
{
_modified = true;
}
public void MarkUnmodified()
{
_modified = false;
}
public bool modified
{
get { return _modified; }
}
}
//class that we will use for serialization
[Serializable]
public struct SerializableDivision
{
public string name;//frame of division (meters)
public float frame;//frame of division (meters)
public float recess;//recess of inner (meters)
public float size;//ratio of division used against sibling divisions
public Portal.ShapeTypes type;
public Portal.DivisionTypes divisionType;
public Surface surface;
public bool identicalChildren;
public int identicalChildCount;
public int childCount;
public int indexOfFirstChild;
//editor value
public bool expanded;
}
}