advancedDynamicTexture.ts 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  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. }
  264. /**
  265. * Helper function used to create a new style
  266. * @returns a new style
  267. * @see http://doc.babylonjs.com/how_to/gui#styles
  268. */
  269. public createStyle(): Style {
  270. return new Style(this);
  271. }
  272. /**
  273. * Adds a new control to the root container
  274. * @param control defines the control to add
  275. * @returns the current texture
  276. */
  277. public addControl(control: Control): AdvancedDynamicTexture {
  278. this._rootContainer.addControl(control);
  279. return this;
  280. }
  281. /**
  282. * Removes a control from the root container
  283. * @param control defines the control to remove
  284. * @returns the current texture
  285. */
  286. public removeControl(control: Control): AdvancedDynamicTexture {
  287. this._rootContainer.removeControl(control);
  288. return this;
  289. }
  290. /**
  291. * Release all resources
  292. */
  293. public dispose(): void {
  294. let scene = this.getScene();
  295. if (!scene) {
  296. return;
  297. }
  298. this._rootCanvas = null;
  299. scene.onBeforeCameraRenderObservable.remove(this._renderObserver);
  300. if (this._resizeObserver) {
  301. scene.getEngine().onResizeObservable.remove(this._resizeObserver);
  302. }
  303. if (this._pointerMoveObserver) {
  304. scene.onPrePointerObservable.remove(this._pointerMoveObserver);
  305. }
  306. if (this._pointerObserver) {
  307. scene.onPointerObservable.remove(this._pointerObserver);
  308. }
  309. if (this._preKeyboardObserver) {
  310. scene.onPreKeyboardObservable.remove(this._preKeyboardObserver);
  311. }
  312. if (this._canvasPointerOutObserver) {
  313. scene.getEngine().onCanvasPointerOutObservable.remove(this._canvasPointerOutObserver);
  314. }
  315. if (this._layerToDispose) {
  316. this._layerToDispose.texture = null;
  317. this._layerToDispose.dispose();
  318. this._layerToDispose = null;
  319. }
  320. this._rootContainer.dispose();
  321. super.dispose();
  322. }
  323. private _onResize(): void {
  324. let scene = this.getScene();
  325. if (!scene) {
  326. return;
  327. }
  328. // Check size
  329. var engine = scene.getEngine();
  330. var textureSize = this.getSize();
  331. var renderWidth = engine.getRenderWidth() * this._renderScale;
  332. var renderHeight = engine.getRenderHeight() * this._renderScale;
  333. if (this._renderAtIdealSize) {
  334. if (this._idealWidth) {
  335. renderHeight = (renderHeight * this._idealWidth) / renderWidth;
  336. renderWidth = this._idealWidth;
  337. } else if (this._idealHeight) {
  338. renderWidth = (renderWidth * this._idealHeight) / renderHeight;
  339. renderHeight = this._idealHeight;
  340. }
  341. }
  342. if (textureSize.width !== renderWidth || textureSize.height !== renderHeight) {
  343. this.scaleTo(renderWidth, renderHeight);
  344. this.markAsDirty();
  345. if (this._idealWidth || this._idealHeight) {
  346. this._rootContainer._markAllAsDirty();
  347. }
  348. }
  349. }
  350. /** @hidden */
  351. public _getGlobalViewport(scene: Scene): Viewport {
  352. var engine = scene.getEngine();
  353. return this._fullscreenViewport.toGlobal(engine.getRenderWidth(), engine.getRenderHeight());
  354. }
  355. /**
  356. * Get screen coordinates for a vector3
  357. * @param position defines the position to project
  358. * @param worldMatrix defines the world matrix to use
  359. * @returns the projected position
  360. */
  361. public getProjectedPosition(position: Vector3, worldMatrix: Matrix): Vector2 {
  362. var scene = this.getScene();
  363. if (!scene) {
  364. return Vector2.Zero();
  365. }
  366. var globalViewport = this._getGlobalViewport(scene);
  367. var projectedPosition = Vector3.Project(position, worldMatrix, scene.getTransformMatrix(), globalViewport);
  368. projectedPosition.scaleInPlace(this.renderScale);
  369. return new Vector2(projectedPosition.x, projectedPosition.y);
  370. }
  371. private _checkUpdate(camera: Camera): void {
  372. if (this._layerToDispose) {
  373. if ((camera.layerMask & this._layerToDispose.layerMask) === 0) {
  374. return;
  375. }
  376. }
  377. if (this._isFullscreen && this._linkedControls.length) {
  378. var scene = this.getScene();
  379. if (!scene) {
  380. return;
  381. }
  382. var globalViewport = this._getGlobalViewport(scene);
  383. for (var control of this._linkedControls) {
  384. if (!control.isVisible) {
  385. continue;
  386. }
  387. var mesh = control._linkedMesh;
  388. if (!mesh || mesh.isDisposed()) {
  389. Tools.SetImmediate(() => {
  390. control.linkWithMesh(null);
  391. });
  392. continue;
  393. }
  394. var position = mesh.getBoundingInfo().boundingSphere.center;
  395. var projectedPosition = Vector3.Project(position, mesh.getWorldMatrix(), scene.getTransformMatrix(), globalViewport);
  396. if (projectedPosition.z < 0 || projectedPosition.z > 1) {
  397. control.notRenderable = true;
  398. continue;
  399. }
  400. control.notRenderable = false;
  401. // Account for RenderScale.
  402. projectedPosition.scaleInPlace(this.renderScale);
  403. control._moveToProjectedPosition(projectedPosition);
  404. }
  405. }
  406. if (!this._isDirty && !this._rootContainer.isDirty) {
  407. return;
  408. }
  409. this._isDirty = false;
  410. this._render();
  411. this.update(true, this.premulAlpha);
  412. }
  413. private _render(): void {
  414. var textureSize = this.getSize();
  415. var renderWidth = textureSize.width;
  416. var renderHeight = textureSize.height;
  417. // Clear
  418. var context = this.getContext();
  419. context.clearRect(0, 0, renderWidth, renderHeight);
  420. if (this._background) {
  421. context.save();
  422. context.fillStyle = this._background;
  423. context.fillRect(0, 0, renderWidth, renderHeight);
  424. context.restore();
  425. }
  426. // Render
  427. context.font = "18px Arial";
  428. context.strokeStyle = "white";
  429. var measure = new Measure(0, 0, renderWidth, renderHeight);
  430. this._rootContainer._draw(measure, context);
  431. }
  432. /** @hidden */
  433. public _changeCursor(cursor: string) {
  434. if (this._rootCanvas) {
  435. this._rootCanvas.style.cursor = cursor;
  436. }
  437. }
  438. private _doPicking(x: number, y: number, type: number, pointerId: number, buttonIndex: number): void {
  439. var scene = this.getScene();
  440. if (!scene) {
  441. return;
  442. }
  443. var engine = scene.getEngine();
  444. var textureSize = this.getSize();
  445. if (this._isFullscreen) {
  446. x = x * (textureSize.width / engine.getRenderWidth());
  447. y = y * (textureSize.height / engine.getRenderHeight());
  448. }
  449. if (this._capturingControl[pointerId]) {
  450. this._capturingControl[pointerId]._processObservables(type, x, y, pointerId, buttonIndex);
  451. return;
  452. }
  453. if (!this._rootContainer._processPicking(x, y, type, pointerId, buttonIndex)) {
  454. this._changeCursor("");
  455. if (type === PointerEventTypes.POINTERMOVE) {
  456. if (this._lastControlOver[pointerId]) {
  457. this._lastControlOver[pointerId]._onPointerOut(this._lastControlOver[pointerId]);
  458. delete this._lastControlOver[pointerId];
  459. }
  460. }
  461. }
  462. this._manageFocus();
  463. }
  464. /** @hidden */
  465. public _cleanControlAfterRemovalFromList(list: { [pointerId: number]: Control }, control: Control) {
  466. for (var pointerId in list) {
  467. if (!list.hasOwnProperty(pointerId)) {
  468. continue;
  469. }
  470. var lastControlOver = list[pointerId];
  471. if (lastControlOver === control) {
  472. delete list[pointerId];
  473. }
  474. }
  475. }
  476. /** @hidden */
  477. public _cleanControlAfterRemoval(control: Control) {
  478. this._cleanControlAfterRemovalFromList(this._lastControlDown, control);
  479. this._cleanControlAfterRemovalFromList(this._lastControlOver, control);
  480. }
  481. /** Attach to all scene events required to support pointer events */
  482. public attach(): void {
  483. var scene = this.getScene();
  484. if (!scene) {
  485. return;
  486. }
  487. this._pointerMoveObserver = scene.onPrePointerObservable.add((pi, state) => {
  488. if (scene!.isPointerCaptured((<PointerEvent>(pi.event)).pointerId)) {
  489. return;
  490. }
  491. if (pi.type !== PointerEventTypes.POINTERMOVE
  492. && pi.type !== PointerEventTypes.POINTERUP
  493. && pi.type !== PointerEventTypes.POINTERDOWN) {
  494. return;
  495. }
  496. if (!scene) {
  497. return;
  498. }
  499. let camera = scene.cameraToUseForPointers || scene.activeCamera;
  500. if (!camera) {
  501. return;
  502. }
  503. let engine = scene.getEngine();
  504. let viewport = camera.viewport;
  505. let x = (scene.pointerX / engine.getHardwareScalingLevel() - viewport.x * engine.getRenderWidth()) / viewport.width;
  506. let y = (scene.pointerY / engine.getHardwareScalingLevel() - viewport.y * engine.getRenderHeight()) / viewport.height;
  507. this._shouldBlockPointer = false;
  508. // Do picking modifies _shouldBlockPointer
  509. this._doPicking(x, y, pi.type, (pi.event as PointerEvent).pointerId || 0, pi.event.button);
  510. // Avoid overwriting a true skipOnPointerObservable to false
  511. if (this._shouldBlockPointer) {
  512. pi.skipOnPointerObservable = this._shouldBlockPointer;
  513. }
  514. });
  515. this._attachToOnPointerOut(scene);
  516. }
  517. /**
  518. * Connect the texture to a hosting mesh to enable interactions
  519. * @param mesh defines the mesh to attach to
  520. * @param supportPointerMove defines a boolean indicating if pointer move events must be catched as well
  521. */
  522. public attachToMesh(mesh: AbstractMesh, supportPointerMove = true): void {
  523. var scene = this.getScene();
  524. if (!scene) {
  525. return;
  526. }
  527. this._pointerObserver = scene.onPointerObservable.add((pi, state) => {
  528. if (pi.type !== PointerEventTypes.POINTERMOVE
  529. && pi.type !== PointerEventTypes.POINTERUP
  530. && pi.type !== PointerEventTypes.POINTERDOWN) {
  531. return;
  532. }
  533. var pointerId = (pi.event as PointerEvent).pointerId || 0;
  534. if (pi.pickInfo && pi.pickInfo.hit && pi.pickInfo.pickedMesh === mesh) {
  535. var uv = pi.pickInfo.getTextureCoordinates();
  536. if (uv) {
  537. let size = this.getSize();
  538. this._doPicking(uv.x * size.width, (1.0 - uv.y) * size.height, pi.type, pointerId, pi.event.button);
  539. }
  540. } else if (pi.type === PointerEventTypes.POINTERUP) {
  541. if (this._lastControlDown[pointerId]) {
  542. this._lastControlDown[pointerId]._forcePointerUp(pointerId);
  543. }
  544. delete this._lastControlDown[pointerId];
  545. if (this.focusedControl) {
  546. const friendlyControls = this.focusedControl.keepsFocusWith();
  547. let canMoveFocus = true;
  548. if (friendlyControls) {
  549. for (var control of friendlyControls) {
  550. // Same host, no need to keep the focus
  551. if (this === control._host) {
  552. continue;
  553. }
  554. // Different hosts
  555. const otherHost = control._host;
  556. if (otherHost._lastControlOver[pointerId] && otherHost._lastControlOver[pointerId].isAscendant(control)) {
  557. canMoveFocus = false;
  558. break;
  559. }
  560. }
  561. }
  562. if (canMoveFocus) {
  563. this.focusedControl = null;
  564. }
  565. }
  566. } else if (pi.type === PointerEventTypes.POINTERMOVE) {
  567. if (this._lastControlOver[pointerId]) {
  568. this._lastControlOver[pointerId]._onPointerOut(this._lastControlOver[pointerId]);
  569. }
  570. delete this._lastControlOver[pointerId];
  571. }
  572. });
  573. mesh.enablePointerMoveEvents = supportPointerMove;
  574. this._attachToOnPointerOut(scene);
  575. }
  576. /**
  577. * Move the focus to a specific control
  578. * @param control defines the control which will receive the focus
  579. */
  580. public moveFocusToControl(control: IFocusableControl): void {
  581. this.focusedControl = control;
  582. this._lastPickedControl = <any>control;
  583. this._blockNextFocusCheck = true;
  584. }
  585. private _manageFocus(): void {
  586. if (this._blockNextFocusCheck) {
  587. this._blockNextFocusCheck = false;
  588. this._lastPickedControl = <any>this._focusedControl;
  589. return;
  590. }
  591. // Focus management
  592. if (this._focusedControl) {
  593. if (this._focusedControl !== (<any>this._lastPickedControl)) {
  594. if (this._lastPickedControl.isFocusInvisible) {
  595. return;
  596. }
  597. this.focusedControl = null;
  598. }
  599. }
  600. }
  601. private _attachToOnPointerOut(scene: Scene): void {
  602. this._canvasPointerOutObserver = scene.getEngine().onCanvasPointerOutObservable.add((pointerEvent) => {
  603. if (this._lastControlOver[pointerEvent.pointerId]) {
  604. this._lastControlOver[pointerEvent.pointerId]._onPointerOut(this._lastControlOver[pointerEvent.pointerId]);
  605. }
  606. delete this._lastControlOver[pointerEvent.pointerId];
  607. if (this._lastControlDown[pointerEvent.pointerId]) {
  608. this._lastControlDown[pointerEvent.pointerId]._forcePointerUp();
  609. }
  610. delete this._lastControlDown[pointerEvent.pointerId];
  611. });
  612. }
  613. // Statics
  614. /**
  615. * Creates a new AdvancedDynamicTexture in projected mode (ie. attached to a mesh)
  616. * @param mesh defines the mesh which will receive the texture
  617. * @param width defines the texture width (1024 by default)
  618. * @param height defines the texture height (1024 by default)
  619. * @param supportPointerMove defines a boolean indicating if the texture must capture move events (true by default)
  620. * @param onlyAlphaTesting defines a boolean indicating that alpha blending will not be used (only alpha testing) (false by default)
  621. * @returns a new AdvancedDynamicTexture
  622. */
  623. public static CreateForMesh(mesh: AbstractMesh, width = 1024, height = 1024, supportPointerMove = true, onlyAlphaTesting = false): AdvancedDynamicTexture {
  624. var result = new AdvancedDynamicTexture(mesh.name + " AdvancedDynamicTexture", width, height, mesh.getScene(), true, Texture.TRILINEAR_SAMPLINGMODE);
  625. var material = new StandardMaterial("AdvancedDynamicTextureMaterial", mesh.getScene());
  626. material.backFaceCulling = false;
  627. material.diffuseColor = Color3.Black();
  628. material.specularColor = Color3.Black();
  629. if (onlyAlphaTesting) {
  630. material.diffuseTexture = result;
  631. material.emissiveTexture = result;
  632. result.hasAlpha = true;
  633. } else {
  634. material.emissiveTexture = result;
  635. material.opacityTexture = result;
  636. }
  637. mesh.material = material;
  638. result.attachToMesh(mesh, supportPointerMove);
  639. return result;
  640. }
  641. /**
  642. * Creates a new AdvancedDynamicTexture in fullscreen mode.
  643. * In this mode the texture will rely on a layer for its rendering.
  644. * This allows it to be treated like any other layer.
  645. * As such, if you have a multi camera setup, you can set the layerMask on the GUI as well.
  646. * LayerMask is set through advancedTexture.layer.layerMask
  647. * @param name defines name for the texture
  648. * @param foreground defines a boolean indicating if the texture must be rendered in foreground (default is true)
  649. * @param scene defines the hsoting scene
  650. * @param sampling defines the texture sampling mode (Texture.BILINEAR_SAMPLINGMODE by default)
  651. * @returns a new AdvancedDynamicTexture
  652. */
  653. public static CreateFullscreenUI(name: string, foreground: boolean = true, scene: Nullable<Scene> = null, sampling = Texture.BILINEAR_SAMPLINGMODE): AdvancedDynamicTexture {
  654. var result = new AdvancedDynamicTexture(name, 0, 0, scene, false, sampling);
  655. // Display
  656. var layer = new Layer(name + "_layer", null, scene, !foreground);
  657. layer.texture = result;
  658. result._layerToDispose = layer;
  659. result._isFullscreen = true;
  660. // Attach
  661. result.attach();
  662. return result;
  663. }
  664. }