aframe-babia-components 1.3.1 → 1.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -45,6 +45,7 @@ AFRAME.registerComponent('babia-boats', {
45
45
  height_building_legend: { type: 'number', default: 0 },
46
46
  legend_scale: { type: 'number', default: 1 },
47
47
  legend_lookat: { type: 'string', default: "[camera]" },
48
+ legend_text: { type: 'string', default: '{name}\n{fheight} (height): {height}\n{farea} (area): {area}\n{fwidth} (width): {width}\n{fdepth} (depth): {depth}\n{fcolor} (color): {color}' },
48
49
  legendsAsChildren: { type: 'boolean', default: false },
49
50
  legendsAsChildrenHeight: { type: 'number', default: 3 },
50
51
  metricsInfoId: { type: 'string', default: "" },
@@ -187,7 +188,16 @@ AFRAME.registerComponent('babia-boats', {
187
188
 
188
189
  if (figure.children) {
189
190
  // Quarter
190
- entity.legend = generateLegend(figure.name, self.data.legend_scale, self.data.legend_lookat, 'black', 'white');
191
+ entity.legend = generateLegend(entity.babiaRawData || {}, {
192
+ name: figure.name,
193
+ scale: self.data.legend_scale,
194
+ lookat: self.data.legend_lookat,
195
+ template: self.data.legend_text || '{name}',
196
+ colorPlane: 'black',
197
+ colorText: 'white',
198
+ isQuarter: true,
199
+ fields: {}
200
+ });
191
201
  let worldPos = new THREE.Vector3();
192
202
  let coordinates = worldPos.setFromMatrixPosition(entity.object3D.matrixWorld);
193
203
  let coordinatesFinal = {
@@ -212,7 +222,22 @@ AFRAME.registerComponent('babia-boats', {
212
222
  entity.alreadyActive = true
213
223
  } else {
214
224
  // Building
215
- entity.legend = generateLegend(figure.name, self.data.legend_scale, self.data.legend_lookat, 'white', 'black', entity.babiaRawData, self.data.height, self.data.area, self.data.depth, self.data.width, self.data.color);
225
+ entity.legend = entity.legend = generateLegend(entity.babiaRawData, {
226
+ name: figure.name,
227
+ scale: self.data.legend_scale,
228
+ lookat: self.data.legend_lookat,
229
+ template: self.data.legend_text,
230
+ colorPlane: 'white',
231
+ colorText: 'black',
232
+ isQuarter: false,
233
+ fields: {
234
+ height: self.data.height,
235
+ area: self.data.area,
236
+ width: self.data.width,
237
+ depth: self.data.depth,
238
+ color: self.data.color,
239
+ }
240
+ });
216
241
  let worldPos = new THREE.Vector3();
217
242
  let coordinates = worldPos.setFromMatrixPosition(entity.object3D.matrixWorld);
218
243
  let height_real = new THREE.Box3().setFromObject(entity.object3D)
@@ -326,27 +351,29 @@ AFRAME.registerComponent('babia-boats', {
326
351
  */
327
352
  generateElements: function (elements, figures, translate, inc) {
328
353
  const self = this;
354
+ const data = this.data;
355
+
329
356
  var increment = inc;
330
357
 
331
- // Vertical Limits
358
+ // Vertical limits (of all figures up to now)
332
359
  var limit_up = 0;
333
360
  var limit_down = 0;
334
- // Horizontal Limits
361
+ // Horizontal limits (of all figures up to now)
335
362
  var limit_right = 0;
336
363
  var limit_left = 0;
337
364
 
338
- //Position Figure
365
+ //Position of the center of current figure
339
366
  var posX = 0;
340
367
  var posY = 0;
341
368
 
342
- // Aux to update the limits
343
- // Save max limit to update last limit in the next step
369
+ // Aux variables to update limits
370
+ // Save max limits to update limits in the next step
344
371
  var max_right = 0;
345
372
  var max_left = 0;
346
373
  var max_down = 0;
347
374
  var max_up = 0;
348
375
 
349
- // control points
376
+ // Control points
350
377
  var current_vertical = 0;
351
378
  var current_horizontal = 0;
352
379
 
@@ -363,31 +390,31 @@ AFRAME.registerComponent('babia-boats', {
363
390
  for (let i = 0; i < elements.length; i++) {
364
391
  // To do not overwrite newData
365
392
  let element = {}
366
- if (this.data.width) {
367
- element.width = elements[i][this.data.width] || 0.5
368
- }
369
- if (this.data.height) {
393
+ if (data.width) {
394
+ element.width = elements[i][data.width] || 0.5
395
+ };
396
+ if (data.depth) {
397
+ element.depth = elements[i][data.depth] || 0.5
398
+ };
399
+ if (data.height) {
370
400
  let height_min = 0;
371
- if (self.data.diffBuildingHeight) {
401
+ if (data.diffBuildingHeight) {
372
402
  height_min = self.babiaMetadata['heightMin'];
373
403
  };
374
404
  element.height = self.normalizeValues(height_min,
375
405
  self.babiaMetadata['heightMax'],
376
406
  self.data.minBuildingHeight,
377
407
  self.data.maxBuildingHeight,
378
- elements[i][this.data.height])
379
- || this.data.minBuildingHeight
380
- }
381
- if (this.data.depth) {
382
- element.depth = elements[i][this.data.depth] || 0.5
383
- }
384
- if (this.data.area) {
385
- element.area = elements[i][this.data.area] || 0.5
408
+ elements[i][data.height]) || data.minBuildingHeight
409
+ };
410
+ if (data.area) {
411
+ element.area = elements[i][data.area] || 0.5;
412
+ element.width = element.depth = Math.sqrt(element.area);
386
413
  }
387
414
  if (elements[i].children) {
388
415
  // There are elements hanging from this element
416
+ // so, it is a quarter. Let's run this function recursively
389
417
  element.children = elements[i].children
390
- this.quarter = true;
391
418
  var children = [];
392
419
  var translate_matrix;
393
420
  // Save Zone's parameters
@@ -396,27 +423,28 @@ AFRAME.registerComponent('babia-boats', {
396
423
  if (!scale) {
397
424
  scale = { x: 1, y: 1, z: 1 }
398
425
  }
399
- element.height = this.data.zone_elevation / scale.y;
400
- increment -= this.data.border * this.data.extra;
426
+ element.height = data.zone_elevation / scale.y;
427
+ increment -= data.border * data.extra;
401
428
 
402
- // TEST TREE
403
- if (self.data.treeLayout) {
429
+ // Tree layout
430
+ if (data.treeLayout) {
404
431
  element.children.forEach(el => {
405
432
  if (!el.children) {
406
- element.treeMetaphorHeight = el[self.data.height]
433
+ element.treeMetaphorHeight = el[data.height]
407
434
  return
408
435
  }
409
436
  });
410
437
  }
411
438
 
412
439
  [element.width, element.depth, translate_matrix, children]
413
- = this.generateElements(element.children, children, translate_matrix, increment);
440
+ = this.generateElements(element.children, children,
441
+ translate_matrix, increment);
414
442
  translate_matrix.y = element.height;
415
443
  increment = inc;
416
- }
444
+ };
417
445
  if (i == 0) {
418
446
  // This is the first element to place
419
- if (this.data.area && !elements[i].children) {
447
+ if (data.area && !elements[i].children) {
420
448
  limit_up += Math.sqrt(element.area) / 2;
421
449
  limit_down -= Math.sqrt(element.area) / 2;
422
450
  limit_right += Math.sqrt(element.area) / 2;
@@ -430,8 +458,19 @@ AFRAME.registerComponent('babia-boats', {
430
458
  //console.log("==== RIGHT SIDE ====");
431
459
  current_horizontal = limit_up
432
460
  } else if (element.height > 0) {
461
+ console.log("Element: ", i);
462
+ console.log("Directions (up, right, down, left):",
463
+ up, right, down, left);
464
+ console.log("Current (vertical, horizontal):",
465
+ current_vertical, current_horizontal);
466
+ console.log("Limits (up, right, down, left):",
467
+ limit_up, limit_right, limit_down, limit_left);
468
+ console.log("Max (up, right, down, left):",
469
+ max_up, max_right, max_down, max_left);
470
+ console.log("Position (x, y):", posX, posY);
433
471
  if (up) {
434
- [current_vertical, posX, posY, max_up] = this.UpSide(element, limit_up, current_vertical, max_up);
472
+ [current_vertical, posX, posY, max_up] =
473
+ this.UpSide(element, limit_up, current_vertical, max_up);
435
474
  if (current_vertical > limit_right) {
436
475
  max_right = current_vertical;
437
476
  up = false;
@@ -442,7 +481,9 @@ AFRAME.registerComponent('babia-boats', {
442
481
  current_horizontal = limit_up;
443
482
  }
444
483
  } else if (right) {
445
- [current_horizontal, posX, posY, max_right] = this.RightSide(element, limit_right, current_horizontal, max_right);
484
+ [current_horizontal, posX, posY, max_right] =
485
+ this.RightSide(element, limit_right, current_horizontal, max_right);
486
+ console.log("RIGHT", current_horizontal, posX, posY, max_right);
446
487
  if (current_horizontal < limit_down) {
447
488
  max_down = current_horizontal;
448
489
  right = false;
@@ -452,8 +493,13 @@ AFRAME.registerComponent('babia-boats', {
452
493
  }
453
494
  current_vertical = limit_right;
454
495
  }
496
+ console.log("RIGHT2",
497
+ current_horizontal, current_vertical, posX, posY, max_right);
455
498
  } else if (down) {
499
+ console.log("DOWN1 (element, limit_down, current_vertical, max_down):",
500
+ element, limit_down, current_vertical, max_down);
456
501
  [current_vertical, posX, posY, max_down] = this.DownSide(element, limit_down, current_vertical, max_down);
502
+ console.log("DOWN2", current_vertical, posX, posY, max_down);
457
503
  if (current_vertical < limit_left) {
458
504
  max_left = current_vertical;
459
505
  down = false;
@@ -481,7 +527,7 @@ AFRAME.registerComponent('babia-boats', {
481
527
  // First, fill in common properties, then add those specific for
482
528
  // bases or buildings
483
529
  let figure = {
484
- id: "boat-" + elements[i][this.data.field],
530
+ id: "boat-" + elements[i][data.field],
485
531
  posX: posX,
486
532
  posY: posY,
487
533
  height: element.height,
@@ -496,44 +542,32 @@ AFRAME.registerComponent('babia-boats', {
496
542
  figure.children = children,
497
543
  figure.alpha = self.data.baseAlpha,
498
544
  figure.translate_matrix = translate_matrix
499
- // TEST TREE
500
- if (self.data.treeLayout) {
501
- figure.treeMetaphorHeight = element.treeMetaphorHeight
545
+ // Tree layout
546
+ if (data.treeLayout) {
547
+ figure.treeMetaphorHeight = element.treeMetaphorHeight;
502
548
  }
549
+ // Hierarchy level
550
+ let hierarchyLevel = figure.name.split("/").length - 1;
551
+ figure.hierarchyLevel = hierarchyLevel;
552
+ figure.renderOrder = hierarchyLevel;
503
553
  // Gradient color
504
- let hierarchyLevel = figure.name.split("/").length - 1
505
- figure.hierarchyLevel = hierarchyLevel
506
- figure.renderOrder = hierarchyLevel
507
- if (self.data.gradientBaseColor) {
508
- figure.color = greenColorsurface(Math.round(
509
- self.normalizeValues(1,
510
- self.babiaMetadata['maxLevels'],
511
- 15,
512
- 100,
513
- hierarchyLevel)
514
- ));
515
- } else {
516
- figure.color = self.data.base_color;
517
- }
554
+ figure.color = self.getColorBase(hierarchyLevel);
518
555
  } else {
519
556
  // This is a building
520
557
  figure.name = (elements[i].name) ? elements[i].name : elements[i][this.data.field],
521
558
  figure.alpha = self.data.buildingAlpha
522
- if (this.data.area) {
523
- figure.width = Math.sqrt(element.area) - this.data.separation;
524
- figure.depth = Math.sqrt(element.area) - this.data.separation;
525
- } else {
526
- figure.width = element.width - this.data.separation;
527
- figure.depth = element.depth - this.data.separation;
528
- };
529
- if (typeof elements[i][self.data.color] === 'number') {
530
- figure.color = heatMapColorforValue(elements[i][self.data.color], self.babiaMetadata['color_max'], self.babiaMetadata['color_min'])
531
- } else if (typeof elements[i][self.data.color] === 'string') {
559
+ figure.width = element.width - data.separation;
560
+ figure.depth = element.depth - data.separation;
561
+
562
+ if (typeof elements[i][data.color] === 'number') {
563
+ figure.color = heatMapColorforValue(elements[i][data.color],
564
+ self.babiaMetadata['color_max'], self.babiaMetadata['color_min'])
565
+ } else if (typeof elements[i][data.color] === 'string') {
532
566
  // Categoric color
533
- if (elements[i][self.data.color] in self.categoricColorMaps) {
534
- figure.color = self.categoricColorMaps[elements[i][self.data.color]]
567
+ if (elements[i][data.color] in self.categoricColorMaps) {
568
+ figure.color = self.categoricColorMaps[elements[i][data.color]]
535
569
  } else {
536
- self.categoricColorMaps[elements[i][self.data.color]] = colorsArray[self.categoricColorIndex]
570
+ self.categoricColorMaps[elements[i][data.color]] = colorsArray[self.categoricColorIndex]
537
571
  figure.color = colorsArray[self.categoricColorIndex]
538
572
 
539
573
  self.categoricColorIndex = self.categoricColorIndex + 1
@@ -543,37 +577,12 @@ AFRAME.registerComponent('babia-boats', {
543
577
  }
544
578
  }
545
579
 
546
-
547
-
548
580
  }
549
581
  figure.rawData = elements[i]
550
582
 
551
- // If transparency by a field on buildings
552
- // Put transparent 80% to those buildings that share same value for a field selected
553
- if (self.data.transparent80ByRepeatedField && (!figure.children && self.idsToNotRepeat80Transparent.includes(figure.rawData[self.data.transparent80ByRepeatedField]))) {
554
- figure.alpha = 0.8
555
- figure.renderOrder = 2000
556
- } else {
557
- self.idsToNotRepeat80Transparent.push(figure.rawData[self.data.transparent80ByRepeatedField])
558
- }
559
-
560
- // Put transparent 20% to those buildings that share same value for a field selected
561
- if (self.data.transparent20ByRepeatedField && (!figure.children && self.idsToNotRepeat20Transparent.includes(figure.rawData[self.data.transparent20ByRepeatedField]))) {
562
- figure.alpha = 0.45
563
- figure.renderOrder = 2000
564
- } else {
565
- self.idsToNotRepeat20Transparent.push(figure.rawData[self.data.transparent20ByRepeatedField])
566
- }
567
-
568
- // Put wireframe to those buildings that share same value for a field selected
569
- if (self.data.wireframeByRepeatedField && (!figure.children && self.idsToNotRepeatWireframe.includes(figure.rawData[self.data.wireframeByRepeatedField]))) {
570
- figure.wireframe = true
571
- } else {
572
- figure.wireframe = false
573
- self.idsToNotRepeatWireframe.push(figure.rawData[self.data.wireframeByRepeatedField])
574
- }
575
-
576
-
583
+ // Set transparency and wireframe, if appropriate
584
+ self.setTransparency(figure);
585
+ figure.wireframe = this.getWireframe(figure, data.wireframeByRepeatedField);
577
586
 
578
587
  figures.push(figure);
579
588
  }
@@ -592,8 +601,7 @@ AFRAME.registerComponent('babia-boats', {
592
601
  limit_right = max_right;
593
602
  }
594
603
 
595
-
596
- // Calculate translate of the center, width and depth of the zone
604
+ // Calculate translation of the center, width and depth of the zone
597
605
  var width = Math.abs(limit_left) + Math.abs(limit_right);
598
606
  var depth = Math.abs(limit_down) + Math.abs(limit_up);
599
607
 
@@ -630,9 +638,9 @@ AFRAME.registerComponent('babia-boats', {
630
638
  z: -y + translate.z
631
639
  }
632
640
 
633
- // TEST TREE
641
+ // Tree layout
634
642
  if (self.data.treeLayout) {
635
- // // Hide quarters that has not children
643
+ // Hide quarters with no children
636
644
  if (self.data.treeHideOneSonQuarters && (figures[i].children && figures[i].children.length === 1)) {
637
645
  figures[i].alphaTest = 1
638
646
  figures[i].dontAddEvents = true
@@ -689,17 +697,16 @@ AFRAME.registerComponent('babia-boats', {
689
697
  let entity = this.createElement(figures[i], position);
690
698
  this.addEvents(entity, figures[i]);
691
699
 
692
-
693
-
694
-
695
700
  element.appendChild(entity);
696
701
  }
697
702
 
698
703
  },
699
704
 
700
705
  RightSide: function (element, limit_right, current_horizontal, max_right) {
706
+ const data = this.data;
707
+
701
708
  let width, depth;
702
- if (this.data.area && !element.children) {
709
+ if (data.area && !element.children) {
703
710
  width = Math.sqrt(element.area);
704
711
  depth = Math.sqrt(element.area);
705
712
  } else {
@@ -721,9 +728,10 @@ AFRAME.registerComponent('babia-boats', {
721
728
  },
722
729
 
723
730
  DownSide: function (element, limit_down, current_vertical, max_down) {
724
- let width, depth;
731
+ const data = this.data;
725
732
 
726
- if (this.data.area && !element.children) {
733
+ let width, depth;
734
+ if (data.area && !element.children) {
727
735
  width = Math.sqrt(element.area);
728
736
  depth = Math.sqrt(element.area);
729
737
  } else {
@@ -745,9 +753,10 @@ AFRAME.registerComponent('babia-boats', {
745
753
  },
746
754
 
747
755
  LeftSide: function (element, limit_left, current_horizontal, max_left) {
748
- let width, depth;
756
+ const data = this.data;
749
757
 
750
- if (this.data.area && !element.children) {
758
+ let width, depth;
759
+ if (data.area && !element.children) {
751
760
  width = Math.sqrt(element.area);
752
761
  depth = Math.sqrt(element.area);
753
762
  } else {
@@ -769,9 +778,10 @@ AFRAME.registerComponent('babia-boats', {
769
778
  },
770
779
 
771
780
  UpSide: function (element, limit_up, current_vertical, max_up) {
781
+ const data = this.data;
782
+
772
783
  let width, depth;
773
-
774
- if (this.data.area && !element.children) {
784
+ if (data.area && !element.children) {
775
785
  width = Math.sqrt(element.area);
776
786
  depth = Math.sqrt(element.area);
777
787
  } else {
@@ -792,6 +802,131 @@ AFRAME.registerComponent('babia-boats', {
792
802
  return [current_vertical, posX, posY, max_up];
793
803
  },
794
804
 
805
+ /**
806
+ * getColor
807
+ *
808
+ * Calculate the color of the element based on its value in color_field.
809
+ * If the value is a number, it will calculate the color based on the
810
+ * heat map from color_min to color_max.
811
+ * If the value is a string, it will assign a color from colorsArray in
812
+ * order of appearence.
813
+ *
814
+ * @param {Object} element - The element to calculate the color for
815
+ * @param {String} color_field - The field in the element to calculate the color from
816
+ *
817
+ * @return {String} - The color for the element
818
+ */
819
+ getColor: function (element, color_field) {
820
+ const self = this;
821
+ let color_value = element[color_field];
822
+ let color;
823
+ if (typeof color_value === 'number') {
824
+ color = heatMapColorforValue(color_value,
825
+ self.babiaMetadata['color_max'], self.babiaMetadata['color_min']);
826
+ } else if (typeof color_value === 'string') {
827
+ // Categoric color
828
+ if (color_value in self.categoricColorMaps) {
829
+ color = self.categoricColorMaps[color_value];
830
+ } else {
831
+ self.categoricColorMaps[color_value]
832
+ = colorsArray[self.categoricColorIndex]
833
+ color = colorsArray[self.categoricColorIndex]
834
+
835
+ self.categoricColorIndex = self.categoricColorIndex + 1
836
+ if (self.categoricColorIndex >= colorsArray.length) {
837
+ self.categoricColorIndex = 0
838
+ }
839
+ }
840
+ }
841
+ return color;
842
+ },
843
+
844
+ /**
845
+ * getColorBase
846
+ *
847
+ * Determines the color for the base in a given level, depending on whether
848
+ * gradientBaseColor is enabled. If enabled, it calculates a color
849
+ * using the greenColorsurface function based on normalized level
850
+ * values. If not enabled, it uses the default base color.
851
+ *
852
+ * @param {number} level - The level for which to determine the color.
853
+ *
854
+ * @return {string} - The calculated base color.
855
+ */
856
+ getColorBase: function (level) {
857
+ let color;
858
+ const self = this;
859
+
860
+ if (self.data.gradientBaseColor) {
861
+ let max_levels = self.babiaMetadata['maxLevels'];
862
+ color = greenColorsurface(Math.round(
863
+ self.normalizeValues(1, max_levels, 15, 100, level)
864
+ ));
865
+ } else {
866
+ color = self.data.base_color;
867
+ };
868
+ return color;
869
+ },
870
+
871
+ /**
872
+ * setTransparency
873
+ *
874
+ * If "transparency by a field" on buildings is selected
875
+ * set transparency to 80% or 20% to those buildings that share
876
+ * same value for the selected field.
877
+ * @param {Object} figure - The figure to set transparency
878
+ */
879
+ setTransparency: function (figure) {
880
+ const self = this;
881
+ const data = this.data;
882
+
883
+ // If transparency by a field on buildings
884
+ // Put transparent 80% to those buildings that share same value for a field selected
885
+ const t80_field = data.transparent80ByRepeatedField;
886
+ const t20_field = data.transparent20ByRepeatedField;
887
+ if (t80_field && (!figure.children &&
888
+ self.idsToNotRepeat80Transparent.includes(figure.rawData[t80_field]))) {
889
+ figure.alpha = 0.8
890
+ figure.renderOrder = 2000
891
+ } else {
892
+ self.idsToNotRepeat80Transparent.push(figure.rawData[t80_field])
893
+ }
894
+
895
+ // Put transparent 20% to those buildings that share same value for a field selected
896
+ if (t20_field && (!figure.children &&
897
+ self.idsToNotRepeat20Transparent.includes(figure.rawData[t20_field]))) {
898
+ figure.alpha = 0.45
899
+ figure.renderOrder = 2000
900
+ } else {
901
+ self.idsToNotRepeat20Transparent.push(figure.rawData[t20_field])
902
+ }
903
+ },
904
+
905
+ /**
906
+ * getWireframe
907
+ *
908
+ * Get boolean to decide if wireframe will be put to the elements
909
+ * that share the same value of the selected field.
910
+ * @param {Object} figure - The figure to set wireframe
911
+ * @param {String} wireframe_field - The field in the element to decide the wireframe
912
+ *
913
+ * @return {Boolean} - True if the element will be visualized as wireframe
914
+ */
915
+ getWireframe: function (figure, wireframe_field) {
916
+ const self = this;
917
+ let wireframe;
918
+
919
+ // Put wireframe to those buildings that share same value for a field selected
920
+ if (wireframe_field && (!figure.children &&
921
+ self.idsToNotRepeatWireframe.includes(figure.rawData[wireframe_field]))) {
922
+ wireframe = true;
923
+ } else {
924
+ wireframe = false;
925
+ self.idsToNotRepeatWireframe.push(figure.rawData[wireframe_field]);
926
+ }
927
+ return wireframe;
928
+ },
929
+
795
930
  Animation: function (element, figures, figures_old, delta, translate, translate_old) {
796
931
  let self = this
797
932
  let new_time = Date.now();
@@ -1382,6 +1517,7 @@ AFRAME.registerComponent('babia-boats', {
1382
1517
  }
1383
1518
 
1384
1519
  // Create city
1520
+ console.log("About to update chart...", this.newData)
1385
1521
  this.updateChart(this.newData)
1386
1522
 
1387
1523
  // Categoric color legend
@@ -1535,7 +1671,16 @@ AFRAME.registerComponent('babia-boats', {
1535
1671
  y: self.data.height_quarter_legend_title,
1536
1672
  z: coordinates.z
1537
1673
  }
1538
- entity.legend = generateLegend(figure.name, self.data.legend_scale, self.data.legend_lookat, 'black', 'white');
1674
+ entity.legend = generateLegend(entity.babiaRawData || {}, {
1675
+ name: figure.name,
1676
+ scale: self.data.legend_scale,
1677
+ lookat: self.data.legend_lookat,
1678
+ template: self.data.legend_text || '{name}',
1679
+ colorPlane: 'black',
1680
+ colorText: 'white',
1681
+ isQuarter: true,
1682
+ fields: {}
1683
+ });
1539
1684
 
1540
1685
  // If legend is a child of the building/quarter
1541
1686
  if (self.data.legendsAsChildren) {
@@ -1650,7 +1795,22 @@ AFRAME.registerComponent('babia-boats', {
1650
1795
  depth: entityGeometry.depth + 0.1,
1651
1796
  width: entityGeometry.width + 0.1
1652
1797
  });
1653
- entity.legend = generateLegend(figure.name, self.data.legend_scale, self.data.legend_lookat, 'white', 'black', entity.babiaRawData, self.data.height, self.data.area, self.data.depth, self.data.width, self.data.color);
1798
+ entity.legend = generateLegend(entity.babiaRawData, {
1799
+ name: figure.name,
1800
+ scale: self.data.legend_scale,
1801
+ lookat: self.data.legend_lookat,
1802
+ template: self.data.legend_text,
1803
+ colorPlane: 'white',
1804
+ colorText: 'black',
1805
+ isQuarter: false,
1806
+ fields: {
1807
+ height: self.data.height,
1808
+ area: self.data.area,
1809
+ width: self.data.width,
1810
+ depth: self.data.depth,
1811
+ color: self.data.color,
1812
+ }
1813
+ });
1654
1814
  let worldPos = new THREE.Vector3();
1655
1815
  let coordinates = worldPos.setFromMatrixPosition(entity.object3D.matrixWorld);
1656
1816
  let height_real = new THREE.Box3().setFromObject(entity.object3D)
@@ -1724,7 +1884,22 @@ AFRAME.registerComponent('babia-boats', {
1724
1884
  depth: entityGeometry.depth + 0.1,
1725
1885
  width: entityGeometry.width + 0.1
1726
1886
  });
1727
- entity.legend = generateLegend(figure.name, self.data.legend_scale, self.data.legend_lookat, 'white', 'black', entity.babiaRawData, self.data.height, self.data.area, self.data.depth, self.data.width, self.data.color);
1887
+ entity.legend = generateLegend(entity.babiaRawData, {
1888
+ name: figure.name,
1889
+ scale: self.data.legend_scale,
1890
+ lookat: self.data.legend_lookat,
1891
+ template: self.data.legend_text,
1892
+ colorPlane: 'white',
1893
+ colorText: 'black',
1894
+ isQuarter: false,
1895
+ fields: {
1896
+ height: self.data.height,
1897
+ area: self.data.area,
1898
+ width: self.data.width,
1899
+ depth: self.data.depth,
1900
+ color: self.data.color,
1901
+ }
1902
+ });
1728
1903
  let worldPos = new THREE.Vector3();
1729
1904
  let coordinates = worldPos.setFromMatrixPosition(entity.object3D.matrixWorld);
1730
1905
  let height_real = new THREE.Box3().setFromObject(entity.object3D)
@@ -1805,74 +1980,87 @@ AFRAME.registerComponent('babia-boats', {
1805
1980
  /**
1806
1981
  * This function generate a plane at the top of the building with the desired text
1807
1982
  */
1808
- let generateLegend = (name, legend_scale, lookat, colorPlane, colorText, data, fheight, farea, fdepth, fwidth, fcolor) => {
1809
- let width = 2;
1810
- let height = 1;
1811
- if (name.length > 16)
1812
- width = name.length / 5;
1813
-
1814
- if (data) {
1815
- let heightText = "\n " + fheight + " (height): " + Math.round(data[fheight] * 100) / 100
1816
- if (heightText.length > 16)
1817
- width = heightText.length / 5;
1818
- name += heightText
1819
-
1820
- if (farea) {
1821
- let areaText = "\n " + farea + " (area): " + Math.round(data[farea] * 100) / 100
1822
- if (areaText.length > 16 && areaText > heightText)
1823
- width = areaText.length / 5;
1824
- name += areaText
1825
- } else {
1826
- let depthText = "\n " + fdepth + " (depth): " + Math.round(data[fdepth] * 100) / 100
1827
- if (depthText.length > 16 && depthText > heightText)
1828
- width = depthText.length / 5;
1829
- name += depthText
1830
-
1831
- let widthText = "\n " + fwidth + " (width): " + Math.round(data[fwidth] * 100) / 100
1832
- if (widthText.length > 16 && widthText > heightText && widthText > depthText)
1833
- width = widthText.length / 5;
1834
- name += widthText
1835
-
1836
- height = 1.5
1837
- }
1983
+ let generateLegend = (data, {
1984
+ name,
1985
+ scale,
1986
+ lookat,
1987
+ template = '{name}',
1988
+ colorPlane = 'white',
1989
+ colorText = 'black',
1990
+ isQuarter = false,
1991
+ fields = {}
1992
+ }) => {
1993
+ const safe = (val, fallback = '') => (typeof val !== 'undefined' ? val : fallback);
1994
+ const round = v => (typeof v === 'number' ? Math.round(v * 100) / 100 : v);
1995
+
1996
+ // Campos usados
1997
+ const fheight = fields.height || '';
1998
+ const farea = fields.area || '';
1999
+ const fwidth = fields.width || '';
2000
+ const fdepth = fields.depth || '';
2001
+ const fcolor = fields.color || '';
2002
+
2003
+ // Datos correspondientes
2004
+ const vheight = round(safe(data?.[fheight]));
2005
+ const varea = round(safe(data?.[farea]));
2006
+ const vwidth = round(safe(data?.[fwidth]));
2007
+ const vdepth = round(safe(data?.[fdepth]));
2008
+ const vcolor = safe(data?.[fcolor]);
2009
+
2010
+ // Texto final
2011
+ let processedTemplate = isQuarter ? '{name}' : template;
2012
+
2013
+ processedTemplate = processedTemplate
2014
+ .replaceAll('{name}', name)
2015
+ .replaceAll('{height}', vheight)
2016
+ .replaceAll('{area}', varea)
2017
+ .replaceAll('{width}', vwidth)
2018
+ .replaceAll('{depth}', vdepth)
2019
+ .replaceAll('{color}', vcolor)
2020
+ .replaceAll('{fheight}', fheight)
2021
+ .replaceAll('{farea}', farea)
2022
+ .replaceAll('{fwidth}', fwidth)
2023
+ .replaceAll('{fdepth}', fdepth)
2024
+ .replaceAll('{fcolor}', fcolor);
2025
+
2026
+ const knownPlaceholders = new Set([
2027
+ 'name', 'height', 'area', 'width', 'depth', 'color',
2028
+ 'fheight', 'farea', 'fwidth', 'fdepth', 'fcolor'
2029
+ ]);
2030
+
2031
+ const finalText = processedTemplate.replace(/\{(\w+)\}/g, (match, variable) => {
2032
+ if (knownPlaceholders.has(variable)) return match; // ya procesado
2033
+ return safe(data?.[variable], match); // si existe en rawData, lo usamos
2034
+ });
1838
2035
 
1839
- if (fcolor) {
1840
- let colorText = "\n " + fcolor
1841
- if (typeof data[fcolor] === 'string') {
1842
- colorText += " (color): " + data[fcolor]
1843
- } else {
1844
- colorText += " (color): " + Math.round(data[fcolor] * 100) / 100
1845
- }
1846
- if (colorText.length > 16 && colorText > heightText)
1847
- width = colorText.length / 5;
1848
- name += colorText
1849
- height += 0.2
1850
- }
1851
- }
2036
+ const lines = finalText.split('\n');
2037
+ const width = Math.max(...lines.map(line => line.length)) / 5;
2038
+ const height = lines.length * 0.3 + 0.3;
1852
2039
 
1853
- let entity = document.createElement('a-plane');
2040
+ const entity = document.createElement('a-plane');
1854
2041
  entity.setAttribute('babia-lookat', lookat);
1855
-
1856
2042
  entity.setAttribute('rotation', { x: 0, y: 0, z: 0 });
1857
2043
  entity.setAttribute('height', height);
1858
2044
  entity.setAttribute('width', width);
1859
2045
  entity.setAttribute('color', colorPlane);
1860
- entity.setAttribute('material', { 'side': 'double' });
2046
+ entity.setAttribute('material', { side: 'double' });
2047
+
1861
2048
  entity.setAttribute('text', {
1862
- 'value': name,
1863
- 'align': 'center',
1864
- 'width': 6,
1865
- 'color': colorText,
1866
- 'alphaTest': 6,
1867
- 'opacity': 6,
1868
- 'transparent': false
2049
+ value: finalText,
2050
+ align: 'center',
2051
+ width: 6,
2052
+ color: colorText,
2053
+ alphaTest: 6,
2054
+ opacity: 6,
2055
+ transparent: false
1869
2056
  });
2057
+
1870
2058
  entity.setAttribute('visible', false);
1871
2059
  entity.setAttribute('class', 'babialegend');
1872
- entity.setAttribute('scale', { x: legend_scale, y: legend_scale, z: legend_scale });
2060
+ entity.setAttribute('scale', { x: scale, y: scale, z: scale });
1873
2061
 
1874
2062
  return entity;
1875
- }
2063
+ };
1876
2064
 
1877
2065
  function setOpacity(entity, opacity) {
1878
2066
  entity.setAttribute('material', 'opacity', opacity);