advancedDynamicTexture.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. return (!this.layer.isBackground);
  97. }
  98. public set isForeground(value: boolean) {
  99. if (this.layer.isBackground === !value) {
  100. return;
  101. }
  102. this.layer.isBackground = !value;
  103. }
  104. constructor(name: string, width = 0, height = 0, scene: Nullable<Scene>, generateMipMaps = false, samplingMode = Texture.NEAREST_SAMPLINGMODE) {
  105. super(name, {width: width, height: height}, scene, generateMipMaps, samplingMode, Engine.TEXTUREFORMAT_RGBA);
  106. scene = this.getScene();
  107. if (!scene || !this._texture) {
  108. return;
  109. }
  110. this._renderObserver = scene.onBeforeCameraRenderObservable.add((camera: Camera) => this._checkUpdate(camera));
  111. this._preKeyboardObserver = scene.onPreKeyboardObservable.add(info => {
  112. if (!this._focusedControl) {
  113. return;
  114. }
  115. if (info.type === KeyboardEventTypes.KEYDOWN) {
  116. this._focusedControl.processKeyboard(info.event);
  117. }
  118. info.skipOnPointerObservable = true;
  119. });
  120. this._rootContainer._link(null, this);
  121. this.hasAlpha = true;
  122. if (!width || !height) {
  123. this._resizeObserver = scene.getEngine().onResizeObservable.add(() => this._onResize());
  124. this._onResize();
  125. }
  126. this._texture.isReady = true;
  127. }
  128. public executeOnAllControls(func: (control: Control) => void, container?: Container) {
  129. if (!container) {
  130. container = this._rootContainer;
  131. }
  132. for (var child of container.children) {
  133. if ((<any>child).children) {
  134. this.executeOnAllControls(func, (<Container>child));
  135. continue;
  136. }
  137. func(child);
  138. }
  139. }
  140. public markAsDirty() {
  141. this._isDirty = true;
  142. }
  143. public addControl(control: Control): AdvancedDynamicTexture {
  144. this._rootContainer.addControl(control);
  145. return this;
  146. }
  147. public removeControl(control: Control): AdvancedDynamicTexture {
  148. this._rootContainer.removeControl(control);
  149. return this;
  150. }
  151. public dispose(): void {
  152. let scene = this.getScene();
  153. if (!scene) {
  154. return;
  155. }
  156. scene.onBeforeCameraRenderObservable.remove(this._renderObserver);
  157. if (this._resizeObserver) {
  158. scene.getEngine().onResizeObservable.remove(this._resizeObserver);
  159. }
  160. if (this._pointerMoveObserver) {
  161. scene.onPrePointerObservable.remove(this._pointerMoveObserver);
  162. }
  163. if (this._pointerObserver) {
  164. scene.onPointerObservable.remove(this._pointerObserver);
  165. }
  166. if (this._canvasPointerOutObserver) {
  167. scene.getEngine().onCanvasPointerOutObservable.remove(this._canvasPointerOutObserver);
  168. }
  169. if (this._layerToDispose) {
  170. this._layerToDispose.texture = null;
  171. this._layerToDispose.dispose();
  172. this._layerToDispose = null;
  173. }
  174. this._rootContainer.dispose();
  175. super.dispose();
  176. }
  177. private _onResize(): void {
  178. let scene = this.getScene();
  179. if (!scene) {
  180. return;
  181. }
  182. // Check size
  183. var engine = scene.getEngine();
  184. var textureSize = this.getSize();
  185. var renderWidth = engine.getRenderWidth();
  186. var renderHeight = engine.getRenderHeight();
  187. if (this._renderAtIdealSize) {
  188. if (this._idealWidth) {
  189. renderHeight = (renderHeight * this._idealWidth) / renderWidth;
  190. renderWidth = this._idealWidth;
  191. } else if (this._idealHeight) {
  192. renderWidth = (renderWidth * this._idealHeight) / renderHeight;
  193. renderHeight = this._idealHeight;
  194. }
  195. }
  196. if (textureSize.width !== renderWidth || textureSize.height !== renderHeight) {
  197. this.scaleTo(renderWidth, renderHeight);
  198. this.markAsDirty();
  199. if (this._idealWidth || this._idealHeight) {
  200. this._rootContainer._markAllAsDirty();
  201. }
  202. }
  203. }
  204. public _getGlobalViewport(scene: Scene): Viewport {
  205. var engine = scene.getEngine();
  206. return this._fullscreenViewport.toGlobal(engine.getRenderWidth(), engine.getRenderHeight());
  207. }
  208. private _checkUpdate(camera: Camera): void {
  209. if (this._layerToDispose) {
  210. if ((camera.layerMask & this._layerToDispose.layerMask) === 0) {
  211. return;
  212. }
  213. }
  214. if (this._isFullscreen && this._linkedControls.length) {
  215. var scene = this.getScene();
  216. if (!scene) {
  217. return;
  218. }
  219. var globalViewport = this._getGlobalViewport(scene);
  220. for (var control of this._linkedControls) {
  221. if (!control.isVisible) {
  222. continue;
  223. }
  224. var mesh = control._linkedMesh;
  225. if (!mesh || mesh.isDisposed()) {
  226. Tools.SetImmediate(()=>{
  227. control.linkWithMesh(null);
  228. });
  229. continue;
  230. }
  231. var position = (<BoundingInfo>mesh.getBoundingInfo()).boundingSphere.center;
  232. var projectedPosition = Vector3.Project(position, mesh.getWorldMatrix(), scene.getTransformMatrix(), globalViewport);
  233. if (projectedPosition.z < 0 || projectedPosition.z > 1) {
  234. control.notRenderable = true;
  235. continue;
  236. }
  237. control.notRenderable = false;
  238. control._moveToProjectedPosition(projectedPosition);
  239. }
  240. }
  241. if (!this._isDirty && !this._rootContainer.isDirty) {
  242. return;
  243. }
  244. this._isDirty = false;
  245. this._render();
  246. this.update();
  247. }
  248. private _render(): void {
  249. var textureSize = this.getSize();
  250. var renderWidth = textureSize.width;
  251. var renderHeight = textureSize.height;
  252. // Clear
  253. var context = this.getContext();
  254. context.clearRect(0, 0, renderWidth, renderHeight);
  255. if (this._background) {
  256. context.save();
  257. context.fillStyle = this._background;
  258. context.fillRect(0, 0, renderWidth, renderHeight);
  259. context.restore();
  260. }
  261. // Render
  262. context.font = "18px Arial";
  263. context.strokeStyle = "white";
  264. var measure = new Measure(0, 0, renderWidth, renderHeight);
  265. this._rootContainer._draw(measure, context);
  266. }
  267. private _doPicking(x: number, y: number, type: number, buttonIndex: number): void {
  268. var scene = this.getScene();
  269. if (!scene) {
  270. return;
  271. }
  272. var engine = scene.getEngine();
  273. var textureSize = this.getSize();
  274. if (this._isFullscreen) {
  275. x = x * (textureSize.width / engine.getRenderWidth());
  276. y = y * (textureSize.height / engine.getRenderHeight());
  277. }
  278. if (this._capturingControl) {
  279. this._capturingControl._processObservables(type, x, y, buttonIndex);
  280. return;
  281. }
  282. if (!this._rootContainer._processPicking(x, y, type, buttonIndex)) {
  283. if (type === BABYLON.PointerEventTypes.POINTERMOVE) {
  284. if (this._lastControlOver) {
  285. this._lastControlOver._onPointerOut(this._lastControlOver);
  286. }
  287. this._lastControlOver = null;
  288. }
  289. }
  290. this._manageFocus();
  291. }
  292. public attach(): void {
  293. var scene = this.getScene();
  294. if (!scene) {
  295. return;
  296. }
  297. this._pointerMoveObserver = scene.onPrePointerObservable.add((pi, state) => {
  298. if (pi.type !== BABYLON.PointerEventTypes.POINTERMOVE
  299. && pi.type !== BABYLON.PointerEventTypes.POINTERUP
  300. && pi.type !== BABYLON.PointerEventTypes.POINTERDOWN) {
  301. return;
  302. }
  303. if (!scene) {
  304. return;
  305. }
  306. let camera = scene.cameraToUseForPointers || scene.activeCamera;
  307. if (!camera) {
  308. return;
  309. }
  310. let engine = scene.getEngine();
  311. let viewport = camera.viewport;
  312. let x = (scene.pointerX / engine.getHardwareScalingLevel() - viewport.x * engine.getRenderWidth()) / viewport.width;
  313. let y = (scene.pointerY / engine.getHardwareScalingLevel() - viewport.y * engine.getRenderHeight()) / viewport.height;
  314. this._shouldBlockPointer = false;
  315. this._doPicking(x, y, pi.type, pi.event.button);
  316. pi.skipOnPointerObservable = this._shouldBlockPointer;
  317. });
  318. this._attachToOnPointerOut(scene);
  319. }
  320. public attachToMesh(mesh: AbstractMesh, supportPointerMove = true): void {
  321. var scene = this.getScene();
  322. if (!scene) {
  323. return;
  324. }
  325. this._pointerObserver = scene.onPointerObservable.add((pi, state) => {
  326. if (pi.type !== BABYLON.PointerEventTypes.POINTERMOVE
  327. && pi.type !== BABYLON.PointerEventTypes.POINTERUP
  328. && pi.type !== BABYLON.PointerEventTypes.POINTERDOWN) {
  329. return;
  330. }
  331. if (pi.pickInfo && pi.pickInfo.hit && pi.pickInfo.pickedMesh === mesh) {
  332. var uv = pi.pickInfo.getTextureCoordinates();
  333. if (uv) {
  334. let size = this.getSize();
  335. this._doPicking(uv.x * size.width, (1.0 - uv.y) * size.height, pi.type, pi.event.button);
  336. }
  337. } else if (pi.type === BABYLON.PointerEventTypes.POINTERUP) {
  338. if (this._lastControlDown) {
  339. this._lastControlDown.forcePointerUp();
  340. }
  341. this._lastControlDown = null;
  342. this.focusedControl = null;
  343. } else if (pi.type === BABYLON.PointerEventTypes.POINTERMOVE) {
  344. if (this._lastControlOver) {
  345. this._lastControlOver._onPointerOut(this._lastControlOver);
  346. }
  347. this._lastControlOver = null;
  348. }
  349. });
  350. mesh.enablePointerMoveEvents = supportPointerMove;
  351. this._attachToOnPointerOut(scene);
  352. }
  353. public moveFocusToControl(control: IFocusableControl): void {
  354. this.focusedControl = control;
  355. this._lastPickedControl = <any>control;
  356. this._blockNextFocusCheck = true;
  357. }
  358. private _manageFocus(): void {
  359. if (this._blockNextFocusCheck) {
  360. this._blockNextFocusCheck = false;
  361. this._lastPickedControl = <any>this._focusedControl;
  362. return;
  363. }
  364. // Focus management
  365. if (this._focusedControl) {
  366. if (this._focusedControl !== (<any>this._lastPickedControl)) {
  367. if (this._lastPickedControl.isFocusInvisible) {
  368. return;
  369. }
  370. this.focusedControl = null;
  371. }
  372. }
  373. }
  374. private _attachToOnPointerOut(scene: Scene): void {
  375. this._canvasPointerOutObserver = scene.getEngine().onCanvasPointerOutObservable.add(() => {
  376. if (this._lastControlOver) {
  377. this._lastControlOver._onPointerOut(this._lastControlOver);
  378. }
  379. this._lastControlOver = null;
  380. if (this._lastControlDown) {
  381. this._lastControlDown.forcePointerUp();
  382. }
  383. this._lastControlDown = null;
  384. });
  385. }
  386. // Statics
  387. public static CreateForMesh(mesh: AbstractMesh, width = 1024, height = 1024, supportPointerMove = true): AdvancedDynamicTexture {
  388. var result = new AdvancedDynamicTexture(mesh.name + " AdvancedDynamicTexture", width, height, mesh.getScene(), true, Texture.TRILINEAR_SAMPLINGMODE);
  389. var material = new BABYLON.StandardMaterial("AdvancedDynamicTextureMaterial", mesh.getScene());
  390. material.backFaceCulling = false;
  391. material.diffuseColor = BABYLON.Color3.Black();
  392. material.specularColor = BABYLON.Color3.Black();
  393. material.emissiveTexture = result;
  394. material.opacityTexture = result;
  395. mesh.material = material;
  396. result.attachToMesh(mesh, supportPointerMove);
  397. return result;
  398. }
  399. public static CreateFullscreenUI(name: string, foreground: boolean = true, scene: Nullable<Scene> = null): AdvancedDynamicTexture {
  400. var result = new AdvancedDynamicTexture(name, 0, 0, scene);
  401. // Display
  402. var layer = new BABYLON.Layer(name + "_layer", null, scene, !foreground);
  403. layer.texture = result;
  404. result._layerToDispose = layer;
  405. result._isFullscreen = true;
  406. // Attach
  407. result.attach();
  408. return result;
  409. }
  410. }
  411. }