advancedDynamicTexture.ts 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. import { DynamicTexture, Nullable, Observer, Camera, Engine, KeyboardInfoPre, PointerInfoPre, PointerInfo, Layer, Viewport, Scene, Texture, KeyboardEventTypes, Vector3, Matrix, Vector2, Tools, PointerEventTypes, AbstractMesh, StandardMaterial, Color3 } from "babylonjs";
  2. import { Container } from "./controls/container";
  3. import { Control } from "./controls/control";
  4. import { Style } from "./style";
  5. import { Measure } from "./measure";
  6. /**
  7. * Interface used to define a control that can receive focus
  8. */
  9. export interface IFocusableControl {
  10. /**
  11. * Function called when the control receives the focus
  12. */
  13. onFocus(): void;
  14. /**
  15. * Function called when the control loses the focus
  16. */
  17. onBlur(): void;
  18. /**
  19. * Function called to let the control handle keyboard events
  20. * @param evt defines the current keyboard event
  21. */
  22. processKeyboard(evt: KeyboardEvent): void;
  23. /**
  24. * Function called to get the list of controls that should not steal the focus from this control
  25. * @returns an array of controls
  26. */
  27. keepsFocusWith(): Nullable<Control[]>;
  28. }
  29. /**
  30. * Class used to create texture to support 2D GUI elements
  31. * @see http://doc.babylonjs.com/how_to/gui
  32. */
  33. export class AdvancedDynamicTexture extends DynamicTexture {
  34. private _isDirty = false;
  35. private _renderObserver: Nullable<Observer<Camera>>;
  36. private _resizeObserver: Nullable<Observer<Engine>>;
  37. private _preKeyboardObserver: Nullable<Observer<KeyboardInfoPre>>;
  38. private _pointerMoveObserver: Nullable<Observer<PointerInfoPre>>;
  39. private _pointerObserver: Nullable<Observer<PointerInfo>>;
  40. private _canvasPointerOutObserver: Nullable<Observer<PointerEvent>>;
  41. private _background: string;
  42. /** @hidden */
  43. public _rootContainer = new Container("root");
  44. /** @hidden */
  45. public _lastPickedControl: Control;
  46. /** @hidden */
  47. public _lastControlOver: { [pointerId: number]: Control } = {};
  48. /** @hidden */
  49. public _lastControlDown: { [pointerId: number]: Control } = {};
  50. /** @hidden */
  51. public _capturingControl: { [pointerId: number]: Control } = {};
  52. /** @hidden */
  53. public _shouldBlockPointer: boolean;
  54. /** @hidden */
  55. public _layerToDispose: Nullable<Layer>;
  56. /** @hidden */
  57. public _linkedControls = new Array<Control>();
  58. private _isFullscreen = false;
  59. private _fullscreenViewport = new Viewport(0, 0, 1, 1);
  60. private _idealWidth = 0;
  61. private _idealHeight = 0;
  62. private _useSmallestIdeal: boolean = false;
  63. private _renderAtIdealSize = false;
  64. private _focusedControl: Nullable<IFocusableControl>;
  65. private _blockNextFocusCheck = false;
  66. private _renderScale = 1;
  67. private _rootCanvas: Nullable<HTMLCanvasElement>;
  68. /**
  69. * Gets or sets a boolean defining if alpha is stored as premultiplied
  70. */
  71. public premulAlpha = false;
  72. /**
  73. * Gets or sets a number used to scale rendering size (2 means that the texture will be twice bigger).
  74. * Useful when you want more antialiasing
  75. */
  76. public get renderScale(): number {
  77. return this._renderScale;
  78. }
  79. public set renderScale(value: number) {
  80. if (value === this._renderScale) {
  81. return;
  82. }
  83. this._renderScale = value;
  84. this._onResize();
  85. }
  86. /** Gets or sets the background color */
  87. public get background(): string {
  88. return this._background;
  89. }
  90. public set background(value: string) {
  91. if (this._background === value) {
  92. return;
  93. }
  94. this._background = value;
  95. this.markAsDirty();
  96. }
  97. /**
  98. * Gets or sets the ideal width used to design controls.
  99. * The GUI will then rescale everything accordingly
  100. * @see http://doc.babylonjs.com/how_to/gui#adaptive-scaling
  101. */
  102. public get idealWidth(): number {
  103. return this._idealWidth;
  104. }
  105. public set idealWidth(value: number) {
  106. if (this._idealWidth === value) {
  107. return;
  108. }
  109. this._idealWidth = value;
  110. this.markAsDirty();
  111. this._rootContainer._markAllAsDirty();
  112. }
  113. /**
  114. * Gets or sets the ideal height used to design controls.
  115. * The GUI will then rescale everything accordingly
  116. * @see http://doc.babylonjs.com/how_to/gui#adaptive-scaling
  117. */
  118. public get idealHeight(): number {
  119. return this._idealHeight;
  120. }
  121. public set idealHeight(value: number) {
  122. if (this._idealHeight === value) {
  123. return;
  124. }
  125. this._idealHeight = value;
  126. this.markAsDirty();
  127. this._rootContainer._markAllAsDirty();
  128. }
  129. /**
  130. * Gets or sets a boolean indicating if the smallest ideal value must be used if idealWidth and idealHeight are both set
  131. * @see http://doc.babylonjs.com/how_to/gui#adaptive-scaling
  132. */
  133. public get useSmallestIdeal(): boolean {
  134. return this._useSmallestIdeal;
  135. }
  136. public set useSmallestIdeal(value: boolean) {
  137. if (this._useSmallestIdeal === value) {
  138. return;
  139. }
  140. this._useSmallestIdeal = value;
  141. this.markAsDirty();
  142. this._rootContainer._markAllAsDirty();
  143. }
  144. /**
  145. * Gets or sets a boolean indicating if adaptive scaling must be used
  146. * @see http://doc.babylonjs.com/how_to/gui#adaptive-scaling
  147. */
  148. public get renderAtIdealSize(): boolean {
  149. return this._renderAtIdealSize;
  150. }
  151. public set renderAtIdealSize(value: boolean) {
  152. if (this._renderAtIdealSize === value) {
  153. return;
  154. }
  155. this._renderAtIdealSize = value;
  156. this._onResize();
  157. }
  158. /**
  159. * Gets the underlying layer used to render the texture when in fullscreen mode
  160. */
  161. public get layer(): Nullable<Layer> {
  162. return this._layerToDispose;
  163. }
  164. /**
  165. * Gets the root container control
  166. */
  167. public get rootContainer(): Container {
  168. return this._rootContainer;
  169. }
  170. /**
  171. * Gets or sets the current focused control
  172. */
  173. public get focusedControl(): Nullable<IFocusableControl> {
  174. return this._focusedControl;
  175. }
  176. public set focusedControl(control: Nullable<IFocusableControl>) {
  177. if (this._focusedControl == control) {
  178. return;
  179. }
  180. if (this._focusedControl) {
  181. this._focusedControl.onBlur();
  182. }
  183. if (control) {
  184. control.onFocus();
  185. }
  186. this._focusedControl = control;
  187. }
  188. /**
  189. * Gets or sets a boolean indicating if the texture must be rendered in background or foreground when in fullscreen mode
  190. */
  191. public get isForeground(): boolean {
  192. if (!this.layer) {
  193. return true;
  194. }
  195. return (!this.layer.isBackground);
  196. }
  197. public set isForeground(value: boolean) {
  198. if (!this.layer) {
  199. return;
  200. }
  201. if (this.layer.isBackground === !value) {
  202. return;
  203. }
  204. this.layer.isBackground = !value;
  205. }
  206. /**
  207. * Creates a new AdvancedDynamicTexture
  208. * @param name defines the name of the texture
  209. * @param width defines the width of the texture
  210. * @param height defines the height of the texture
  211. * @param scene defines the hosting scene
  212. * @param generateMipMaps defines a boolean indicating if mipmaps must be generated (false by default)
  213. * @param samplingMode defines the texture sampling mode (Texture.NEAREST_SAMPLINGMODE by default)
  214. */
  215. constructor(name: string, width = 0, height = 0, scene: Nullable<Scene>, generateMipMaps = false, samplingMode = Texture.NEAREST_SAMPLINGMODE) {
  216. super(name, { width: width, height: height }, scene, generateMipMaps, samplingMode, Engine.TEXTUREFORMAT_RGBA);
  217. scene = this.getScene();
  218. if (!scene || !this._texture) {
  219. return;
  220. }
  221. this._rootCanvas = scene.getEngine()!.getRenderingCanvas()!;
  222. this._renderObserver = scene.onBeforeCameraRenderObservable.add((camera: Camera) => this._checkUpdate(camera));
  223. this._preKeyboardObserver = scene.onPreKeyboardObservable.add(info => {
  224. if (!this._focusedControl) {
  225. return;
  226. }
  227. if (info.type === KeyboardEventTypes.KEYDOWN) {
  228. this._focusedControl.processKeyboard(info.event);
  229. }
  230. info.skipOnPointerObservable = true;
  231. });
  232. this._rootContainer._link(null, this);
  233. this.hasAlpha = true;
  234. if (!width || !height) {
  235. this._resizeObserver = scene.getEngine().onResizeObservable.add(() => this._onResize());
  236. this._onResize();
  237. }
  238. this._texture.isReady = true;
  239. }
  240. /**
  241. * Function used to execute a function on all controls
  242. * @param func defines the function to execute
  243. * @param container defines the container where controls belong. If null the root container will be used
  244. */
  245. public executeOnAllControls(func: (control: Control) => void, container?: Container) {
  246. if (!container) {
  247. container = this._rootContainer;
  248. }
  249. func(container);
  250. for (var child of container.children) {
  251. if ((<any>child).children) {
  252. this.executeOnAllControls(func, (<Container>child));
  253. continue;
  254. }
  255. func(child);
  256. }
  257. }
  258. /**
  259. * Marks the texture as dirty forcing a complete update
  260. */
  261. public markAsDirty() {
  262. this._isDirty = true;
  263. this.executeOnAllControls((control) => {
  264. if (control._isFontSizeInPercentage) {
  265. control._resetFontCache();
  266. }
  267. });
  268. }
  269. /**
  270. * Helper function used to create a new style
  271. * @returns a new style
  272. * @see http://doc.babylonjs.com/how_to/gui#styles
  273. */
  274. public createStyle(): Style {
  275. return new Style(this);
  276. }
  277. /**
  278. * Adds a new control to the root container
  279. * @param control defines the control to add
  280. * @returns the current texture
  281. */
  282. public addControl(control: Control): AdvancedDynamicTexture {
  283. this._rootContainer.addControl(control);
  284. return this;
  285. }
  286. /**
  287. * Removes a control from the root container
  288. * @param control defines the control to remove
  289. * @returns the current texture
  290. */
  291. public removeControl(control: Control): AdvancedDynamicTexture {
  292. this._rootContainer.removeControl(control);
  293. return this;
  294. }
  295. /**
  296. * Release all resources
  297. */
  298. public dispose(): void {
  299. let scene = this.getScene();
  300. if (!scene) {
  301. return;
  302. }
  303. this._rootCanvas = null;
  304. scene.onBeforeCameraRenderObservable.remove(this._renderObserver);
  305. if (this._resizeObserver) {
  306. scene.getEngine().onResizeObservable.remove(this._resizeObserver);
  307. }
  308. if (this._pointerMoveObserver) {
  309. scene.onPrePointerObservable.remove(this._pointerMoveObserver);
  310. }
  311. if (this._pointerObserver) {
  312. scene.onPointerObservable.remove(this._pointerObserver);
  313. }
  314. if (this._preKeyboardObserver) {
  315. scene.onPreKeyboardObservable.remove(this._preKeyboardObserver);
  316. }
  317. if (this._canvasPointerOutObserver) {
  318. scene.getEngine().onCanvasPointerOutObservable.remove(this._canvasPointerOutObserver);
  319. }
  320. if (this._layerToDispose) {
  321. this._layerToDispose.texture = null;
  322. this._layerToDispose.dispose();
  323. this._layerToDispose = null;
  324. }
  325. this._rootContainer.dispose();
  326. super.dispose();
  327. }
  328. private _onResize(): void {
  329. let scene = this.getScene();
  330. if (!scene) {
  331. return;
  332. }
  333. // Check size
  334. var engine = scene.getEngine();
  335. var textureSize = this.getSize();
  336. var renderWidth = engine.getRenderWidth() * this._renderScale;
  337. var renderHeight = engine.getRenderHeight() * this._renderScale;
  338. if (this._renderAtIdealSize) {
  339. if (this._idealWidth) {
  340. renderHeight = (renderHeight * this._idealWidth) / renderWidth;
  341. renderWidth = this._idealWidth;
  342. } else if (this._idealHeight) {
  343. renderWidth = (renderWidth * this._idealHeight) / renderHeight;
  344. renderHeight = this._idealHeight;
  345. }
  346. }
  347. if (textureSize.width !== renderWidth || textureSize.height !== renderHeight) {
  348. this.scaleTo(renderWidth, renderHeight);
  349. this.markAsDirty();
  350. if (this._idealWidth || this._idealHeight) {
  351. this._rootContainer._markAllAsDirty();
  352. }
  353. }
  354. }
  355. /** @hidden */
  356. public _getGlobalViewport(scene: Scene): Viewport {
  357. var engine = scene.getEngine();
  358. return this._fullscreenViewport.toGlobal(engine.getRenderWidth(), engine.getRenderHeight());
  359. }
  360. /**
  361. * Get screen coordinates for a vector3
  362. * @param position defines the position to project
  363. * @param worldMatrix defines the world matrix to use
  364. * @returns the projected position
  365. */
  366. public getProjectedPosition(position: Vector3, worldMatrix: Matrix): Vector2 {
  367. var scene = this.getScene();
  368. if (!scene) {
  369. return Vector2.Zero();
  370. }
  371. var globalViewport = this._getGlobalViewport(scene);
  372. var projectedPosition = Vector3.Project(position, worldMatrix, scene.getTransformMatrix(), globalViewport);
  373. projectedPosition.scaleInPlace(this.renderScale);
  374. return new Vector2(projectedPosition.x, projectedPosition.y);
  375. }
  376. private _checkUpdate(camera: Camera): void {
  377. if (this._layerToDispose) {
  378. if ((camera.layerMask & this._layerToDispose.layerMask) === 0) {
  379. return;
  380. }
  381. }
  382. if (this._isFullscreen && this._linkedControls.length) {
  383. var scene = this.getScene();
  384. if (!scene) {
  385. return;
  386. }
  387. var globalViewport = this._getGlobalViewport(scene);
  388. for (var control of this._linkedControls) {
  389. if (!control.isVisible) {
  390. continue;
  391. }
  392. var mesh = control._linkedMesh;
  393. if (!mesh || mesh.isDisposed()) {
  394. Tools.SetImmediate(() => {
  395. control.linkWithMesh(null);
  396. });
  397. continue;
  398. }
  399. var position = mesh.getBoundingInfo().boundingSphere.center;
  400. var projectedPosition = Vector3.Project(position, mesh.getWorldMatrix(), scene.getTransformMatrix(), globalViewport);
  401. if (projectedPosition.z < 0 || projectedPosition.z > 1) {
  402. control.notRenderable = true;
  403. continue;
  404. }
  405. control.notRenderable = false;
  406. // Account for RenderScale.
  407. projectedPosition.scaleInPlace(this.renderScale);
  408. control._moveToProjectedPosition(projectedPosition);
  409. }
  410. }
  411. if (!this._isDirty && !this._rootContainer.isDirty) {
  412. return;
  413. }
  414. this._isDirty = false;
  415. this._render();
  416. this.update(true, this.premulAlpha);
  417. }
  418. private _render(): void {
  419. var textureSize = this.getSize();
  420. var renderWidth = textureSize.width;
  421. var renderHeight = textureSize.height;
  422. // Clear
  423. var context = this.getContext();
  424. context.clearRect(0, 0, renderWidth, renderHeight);
  425. if (this._background) {
  426. context.save();
  427. context.fillStyle = this._background;
  428. context.fillRect(0, 0, renderWidth, renderHeight);
  429. context.restore();
  430. }
  431. // Render
  432. context.font = "18px Arial";
  433. context.strokeStyle = "white";
  434. var measure = new Measure(0, 0, renderWidth, renderHeight);
  435. this._rootContainer._draw(measure, context);
  436. }
  437. /** @hidden */
  438. public _changeCursor(cursor: string) {
  439. if (this._rootCanvas) {
  440. this._rootCanvas.style.cursor = cursor;
  441. }
  442. }
  443. private _doPicking(x: number, y: number, type: number, pointerId: number, buttonIndex: number): void {
  444. var scene = this.getScene();
  445. if (!scene) {
  446. return;
  447. }
  448. var engine = scene.getEngine();
  449. var textureSize = this.getSize();
  450. if (this._isFullscreen) {
  451. x = x * (textureSize.width / engine.getRenderWidth());
  452. y = y * (textureSize.height / engine.getRenderHeight());
  453. }
  454. if (this._capturingControl[pointerId]) {
  455. this._capturingControl[pointerId]._processObservables(type, x, y, pointerId, buttonIndex);
  456. return;
  457. }
  458. if (!this._rootContainer._processPicking(x, y, type, pointerId, buttonIndex)) {
  459. this._changeCursor("");
  460. if (type === PointerEventTypes.POINTERMOVE) {
  461. if (this._lastControlOver[pointerId]) {
  462. this._lastControlOver[pointerId]._onPointerOut(this._lastControlOver[pointerId]);
  463. }
  464. delete this._lastControlOver[pointerId];
  465. }
  466. }
  467. this._manageFocus();
  468. }
  469. /** @hidden */
  470. public _cleanControlAfterRemovalFromList(list: { [pointerId: number]: Control }, control: Control) {
  471. for (var pointerId in list) {
  472. if (!list.hasOwnProperty(pointerId)) {
  473. continue;
  474. }
  475. var lastControlOver = list[pointerId];
  476. if (lastControlOver === control) {
  477. delete list[pointerId];
  478. }
  479. }
  480. }
  481. /** @hidden */
  482. public _cleanControlAfterRemoval(control: Control) {
  483. this._cleanControlAfterRemovalFromList(this._lastControlDown, control);
  484. this._cleanControlAfterRemovalFromList(this._lastControlOver, control);
  485. }
  486. /** Attach to all scene events required to support pointer events */
  487. public attach(): void {
  488. var scene = this.getScene();
  489. if (!scene) {
  490. return;
  491. }
  492. this._pointerMoveObserver = scene.onPrePointerObservable.add((pi, state) => {
  493. if (scene!.isPointerCaptured((<PointerEvent>(pi.event)).pointerId)) {
  494. return;
  495. }
  496. if (pi.type !== PointerEventTypes.POINTERMOVE
  497. && pi.type !== PointerEventTypes.POINTERUP
  498. && pi.type !== PointerEventTypes.POINTERDOWN) {
  499. return;
  500. }
  501. if (!scene) {
  502. return;
  503. }
  504. let camera = scene.cameraToUseForPointers || scene.activeCamera;
  505. if (!camera) {
  506. return;
  507. }
  508. let engine = scene.getEngine();
  509. let viewport = camera.viewport;
  510. let x = (scene.pointerX / engine.getHardwareScalingLevel() - viewport.x * engine.getRenderWidth()) / viewport.width;
  511. let y = (scene.pointerY / engine.getHardwareScalingLevel() - viewport.y * engine.getRenderHeight()) / viewport.height;
  512. this._shouldBlockPointer = false;
  513. // Do picking modifies _shouldBlockPointer
  514. this._doPicking(x, y, pi.type, (pi.event as PointerEvent).pointerId || 0, pi.event.button);
  515. // Avoid overwriting a true skipOnPointerObservable to false
  516. if(this._shouldBlockPointer){
  517. pi.skipOnPointerObservable = this._shouldBlockPointer;
  518. }
  519. });
  520. this._attachToOnPointerOut(scene);
  521. }
  522. /**
  523. * Connect the texture to a hosting mesh to enable interactions
  524. * @param mesh defines the mesh to attach to
  525. * @param supportPointerMove defines a boolean indicating if pointer move events must be catched as well
  526. */
  527. public attachToMesh(mesh: AbstractMesh, supportPointerMove = true): void {
  528. var scene = this.getScene();
  529. if (!scene) {
  530. return;
  531. }
  532. this._pointerObserver = scene.onPointerObservable.add((pi, state) => {
  533. if (pi.type !== PointerEventTypes.POINTERMOVE
  534. && pi.type !== PointerEventTypes.POINTERUP
  535. && pi.type !== PointerEventTypes.POINTERDOWN) {
  536. return;
  537. }
  538. var pointerId = (pi.event as PointerEvent).pointerId || 0;
  539. if (pi.pickInfo && pi.pickInfo.hit && pi.pickInfo.pickedMesh === mesh) {
  540. var uv = pi.pickInfo.getTextureCoordinates();
  541. if (uv) {
  542. let size = this.getSize();
  543. this._doPicking(uv.x * size.width, (1.0 - uv.y) * size.height, pi.type, pointerId, pi.event.button);
  544. }
  545. } else if (pi.type === PointerEventTypes.POINTERUP) {
  546. if (this._lastControlDown[pointerId]) {
  547. this._lastControlDown[pointerId]._forcePointerUp(pointerId);
  548. }
  549. delete this._lastControlDown[pointerId];
  550. if (this.focusedControl) {
  551. const friendlyControls = this.focusedControl.keepsFocusWith();
  552. let canMoveFocus = true;
  553. if (friendlyControls) {
  554. for (var control of friendlyControls) {
  555. // Same host, no need to keep the focus
  556. if (this === control._host) {
  557. continue;
  558. }
  559. // Different hosts
  560. const otherHost = control._host;
  561. if (otherHost._lastControlOver[pointerId] && otherHost._lastControlOver[pointerId].isAscendant(control)) {
  562. canMoveFocus = false;
  563. break;
  564. }
  565. }
  566. }
  567. if (canMoveFocus) {
  568. this.focusedControl = null;
  569. }
  570. }
  571. } else if (pi.type === PointerEventTypes.POINTERMOVE) {
  572. if (this._lastControlOver[pointerId]) {
  573. this._lastControlOver[pointerId]._onPointerOut(this._lastControlOver[pointerId]);
  574. }
  575. delete this._lastControlOver[pointerId];
  576. }
  577. });
  578. mesh.enablePointerMoveEvents = supportPointerMove;
  579. this._attachToOnPointerOut(scene);
  580. }
  581. /**
  582. * Move the focus to a specific control
  583. * @param control defines the control which will receive the focus
  584. */
  585. public moveFocusToControl(control: IFocusableControl): void {
  586. this.focusedControl = control;
  587. this._lastPickedControl = <any>control;
  588. this._blockNextFocusCheck = true;
  589. }
  590. private _manageFocus(): void {
  591. if (this._blockNextFocusCheck) {
  592. this._blockNextFocusCheck = false;
  593. this._lastPickedControl = <any>this._focusedControl;
  594. return;
  595. }
  596. // Focus management
  597. if (this._focusedControl) {
  598. if (this._focusedControl !== (<any>this._lastPickedControl)) {
  599. if (this._lastPickedControl.isFocusInvisible) {
  600. return;
  601. }
  602. this.focusedControl = null;
  603. }
  604. }
  605. }
  606. private _attachToOnPointerOut(scene: Scene): void {
  607. this._canvasPointerOutObserver = scene.getEngine().onCanvasPointerOutObservable.add((pointerEvent) => {
  608. if (this._lastControlOver[pointerEvent.pointerId]) {
  609. this._lastControlOver[pointerEvent.pointerId]._onPointerOut(this._lastControlOver[pointerEvent.pointerId]);
  610. }
  611. delete this._lastControlOver[pointerEvent.pointerId];
  612. if (this._lastControlDown[pointerEvent.pointerId]) {
  613. this._lastControlDown[pointerEvent.pointerId]._forcePointerUp();
  614. }
  615. delete this._lastControlDown[pointerEvent.pointerId];
  616. });
  617. }
  618. // Statics
  619. /**
  620. * Creates a new AdvancedDynamicTexture in projected mode (ie. attached to a mesh)
  621. * @param mesh defines the mesh which will receive the texture
  622. * @param width defines the texture width (1024 by default)
  623. * @param height defines the texture height (1024 by default)
  624. * @param supportPointerMove defines a boolean indicating if the texture must capture move events (true by default)
  625. * @param onlyAlphaTesting defines a boolean indicating that alpha blending will not be used (only alpha testing) (false by default)
  626. * @returns a new AdvancedDynamicTexture
  627. */
  628. public static CreateForMesh(mesh: AbstractMesh, width = 1024, height = 1024, supportPointerMove = true, onlyAlphaTesting = false): AdvancedDynamicTexture {
  629. var result = new AdvancedDynamicTexture(mesh.name + " AdvancedDynamicTexture", width, height, mesh.getScene(), true, Texture.TRILINEAR_SAMPLINGMODE);
  630. var material = new StandardMaterial("AdvancedDynamicTextureMaterial", mesh.getScene());
  631. material.backFaceCulling = false;
  632. material.diffuseColor = Color3.Black();
  633. material.specularColor = Color3.Black();
  634. if (onlyAlphaTesting) {
  635. material.diffuseTexture = result;
  636. material.emissiveTexture = result;
  637. result.hasAlpha = true;
  638. } else {
  639. material.emissiveTexture = result;
  640. material.opacityTexture = result;
  641. }
  642. mesh.material = material;
  643. result.attachToMesh(mesh, supportPointerMove);
  644. return result;
  645. }
  646. /**
  647. * Creates a new AdvancedDynamicTexture in fullscreen mode.
  648. * In this mode the texture will rely on a layer for its rendering.
  649. * This allows it to be treated like any other layer.
  650. * As such, if you have a multi camera setup, you can set the layerMask on the GUI as well.
  651. * LayerMask is set through advancedTexture.layer.layerMask
  652. * @param name defines name for the texture
  653. * @param foreground defines a boolean indicating if the texture must be rendered in foreground (default is true)
  654. * @param scene defines the hsoting scene
  655. * @param sampling defines the texture sampling mode (Texture.BILINEAR_SAMPLINGMODE by default)
  656. * @returns a new AdvancedDynamicTexture
  657. */
  658. public static CreateFullscreenUI(name: string, foreground: boolean = true, scene: Nullable<Scene> = null, sampling = Texture.BILINEAR_SAMPLINGMODE): AdvancedDynamicTexture {
  659. var result = new AdvancedDynamicTexture(name, 0, 0, scene, false, sampling);
  660. // Display
  661. var layer = new Layer(name + "_layer", null, scene, !foreground);
  662. layer.texture = result;
  663. result._layerToDispose = layer;
  664. result._isFullscreen = true;
  665. // Attach
  666. result.attach();
  667. return result;
  668. }
  669. }