LabelTool.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. module INSPECTOR {
  2. export class LabelTool extends AbstractTool {
  3. /** True if label are displayed, false otherwise */
  4. private _isDisplayed : boolean = false;
  5. private _advancedTexture : BABYLON.GUI.AdvancedDynamicTexture = null;
  6. private _labelInitialized : boolean = false;
  7. private _scene : BABYLON.Scene = null;
  8. private _guiLoaded : boolean = false;
  9. constructor(parent:HTMLElement, inspector:Inspector) {
  10. super('fa-tags', parent, inspector, 'Display mesh names on the canvas');
  11. this._scene = inspector.scene;
  12. }
  13. public dispose() {
  14. if(this._advancedTexture){
  15. this._advancedTexture.dispose();
  16. }
  17. }
  18. private _checkGUILoaded(): boolean {
  19. if (this._guiLoaded === true) {
  20. return true;
  21. }
  22. if (BABYLON.GUI) {
  23. this._guiLoaded = true;
  24. }
  25. return this._guiLoaded;
  26. }
  27. private _initializeLabels() {
  28. // Check if the label are already initialized and quit if it's the case
  29. if (this._labelInitialized) {
  30. return;
  31. }
  32. // Can't initialize them if the GUI lib is not loaded yet
  33. if (!this._checkGUILoaded()) {
  34. return;
  35. }
  36. // Create the canvas that will be used to display the labels
  37. this._advancedTexture = BABYLON.GUI.AdvancedDynamicTexture.CreateFullscreenUI("UI");
  38. // Create label for all the Meshes, Lights and Cameras
  39. // Those that will be created/removed after this method is called will be taken care by the event handlers added below
  40. for (let m of this._scene.meshes) {
  41. this._createLabel(m);
  42. }
  43. this._scene.onNewMeshAddedObservable.add((m, s) => {
  44. this._createLabel(m);
  45. });
  46. this._scene.onMeshRemovedObservable.add((m, s) => {
  47. this._removeLabel(m);
  48. });
  49. this._labelInitialized = true;
  50. }
  51. private _createLabel(mesh: BABYLON.AbstractMesh){//: BABYLON.Group2D {
  52. // Don't create label for "system nodes" (starting and ending with ###)
  53. let name = mesh.name;
  54. if (Helpers.IsSystemName(name)) {
  55. return;
  56. }
  57. if(mesh){
  58. let rect1 = new BABYLON.GUI.Rectangle();
  59. rect1.width = 4 + 10 * name.length + "px";
  60. rect1.height = "22px";
  61. rect1.background = "rgba(0,0,0,0.6)";
  62. rect1.color = "black";
  63. this._advancedTexture.addControl(rect1);
  64. let label = new BABYLON.GUI.TextBlock();
  65. label.text = name;
  66. label.fontSize = 12;
  67. rect1.addControl(label);
  68. rect1.linkWithMesh(mesh);
  69. }
  70. }
  71. private _removeLabel(mesh: BABYLON.AbstractMesh) {
  72. for (let g of this._advancedTexture._rootContainer.children) {
  73. let ed = g._linkedMesh;
  74. if (ed === mesh) {
  75. this._advancedTexture.removeControl(g);
  76. break;
  77. }
  78. }
  79. }
  80. // Action : Display/hide mesh names on the canvas
  81. public action() {
  82. // Don't toggle if the script is not loaded
  83. if (!this._checkGUILoaded()) {
  84. return;
  85. }
  86. // Toggle the label display state
  87. this._isDisplayed = !this._isDisplayed;
  88. // Check if we have to display the labels
  89. if (this._isDisplayed) {
  90. this._initializeLabels();
  91. this._advancedTexture._rootContainer.isVisible = true;
  92. }
  93. // Or to hide them
  94. else {
  95. this._advancedTexture._rootContainer.isVisible = false;
  96. }
  97. }
  98. }
  99. }