advancedDynamicTexture.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /// <reference path="../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON.GUI {
  3. export class AdvancedDynamicTexture extends DynamicTexture {
  4. private _isDirty = false;
  5. private _renderObserver: Observer<Camera>;
  6. private _resizeObserver: Observer<Engine>;
  7. private _pointerMoveObserver: Observer<PointerInfoPre>;
  8. private _pointerObserver: Observer<PointerInfo>;
  9. private _canvasBlurObserver: Observer<Engine>;
  10. private _background: string;
  11. public _rootContainer = new Container("root");
  12. public _lastControlOver: Control;
  13. public _lastControlDown: Control;
  14. public _capturingControl: Control;
  15. public _shouldBlockPointer: boolean;
  16. public _layerToDispose: Layer;
  17. public _linkedControls = new Array<Control>();
  18. private _isFullscreen = false;
  19. private _fullscreenViewport = new Viewport(0, 0, 1, 1);
  20. private _idealWidth = 0;
  21. private _idealHeight = 0;
  22. private _renderAtIdealSize = false;
  23. public get background(): string {
  24. return this._background;
  25. }
  26. public set background(value: string) {
  27. if (this._background === value) {
  28. return;
  29. }
  30. this._background = value;
  31. this.markAsDirty();
  32. }
  33. public get idealWidth(): number {
  34. return this._idealWidth;
  35. }
  36. public set idealWidth(value: number) {
  37. if (this._idealWidth === value) {
  38. return;
  39. }
  40. this._idealWidth = value;
  41. this.markAsDirty();
  42. this._rootContainer._markAllAsDirty();
  43. }
  44. public get idealHeight(): number {
  45. return this._idealHeight;
  46. }
  47. public set idealHeight(value: number) {
  48. if (this._idealHeight === value) {
  49. return;
  50. }
  51. this._idealHeight = value;
  52. this.markAsDirty();
  53. this._rootContainer._markAllAsDirty();
  54. }
  55. public get renderAtIdealSize(): boolean {
  56. return this._renderAtIdealSize;
  57. }
  58. public set renderAtIdealSize(value: boolean) {
  59. if (this._renderAtIdealSize === value) {
  60. return;
  61. }
  62. this._renderAtIdealSize = value;
  63. this._onResize();
  64. }
  65. public get layer(): Layer {
  66. return this._layerToDispose;
  67. }
  68. constructor(name: string, width = 0, height = 0, scene: Scene, generateMipMaps = false, samplingMode = Texture.NEAREST_SAMPLINGMODE) {
  69. super(name, {width: width, height: height}, scene, generateMipMaps, samplingMode, Engine.TEXTUREFORMAT_RGBA);
  70. this._renderObserver = this.getScene().onBeforeCameraRenderObservable.add((camera: Camera) => this._checkUpdate(camera));
  71. this._rootContainer._link(null, this);
  72. this.hasAlpha = true;
  73. if (!width || !height) {
  74. this._resizeObserver = this.getScene().getEngine().onResizeObservable.add(() => this._onResize());
  75. this._onResize();
  76. }
  77. this._texture.isReady = true;
  78. }
  79. public executeOnAllControls(func: (control: Control) => void, container?: Container) {
  80. if (!container) {
  81. container = this._rootContainer;
  82. }
  83. for (var child of container.children) {
  84. if ((<any>child).children) {
  85. this.executeOnAllControls(func, (<Container>child));
  86. continue;
  87. }
  88. func(child);
  89. }
  90. }
  91. public markAsDirty() {
  92. this._isDirty = true;
  93. }
  94. public addControl(control: Control): AdvancedDynamicTexture {
  95. this._rootContainer.addControl(control);
  96. return this;
  97. }
  98. public removeControl(control: Control): AdvancedDynamicTexture {
  99. this._rootContainer.removeControl(control);
  100. return this;
  101. }
  102. public dispose() {
  103. this.getScene().onBeforeCameraRenderObservable.remove(this._renderObserver);
  104. if (this._resizeObserver) {
  105. this.getScene().getEngine().onResizeObservable.remove(this._resizeObserver);
  106. }
  107. if (this._pointerMoveObserver) {
  108. this.getScene().onPrePointerObservable.remove(this._pointerMoveObserver);
  109. }
  110. if (this._pointerObserver) {
  111. this.getScene().onPointerObservable.remove(this._pointerObserver);
  112. }
  113. if (this._canvasBlurObserver) {
  114. this.getScene().getEngine().onCanvasBlurObservable.remove(this._canvasBlurObserver);
  115. }
  116. if (this._layerToDispose) {
  117. this._layerToDispose.texture = null;
  118. this._layerToDispose.dispose();
  119. this._layerToDispose = null;
  120. }
  121. super.dispose();
  122. }
  123. private _onResize(): void {
  124. // Check size
  125. var engine = this.getScene().getEngine();
  126. var textureSize = this.getSize();
  127. var renderWidth = engine.getRenderWidth();
  128. var renderHeight = engine.getRenderHeight();
  129. if (this._renderAtIdealSize) {
  130. if (this._idealWidth) {
  131. renderHeight = (renderHeight * this._idealWidth) / renderWidth;
  132. renderWidth = this._idealWidth;
  133. } else if (this._idealHeight) {
  134. renderWidth = (renderWidth * this._idealHeight) / renderHeight;
  135. renderHeight = this._idealHeight;
  136. }
  137. }
  138. if (textureSize.width !== renderWidth || textureSize.height !== renderHeight) {
  139. this.scaleTo(renderWidth, renderHeight);
  140. this.markAsDirty();
  141. if (this._idealWidth || this._idealHeight) {
  142. this._rootContainer._markAllAsDirty();
  143. }
  144. }
  145. }
  146. public _getGlobalViewport(scene: Scene): Viewport {
  147. var engine = scene.getEngine();
  148. return this._fullscreenViewport.toGlobal(engine.getRenderWidth(), engine.getRenderHeight());
  149. }
  150. private _checkUpdate(camera: Camera): void {
  151. if (this._layerToDispose) {
  152. if ((camera.layerMask & this._layerToDispose.layerMask) === 0) {
  153. return;
  154. }
  155. }
  156. if (this._isFullscreen && this._linkedControls.length) {
  157. var scene = this.getScene();
  158. var globalViewport = this._getGlobalViewport(scene);
  159. for (var control of this._linkedControls) {
  160. var mesh = control._linkedMesh;
  161. var position = mesh.getBoundingInfo().boundingSphere.center;
  162. var projectedPosition = Vector3.Project(position, mesh.getWorldMatrix(), scene.getTransformMatrix(), globalViewport);
  163. if (projectedPosition.z < 0 || projectedPosition.z > 1) {
  164. control.isVisible = false;
  165. continue;
  166. }
  167. control.isVisible = true;
  168. control._moveToProjectedPosition(projectedPosition);
  169. }
  170. }
  171. if (!this._isDirty && !this._rootContainer.isDirty) {
  172. return;
  173. }
  174. this._isDirty = false;
  175. this._render();
  176. this.update();
  177. }
  178. private _render(): void {
  179. var engine = this.getScene().getEngine();
  180. var textureSize = this.getSize();
  181. var renderWidth = textureSize.width;
  182. var renderHeight = textureSize.height;
  183. // Clear
  184. var context = this.getContext();
  185. context.clearRect(0, 0, renderWidth, renderHeight);
  186. if (this._background) {
  187. context.save();
  188. context.fillStyle = this._background;
  189. context.fillRect(0, 0, renderWidth, renderHeight);
  190. context.restore();
  191. }
  192. // Render
  193. context.font = "18px Arial";
  194. var measure = new Measure(0, 0, renderWidth, renderHeight);
  195. this._rootContainer._draw(measure, context);
  196. }
  197. private _doPicking(x: number, y: number, type: number): void {
  198. var engine = this.getScene().getEngine();
  199. var textureSize = this.getSize();
  200. x = x * (textureSize.width / engine.getRenderWidth());
  201. y = y * (textureSize.height / engine.getRenderHeight());
  202. if (this._capturingControl) {
  203. this._capturingControl._processObservables(type, x, y);
  204. return;
  205. }
  206. if (!this._rootContainer._processPicking(x, y, type)) {
  207. if (type === BABYLON.PointerEventTypes.POINTERMOVE) {
  208. if (this._lastControlOver && this._lastControlOver.onPointerOutObservable.hasObservers()) {
  209. this._lastControlOver.onPointerOutObservable.notifyObservers(this._lastControlOver);
  210. }
  211. this._lastControlOver = null;
  212. }
  213. }
  214. }
  215. public attach(): void {
  216. var scene = this.getScene();
  217. this._pointerMoveObserver = scene.onPrePointerObservable.add((pi, state) => {
  218. if (pi.type !== BABYLON.PointerEventTypes.POINTERMOVE
  219. && pi.type !== BABYLON.PointerEventTypes.POINTERUP
  220. && pi.type !== BABYLON.PointerEventTypes.POINTERDOWN) {
  221. return;
  222. }
  223. this._shouldBlockPointer = false;
  224. this._doPicking(scene.pointerX, scene.pointerY, pi.type);
  225. pi.skipOnPointerObservable = this._shouldBlockPointer && pi.type !== BABYLON.PointerEventTypes.POINTERUP;
  226. });
  227. this._attachToOnBlur(scene);
  228. }
  229. public attachToMesh(mesh: AbstractMesh): void {
  230. var scene = this.getScene();
  231. this._pointerObserver = scene.onPointerObservable.add((pi, state) => {
  232. if (pi.type !== BABYLON.PointerEventTypes.POINTERUP && pi.type !== BABYLON.PointerEventTypes.POINTERDOWN) {
  233. return;
  234. }
  235. if (pi.pickInfo.hit && pi.pickInfo.pickedMesh === mesh) {
  236. var uv = pi.pickInfo.getTextureCoordinates();
  237. var size = this.getSize();
  238. this._doPicking(uv.x * size.width, (1.0 - uv.y) * size.height, pi.type);
  239. } else if (pi.type === BABYLON.PointerEventTypes.POINTERUP) {
  240. if (this._lastControlDown) {
  241. this._lastControlDown.forcePointerUp();
  242. }
  243. this._lastControlDown = null;
  244. }
  245. });
  246. this._attachToOnBlur(scene);
  247. }
  248. private _attachToOnBlur(scene: Scene): void {
  249. this._canvasBlurObserver = scene.getEngine().onCanvasBlurObservable.add(() => {
  250. if (this._lastControlOver && this._lastControlOver.onPointerOutObservable.hasObservers()) {
  251. this._lastControlOver.onPointerOutObservable.notifyObservers(this._lastControlOver);
  252. }
  253. this._lastControlOver = null;
  254. if (this._lastControlDown) {
  255. this._lastControlDown.forcePointerUp();
  256. }
  257. this._lastControlDown = null;
  258. });
  259. }
  260. // Statics
  261. public static CreateForMesh(mesh: AbstractMesh, width = 1024, height = 1024): AdvancedDynamicTexture {
  262. var result = new AdvancedDynamicTexture(mesh.name + " AdvancedDynamicTexture", width, height, mesh.getScene(), true, Texture.TRILINEAR_SAMPLINGMODE);
  263. var material = new BABYLON.StandardMaterial("AdvancedDynamicTextureMaterial", mesh.getScene());
  264. material.backFaceCulling = false;
  265. material.diffuseColor = BABYLON.Color3.Black();
  266. material.specularColor = BABYLON.Color3.Black();
  267. material.emissiveTexture = result;
  268. material.opacityTexture = result;
  269. mesh.material = material;
  270. result.attachToMesh(mesh);
  271. return result;
  272. }
  273. public static CreateFullscreenUI(name: string, foreground: boolean = true, scene: Scene = null): AdvancedDynamicTexture {
  274. var result = new AdvancedDynamicTexture(name, 0, 0, scene);
  275. // Display
  276. var layer = new BABYLON.Layer(name + "_layer", null, scene, !foreground);
  277. layer.texture = result;
  278. result._layerToDispose = layer;
  279. result._isFullscreen = true;
  280. // Attach
  281. result.attach();
  282. return result;
  283. }
  284. }
  285. }