advancedDynamicTexture.ts 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  1. import { Nullable } from "babylonjs/types";
  2. import { Observable, Observer } from "babylonjs/Misc/observable";
  3. import { Viewport, Color3, Vector2, Vector3, Matrix } from "babylonjs/Maths/math";
  4. import { Tools } from "babylonjs/Misc/tools";
  5. import { PointerInfoPre, PointerInfo, PointerEventTypes } from 'babylonjs/Events/pointerEvents';
  6. import { ClipboardEventTypes, ClipboardInfo } from "babylonjs/Events/clipboardEvents";
  7. import { KeyboardInfoPre, KeyboardEventTypes } from "babylonjs/Events/keyboardEvents";
  8. import { Camera } from "babylonjs/Cameras/camera";
  9. import { StandardMaterial } from "babylonjs/Materials/standardMaterial";
  10. import { Texture } from "babylonjs/Materials/Textures/texture";
  11. import { DynamicTexture } from "babylonjs/Materials/Textures/dynamicTexture";
  12. import { AbstractMesh } from "babylonjs/Meshes/abstractMesh";
  13. import { Layer } from "babylonjs/Layers/layer";
  14. import { Engine } from "babylonjs/Engines/engine";
  15. import { Scene } from "babylonjs/scene";
  16. import { Container } from "./controls/container";
  17. import { Control } from "./controls/control";
  18. import { Style } from "./style";
  19. import { Measure } from "./measure";
  20. import { Constants } from 'babylonjs/Engines/constants';
  21. /**
  22. * Interface used to define a control that can receive focus
  23. */
  24. export interface IFocusableControl {
  25. /**
  26. * Function called when the control receives the focus
  27. */
  28. onFocus(): void;
  29. /**
  30. * Function called when the control loses the focus
  31. */
  32. onBlur(): void;
  33. /**
  34. * Function called to let the control handle keyboard events
  35. * @param evt defines the current keyboard event
  36. */
  37. processKeyboard(evt: KeyboardEvent): void;
  38. /**
  39. * Function called to get the list of controls that should not steal the focus from this control
  40. * @returns an array of controls
  41. */
  42. keepsFocusWith(): Nullable<Control[]>;
  43. }
  44. /**
  45. * Class used to create texture to support 2D GUI elements
  46. * @see http://doc.babylonjs.com/how_to/gui
  47. */
  48. export class AdvancedDynamicTexture extends DynamicTexture {
  49. private _isDirty = false;
  50. private _renderObserver: Nullable<Observer<Camera>>;
  51. private _resizeObserver: Nullable<Observer<Engine>>;
  52. private _preKeyboardObserver: Nullable<Observer<KeyboardInfoPre>>;
  53. private _pointerMoveObserver: Nullable<Observer<PointerInfoPre>>;
  54. private _pointerObserver: Nullable<Observer<PointerInfo>>;
  55. private _canvasPointerOutObserver: Nullable<Observer<PointerEvent>>;
  56. private _background: string;
  57. /** @hidden */
  58. public _rootContainer = new Container("root");
  59. /** @hidden */
  60. public _lastPickedControl: Control;
  61. /** @hidden */
  62. public _lastControlOver: { [pointerId: number]: Control } = {};
  63. /** @hidden */
  64. public _lastControlDown: { [pointerId: number]: Control } = {};
  65. /** @hidden */
  66. public _capturingControl: { [pointerId: number]: Control } = {};
  67. /** @hidden */
  68. public _shouldBlockPointer: boolean;
  69. /** @hidden */
  70. public _layerToDispose: Nullable<Layer>;
  71. /** @hidden */
  72. public _linkedControls = new Array<Control>();
  73. private _isFullscreen = false;
  74. private _fullscreenViewport = new Viewport(0, 0, 1, 1);
  75. private _idealWidth = 0;
  76. private _idealHeight = 0;
  77. private _useSmallestIdeal: boolean = false;
  78. private _renderAtIdealSize = false;
  79. private _focusedControl: Nullable<IFocusableControl>;
  80. private _blockNextFocusCheck = false;
  81. private _renderScale = 1;
  82. private _rootElement: Nullable<HTMLElement>;
  83. private _cursorChanged = false;
  84. /**
  85. * Define type to string to ensure compatibility across browsers
  86. * Safari doesn't support DataTransfer constructor
  87. */
  88. private _clipboardData: string = "";
  89. /**
  90. * Observable event triggered each time an clipboard event is received from the rendering canvas
  91. */
  92. public onClipboardObservable = new Observable<ClipboardInfo>();
  93. /**
  94. * Observable event triggered each time a pointer down is intercepted by a control
  95. */
  96. public onControlPickedObservable = new Observable<Control>();
  97. /**
  98. * Observable event triggered before layout is evaluated
  99. */
  100. public onBeginLayoutObservable = new Observable<AdvancedDynamicTexture>();
  101. /**
  102. * Observable event triggered after the layout was evaluated
  103. */
  104. public onEndLayoutObservable = new Observable<AdvancedDynamicTexture>();
  105. /**
  106. * Observable event triggered before the texture is rendered
  107. */
  108. public onBeginRenderObservable = new Observable<AdvancedDynamicTexture>();
  109. /**
  110. * Observable event triggered after the texture was rendered
  111. */
  112. public onEndRenderObservable = new Observable<AdvancedDynamicTexture>();
  113. /**
  114. * Gets or sets a boolean defining if alpha is stored as premultiplied
  115. */
  116. public premulAlpha = false;
  117. /**
  118. * Gets or sets a number used to scale rendering size (2 means that the texture will be twice bigger).
  119. * Useful when you want more antialiasing
  120. */
  121. public get renderScale(): number {
  122. return this._renderScale;
  123. }
  124. public set renderScale(value: number) {
  125. if (value === this._renderScale) {
  126. return;
  127. }
  128. this._renderScale = value;
  129. this._onResize();
  130. }
  131. /** Gets or sets the background color */
  132. public get background(): string {
  133. return this._background;
  134. }
  135. public set background(value: string) {
  136. if (this._background === value) {
  137. return;
  138. }
  139. this._background = value;
  140. this.markAsDirty();
  141. }
  142. /**
  143. * Gets or sets the ideal width used to design controls.
  144. * The GUI will then rescale everything accordingly
  145. * @see http://doc.babylonjs.com/how_to/gui#adaptive-scaling
  146. */
  147. public get idealWidth(): number {
  148. return this._idealWidth;
  149. }
  150. public set idealWidth(value: number) {
  151. if (this._idealWidth === value) {
  152. return;
  153. }
  154. this._idealWidth = value;
  155. this.markAsDirty();
  156. this._rootContainer._markAllAsDirty();
  157. }
  158. /**
  159. * Gets or sets the ideal height used to design controls.
  160. * The GUI will then rescale everything accordingly
  161. * @see http://doc.babylonjs.com/how_to/gui#adaptive-scaling
  162. */
  163. public get idealHeight(): number {
  164. return this._idealHeight;
  165. }
  166. public set idealHeight(value: number) {
  167. if (this._idealHeight === value) {
  168. return;
  169. }
  170. this._idealHeight = value;
  171. this.markAsDirty();
  172. this._rootContainer._markAllAsDirty();
  173. }
  174. /**
  175. * Gets or sets a boolean indicating if the smallest ideal value must be used if idealWidth and idealHeight are both set
  176. * @see http://doc.babylonjs.com/how_to/gui#adaptive-scaling
  177. */
  178. public get useSmallestIdeal(): boolean {
  179. return this._useSmallestIdeal;
  180. }
  181. public set useSmallestIdeal(value: boolean) {
  182. if (this._useSmallestIdeal === value) {
  183. return;
  184. }
  185. this._useSmallestIdeal = value;
  186. this.markAsDirty();
  187. this._rootContainer._markAllAsDirty();
  188. }
  189. /**
  190. * Gets or sets a boolean indicating if adaptive scaling must be used
  191. * @see http://doc.babylonjs.com/how_to/gui#adaptive-scaling
  192. */
  193. public get renderAtIdealSize(): boolean {
  194. return this._renderAtIdealSize;
  195. }
  196. public set renderAtIdealSize(value: boolean) {
  197. if (this._renderAtIdealSize === value) {
  198. return;
  199. }
  200. this._renderAtIdealSize = value;
  201. this._onResize();
  202. }
  203. /**
  204. * Gets the underlying layer used to render the texture when in fullscreen mode
  205. */
  206. public get layer(): Nullable<Layer> {
  207. return this._layerToDispose;
  208. }
  209. /**
  210. * Gets the root container control
  211. */
  212. public get rootContainer(): Container {
  213. return this._rootContainer;
  214. }
  215. /**
  216. * Returns an array containing the root container.
  217. * This is mostly used to let the Inspector introspects the ADT
  218. * @returns an array containing the rootContainer
  219. */
  220. public getChildren(): Array<Container> {
  221. return [this._rootContainer];
  222. }
  223. /**
  224. * Will return all controls that are inside this texture
  225. * @param directDescendantsOnly defines if true only direct descendants of 'this' will be considered, if false direct and also indirect (children of children, an so on in a recursive manner) descendants of 'this' will be considered
  226. * @param predicate defines an optional predicate that will be called on every evaluated child, the predicate must return true for a given child to be part of the result, otherwise it will be ignored
  227. * @return all child controls
  228. */
  229. public getDescendants(directDescendantsOnly?: boolean, predicate?: (control: Control) => boolean): Control[] {
  230. return this._rootContainer.getDescendants(directDescendantsOnly, predicate);
  231. }
  232. /**
  233. * Gets or sets the current focused control
  234. */
  235. public get focusedControl(): Nullable<IFocusableControl> {
  236. return this._focusedControl;
  237. }
  238. public set focusedControl(control: Nullable<IFocusableControl>) {
  239. if (this._focusedControl == control) {
  240. return;
  241. }
  242. if (this._focusedControl) {
  243. this._focusedControl.onBlur();
  244. }
  245. if (control) {
  246. control.onFocus();
  247. }
  248. this._focusedControl = control;
  249. }
  250. /**
  251. * Gets or sets a boolean indicating if the texture must be rendered in background or foreground when in fullscreen mode
  252. */
  253. public get isForeground(): boolean {
  254. if (!this.layer) {
  255. return true;
  256. }
  257. return (!this.layer.isBackground);
  258. }
  259. public set isForeground(value: boolean) {
  260. if (!this.layer) {
  261. return;
  262. }
  263. if (this.layer.isBackground === !value) {
  264. return;
  265. }
  266. this.layer.isBackground = !value;
  267. }
  268. /**
  269. * Gets or set information about clipboardData
  270. */
  271. public get clipboardData(): string {
  272. return this._clipboardData;
  273. }
  274. public set clipboardData(value: string) {
  275. this._clipboardData = value;
  276. }
  277. /**
  278. * Creates a new AdvancedDynamicTexture
  279. * @param name defines the name of the texture
  280. * @param width defines the width of the texture
  281. * @param height defines the height of the texture
  282. * @param scene defines the hosting scene
  283. * @param generateMipMaps defines a boolean indicating if mipmaps must be generated (false by default)
  284. * @param samplingMode defines the texture sampling mode (Texture.NEAREST_SAMPLINGMODE by default)
  285. */
  286. constructor(name: string, width = 0, height = 0, scene: Nullable<Scene>, generateMipMaps = false, samplingMode = Texture.NEAREST_SAMPLINGMODE) {
  287. super(name, { width: width, height: height }, scene, generateMipMaps, samplingMode, Constants.TEXTUREFORMAT_RGBA);
  288. scene = this.getScene();
  289. if (!scene || !this._texture) {
  290. return;
  291. }
  292. this._rootElement = scene.getEngine()!.getInputElement()!;
  293. this._renderObserver = scene.onBeforeCameraRenderObservable.add((camera: Camera) => this._checkUpdate(camera));
  294. this._preKeyboardObserver = scene.onPreKeyboardObservable.add((info) => {
  295. if (!this._focusedControl) {
  296. return;
  297. }
  298. if (info.type === KeyboardEventTypes.KEYDOWN) {
  299. this._focusedControl.processKeyboard(info.event);
  300. }
  301. info.skipOnPointerObservable = true;
  302. });
  303. this._rootContainer._link(this);
  304. this.hasAlpha = true;
  305. if (!width || !height) {
  306. this._resizeObserver = scene.getEngine().onResizeObservable.add(() => this._onResize());
  307. this._onResize();
  308. }
  309. this._texture.isReady = true;
  310. }
  311. /**
  312. * Get the current class name of the texture useful for serialization or dynamic coding.
  313. * @returns "AdvancedDynamicTexture"
  314. */
  315. public getClassName(): string {
  316. return "AdvancedDynamicTexture";
  317. }
  318. /**
  319. * Function used to execute a function on all controls
  320. * @param func defines the function to execute
  321. * @param container defines the container where controls belong. If null the root container will be used
  322. */
  323. public executeOnAllControls(func: (control: Control) => void, container?: Container) {
  324. if (!container) {
  325. container = this._rootContainer;
  326. }
  327. func(container);
  328. for (var child of container.children) {
  329. if ((<any>child).children) {
  330. this.executeOnAllControls(func, (<Container>child));
  331. continue;
  332. }
  333. func(child);
  334. }
  335. }
  336. private _useInvalidateRectOptimization = true;
  337. /**
  338. * Gets or sets a boolean indicating if the InvalidateRect optimization should be turned on
  339. */
  340. public get useInvalidateRectOptimization(): boolean {
  341. return this._useInvalidateRectOptimization;
  342. }
  343. public set useInvalidateRectOptimization(value: boolean) {
  344. this._useInvalidateRectOptimization = value;
  345. }
  346. // Invalidated rectangle which is the combination of all invalidated controls after they have been rotated into absolute position
  347. private _invalidatedRectangle: Nullable<Measure> = null;
  348. /**
  349. * Invalidates a rectangle area on the gui texture
  350. * @param invalidMinX left most position of the rectangle to invalidate in the texture
  351. * @param invalidMinY top most position of the rectangle to invalidate in the texture
  352. * @param invalidMaxX right most position of the rectangle to invalidate in the texture
  353. * @param invalidMaxY bottom most position of the rectangle to invalidate in the texture
  354. */
  355. public invalidateRect(invalidMinX: number, invalidMinY: number, invalidMaxX: number, invalidMaxY: number) {
  356. if (!this._useInvalidateRectOptimization) {
  357. return;
  358. }
  359. if (!this._invalidatedRectangle) {
  360. this._invalidatedRectangle = new Measure(invalidMinX, invalidMinY, invalidMaxX - invalidMinX + 1, invalidMaxY - invalidMinY + 1);
  361. } else {
  362. // Compute intersection
  363. var maxX = Math.ceil(Math.max(this._invalidatedRectangle.left + this._invalidatedRectangle.width - 1, invalidMaxX));
  364. var maxY = Math.ceil(Math.max(this._invalidatedRectangle.top + this._invalidatedRectangle.height - 1, invalidMaxY));
  365. this._invalidatedRectangle.left = Math.floor(Math.min(this._invalidatedRectangle.left, invalidMinX));
  366. this._invalidatedRectangle.top = Math.floor(Math.min(this._invalidatedRectangle.top, invalidMinY));
  367. this._invalidatedRectangle.width = maxX - this._invalidatedRectangle.left + 1;
  368. this._invalidatedRectangle.height = maxY - this._invalidatedRectangle.top + 1;
  369. }
  370. }
  371. /**
  372. * Marks the texture as dirty forcing a complete update
  373. */
  374. public markAsDirty() {
  375. this._isDirty = true;
  376. }
  377. /**
  378. * Helper function used to create a new style
  379. * @returns a new style
  380. * @see http://doc.babylonjs.com/how_to/gui#styles
  381. */
  382. public createStyle(): Style {
  383. return new Style(this);
  384. }
  385. /**
  386. * Adds a new control to the root container
  387. * @param control defines the control to add
  388. * @returns the current texture
  389. */
  390. public addControl(control: Control): AdvancedDynamicTexture {
  391. this._rootContainer.addControl(control);
  392. return this;
  393. }
  394. /**
  395. * Removes a control from the root container
  396. * @param control defines the control to remove
  397. * @returns the current texture
  398. */
  399. public removeControl(control: Control): AdvancedDynamicTexture {
  400. this._rootContainer.removeControl(control);
  401. return this;
  402. }
  403. /**
  404. * Release all resources
  405. */
  406. public dispose(): void {
  407. let scene = this.getScene();
  408. if (!scene) {
  409. return;
  410. }
  411. this._rootElement = null;
  412. scene.onBeforeCameraRenderObservable.remove(this._renderObserver);
  413. if (this._resizeObserver) {
  414. scene.getEngine().onResizeObservable.remove(this._resizeObserver);
  415. }
  416. if (this._pointerMoveObserver) {
  417. scene.onPrePointerObservable.remove(this._pointerMoveObserver);
  418. }
  419. if (this._pointerObserver) {
  420. scene.onPointerObservable.remove(this._pointerObserver);
  421. }
  422. if (this._preKeyboardObserver) {
  423. scene.onPreKeyboardObservable.remove(this._preKeyboardObserver);
  424. }
  425. if (this._canvasPointerOutObserver) {
  426. scene.getEngine().onCanvasPointerOutObservable.remove(this._canvasPointerOutObserver);
  427. }
  428. if (this._layerToDispose) {
  429. this._layerToDispose.texture = null;
  430. this._layerToDispose.dispose();
  431. this._layerToDispose = null;
  432. }
  433. this._rootContainer.dispose();
  434. this.onClipboardObservable.clear();
  435. this.onControlPickedObservable.clear();
  436. this.onBeginRenderObservable.clear();
  437. this.onEndRenderObservable.clear();
  438. this.onBeginLayoutObservable.clear();
  439. this.onEndLayoutObservable.clear();
  440. super.dispose();
  441. }
  442. private _onResize(): void {
  443. let scene = this.getScene();
  444. if (!scene) {
  445. return;
  446. }
  447. // Check size
  448. var engine = scene.getEngine();
  449. var textureSize = this.getSize();
  450. var renderWidth = engine.getRenderWidth() * this._renderScale;
  451. var renderHeight = engine.getRenderHeight() * this._renderScale;
  452. if (this._renderAtIdealSize) {
  453. if (this._idealWidth) {
  454. renderHeight = (renderHeight * this._idealWidth) / renderWidth;
  455. renderWidth = this._idealWidth;
  456. } else if (this._idealHeight) {
  457. renderWidth = (renderWidth * this._idealHeight) / renderHeight;
  458. renderHeight = this._idealHeight;
  459. }
  460. }
  461. if (textureSize.width !== renderWidth || textureSize.height !== renderHeight) {
  462. this.scaleTo(renderWidth, renderHeight);
  463. this.markAsDirty();
  464. if (this._idealWidth || this._idealHeight) {
  465. this._rootContainer._markAllAsDirty();
  466. }
  467. }
  468. this.invalidateRect(0, 0, textureSize.width - 1, textureSize.height - 1);
  469. }
  470. /** @hidden */
  471. public _getGlobalViewport(scene: Scene): Viewport {
  472. var engine = scene.getEngine();
  473. return this._fullscreenViewport.toGlobal(engine.getRenderWidth(), engine.getRenderHeight());
  474. }
  475. /**
  476. * Get screen coordinates for a vector3
  477. * @param position defines the position to project
  478. * @param worldMatrix defines the world matrix to use
  479. * @returns the projected position
  480. */
  481. public getProjectedPosition(position: Vector3, worldMatrix: Matrix): Vector2 {
  482. var scene = this.getScene();
  483. if (!scene) {
  484. return Vector2.Zero();
  485. }
  486. var globalViewport = this._getGlobalViewport(scene);
  487. var projectedPosition = Vector3.Project(position, worldMatrix, scene.getTransformMatrix(), globalViewport);
  488. projectedPosition.scaleInPlace(this.renderScale);
  489. return new Vector2(projectedPosition.x, projectedPosition.y);
  490. }
  491. private _checkUpdate(camera: Camera): void {
  492. if (this._layerToDispose) {
  493. if ((camera.layerMask & this._layerToDispose.layerMask) === 0) {
  494. return;
  495. }
  496. }
  497. if (this._isFullscreen && this._linkedControls.length) {
  498. var scene = this.getScene();
  499. if (!scene) {
  500. return;
  501. }
  502. var globalViewport = this._getGlobalViewport(scene);
  503. for (var control of this._linkedControls) {
  504. if (!control.isVisible) {
  505. continue;
  506. }
  507. var mesh = control._linkedMesh;
  508. if (!mesh || mesh.isDisposed()) {
  509. Tools.SetImmediate(() => {
  510. control.linkWithMesh(null);
  511. });
  512. continue;
  513. }
  514. var position = mesh.getBoundingInfo ? mesh.getBoundingInfo().boundingSphere.center : (Vector3.ZeroReadOnly as Vector3);
  515. var projectedPosition = Vector3.Project(position, mesh.getWorldMatrix(), scene.getTransformMatrix(), globalViewport);
  516. if (projectedPosition.z < 0 || projectedPosition.z > 1) {
  517. control.notRenderable = true;
  518. continue;
  519. }
  520. control.notRenderable = false;
  521. // Account for RenderScale.
  522. projectedPosition.scaleInPlace(this.renderScale);
  523. control._moveToProjectedPosition(projectedPosition);
  524. }
  525. }
  526. if (!this._isDirty && !this._rootContainer.isDirty) {
  527. return;
  528. }
  529. this._isDirty = false;
  530. this._render();
  531. this.update(true, this.premulAlpha);
  532. }
  533. private _clearMeasure = new Measure(0, 0, 0, 0);
  534. private _render(): void {
  535. var textureSize = this.getSize();
  536. var renderWidth = textureSize.width;
  537. var renderHeight = textureSize.height;
  538. var context = this.getContext();
  539. context.font = "18px Arial";
  540. context.strokeStyle = "white";
  541. // Layout
  542. this.onBeginLayoutObservable.notifyObservers(this);
  543. var measure = new Measure(0, 0, renderWidth, renderHeight);
  544. Control.numLayoutCalls = 0;
  545. this._rootContainer._layout(measure, context);
  546. this.onEndLayoutObservable.notifyObservers(this);
  547. this._isDirty = false; // Restoring the dirty state that could have been set by controls during layout processing
  548. // Clear
  549. if (this._invalidatedRectangle) {
  550. this._clearMeasure.copyFrom(this._invalidatedRectangle);
  551. } else {
  552. this._clearMeasure.copyFromFloats(0, 0, renderWidth, renderHeight);
  553. }
  554. context.clearRect(this._clearMeasure.left, this._clearMeasure.top, this._clearMeasure.width, this._clearMeasure.height);
  555. if (this._background) {
  556. context.save();
  557. context.fillStyle = this._background;
  558. context.fillRect(this._clearMeasure.left, this._clearMeasure.top, this._clearMeasure.width, this._clearMeasure.height);
  559. context.restore();
  560. }
  561. // Render
  562. this.onBeginRenderObservable.notifyObservers(this);
  563. Control.numRenderCalls = 0;
  564. this._rootContainer._render(context, this._invalidatedRectangle);
  565. this.onEndRenderObservable.notifyObservers(this);
  566. this._invalidatedRectangle = null;
  567. }
  568. /** @hidden */
  569. public _changeCursor(cursor: string) {
  570. if (this._rootElement) {
  571. this._rootElement.style.cursor = cursor;
  572. this._cursorChanged = true;
  573. }
  574. }
  575. /** @hidden */
  576. public _registerLastControlDown(control: Control, pointerId: number) {
  577. this._lastControlDown[pointerId] = control;
  578. this.onControlPickedObservable.notifyObservers(control);
  579. }
  580. private _doPicking(x: number, y: number, type: number, pointerId: number, buttonIndex: number): void {
  581. var scene = this.getScene();
  582. if (!scene) {
  583. return;
  584. }
  585. var engine = scene.getEngine();
  586. var textureSize = this.getSize();
  587. if (this._isFullscreen) {
  588. let camera = scene.cameraToUseForPointers || scene.activeCamera;
  589. let viewport = camera!.viewport;
  590. x = x * (textureSize.width / (engine.getRenderWidth() * viewport.width));
  591. y = y * (textureSize.height / (engine.getRenderHeight() * viewport.height));
  592. }
  593. if (this._capturingControl[pointerId]) {
  594. this._capturingControl[pointerId]._processObservables(type, x, y, pointerId, buttonIndex);
  595. return;
  596. }
  597. this._cursorChanged = false;
  598. if (!this._rootContainer._processPicking(x, y, type, pointerId, buttonIndex)) {
  599. this._changeCursor("");
  600. if (type === PointerEventTypes.POINTERMOVE) {
  601. if (this._lastControlOver[pointerId]) {
  602. this._lastControlOver[pointerId]._onPointerOut(this._lastControlOver[pointerId]);
  603. delete this._lastControlOver[pointerId];
  604. }
  605. }
  606. }
  607. if (!this._cursorChanged) {
  608. this._changeCursor("");
  609. }
  610. this._manageFocus();
  611. }
  612. /** @hidden */
  613. public _cleanControlAfterRemovalFromList(list: { [pointerId: number]: Control }, control: Control) {
  614. for (var pointerId in list) {
  615. if (!list.hasOwnProperty(pointerId)) {
  616. continue;
  617. }
  618. var lastControlOver = list[pointerId];
  619. if (lastControlOver === control) {
  620. delete list[pointerId];
  621. }
  622. }
  623. }
  624. /** @hidden */
  625. public _cleanControlAfterRemoval(control: Control) {
  626. this._cleanControlAfterRemovalFromList(this._lastControlDown, control);
  627. this._cleanControlAfterRemovalFromList(this._lastControlOver, control);
  628. }
  629. /** Attach to all scene events required to support pointer events */
  630. public attach(): void {
  631. var scene = this.getScene();
  632. if (!scene) {
  633. return;
  634. }
  635. let tempViewport = new Viewport(0, 0, 0, 0);
  636. this._pointerMoveObserver = scene.onPrePointerObservable.add((pi, state) => {
  637. if (scene!.isPointerCaptured((<PointerEvent>(pi.event)).pointerId)) {
  638. return;
  639. }
  640. if (pi.type !== PointerEventTypes.POINTERMOVE
  641. && pi.type !== PointerEventTypes.POINTERUP
  642. && pi.type !== PointerEventTypes.POINTERDOWN) {
  643. return;
  644. }
  645. if (!scene) {
  646. return;
  647. }
  648. let camera = scene.cameraToUseForPointers || scene.activeCamera;
  649. let engine = scene.getEngine();
  650. if (!camera) {
  651. tempViewport.x = 0;
  652. tempViewport.y = 0;
  653. tempViewport.width = engine.getRenderWidth();
  654. tempViewport.height = engine.getRenderHeight();
  655. } else {
  656. camera.viewport.toGlobalToRef(engine.getRenderWidth(), engine.getRenderHeight(), tempViewport);
  657. }
  658. let x = scene.pointerX / engine.getHardwareScalingLevel() - tempViewport.x;
  659. let y = scene.pointerY / engine.getHardwareScalingLevel() - (engine.getRenderHeight() - tempViewport.y - tempViewport.height);
  660. this._shouldBlockPointer = false;
  661. // Do picking modifies _shouldBlockPointer
  662. this._doPicking(x, y, pi.type, (pi.event as PointerEvent).pointerId || 0, pi.event.button);
  663. // Avoid overwriting a true skipOnPointerObservable to false
  664. if (this._shouldBlockPointer) {
  665. pi.skipOnPointerObservable = this._shouldBlockPointer;
  666. }
  667. });
  668. this._attachToOnPointerOut(scene);
  669. }
  670. /** @hidden */
  671. private onClipboardCopy = (rawEvt: Event) => {
  672. const evt = rawEvt as ClipboardEvent;
  673. let ev = new ClipboardInfo(ClipboardEventTypes.COPY, evt);
  674. this.onClipboardObservable.notifyObservers(ev);
  675. evt.preventDefault();
  676. }
  677. /** @hidden */
  678. private onClipboardCut = (rawEvt: Event) => {
  679. const evt = rawEvt as ClipboardEvent;
  680. let ev = new ClipboardInfo(ClipboardEventTypes.CUT, evt);
  681. this.onClipboardObservable.notifyObservers(ev);
  682. evt.preventDefault();
  683. }
  684. /** @hidden */
  685. private onClipboardPaste = (rawEvt: Event) => {
  686. const evt = rawEvt as ClipboardEvent;
  687. let ev = new ClipboardInfo(ClipboardEventTypes.PASTE, evt);
  688. this.onClipboardObservable.notifyObservers(ev);
  689. evt.preventDefault();
  690. }
  691. /**
  692. * Register the clipboard Events onto the canvas
  693. */
  694. public registerClipboardEvents(): void {
  695. self.addEventListener("copy", this.onClipboardCopy, false);
  696. self.addEventListener("cut", this.onClipboardCut, false);
  697. self.addEventListener("paste", this.onClipboardPaste, false);
  698. }
  699. /**
  700. * Unregister the clipboard Events from the canvas
  701. */
  702. public unRegisterClipboardEvents(): void {
  703. self.removeEventListener("copy", this.onClipboardCopy);
  704. self.removeEventListener("cut", this.onClipboardCut);
  705. self.removeEventListener("paste", this.onClipboardPaste);
  706. }
  707. /**
  708. * Connect the texture to a hosting mesh to enable interactions
  709. * @param mesh defines the mesh to attach to
  710. * @param supportPointerMove defines a boolean indicating if pointer move events must be catched as well
  711. */
  712. public attachToMesh(mesh: AbstractMesh, supportPointerMove = true): void {
  713. var scene = this.getScene();
  714. if (!scene) {
  715. return;
  716. }
  717. this._pointerObserver = scene.onPointerObservable.add((pi, state) => {
  718. if (pi.type !== PointerEventTypes.POINTERMOVE
  719. && pi.type !== PointerEventTypes.POINTERUP
  720. && pi.type !== PointerEventTypes.POINTERDOWN) {
  721. return;
  722. }
  723. var pointerId = (pi.event as PointerEvent).pointerId || 0;
  724. if (pi.pickInfo && pi.pickInfo.hit && pi.pickInfo.pickedMesh === mesh) {
  725. var uv = pi.pickInfo.getTextureCoordinates();
  726. if (uv) {
  727. let size = this.getSize();
  728. this._doPicking(uv.x * size.width, (1.0 - uv.y) * size.height, pi.type, pointerId, pi.event.button);
  729. }
  730. } else if (pi.type === PointerEventTypes.POINTERUP) {
  731. if (this._lastControlDown[pointerId]) {
  732. this._lastControlDown[pointerId]._forcePointerUp(pointerId);
  733. }
  734. delete this._lastControlDown[pointerId];
  735. if (this.focusedControl) {
  736. const friendlyControls = this.focusedControl.keepsFocusWith();
  737. let canMoveFocus = true;
  738. if (friendlyControls) {
  739. for (var control of friendlyControls) {
  740. // Same host, no need to keep the focus
  741. if (this === control._host) {
  742. continue;
  743. }
  744. // Different hosts
  745. const otherHost = control._host;
  746. if (otherHost._lastControlOver[pointerId] && otherHost._lastControlOver[pointerId].isAscendant(control)) {
  747. canMoveFocus = false;
  748. break;
  749. }
  750. }
  751. }
  752. if (canMoveFocus) {
  753. this.focusedControl = null;
  754. }
  755. }
  756. } else if (pi.type === PointerEventTypes.POINTERMOVE) {
  757. if (this._lastControlOver[pointerId]) {
  758. this._lastControlOver[pointerId]._onPointerOut(this._lastControlOver[pointerId], true);
  759. }
  760. delete this._lastControlOver[pointerId];
  761. }
  762. });
  763. mesh.enablePointerMoveEvents = supportPointerMove;
  764. this._attachToOnPointerOut(scene);
  765. }
  766. /**
  767. * Move the focus to a specific control
  768. * @param control defines the control which will receive the focus
  769. */
  770. public moveFocusToControl(control: IFocusableControl): void {
  771. this.focusedControl = control;
  772. this._lastPickedControl = <any>control;
  773. this._blockNextFocusCheck = true;
  774. }
  775. private _manageFocus(): void {
  776. if (this._blockNextFocusCheck) {
  777. this._blockNextFocusCheck = false;
  778. this._lastPickedControl = <any>this._focusedControl;
  779. return;
  780. }
  781. // Focus management
  782. if (this._focusedControl) {
  783. if (this._focusedControl !== (<any>this._lastPickedControl)) {
  784. if (this._lastPickedControl.isFocusInvisible) {
  785. return;
  786. }
  787. this.focusedControl = null;
  788. }
  789. }
  790. }
  791. private _attachToOnPointerOut(scene: Scene): void {
  792. this._canvasPointerOutObserver = scene.getEngine().onCanvasPointerOutObservable.add((pointerEvent) => {
  793. if (this._lastControlOver[pointerEvent.pointerId]) {
  794. this._lastControlOver[pointerEvent.pointerId]._onPointerOut(this._lastControlOver[pointerEvent.pointerId]);
  795. }
  796. delete this._lastControlOver[pointerEvent.pointerId];
  797. if (this._lastControlDown[pointerEvent.pointerId] && this._lastControlDown[pointerEvent.pointerId] !== this._capturingControl[pointerEvent.pointerId]) {
  798. this._lastControlDown[pointerEvent.pointerId]._forcePointerUp();
  799. delete this._lastControlDown[pointerEvent.pointerId];
  800. }
  801. });
  802. }
  803. // Statics
  804. /**
  805. * Creates a new AdvancedDynamicTexture in projected mode (ie. attached to a mesh)
  806. * @param mesh defines the mesh which will receive the texture
  807. * @param width defines the texture width (1024 by default)
  808. * @param height defines the texture height (1024 by default)
  809. * @param supportPointerMove defines a boolean indicating if the texture must capture move events (true by default)
  810. * @param onlyAlphaTesting defines a boolean indicating that alpha blending will not be used (only alpha testing) (false by default)
  811. * @returns a new AdvancedDynamicTexture
  812. */
  813. public static CreateForMesh(mesh: AbstractMesh, width = 1024, height = 1024, supportPointerMove = true, onlyAlphaTesting = false): AdvancedDynamicTexture {
  814. var result = new AdvancedDynamicTexture(mesh.name + " AdvancedDynamicTexture", width, height, mesh.getScene(), true, Texture.TRILINEAR_SAMPLINGMODE);
  815. var material = new StandardMaterial("AdvancedDynamicTextureMaterial", mesh.getScene());
  816. material.backFaceCulling = false;
  817. material.diffuseColor = Color3.Black();
  818. material.specularColor = Color3.Black();
  819. if (onlyAlphaTesting) {
  820. material.diffuseTexture = result;
  821. material.emissiveTexture = result;
  822. result.hasAlpha = true;
  823. } else {
  824. material.emissiveTexture = result;
  825. material.opacityTexture = result;
  826. }
  827. mesh.material = material;
  828. result.attachToMesh(mesh, supportPointerMove);
  829. return result;
  830. }
  831. /**
  832. * Creates a new AdvancedDynamicTexture in fullscreen mode.
  833. * In this mode the texture will rely on a layer for its rendering.
  834. * This allows it to be treated like any other layer.
  835. * As such, if you have a multi camera setup, you can set the layerMask on the GUI as well.
  836. * LayerMask is set through advancedTexture.layer.layerMask
  837. * @param name defines name for the texture
  838. * @param foreground defines a boolean indicating if the texture must be rendered in foreground (default is true)
  839. * @param scene defines the hsoting scene
  840. * @param sampling defines the texture sampling mode (Texture.BILINEAR_SAMPLINGMODE by default)
  841. * @returns a new AdvancedDynamicTexture
  842. */
  843. public static CreateFullscreenUI(name: string, foreground: boolean = true, scene: Nullable<Scene> = null, sampling = Texture.BILINEAR_SAMPLINGMODE): AdvancedDynamicTexture {
  844. var result = new AdvancedDynamicTexture(name, 0, 0, scene, false, sampling);
  845. // Display
  846. var layer = new Layer(name + "_layer", null, scene, !foreground);
  847. layer.texture = result;
  848. result._layerToDispose = layer;
  849. result._isFullscreen = true;
  850. // Attach
  851. result.attach();
  852. return result;
  853. }
  854. }