advancedDynamicTexture.ts 17 KB

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