advancedDynamicTexture.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. /// <reference path="../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON.GUI {
  3. export interface IFocusableControl {
  4. onFocus(): void;
  5. onBlur(): void;
  6. processKeyboard(evt: KeyboardEvent): void;
  7. }
  8. export class AdvancedDynamicTexture extends DynamicTexture {
  9. private _isDirty = false;
  10. private _renderObserver: Nullable<Observer<Camera>>;
  11. private _resizeObserver: Nullable<Observer<Engine>>;
  12. private _preKeyboardObserver: Nullable<Observer<KeyboardInfoPre>>;
  13. private _pointerMoveObserver: Nullable<Observer<PointerInfoPre>>;
  14. private _pointerObserver: Nullable<Observer<PointerInfo>>;
  15. private _canvasPointerOutObserver: Nullable<Observer<Engine>>;
  16. private _background: string;
  17. public _rootContainer = new Container("root");
  18. public _lastPickedControl: Control;
  19. public _lastControlOver: Nullable<Control>;
  20. public _lastControlDown: Nullable<Control>;
  21. public _capturingControl: Nullable<Control>;
  22. public _shouldBlockPointer: boolean;
  23. public _layerToDispose: Nullable<Layer>;
  24. public _linkedControls = new Array<Control>();
  25. private _isFullscreen = false;
  26. private _fullscreenViewport = new Viewport(0, 0, 1, 1);
  27. private _idealWidth = 0;
  28. private _idealHeight = 0;
  29. private _renderAtIdealSize = false;
  30. private _focusedControl: Nullable<IFocusableControl>;
  31. private _blockNextFocusCheck = false;
  32. public get background(): string {
  33. return this._background;
  34. }
  35. public set background(value: string) {
  36. if (this._background === value) {
  37. return;
  38. }
  39. this._background = value;
  40. this.markAsDirty();
  41. }
  42. public get idealWidth(): number {
  43. return this._idealWidth;
  44. }
  45. public set idealWidth(value: number) {
  46. if (this._idealWidth === value) {
  47. return;
  48. }
  49. this._idealWidth = value;
  50. this.markAsDirty();
  51. this._rootContainer._markAllAsDirty();
  52. }
  53. public get idealHeight(): number {
  54. return this._idealHeight;
  55. }
  56. public set idealHeight(value: number) {
  57. if (this._idealHeight === value) {
  58. return;
  59. }
  60. this._idealHeight = value;
  61. this.markAsDirty();
  62. this._rootContainer._markAllAsDirty();
  63. }
  64. public get renderAtIdealSize(): boolean {
  65. return this._renderAtIdealSize;
  66. }
  67. public set renderAtIdealSize(value: boolean) {
  68. if (this._renderAtIdealSize === value) {
  69. return;
  70. }
  71. this._renderAtIdealSize = value;
  72. this._onResize();
  73. }
  74. public get layer(): Nullable<Layer> {
  75. return this._layerToDispose;
  76. }
  77. public get rootContainer(): Container {
  78. return this._rootContainer;
  79. }
  80. public get focusedControl(): Nullable<IFocusableControl> {
  81. return this._focusedControl;
  82. }
  83. public set focusedControl(control: Nullable<IFocusableControl>) {
  84. if (this._focusedControl == control) {
  85. return;
  86. }
  87. if (this._focusedControl) {
  88. this._focusedControl.onBlur();
  89. }
  90. if (control) {
  91. control.onFocus();
  92. }
  93. this._focusedControl = control;
  94. }
  95. public get isForeground(): boolean {
  96. if (!this.layer) {
  97. return true;
  98. }
  99. return (!this.layer.isBackground);
  100. }
  101. public set isForeground(value: boolean) {
  102. if (!this.layer) {
  103. return;
  104. }
  105. if (this.layer.isBackground === !value) {
  106. return;
  107. }
  108. this.layer.isBackground = !value;
  109. }
  110. constructor(name: string, width = 0, height = 0, scene: Nullable<Scene>, generateMipMaps = false, samplingMode = Texture.NEAREST_SAMPLINGMODE) {
  111. super(name, {width: width, height: height}, scene, generateMipMaps, samplingMode, Engine.TEXTUREFORMAT_RGBA);
  112. scene = this.getScene();
  113. if (!scene || !this._texture) {
  114. return;
  115. }
  116. this._renderObserver = scene.onBeforeCameraRenderObservable.add((camera: Camera) => this._checkUpdate(camera));
  117. this._preKeyboardObserver = scene.onPreKeyboardObservable.add(info => {
  118. if (!this._focusedControl) {
  119. return;
  120. }
  121. if (info.type === KeyboardEventTypes.KEYDOWN) {
  122. this._focusedControl.processKeyboard(info.event);
  123. }
  124. info.skipOnPointerObservable = true;
  125. });
  126. this._rootContainer._link(null, this);
  127. this.hasAlpha = true;
  128. if (!width || !height) {
  129. this._resizeObserver = scene.getEngine().onResizeObservable.add(() => this._onResize());
  130. this._onResize();
  131. }
  132. this._texture.isReady = true;
  133. }
  134. public executeOnAllControls(func: (control: Control) => void, container?: Container) {
  135. if (!container) {
  136. container = this._rootContainer;
  137. }
  138. for (var child of container.children) {
  139. if ((<any>child).children) {
  140. this.executeOnAllControls(func, (<Container>child));
  141. continue;
  142. }
  143. func(child);
  144. }
  145. }
  146. public markAsDirty() {
  147. this._isDirty = true;
  148. }
  149. public addControl(control: Control): AdvancedDynamicTexture {
  150. this._rootContainer.addControl(control);
  151. return this;
  152. }
  153. public removeControl(control: Control): AdvancedDynamicTexture {
  154. this._rootContainer.removeControl(control);
  155. return this;
  156. }
  157. public dispose(): void {
  158. let scene = this.getScene();
  159. if (!scene) {
  160. return;
  161. }
  162. scene.onBeforeCameraRenderObservable.remove(this._renderObserver);
  163. if (this._resizeObserver) {
  164. scene.getEngine().onResizeObservable.remove(this._resizeObserver);
  165. }
  166. if (this._pointerMoveObserver) {
  167. scene.onPrePointerObservable.remove(this._pointerMoveObserver);
  168. }
  169. if (this._pointerObserver) {
  170. scene.onPointerObservable.remove(this._pointerObserver);
  171. }
  172. if (this._preKeyboardObserver) {
  173. scene.onPreKeyboardObservable.remove(this._preKeyboardObserver);
  174. }
  175. if (this._canvasPointerOutObserver) {
  176. scene.getEngine().onCanvasPointerOutObservable.remove(this._canvasPointerOutObserver);
  177. }
  178. if (this._layerToDispose) {
  179. this._layerToDispose.texture = null;
  180. this._layerToDispose.dispose();
  181. this._layerToDispose = null;
  182. }
  183. this._rootContainer.dispose();
  184. super.dispose();
  185. }
  186. private _onResize(): void {
  187. let scene = this.getScene();
  188. if (!scene) {
  189. return;
  190. }
  191. // Check size
  192. var engine = scene.getEngine();
  193. var textureSize = this.getSize();
  194. var renderWidth = engine.getRenderWidth();
  195. var renderHeight = engine.getRenderHeight();
  196. if (this._renderAtIdealSize) {
  197. if (this._idealWidth) {
  198. renderHeight = (renderHeight * this._idealWidth) / renderWidth;
  199. renderWidth = this._idealWidth;
  200. } else if (this._idealHeight) {
  201. renderWidth = (renderWidth * this._idealHeight) / renderHeight;
  202. renderHeight = this._idealHeight;
  203. }
  204. }
  205. if (textureSize.width !== renderWidth || textureSize.height !== renderHeight) {
  206. this.scaleTo(renderWidth, renderHeight);
  207. this.markAsDirty();
  208. if (this._idealWidth || this._idealHeight) {
  209. this._rootContainer._markAllAsDirty();
  210. }
  211. }
  212. }
  213. public _getGlobalViewport(scene: Scene): Viewport {
  214. var engine = scene.getEngine();
  215. return this._fullscreenViewport.toGlobal(engine.getRenderWidth(), engine.getRenderHeight());
  216. }
  217. private _checkUpdate(camera: Camera): void {
  218. if (this._layerToDispose) {
  219. if ((camera.layerMask & this._layerToDispose.layerMask) === 0) {
  220. return;
  221. }
  222. }
  223. if (this._isFullscreen && this._linkedControls.length) {
  224. var scene = this.getScene();
  225. if (!scene) {
  226. return;
  227. }
  228. var globalViewport = this._getGlobalViewport(scene);
  229. for (var control of this._linkedControls) {
  230. if (!control.isVisible) {
  231. continue;
  232. }
  233. var mesh = control._linkedMesh;
  234. if (!mesh || mesh.isDisposed()) {
  235. Tools.SetImmediate(()=>{
  236. control.linkWithMesh(null);
  237. });
  238. continue;
  239. }
  240. var position = mesh.getBoundingInfo().boundingSphere.center;
  241. var projectedPosition = Vector3.Project(position, mesh.getWorldMatrix(), scene.getTransformMatrix(), globalViewport);
  242. if (projectedPosition.z < 0 || projectedPosition.z > 1) {
  243. control.notRenderable = true;
  244. continue;
  245. }
  246. control.notRenderable = false;
  247. control._moveToProjectedPosition(projectedPosition);
  248. }
  249. }
  250. if (!this._isDirty && !this._rootContainer.isDirty) {
  251. return;
  252. }
  253. this._isDirty = false;
  254. this._render();
  255. this.update();
  256. }
  257. private _render(): void {
  258. var textureSize = this.getSize();
  259. var renderWidth = textureSize.width;
  260. var renderHeight = textureSize.height;
  261. // Clear
  262. var context = this.getContext();
  263. context.clearRect(0, 0, renderWidth, renderHeight);
  264. if (this._background) {
  265. context.save();
  266. context.fillStyle = this._background;
  267. context.fillRect(0, 0, renderWidth, renderHeight);
  268. context.restore();
  269. }
  270. // Render
  271. context.font = "18px Arial";
  272. context.strokeStyle = "white";
  273. var measure = new Measure(0, 0, renderWidth, renderHeight);
  274. this._rootContainer._draw(measure, context);
  275. }
  276. private _doPicking(x: number, y: number, type: number, buttonIndex: number): void {
  277. var scene = this.getScene();
  278. if (!scene) {
  279. return;
  280. }
  281. var engine = scene.getEngine();
  282. var textureSize = this.getSize();
  283. if (this._isFullscreen) {
  284. x = x * (textureSize.width / engine.getRenderWidth());
  285. y = y * (textureSize.height / engine.getRenderHeight());
  286. }
  287. if (this._capturingControl) {
  288. this._capturingControl._processObservables(type, x, y, buttonIndex);
  289. return;
  290. }
  291. if (!this._rootContainer._processPicking(x, y, type, buttonIndex)) {
  292. if (type === BABYLON.PointerEventTypes.POINTERMOVE) {
  293. if (this._lastControlOver) {
  294. this._lastControlOver._onPointerOut(this._lastControlOver);
  295. }
  296. this._lastControlOver = null;
  297. }
  298. }
  299. this._manageFocus();
  300. }
  301. public attach(): void {
  302. var scene = this.getScene();
  303. if (!scene) {
  304. return;
  305. }
  306. this._pointerMoveObserver = scene.onPrePointerObservable.add((pi, state) => {
  307. if (pi.type !== BABYLON.PointerEventTypes.POINTERMOVE
  308. && pi.type !== BABYLON.PointerEventTypes.POINTERUP
  309. && pi.type !== BABYLON.PointerEventTypes.POINTERDOWN) {
  310. return;
  311. }
  312. if (!scene) {
  313. return;
  314. }
  315. let camera = scene.cameraToUseForPointers || scene.activeCamera;
  316. if (!camera) {
  317. return;
  318. }
  319. let engine = scene.getEngine();
  320. let viewport = camera.viewport;
  321. let x = (scene.pointerX / engine.getHardwareScalingLevel() - viewport.x * engine.getRenderWidth()) / viewport.width;
  322. let y = (scene.pointerY / engine.getHardwareScalingLevel() - viewport.y * engine.getRenderHeight()) / viewport.height;
  323. this._shouldBlockPointer = false;
  324. this._doPicking(x, y, pi.type, pi.event.button);
  325. pi.skipOnPointerObservable = this._shouldBlockPointer;
  326. });
  327. this._attachToOnPointerOut(scene);
  328. }
  329. public attachToMesh(mesh: AbstractMesh, supportPointerMove = true): void {
  330. var scene = this.getScene();
  331. if (!scene) {
  332. return;
  333. }
  334. this._pointerObserver = scene.onPointerObservable.add((pi, state) => {
  335. if (pi.type !== BABYLON.PointerEventTypes.POINTERMOVE
  336. && pi.type !== BABYLON.PointerEventTypes.POINTERUP
  337. && pi.type !== BABYLON.PointerEventTypes.POINTERDOWN) {
  338. return;
  339. }
  340. if (pi.pickInfo && pi.pickInfo.hit && pi.pickInfo.pickedMesh === mesh) {
  341. var uv = pi.pickInfo.getTextureCoordinates();
  342. if (uv) {
  343. let size = this.getSize();
  344. this._doPicking(uv.x * size.width, (1.0 - uv.y) * size.height, pi.type, pi.event.button);
  345. }
  346. } else if (pi.type === BABYLON.PointerEventTypes.POINTERUP) {
  347. if (this._lastControlDown) {
  348. this._lastControlDown.forcePointerUp();
  349. }
  350. this._lastControlDown = null;
  351. this.focusedControl = null;
  352. } else if (pi.type === BABYLON.PointerEventTypes.POINTERMOVE) {
  353. if (this._lastControlOver) {
  354. this._lastControlOver._onPointerOut(this._lastControlOver);
  355. }
  356. this._lastControlOver = null;
  357. }
  358. });
  359. mesh.enablePointerMoveEvents = supportPointerMove;
  360. this._attachToOnPointerOut(scene);
  361. }
  362. public moveFocusToControl(control: IFocusableControl): void {
  363. this.focusedControl = control;
  364. this._lastPickedControl = <any>control;
  365. this._blockNextFocusCheck = true;
  366. }
  367. private _manageFocus(): void {
  368. if (this._blockNextFocusCheck) {
  369. this._blockNextFocusCheck = false;
  370. this._lastPickedControl = <any>this._focusedControl;
  371. return;
  372. }
  373. // Focus management
  374. if (this._focusedControl) {
  375. if (this._focusedControl !== (<any>this._lastPickedControl)) {
  376. if (this._lastPickedControl.isFocusInvisible) {
  377. return;
  378. }
  379. this.focusedControl = null;
  380. }
  381. }
  382. }
  383. private _attachToOnPointerOut(scene: Scene): void {
  384. this._canvasPointerOutObserver = scene.getEngine().onCanvasPointerOutObservable.add(() => {
  385. if (this._lastControlOver) {
  386. this._lastControlOver._onPointerOut(this._lastControlOver);
  387. }
  388. this._lastControlOver = null;
  389. if (this._lastControlDown) {
  390. this._lastControlDown.forcePointerUp();
  391. }
  392. this._lastControlDown = null;
  393. });
  394. }
  395. // Statics
  396. public static CreateForMesh(mesh: AbstractMesh, width = 1024, height = 1024, supportPointerMove = true): AdvancedDynamicTexture {
  397. var result = new AdvancedDynamicTexture(mesh.name + " AdvancedDynamicTexture", width, height, mesh.getScene(), true, Texture.TRILINEAR_SAMPLINGMODE);
  398. var material = new BABYLON.StandardMaterial("AdvancedDynamicTextureMaterial", mesh.getScene());
  399. material.backFaceCulling = false;
  400. material.diffuseColor = BABYLON.Color3.Black();
  401. material.specularColor = BABYLON.Color3.Black();
  402. material.emissiveTexture = result;
  403. material.opacityTexture = result;
  404. mesh.material = material;
  405. result.attachToMesh(mesh, supportPointerMove);
  406. return result;
  407. }
  408. public static CreateFullscreenUI(name: string, foreground: boolean = true, scene: Nullable<Scene> = null): AdvancedDynamicTexture {
  409. var result = new AdvancedDynamicTexture(name, 0, 0, scene, false, Texture.BILINEAR_SAMPLINGMODE);
  410. // Display
  411. var layer = new BABYLON.Layer(name + "_layer", null, scene, !foreground);
  412. layer.texture = result;
  413. result._layerToDispose = layer;
  414. result._isFullscreen = true;
  415. // Attach
  416. result.attach();
  417. return result;
  418. }
  419. }
  420. }