scrollViewer.ts 13 KB

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