scrollViewer.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. import { Rectangle } from "../rectangle";
  2. import { Grid } from "../grid";
  3. import { Control } from "../control";
  4. import { Container } from "../container";
  5. import { PointerInfo, Observer, Nullable } from "babylonjs";
  6. import { AdvancedDynamicTexture, Measure } from "2D";
  7. import { _ScrollViewerWindow } from "./scrollViewerWindow";
  8. import { ScrollBar } from "../sliders/scrollBar";
  9. /**
  10. * Class used to hold a viewer window and sliders in a grid
  11. */
  12. export class ScrollViewer extends Rectangle {
  13. private _grid: Grid;
  14. private _horizontalBarSpace: Rectangle;
  15. private _verticalBarSpace: Rectangle;
  16. private _dragSpace: Rectangle;
  17. private _horizontalBar: ScrollBar;
  18. private _verticalBar: ScrollBar;
  19. private _barColor: string;
  20. private _barBackground: string;
  21. private _barSize: number = 20;
  22. private _endLeft: number;
  23. private _endTop: number;
  24. private _window: _ScrollViewerWindow;
  25. private _pointerIsOver: Boolean = false;
  26. private _wheelPrecision: number = 0.05;
  27. private _onPointerObserver: Nullable<Observer<PointerInfo>>;
  28. private _clientWidth: number;
  29. private _clientHeight: number;
  30. /**
  31. * Gets the horizontal scrollbar
  32. */
  33. public get horizontalBar(): ScrollBar {
  34. return this._horizontalBar;
  35. }
  36. /**
  37. * Gets the vertical scrollbar
  38. */
  39. public get verticalBar(): ScrollBar {
  40. return this._verticalBar;
  41. }
  42. /**
  43. * Adds a new control to the current container
  44. * @param control defines the control to add
  45. * @returns the current container
  46. */
  47. public addControl(control: Nullable<Control>): Container {
  48. if (!control) {
  49. return this;
  50. }
  51. this._window.addControl(control);
  52. return this;
  53. }
  54. /**
  55. * Removes a control from the current container
  56. * @param control defines the control to remove
  57. * @returns the current container
  58. */
  59. public removeControl(control: Control): Container {
  60. this._window.removeControl(control);
  61. return this;
  62. }
  63. /** Gets the list of children */
  64. public get children(): Control[] {
  65. return this._window.children;
  66. }
  67. public _flagDescendantsAsMatrixDirty(): void {
  68. for (var child of this._children) {
  69. child._markMatrixAsDirty();
  70. }
  71. }
  72. /**
  73. * Creates a new ScrollViewer
  74. * @param name of ScrollViewer
  75. */
  76. constructor(name?: string) {
  77. super(name);
  78. this.onDirtyObservable.add(() => {
  79. this._horizontalBarSpace.color = this.color;
  80. this._verticalBarSpace.color = this.color;
  81. this._dragSpace.color = this.color;
  82. });
  83. this.onPointerEnterObservable.add(() => {
  84. this._pointerIsOver = true;
  85. });
  86. this.onPointerOutObservable.add(() => {
  87. this._pointerIsOver = false;
  88. });
  89. this._grid = new Grid();
  90. this._horizontalBar = new ScrollBar();
  91. this._verticalBar = new ScrollBar();
  92. this._window = new _ScrollViewerWindow();
  93. this._window.horizontalAlignment = Control.HORIZONTAL_ALIGNMENT_LEFT;
  94. this._window.verticalAlignment = Control.VERTICAL_ALIGNMENT_TOP;
  95. this._grid.addColumnDefinition(1);
  96. this._grid.addColumnDefinition(0, true);
  97. this._grid.addRowDefinition(1);
  98. this._grid.addRowDefinition(0, true);
  99. super.addControl(this._grid);
  100. this._grid.addControl(this._window, 0, 0);
  101. this._verticalBar.paddingLeft = 0;
  102. this._verticalBar.width = "100%";
  103. this._verticalBar.height = "100%";
  104. this._verticalBar.barOffset = 0;
  105. this._verticalBar.value = 0;
  106. this._verticalBar.maximum = 1;
  107. this._verticalBar.horizontalAlignment = Control.HORIZONTAL_ALIGNMENT_CENTER;
  108. this._verticalBar.verticalAlignment = Control.VERTICAL_ALIGNMENT_CENTER;
  109. this._verticalBar.isVertical = true;
  110. this._verticalBar.rotation = Math.PI;
  111. this._verticalBar.isVisible = false;
  112. this._verticalBarSpace = new Rectangle();
  113. this._verticalBarSpace.horizontalAlignment = Control.HORIZONTAL_ALIGNMENT_LEFT;
  114. this._verticalBarSpace.verticalAlignment = Control.VERTICAL_ALIGNMENT_TOP;
  115. this._verticalBarSpace.thickness = 1;
  116. this._grid.addControl(this._verticalBarSpace, 0, 1);
  117. this._verticalBarSpace.addControl(this._verticalBar);
  118. this._verticalBar.onValueChangedObservable.add((value) => {
  119. this._window.top = value * this._endTop + "px";
  120. });
  121. this._horizontalBar.paddingLeft = 0;
  122. this._horizontalBar.width = "100%";
  123. this._horizontalBar.height = "100%";
  124. this._horizontalBar.barOffset = 0;
  125. this._horizontalBar.value = 0;
  126. this._horizontalBar.maximum = 1;
  127. this._horizontalBar.horizontalAlignment = Control.HORIZONTAL_ALIGNMENT_CENTER;
  128. this._horizontalBar.verticalAlignment = Control.VERTICAL_ALIGNMENT_CENTER;
  129. this._horizontalBar.isVisible = false;
  130. this._horizontalBarSpace = new Rectangle();
  131. this._horizontalBarSpace.horizontalAlignment = Control.HORIZONTAL_ALIGNMENT_LEFT;
  132. this._horizontalBarSpace.verticalAlignment = Control.VERTICAL_ALIGNMENT_TOP;
  133. this._horizontalBarSpace.thickness = 1;
  134. this._grid.addControl(this._horizontalBarSpace, 1, 0);
  135. this._horizontalBarSpace.addControl(this._horizontalBar);
  136. this._horizontalBar.onValueChangedObservable.add((value) => {
  137. this._window.left = value * this._endLeft + "px";
  138. });
  139. this._dragSpace = new Rectangle();
  140. this._dragSpace.thickness = 1;
  141. this._grid.addControl(this._dragSpace, 1, 1);
  142. // Colors
  143. this.barColor = "grey";
  144. this.barBackground = "transparent";
  145. }
  146. /** Reset the scroll viewer window to initial size */
  147. public resetWindow() {
  148. this._window.width = "100%";
  149. this._window.height = "100%";
  150. }
  151. protected _getTypeName(): string {
  152. return "ScrollViewer";
  153. }
  154. private _buildClientSizes() {
  155. this._window.parentClientWidth = this._currentMeasure.width - (this._verticalBar.isVisible ? this._barSize : 0) - 2 * this.thickness;
  156. this._window.parentClientHeight = this._currentMeasure.height - (this._horizontalBar.isVisible ? this._barSize : 0) - 2 * this.thickness;
  157. this._clientWidth = this._window.parentClientWidth;
  158. this._clientHeight = this._window.parentClientHeight;
  159. }
  160. protected _additionalProcessing(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  161. super._additionalProcessing(parentMeasure, context);
  162. this._buildClientSizes();
  163. }
  164. protected _postMeasure(): void {
  165. super._postMeasure();
  166. this._updateScroller();
  167. }
  168. /**
  169. * Gets or sets the mouse wheel precision
  170. * from 0 to 1 with a default value of 0.05
  171. * */
  172. public get wheelPrecision(): number {
  173. return this._wheelPrecision;
  174. }
  175. public set wheelPrecision(value: number) {
  176. if (this._wheelPrecision === value) {
  177. return;
  178. }
  179. if (value < 0) {
  180. value = 0;
  181. }
  182. if (value > 1) {
  183. value = 1;
  184. }
  185. this._wheelPrecision = value;
  186. }
  187. /** Gets or sets the bar color */
  188. public get barColor(): string {
  189. return this._barColor;
  190. }
  191. public set barColor(color: string) {
  192. if (this._barColor === color) {
  193. return;
  194. }
  195. this._barColor = color;
  196. this._horizontalBar.color = color;
  197. this._verticalBar.color = color;
  198. }
  199. /** Gets or sets the size of the bar */
  200. public get barSize(): number {
  201. return this._barSize;
  202. }
  203. public set barSize(value: number) {
  204. if (this._barSize === value) {
  205. return;
  206. }
  207. this._barSize = value;
  208. this._markAsDirty();
  209. if (this._horizontalBar.isVisible) {
  210. this._grid.setRowDefinition(1, this._barSize, true);
  211. }
  212. if (this._verticalBar.isVisible) {
  213. this._grid.setColumnDefinition(1, this._barSize, true);
  214. }
  215. }
  216. /** Gets or sets the bar background */
  217. public get barBackground(): string {
  218. return this._barBackground;
  219. }
  220. public set barBackground(color: string) {
  221. if (this._barBackground === color) {
  222. return;
  223. }
  224. this._barBackground = color;
  225. this._horizontalBar.background = color;
  226. this._verticalBar.background = color;
  227. this._dragSpace.background = color;
  228. }
  229. /** @hidden */
  230. private _updateScroller(): void {
  231. let windowContentsWidth = this._window._currentMeasure.width;
  232. let windowContentsHeight = this._window._currentMeasure.height;
  233. if (this._horizontalBar.isVisible && windowContentsWidth <= this._clientWidth) {
  234. this._grid.setRowDefinition(1, 0, true);
  235. this._horizontalBar.isVisible = false;
  236. this._horizontalBar.value = 0;
  237. this._rebuildLayout = true;
  238. }
  239. else if (!this._horizontalBar.isVisible && windowContentsWidth > this._clientWidth) {
  240. this._grid.setRowDefinition(1, this._barSize, true);
  241. this._horizontalBar.isVisible = true;
  242. this._rebuildLayout = true;
  243. }
  244. if (this._verticalBar.isVisible && windowContentsHeight <= this._clientHeight) {
  245. this._grid.setColumnDefinition(1, 0, true);
  246. this._verticalBar.isVisible = false;
  247. this._verticalBar.value = 0;
  248. this._rebuildLayout = true;
  249. }
  250. else if (!this._verticalBar.isVisible && windowContentsHeight > this._clientHeight) {
  251. this._grid.setColumnDefinition(1, this._barSize, true);
  252. this._verticalBar.isVisible = true;
  253. this._rebuildLayout = true;
  254. }
  255. this._buildClientSizes();
  256. this._endLeft = this._clientWidth - windowContentsWidth;
  257. this._endTop = this._clientHeight - windowContentsHeight;
  258. const newLeft = this._horizontalBar.value * this._endLeft + "px";
  259. const newTop = this._verticalBar.value * this._endTop + "px";
  260. if (newLeft !== this._window.left) {
  261. this._window.left = newLeft;
  262. this._rebuildLayout = true;
  263. }
  264. if (newTop !== this._window.top) {
  265. this._window.top = newTop;
  266. this._rebuildLayout = true;
  267. }
  268. let horizontalMultiplicator = this._clientWidth / windowContentsWidth;
  269. let verticalMultiplicator = this._clientHeight / windowContentsHeight;
  270. this._horizontalBar.thumbWidth = (this._clientWidth * horizontalMultiplicator) + "px";
  271. this._verticalBar.thumbWidth = (this._clientHeight * verticalMultiplicator) + "px";
  272. }
  273. public _link(host: AdvancedDynamicTexture): void {
  274. super._link(host);
  275. this._attachWheel();
  276. }
  277. /** @hidden */
  278. private _attachWheel() {
  279. if (this._onPointerObserver) {
  280. return;
  281. }
  282. let scene = this._host.getScene();
  283. this._onPointerObserver = scene!.onPointerObservable.add((pi, state) => {
  284. if (!this._pointerIsOver || pi.type !== BABYLON.PointerEventTypes.POINTERWHEEL) {
  285. return;
  286. }
  287. if (this._verticalBar.isVisible == true) {
  288. if ((<MouseWheelEvent>pi.event).deltaY < 0 && this._verticalBar.value > 0) {
  289. this._verticalBar.value -= this._wheelPrecision;
  290. } else if ((<MouseWheelEvent>pi.event).deltaY > 0 && this._verticalBar.value < this._verticalBar.maximum) {
  291. this._verticalBar.value += this._wheelPrecision;
  292. }
  293. }
  294. if (this._horizontalBar.isVisible == true) {
  295. if ((<MouseWheelEvent>pi.event).deltaX < 0 && this._horizontalBar.value < this._horizontalBar.maximum) {
  296. this._horizontalBar.value += this._wheelPrecision;
  297. } else if ((<MouseWheelEvent>pi.event).deltaX > 0 && this._horizontalBar.value > 0) {
  298. this._horizontalBar.value -= this._wheelPrecision;
  299. }
  300. }
  301. });
  302. }
  303. public _renderHighlightSpecific(context: CanvasRenderingContext2D): void {
  304. if (!this.isHighlighted) {
  305. return;
  306. }
  307. super._renderHighlightSpecific(context);
  308. this._grid._renderHighlightSpecific(context);
  309. context.restore();
  310. }
  311. /** Releases associated resources */
  312. public dispose() {
  313. let scene = this._host.getScene();
  314. if (scene && this._onPointerObserver) {
  315. scene.onPointerObservable.remove(this._onPointerObserver);
  316. this._onPointerObserver = null;
  317. }
  318. super.dispose();
  319. }
  320. }