control.ts 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874
  1. import { Nullable } from "babylonjs/types";
  2. import { Observable, Observer } from "babylonjs/Misc/observable";
  3. import { Vector2, Vector3, Matrix } from "babylonjs/Maths/math";
  4. import { PointerEventTypes } from 'babylonjs/Events/pointerEvents';
  5. import { Logger } from "babylonjs/Misc/logger";
  6. import { Tools } from "babylonjs/Misc/tools";
  7. import { AbstractMesh } from "babylonjs/Meshes/abstractMesh";
  8. import { Scene } from "babylonjs/scene";
  9. import { Container } from "./container";
  10. import { AdvancedDynamicTexture } from "../advancedDynamicTexture";
  11. import { ValueAndUnit } from "../valueAndUnit";
  12. import { Measure } from "../measure";
  13. import { Style } from "../style";
  14. import { Matrix2D, Vector2WithInfo } from "../math2D";
  15. /**
  16. * Root class used for all 2D controls
  17. * @see http://doc.babylonjs.com/how_to/gui#controls
  18. */
  19. export class Control {
  20. /**
  21. * Gets or sets a boolean indicating if alpha must be an inherited value (false by default)
  22. */
  23. public static AllowAlphaInheritance = false;
  24. private _alpha = 1;
  25. private _alphaSet = false;
  26. private _zIndex = 0;
  27. /** @hidden */
  28. public _host: AdvancedDynamicTexture;
  29. /** Gets or sets the control parent */
  30. public parent: Nullable<Container>;
  31. /** @hidden */
  32. public _currentMeasure = Measure.Empty();
  33. private _fontFamily = "Arial";
  34. private _fontStyle = "";
  35. private _fontWeight = "";
  36. private _fontSize = new ValueAndUnit(18, ValueAndUnit.UNITMODE_PIXEL, false);
  37. private _font: string;
  38. /** @hidden */
  39. public _width = new ValueAndUnit(1, ValueAndUnit.UNITMODE_PERCENTAGE, false);
  40. /** @hidden */
  41. public _height = new ValueAndUnit(1, ValueAndUnit.UNITMODE_PERCENTAGE, false);
  42. /** @hidden */
  43. protected _fontOffset: { ascent: number, height: number, descent: number };
  44. private _color = "";
  45. private _style: Nullable<Style> = null;
  46. private _styleObserver: Nullable<Observer<Style>>;
  47. /** @hidden */
  48. protected _horizontalAlignment = Control.HORIZONTAL_ALIGNMENT_CENTER;
  49. /** @hidden */
  50. protected _verticalAlignment = Control.VERTICAL_ALIGNMENT_CENTER;
  51. /** @hidden */
  52. protected _isDirty = true;
  53. /** @hidden */
  54. protected _wasDirty = false;
  55. /** @hidden */
  56. public _tempParentMeasure = Measure.Empty();
  57. /** @hidden */
  58. public _prevCurrentMeasureTransformedIntoGlobalSpace = Measure.Empty();
  59. /** @hidden */
  60. protected _cachedParentMeasure = Measure.Empty();
  61. private _paddingLeft = new ValueAndUnit(0);
  62. private _paddingRight = new ValueAndUnit(0);
  63. private _paddingTop = new ValueAndUnit(0);
  64. private _paddingBottom = new ValueAndUnit(0);
  65. /** @hidden */
  66. public _left = new ValueAndUnit(0);
  67. /** @hidden */
  68. public _top = new ValueAndUnit(0);
  69. private _scaleX = 1.0;
  70. private _scaleY = 1.0;
  71. private _rotation = 0;
  72. private _transformCenterX = 0.5;
  73. private _transformCenterY = 0.5;
  74. /** @hidden */
  75. public _transformMatrix = Matrix2D.Identity();
  76. /** @hidden */
  77. protected _invertTransformMatrix = Matrix2D.Identity();
  78. /** @hidden */
  79. protected _transformedPosition = Vector2.Zero();
  80. private _isMatrixDirty = true;
  81. private _cachedOffsetX: number;
  82. private _cachedOffsetY: number;
  83. private _isVisible = true;
  84. private _isHighlighted = false;
  85. /** @hidden */
  86. public _linkedMesh: Nullable<AbstractMesh>;
  87. private _fontSet = false;
  88. private _dummyVector2 = Vector2.Zero();
  89. private _downCount = 0;
  90. private _enterCount = -1;
  91. private _doNotRender = false;
  92. private _downPointerIds: { [id: number]: boolean } = {};
  93. protected _isEnabled = true;
  94. protected _disabledColor = "#9a9a9a";
  95. /** @hidden */
  96. protected _rebuildLayout = false;
  97. /** @hidden */
  98. public _isClipped = false;
  99. /** @hidden */
  100. public _tag: any;
  101. /**
  102. * Gets or sets the unique id of the node. Please note that this number will be updated when the control is added to a container
  103. */
  104. public uniqueId: number;
  105. /**
  106. * Gets or sets an object used to store user defined information for the node
  107. */
  108. public metadata: any = null;
  109. /** Gets or sets a boolean indicating if the control can be hit with pointer events */
  110. public isHitTestVisible = true;
  111. /** Gets or sets a boolean indicating if the control can block pointer events */
  112. public isPointerBlocker = false;
  113. /** Gets or sets a boolean indicating if the control can be focusable */
  114. public isFocusInvisible = false;
  115. /**
  116. * Gets or sets a boolean indicating if the children are clipped to the current control bounds.
  117. * Please note that not clipping children may generate issues with adt.useInvalidateRectOptimization so it is recommended to turn this optimization off if you want to use unclipped children
  118. */
  119. public clipChildren = true;
  120. /**
  121. * Gets or sets a boolean indicating that the current control should cache its rendering (useful when the control does not change often)
  122. */
  123. public useBitmapCache = false;
  124. private _cacheData: Nullable<ImageData>;
  125. private _shadowOffsetX = 0;
  126. /** Gets or sets a value indicating the offset to apply on X axis to render the shadow */
  127. public get shadowOffsetX() {
  128. return this._shadowOffsetX;
  129. }
  130. public set shadowOffsetX(value: number) {
  131. if (this._shadowOffsetX === value) {
  132. return;
  133. }
  134. this._shadowOffsetX = value;
  135. this._markAsDirty();
  136. }
  137. private _shadowOffsetY = 0;
  138. /** Gets or sets a value indicating the offset to apply on Y axis to render the shadow */
  139. public get shadowOffsetY() {
  140. return this._shadowOffsetY;
  141. }
  142. public set shadowOffsetY(value: number) {
  143. if (this._shadowOffsetY === value) {
  144. return;
  145. }
  146. this._shadowOffsetY = value;
  147. this._markAsDirty();
  148. }
  149. private _shadowBlur = 0;
  150. /** Gets or sets a value indicating the amount of blur to use to render the shadow */
  151. public get shadowBlur() {
  152. return this._shadowBlur;
  153. }
  154. public set shadowBlur(value: number) {
  155. if (this._shadowBlur === value) {
  156. return;
  157. }
  158. this._shadowBlur = value;
  159. this._markAsDirty();
  160. }
  161. private _shadowColor = 'black';
  162. /** Gets or sets a value indicating the color of the shadow (black by default ie. "#000") */
  163. public get shadowColor() {
  164. return this._shadowColor;
  165. }
  166. public set shadowColor(value: string) {
  167. if (this._shadowColor === value) {
  168. return;
  169. }
  170. this._shadowColor = value;
  171. this._markAsDirty();
  172. }
  173. /** Gets or sets the cursor to use when the control is hovered */
  174. public hoverCursor = "";
  175. /** @hidden */
  176. protected _linkOffsetX = new ValueAndUnit(0);
  177. /** @hidden */
  178. protected _linkOffsetY = new ValueAndUnit(0);
  179. // Properties
  180. /** Gets the control type name */
  181. public get typeName(): string {
  182. return this._getTypeName();
  183. }
  184. /**
  185. * Get the current class name of the control.
  186. * @returns current class name
  187. */
  188. public getClassName(): string {
  189. return this._getTypeName();
  190. }
  191. /**
  192. * An event triggered when the pointer move over the control.
  193. */
  194. public onPointerMoveObservable = new Observable<Vector2>();
  195. /**
  196. * An event triggered when the pointer move out of the control.
  197. */
  198. public onPointerOutObservable = new Observable<Control>();
  199. /**
  200. * An event triggered when the pointer taps the control
  201. */
  202. public onPointerDownObservable = new Observable<Vector2WithInfo>();
  203. /**
  204. * An event triggered when pointer up
  205. */
  206. public onPointerUpObservable = new Observable<Vector2WithInfo>();
  207. /**
  208. * An event triggered when a control is clicked on
  209. */
  210. public onPointerClickObservable = new Observable<Vector2WithInfo>();
  211. /**
  212. * An event triggered when pointer enters the control
  213. */
  214. public onPointerEnterObservable = new Observable<Control>();
  215. /**
  216. * An event triggered when the control is marked as dirty
  217. */
  218. public onDirtyObservable = new Observable<Control>();
  219. /**
  220. * An event triggered before drawing the control
  221. */
  222. public onBeforeDrawObservable = new Observable<Control>();
  223. /**
  224. * An event triggered after the control was drawn
  225. */
  226. public onAfterDrawObservable = new Observable<Control>();
  227. /**
  228. * Get the hosting AdvancedDynamicTexture
  229. */
  230. public get host(): AdvancedDynamicTexture {
  231. return this._host;
  232. }
  233. /** Gets or set information about font offsets (used to render and align text) */
  234. public get fontOffset(): { ascent: number, height: number, descent: number } {
  235. return this._fontOffset;
  236. }
  237. public set fontOffset(offset: { ascent: number, height: number, descent: number }) {
  238. this._fontOffset = offset;
  239. }
  240. /** Gets or sets alpha value for the control (1 means opaque and 0 means entirely transparent) */
  241. public get alpha(): number {
  242. return this._alpha;
  243. }
  244. public set alpha(value: number) {
  245. if (this._alpha === value) {
  246. return;
  247. }
  248. this._alphaSet = true;
  249. this._alpha = value;
  250. this._markAsDirty();
  251. }
  252. /**
  253. * Gets or sets a boolean indicating that we want to highlight the control (mostly for debugging purpose)
  254. */
  255. public get isHighlighted(): boolean {
  256. return this._isHighlighted;
  257. }
  258. public set isHighlighted(value: boolean) {
  259. if (this._isHighlighted === value) {
  260. return;
  261. }
  262. this._isHighlighted = value;
  263. this._markAsDirty();
  264. }
  265. /** Gets or sets a value indicating the scale factor on X axis (1 by default)
  266. * @see http://doc.babylonjs.com/how_to/gui#rotation-and-scaling
  267. */
  268. public get scaleX(): number {
  269. return this._scaleX;
  270. }
  271. public set scaleX(value: number) {
  272. if (this._scaleX === value) {
  273. return;
  274. }
  275. this._scaleX = value;
  276. this._markAsDirty();
  277. this._markMatrixAsDirty();
  278. }
  279. /** Gets or sets a value indicating the scale factor on Y axis (1 by default)
  280. * @see http://doc.babylonjs.com/how_to/gui#rotation-and-scaling
  281. */
  282. public get scaleY(): number {
  283. return this._scaleY;
  284. }
  285. public set scaleY(value: number) {
  286. if (this._scaleY === value) {
  287. return;
  288. }
  289. this._scaleY = value;
  290. this._markAsDirty();
  291. this._markMatrixAsDirty();
  292. }
  293. /** Gets or sets the rotation angle (0 by default)
  294. * @see http://doc.babylonjs.com/how_to/gui#rotation-and-scaling
  295. */
  296. public get rotation(): number {
  297. return this._rotation;
  298. }
  299. public set rotation(value: number) {
  300. if (this._rotation === value) {
  301. return;
  302. }
  303. this._rotation = value;
  304. this._markAsDirty();
  305. this._markMatrixAsDirty();
  306. }
  307. /** Gets or sets the transformation center on Y axis (0 by default)
  308. * @see http://doc.babylonjs.com/how_to/gui#rotation-and-scaling
  309. */
  310. public get transformCenterY(): number {
  311. return this._transformCenterY;
  312. }
  313. public set transformCenterY(value: number) {
  314. if (this._transformCenterY === value) {
  315. return;
  316. }
  317. this._transformCenterY = value;
  318. this._markAsDirty();
  319. this._markMatrixAsDirty();
  320. }
  321. /** Gets or sets the transformation center on X axis (0 by default)
  322. * @see http://doc.babylonjs.com/how_to/gui#rotation-and-scaling
  323. */
  324. public get transformCenterX(): number {
  325. return this._transformCenterX;
  326. }
  327. public set transformCenterX(value: number) {
  328. if (this._transformCenterX === value) {
  329. return;
  330. }
  331. this._transformCenterX = value;
  332. this._markAsDirty();
  333. this._markMatrixAsDirty();
  334. }
  335. /**
  336. * Gets or sets the horizontal alignment
  337. * @see http://doc.babylonjs.com/how_to/gui#alignments
  338. */
  339. public get horizontalAlignment(): number {
  340. return this._horizontalAlignment;
  341. }
  342. public set horizontalAlignment(value: number) {
  343. if (this._horizontalAlignment === value) {
  344. return;
  345. }
  346. this._horizontalAlignment = value;
  347. this._markAsDirty();
  348. }
  349. /**
  350. * Gets or sets the vertical alignment
  351. * @see http://doc.babylonjs.com/how_to/gui#alignments
  352. */
  353. public get verticalAlignment(): number {
  354. return this._verticalAlignment;
  355. }
  356. public set verticalAlignment(value: number) {
  357. if (this._verticalAlignment === value) {
  358. return;
  359. }
  360. this._verticalAlignment = value;
  361. this._markAsDirty();
  362. }
  363. /**
  364. * Gets or sets control width
  365. * @see http://doc.babylonjs.com/how_to/gui#position-and-size
  366. */
  367. public get width(): string | number {
  368. return this._width.toString(this._host);
  369. }
  370. /**
  371. * Gets control width in pixel
  372. * @see http://doc.babylonjs.com/how_to/gui#position-and-size
  373. */
  374. public get widthInPixels(): number {
  375. return this._width.getValueInPixel(this._host, this._cachedParentMeasure.width);
  376. }
  377. public set width(value: string | number) {
  378. if (this._width.toString(this._host) === value) {
  379. return;
  380. }
  381. if (this._width.fromString(value)) {
  382. this._markAsDirty();
  383. }
  384. }
  385. /**
  386. * Gets or sets control height
  387. * @see http://doc.babylonjs.com/how_to/gui#position-and-size
  388. */
  389. public get height(): string | number {
  390. return this._height.toString(this._host);
  391. }
  392. /**
  393. * Gets control height in pixel
  394. * @see http://doc.babylonjs.com/how_to/gui#position-and-size
  395. */
  396. public get heightInPixels(): number {
  397. return this._height.getValueInPixel(this._host, this._cachedParentMeasure.height);
  398. }
  399. public set height(value: string | number) {
  400. if (this._height.toString(this._host) === value) {
  401. return;
  402. }
  403. if (this._height.fromString(value)) {
  404. this._markAsDirty();
  405. }
  406. }
  407. /** Gets or set font family */
  408. public get fontFamily(): string {
  409. if (!this._fontSet) {
  410. return "";
  411. }
  412. return this._fontFamily;
  413. }
  414. public set fontFamily(value: string) {
  415. if (this._fontFamily === value) {
  416. return;
  417. }
  418. this._fontFamily = value;
  419. this._resetFontCache();
  420. }
  421. /** Gets or sets font style */
  422. public get fontStyle(): string {
  423. return this._fontStyle;
  424. }
  425. public set fontStyle(value: string) {
  426. if (this._fontStyle === value) {
  427. return;
  428. }
  429. this._fontStyle = value;
  430. this._resetFontCache();
  431. }
  432. /** Gets or sets font weight */
  433. public get fontWeight(): string {
  434. return this._fontWeight;
  435. }
  436. public set fontWeight(value: string) {
  437. if (this._fontWeight === value) {
  438. return;
  439. }
  440. this._fontWeight = value;
  441. this._resetFontCache();
  442. }
  443. /**
  444. * Gets or sets style
  445. * @see http://doc.babylonjs.com/how_to/gui#styles
  446. */
  447. public get style(): Nullable<Style> {
  448. return this._style;
  449. }
  450. public set style(value: Nullable<Style>) {
  451. if (this._style) {
  452. this._style.onChangedObservable.remove(this._styleObserver);
  453. this._styleObserver = null;
  454. }
  455. this._style = value;
  456. if (this._style) {
  457. this._styleObserver = this._style.onChangedObservable.add(() => {
  458. this._markAsDirty();
  459. this._resetFontCache();
  460. });
  461. }
  462. this._markAsDirty();
  463. this._resetFontCache();
  464. }
  465. /** @hidden */
  466. public get _isFontSizeInPercentage(): boolean {
  467. return this._fontSize.isPercentage;
  468. }
  469. /** Gets font size in pixels */
  470. public get fontSizeInPixels(): number {
  471. let fontSizeToUse = this._style ? this._style._fontSize : this._fontSize;
  472. if (fontSizeToUse.isPixel) {
  473. return fontSizeToUse.getValue(this._host);
  474. }
  475. return fontSizeToUse.getValueInPixel(this._host, this._tempParentMeasure.height || this._cachedParentMeasure.height);
  476. }
  477. /** Gets or sets font size */
  478. public get fontSize(): string | number {
  479. return this._fontSize.toString(this._host);
  480. }
  481. public set fontSize(value: string | number) {
  482. if (this._fontSize.toString(this._host) === value) {
  483. return;
  484. }
  485. if (this._fontSize.fromString(value)) {
  486. this._markAsDirty();
  487. this._resetFontCache();
  488. }
  489. }
  490. /** Gets or sets foreground color */
  491. public get color(): string {
  492. return this._color;
  493. }
  494. public set color(value: string) {
  495. if (this._color === value) {
  496. return;
  497. }
  498. this._color = value;
  499. this._markAsDirty();
  500. }
  501. /** Gets or sets z index which is used to reorder controls on the z axis */
  502. public get zIndex(): number {
  503. return this._zIndex;
  504. }
  505. public set zIndex(value: number) {
  506. if (this.zIndex === value) {
  507. return;
  508. }
  509. this._zIndex = value;
  510. if (this.parent) {
  511. this.parent._reOrderControl(this);
  512. }
  513. }
  514. /** Gets or sets a boolean indicating if the control can be rendered */
  515. public get notRenderable(): boolean {
  516. return this._doNotRender;
  517. }
  518. public set notRenderable(value: boolean) {
  519. if (this._doNotRender === value) {
  520. return;
  521. }
  522. this._doNotRender = value;
  523. this._markAsDirty();
  524. }
  525. /** Gets or sets a boolean indicating if the control is visible */
  526. public get isVisible(): boolean {
  527. return this._isVisible;
  528. }
  529. public set isVisible(value: boolean) {
  530. if (this._isVisible === value) {
  531. return;
  532. }
  533. this._isVisible = value;
  534. this._markAsDirty(true);
  535. }
  536. /** Gets a boolean indicating that the control needs to update its rendering */
  537. public get isDirty(): boolean {
  538. return this._isDirty;
  539. }
  540. /**
  541. * Gets the current linked mesh (or null if none)
  542. */
  543. public get linkedMesh(): Nullable<AbstractMesh> {
  544. return this._linkedMesh;
  545. }
  546. /**
  547. * Gets or sets a value indicating the padding to use on the left of the control
  548. * @see http://doc.babylonjs.com/how_to/gui#position-and-size
  549. */
  550. public get paddingLeft(): string | number {
  551. return this._paddingLeft.toString(this._host);
  552. }
  553. /**
  554. * Gets a value indicating the padding in pixels to use on the left of the control
  555. * @see http://doc.babylonjs.com/how_to/gui#position-and-size
  556. */
  557. public get paddingLeftInPixels(): number {
  558. return this._paddingLeft.getValueInPixel(this._host, this._cachedParentMeasure.width);
  559. }
  560. public set paddingLeft(value: string | number) {
  561. if (this._paddingLeft.fromString(value)) {
  562. this._markAsDirty();
  563. }
  564. }
  565. /**
  566. * Gets or sets a value indicating the padding to use on the right of the control
  567. * @see http://doc.babylonjs.com/how_to/gui#position-and-size
  568. */
  569. public get paddingRight(): string | number {
  570. return this._paddingRight.toString(this._host);
  571. }
  572. /**
  573. * Gets a value indicating the padding in pixels to use on the right of the control
  574. * @see http://doc.babylonjs.com/how_to/gui#position-and-size
  575. */
  576. public get paddingRightInPixels(): number {
  577. return this._paddingRight.getValueInPixel(this._host, this._cachedParentMeasure.width);
  578. }
  579. public set paddingRight(value: string | number) {
  580. if (this._paddingRight.fromString(value)) {
  581. this._markAsDirty();
  582. }
  583. }
  584. /**
  585. * Gets or sets a value indicating the padding to use on the top of the control
  586. * @see http://doc.babylonjs.com/how_to/gui#position-and-size
  587. */
  588. public get paddingTop(): string | number {
  589. return this._paddingTop.toString(this._host);
  590. }
  591. /**
  592. * Gets a value indicating the padding in pixels to use on the top of the control
  593. * @see http://doc.babylonjs.com/how_to/gui#position-and-size
  594. */
  595. public get paddingTopInPixels(): number {
  596. return this._paddingTop.getValueInPixel(this._host, this._cachedParentMeasure.height);
  597. }
  598. public set paddingTop(value: string | number) {
  599. if (this._paddingTop.fromString(value)) {
  600. this._markAsDirty();
  601. }
  602. }
  603. /**
  604. * Gets or sets a value indicating the padding to use on the bottom of the control
  605. * @see http://doc.babylonjs.com/how_to/gui#position-and-size
  606. */
  607. public get paddingBottom(): string | number {
  608. return this._paddingBottom.toString(this._host);
  609. }
  610. /**
  611. * Gets a value indicating the padding in pixels to use on the bottom of the control
  612. * @see http://doc.babylonjs.com/how_to/gui#position-and-size
  613. */
  614. public get paddingBottomInPixels(): number {
  615. return this._paddingBottom.getValueInPixel(this._host, this._cachedParentMeasure.height);
  616. }
  617. public set paddingBottom(value: string | number) {
  618. if (this._paddingBottom.fromString(value)) {
  619. this._markAsDirty();
  620. }
  621. }
  622. /**
  623. * Gets or sets a value indicating the left coordinate of the control
  624. * @see http://doc.babylonjs.com/how_to/gui#position-and-size
  625. */
  626. public get left(): string | number {
  627. return this._left.toString(this._host);
  628. }
  629. /**
  630. * Gets a value indicating the left coordinate in pixels of the control
  631. * @see http://doc.babylonjs.com/how_to/gui#position-and-size
  632. */
  633. public get leftInPixels(): number {
  634. return this._left.getValueInPixel(this._host, this._cachedParentMeasure.width);
  635. }
  636. public set left(value: string | number) {
  637. if (this._left.fromString(value)) {
  638. this._markAsDirty();
  639. }
  640. }
  641. /**
  642. * Gets or sets a value indicating the top coordinate of the control
  643. * @see http://doc.babylonjs.com/how_to/gui#position-and-size
  644. */
  645. public get top(): string | number {
  646. return this._top.toString(this._host);
  647. }
  648. /**
  649. * Gets a value indicating the top coordinate in pixels of the control
  650. * @see http://doc.babylonjs.com/how_to/gui#position-and-size
  651. */
  652. public get topInPixels(): number {
  653. return this._top.getValueInPixel(this._host, this._cachedParentMeasure.height);
  654. }
  655. public set top(value: string | number) {
  656. if (this._top.fromString(value)) {
  657. this._markAsDirty();
  658. }
  659. }
  660. /**
  661. * Gets or sets a value indicating the offset on X axis to the linked mesh
  662. * @see http://doc.babylonjs.com/how_to/gui#tracking-positions
  663. */
  664. public get linkOffsetX(): string | number {
  665. return this._linkOffsetX.toString(this._host);
  666. }
  667. /**
  668. * Gets a value indicating the offset in pixels on X axis to the linked mesh
  669. * @see http://doc.babylonjs.com/how_to/gui#tracking-positions
  670. */
  671. public get linkOffsetXInPixels(): number {
  672. return this._linkOffsetX.getValueInPixel(this._host, this._cachedParentMeasure.width);
  673. }
  674. public set linkOffsetX(value: string | number) {
  675. if (this._linkOffsetX.fromString(value)) {
  676. this._markAsDirty();
  677. }
  678. }
  679. /**
  680. * Gets or sets a value indicating the offset on Y axis to the linked mesh
  681. * @see http://doc.babylonjs.com/how_to/gui#tracking-positions
  682. */
  683. public get linkOffsetY(): string | number {
  684. return this._linkOffsetY.toString(this._host);
  685. }
  686. /**
  687. * Gets a value indicating the offset in pixels on Y axis to the linked mesh
  688. * @see http://doc.babylonjs.com/how_to/gui#tracking-positions
  689. */
  690. public get linkOffsetYInPixels(): number {
  691. return this._linkOffsetY.getValueInPixel(this._host, this._cachedParentMeasure.height);
  692. }
  693. public set linkOffsetY(value: string | number) {
  694. if (this._linkOffsetY.fromString(value)) {
  695. this._markAsDirty();
  696. }
  697. }
  698. /** Gets the center coordinate on X axis */
  699. public get centerX(): number {
  700. return this._currentMeasure.left + this._currentMeasure.width / 2;
  701. }
  702. /** Gets the center coordinate on Y axis */
  703. public get centerY(): number {
  704. return this._currentMeasure.top + this._currentMeasure.height / 2;
  705. }
  706. /** Gets or sets if control is Enabled*/
  707. public get isEnabled(): boolean {
  708. return this._isEnabled;
  709. }
  710. public set isEnabled(value: boolean) {
  711. if (this._isEnabled === value) {
  712. return;
  713. }
  714. this._isEnabled = value;
  715. this._markAsDirty();
  716. }
  717. /** Gets or sets background color of control if it's disabled*/
  718. public get disabledColor(): string {
  719. return this._disabledColor;
  720. }
  721. public set disabledColor(value: string) {
  722. if (this._disabledColor === value) {
  723. return;
  724. }
  725. this._disabledColor = value;
  726. this._markAsDirty();
  727. }
  728. // Functions
  729. /**
  730. * Creates a new control
  731. * @param name defines the name of the control
  732. */
  733. constructor(
  734. /** defines the name of the control */
  735. public name?: string) {
  736. }
  737. /** @hidden */
  738. protected _getTypeName(): string {
  739. return "Control";
  740. }
  741. /**
  742. * Gets the first ascendant in the hierarchy of the given type
  743. * @param className defines the required type
  744. * @returns the ascendant or null if not found
  745. */
  746. public getAscendantOfClass(className: string): Nullable<Control> {
  747. if (!this.parent) {
  748. return null;
  749. }
  750. if (this.parent.getClassName() === className) {
  751. return this.parent;
  752. }
  753. return this.parent.getAscendantOfClass(className);
  754. }
  755. /** @hidden */
  756. public _resetFontCache(): void {
  757. this._fontSet = true;
  758. this._markAsDirty();
  759. }
  760. /**
  761. * Determines if a container is an ascendant of the current control
  762. * @param container defines the container to look for
  763. * @returns true if the container is one of the ascendant of the control
  764. */
  765. public isAscendant(container: Control): boolean {
  766. if (!this.parent) {
  767. return false;
  768. }
  769. if (this.parent === container) {
  770. return true;
  771. }
  772. return this.parent.isAscendant(container);
  773. }
  774. /**
  775. * Gets coordinates in local control space
  776. * @param globalCoordinates defines the coordinates to transform
  777. * @returns the new coordinates in local space
  778. */
  779. public getLocalCoordinates(globalCoordinates: Vector2): Vector2 {
  780. var result = Vector2.Zero();
  781. this.getLocalCoordinatesToRef(globalCoordinates, result);
  782. return result;
  783. }
  784. /**
  785. * Gets coordinates in local control space
  786. * @param globalCoordinates defines the coordinates to transform
  787. * @param result defines the target vector2 where to store the result
  788. * @returns the current control
  789. */
  790. public getLocalCoordinatesToRef(globalCoordinates: Vector2, result: Vector2): Control {
  791. result.x = globalCoordinates.x - this._currentMeasure.left;
  792. result.y = globalCoordinates.y - this._currentMeasure.top;
  793. return this;
  794. }
  795. /**
  796. * Gets coordinates in parent local control space
  797. * @param globalCoordinates defines the coordinates to transform
  798. * @returns the new coordinates in parent local space
  799. */
  800. public getParentLocalCoordinates(globalCoordinates: Vector2): Vector2 {
  801. var result = Vector2.Zero();
  802. result.x = globalCoordinates.x - this._cachedParentMeasure.left;
  803. result.y = globalCoordinates.y - this._cachedParentMeasure.top;
  804. return result;
  805. }
  806. /**
  807. * Move the current control to a vector3 position projected onto the screen.
  808. * @param position defines the target position
  809. * @param scene defines the hosting scene
  810. */
  811. public moveToVector3(position: Vector3, scene: Scene): void {
  812. if (!this._host || this.parent !== this._host._rootContainer) {
  813. Tools.Error("Cannot move a control to a vector3 if the control is not at root level");
  814. return;
  815. }
  816. this.horizontalAlignment = Control.HORIZONTAL_ALIGNMENT_LEFT;
  817. this.verticalAlignment = Control.VERTICAL_ALIGNMENT_TOP;
  818. var globalViewport = this._host._getGlobalViewport(scene);
  819. var projectedPosition = Vector3.Project(position, Matrix.Identity(), scene.getTransformMatrix(), globalViewport);
  820. this._moveToProjectedPosition(projectedPosition);
  821. if (projectedPosition.z < 0 || projectedPosition.z > 1) {
  822. this.notRenderable = true;
  823. return;
  824. }
  825. this.notRenderable = false;
  826. }
  827. /** @hidden */
  828. public _getDescendants(results: Control[], directDescendantsOnly: boolean = false, predicate?: (control: Control) => boolean): void {
  829. // Do nothing by default
  830. }
  831. /**
  832. * Will return all controls that have this control as ascendant
  833. * @param directDescendantsOnly defines if true only direct descendants of 'this' will be considered, if false direct and also indirect (children of children, an so on in a recursive manner) descendants of 'this' will be considered
  834. * @param predicate defines an optional predicate that will be called on every evaluated child, the predicate must return true for a given child to be part of the result, otherwise it will be ignored
  835. * @return all child controls
  836. */
  837. public getDescendants(directDescendantsOnly?: boolean, predicate?: (control: Control) => boolean): Control[] {
  838. var results = new Array<Control>();
  839. this._getDescendants(results, directDescendantsOnly, predicate);
  840. return results;
  841. }
  842. /**
  843. * Link current control with a target mesh
  844. * @param mesh defines the mesh to link with
  845. * @see http://doc.babylonjs.com/how_to/gui#tracking-positions
  846. */
  847. public linkWithMesh(mesh: Nullable<AbstractMesh>): void {
  848. if (!this._host || this.parent && this.parent !== this._host._rootContainer) {
  849. if (mesh) {
  850. Tools.Error("Cannot link a control to a mesh if the control is not at root level");
  851. }
  852. return;
  853. }
  854. var index = this._host._linkedControls.indexOf(this);
  855. if (index !== -1) {
  856. this._linkedMesh = mesh;
  857. if (!mesh) {
  858. this._host._linkedControls.splice(index, 1);
  859. }
  860. return;
  861. } else if (!mesh) {
  862. return;
  863. }
  864. this.horizontalAlignment = Control.HORIZONTAL_ALIGNMENT_LEFT;
  865. this.verticalAlignment = Control.VERTICAL_ALIGNMENT_TOP;
  866. this._linkedMesh = mesh;
  867. this._host._linkedControls.push(this);
  868. }
  869. /** @hidden */
  870. public _moveToProjectedPosition(projectedPosition: Vector3): void {
  871. let oldLeft = this._left.getValue(this._host);
  872. let oldTop = this._top.getValue(this._host);
  873. var newLeft = ((projectedPosition.x + this._linkOffsetX.getValue(this._host)) - this._currentMeasure.width / 2);
  874. var newTop = ((projectedPosition.y + this._linkOffsetY.getValue(this._host)) - this._currentMeasure.height / 2);
  875. if (this._left.ignoreAdaptiveScaling && this._top.ignoreAdaptiveScaling) {
  876. if (Math.abs(newLeft - oldLeft) < 0.5) {
  877. newLeft = oldLeft;
  878. }
  879. if (Math.abs(newTop - oldTop) < 0.5) {
  880. newTop = oldTop;
  881. }
  882. }
  883. this.left = newLeft + "px";
  884. this.top = newTop + "px";
  885. this._left.ignoreAdaptiveScaling = true;
  886. this._top.ignoreAdaptiveScaling = true;
  887. this._markAsDirty();
  888. }
  889. /** @hidden */
  890. public _offsetLeft(offset: number) {
  891. this._isDirty = true;
  892. this._currentMeasure.left += offset;
  893. }
  894. /** @hidden */
  895. public _offsetTop(offset: number) {
  896. this._isDirty = true;
  897. this._currentMeasure.top += offset;
  898. }
  899. /** @hidden */
  900. public _markMatrixAsDirty(): void {
  901. this._isMatrixDirty = true;
  902. this._flagDescendantsAsMatrixDirty();
  903. }
  904. /** @hidden */
  905. public _flagDescendantsAsMatrixDirty(): void {
  906. // No child
  907. }
  908. /** @hidden */
  909. public _intersectsRect(rect: Measure) {
  910. // Rotate the control's current measure into local space and check if it intersects the passed in rectangle
  911. this._currentMeasure.transformToRef(this._transformMatrix, this._tmpMeasureA);
  912. if (this._tmpMeasureA.left >= rect.left + rect.width) {
  913. return false;
  914. }
  915. if (this._tmpMeasureA.top >= rect.top + rect.height) {
  916. return false;
  917. }
  918. if (this._tmpMeasureA.left + this._tmpMeasureA.width <= rect.left) {
  919. return false;
  920. }
  921. if (this._tmpMeasureA.top + this._tmpMeasureA.height <= rect.top) {
  922. return false;
  923. }
  924. return true;
  925. }
  926. /** @hidden */
  927. protected invalidateRect() {
  928. this._transform();
  929. if (this.host && this.host.useInvalidateRectOptimization) {
  930. // Rotate by transform to get the measure transformed to global space
  931. this._currentMeasure.transformToRef(this._transformMatrix, this._tmpMeasureA);
  932. // get the boudning box of the current measure and last frames measure in global space and invalidate it
  933. // the previous measure is used to properly clear a control that is scaled down
  934. Measure.CombineToRef(this._tmpMeasureA, this._prevCurrentMeasureTransformedIntoGlobalSpace, this._tmpMeasureA);
  935. // Expand rect based on shadows
  936. var shadowOffsetX = this.shadowOffsetX;
  937. var shadowOffsetY = this.shadowOffsetY;
  938. var shadowBlur = this.shadowBlur;
  939. var leftShadowOffset = Math.min(Math.min(shadowOffsetX, 0) - shadowBlur * 2, 0);
  940. var rightShadowOffset = Math.max(Math.max(shadowOffsetX, 0) + shadowBlur * 2, 0);
  941. var topShadowOffset = Math.min(Math.min(shadowOffsetY, 0) - shadowBlur * 2, 0);
  942. var bottomShadowOffset = Math.max(Math.max(shadowOffsetY, 0) + shadowBlur * 2, 0);
  943. this.host.invalidateRect(
  944. Math.floor(this._tmpMeasureA.left + leftShadowOffset),
  945. Math.floor(this._tmpMeasureA.top + topShadowOffset),
  946. Math.ceil(this._tmpMeasureA.left + this._tmpMeasureA.width + rightShadowOffset),
  947. Math.ceil(this._tmpMeasureA.top + this._tmpMeasureA.height + bottomShadowOffset),
  948. );
  949. }
  950. }
  951. /** @hidden */
  952. public _markAsDirty(force = false): void {
  953. if (!this._isVisible && !force) {
  954. return;
  955. }
  956. this._isDirty = true;
  957. // Redraw only this rectangle
  958. if (this._host) {
  959. this._host.markAsDirty();
  960. }
  961. }
  962. /** @hidden */
  963. public _markAllAsDirty(): void {
  964. this._markAsDirty();
  965. if (this._font) {
  966. this._prepareFont();
  967. }
  968. }
  969. /** @hidden */
  970. public _link(host: AdvancedDynamicTexture): void {
  971. this._host = host;
  972. if (this._host) {
  973. this.uniqueId = this._host.getScene()!.getUniqueId();
  974. }
  975. }
  976. /** @hidden */
  977. protected _transform(context?: CanvasRenderingContext2D): void {
  978. if (!this._isMatrixDirty && this._scaleX === 1 && this._scaleY === 1 && this._rotation === 0) {
  979. return;
  980. }
  981. // postTranslate
  982. var offsetX = this._currentMeasure.width * this._transformCenterX + this._currentMeasure.left;
  983. var offsetY = this._currentMeasure.height * this._transformCenterY + this._currentMeasure.top;
  984. if (context) {
  985. context.translate(offsetX, offsetY);
  986. // rotate
  987. context.rotate(this._rotation);
  988. // scale
  989. context.scale(this._scaleX, this._scaleY);
  990. // preTranslate
  991. context.translate(-offsetX, -offsetY);
  992. }
  993. // Need to update matrices?
  994. if (this._isMatrixDirty || this._cachedOffsetX !== offsetX || this._cachedOffsetY !== offsetY) {
  995. this._cachedOffsetX = offsetX;
  996. this._cachedOffsetY = offsetY;
  997. this._isMatrixDirty = false;
  998. this._flagDescendantsAsMatrixDirty();
  999. Matrix2D.ComposeToRef(-offsetX, -offsetY, this._rotation, this._scaleX, this._scaleY, this.parent ? this.parent._transformMatrix : null, this._transformMatrix);
  1000. this._transformMatrix.invertToRef(this._invertTransformMatrix);
  1001. }
  1002. }
  1003. /** @hidden */
  1004. public _renderHighlight(context: CanvasRenderingContext2D): void {
  1005. if (!this.isHighlighted) {
  1006. return;
  1007. }
  1008. context.save();
  1009. context.strokeStyle = "#4affff";
  1010. context.lineWidth = 2;
  1011. this._renderHighlightSpecific(context);
  1012. context.restore();
  1013. }
  1014. /** @hidden */
  1015. public _renderHighlightSpecific(context: CanvasRenderingContext2D): void {
  1016. context.strokeRect(this._currentMeasure.left, this._currentMeasure.top, this._currentMeasure.width, this._currentMeasure.height);
  1017. }
  1018. /** @hidden */
  1019. protected _applyStates(context: CanvasRenderingContext2D): void {
  1020. if (this._isFontSizeInPercentage) {
  1021. this._fontSet = true;
  1022. }
  1023. if (this._fontSet) {
  1024. this._prepareFont();
  1025. this._fontSet = false;
  1026. }
  1027. if (this._font) {
  1028. context.font = this._font;
  1029. }
  1030. if (this._color) {
  1031. context.fillStyle = this._color;
  1032. }
  1033. if (Control.AllowAlphaInheritance) {
  1034. context.globalAlpha *= this._alpha;
  1035. } else if (this._alphaSet) {
  1036. context.globalAlpha = this.parent ? this.parent.alpha * this._alpha : this._alpha;
  1037. }
  1038. }
  1039. /** @hidden */
  1040. public _layout(parentMeasure: Measure, context: CanvasRenderingContext2D): boolean {
  1041. if (!this.isDirty && (!this.isVisible || this.notRenderable)) {
  1042. return false;
  1043. }
  1044. if (this._isDirty || !this._cachedParentMeasure.isEqualsTo(parentMeasure)) {
  1045. this._currentMeasure.transformToRef(this._transformMatrix, this._prevCurrentMeasureTransformedIntoGlobalSpace);
  1046. context.save();
  1047. this._applyStates(context);
  1048. let rebuildCount = 0;
  1049. do {
  1050. this._rebuildLayout = false;
  1051. this._processMeasures(parentMeasure, context);
  1052. rebuildCount++;
  1053. }
  1054. while (this._rebuildLayout && rebuildCount < 3);
  1055. if (rebuildCount >= 3) {
  1056. Logger.Error(`Layout cycle detected in GUI (Control name=${this.name}, uniqueId=${this.uniqueId})`);
  1057. }
  1058. context.restore();
  1059. this.invalidateRect();
  1060. this._evaluateClippingState(parentMeasure);
  1061. }
  1062. this._wasDirty = this._isDirty;
  1063. this._isDirty = false;
  1064. return true;
  1065. }
  1066. /** @hidden */
  1067. protected _processMeasures(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  1068. this._currentMeasure.copyFrom(parentMeasure);
  1069. // Let children take some pre-measurement actions
  1070. this._preMeasure(parentMeasure, context);
  1071. this._measure();
  1072. this._computeAlignment(parentMeasure, context);
  1073. // Convert to int values
  1074. this._currentMeasure.left = this._currentMeasure.left | 0;
  1075. this._currentMeasure.top = this._currentMeasure.top | 0;
  1076. this._currentMeasure.width = this._currentMeasure.width | 0;
  1077. this._currentMeasure.height = this._currentMeasure.height | 0;
  1078. // Let children add more features
  1079. this._additionalProcessing(parentMeasure, context);
  1080. this._cachedParentMeasure.copyFrom(parentMeasure);
  1081. if (this.onDirtyObservable.hasObservers()) {
  1082. this.onDirtyObservable.notifyObservers(this);
  1083. }
  1084. }
  1085. protected _evaluateClippingState(parentMeasure: Measure) {
  1086. if (this.parent && this.parent.clipChildren) {
  1087. // Early clip
  1088. if (this._currentMeasure.left > parentMeasure.left + parentMeasure.width) {
  1089. this._isClipped = true;
  1090. return;
  1091. }
  1092. if (this._currentMeasure.left + this._currentMeasure.width < parentMeasure.left) {
  1093. this._isClipped = true;
  1094. return;
  1095. }
  1096. if (this._currentMeasure.top > parentMeasure.top + parentMeasure.height) {
  1097. this._isClipped = true;
  1098. return;
  1099. }
  1100. if (this._currentMeasure.top + this._currentMeasure.height < parentMeasure.top) {
  1101. this._isClipped = true;
  1102. return;
  1103. }
  1104. }
  1105. this._isClipped = false;
  1106. }
  1107. /** @hidden */
  1108. public _measure(): void {
  1109. // Width / Height
  1110. if (this._width.isPixel) {
  1111. this._currentMeasure.width = this._width.getValue(this._host);
  1112. } else {
  1113. this._currentMeasure.width *= this._width.getValue(this._host);
  1114. }
  1115. if (this._height.isPixel) {
  1116. this._currentMeasure.height = this._height.getValue(this._host);
  1117. } else {
  1118. this._currentMeasure.height *= this._height.getValue(this._host);
  1119. }
  1120. }
  1121. /** @hidden */
  1122. protected _computeAlignment(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  1123. var width = this._currentMeasure.width;
  1124. var height = this._currentMeasure.height;
  1125. var parentWidth = parentMeasure.width;
  1126. var parentHeight = parentMeasure.height;
  1127. // Left / top
  1128. var x = 0;
  1129. var y = 0;
  1130. switch (this.horizontalAlignment) {
  1131. case Control.HORIZONTAL_ALIGNMENT_LEFT:
  1132. x = 0;
  1133. break;
  1134. case Control.HORIZONTAL_ALIGNMENT_RIGHT:
  1135. x = parentWidth - width;
  1136. break;
  1137. case Control.HORIZONTAL_ALIGNMENT_CENTER:
  1138. x = (parentWidth - width) / 2;
  1139. break;
  1140. }
  1141. switch (this.verticalAlignment) {
  1142. case Control.VERTICAL_ALIGNMENT_TOP:
  1143. y = 0;
  1144. break;
  1145. case Control.VERTICAL_ALIGNMENT_BOTTOM:
  1146. y = parentHeight - height;
  1147. break;
  1148. case Control.VERTICAL_ALIGNMENT_CENTER:
  1149. y = (parentHeight - height) / 2;
  1150. break;
  1151. }
  1152. if (this._paddingLeft.isPixel) {
  1153. this._currentMeasure.left += this._paddingLeft.getValue(this._host);
  1154. this._currentMeasure.width -= this._paddingLeft.getValue(this._host);
  1155. } else {
  1156. this._currentMeasure.left += parentWidth * this._paddingLeft.getValue(this._host);
  1157. this._currentMeasure.width -= parentWidth * this._paddingLeft.getValue(this._host);
  1158. }
  1159. if (this._paddingRight.isPixel) {
  1160. this._currentMeasure.width -= this._paddingRight.getValue(this._host);
  1161. } else {
  1162. this._currentMeasure.width -= parentWidth * this._paddingRight.getValue(this._host);
  1163. }
  1164. if (this._paddingTop.isPixel) {
  1165. this._currentMeasure.top += this._paddingTop.getValue(this._host);
  1166. this._currentMeasure.height -= this._paddingTop.getValue(this._host);
  1167. } else {
  1168. this._currentMeasure.top += parentHeight * this._paddingTop.getValue(this._host);
  1169. this._currentMeasure.height -= parentHeight * this._paddingTop.getValue(this._host);
  1170. }
  1171. if (this._paddingBottom.isPixel) {
  1172. this._currentMeasure.height -= this._paddingBottom.getValue(this._host);
  1173. } else {
  1174. this._currentMeasure.height -= parentHeight * this._paddingBottom.getValue(this._host);
  1175. }
  1176. if (this._left.isPixel) {
  1177. this._currentMeasure.left += this._left.getValue(this._host);
  1178. } else {
  1179. this._currentMeasure.left += parentWidth * this._left.getValue(this._host);
  1180. }
  1181. if (this._top.isPixel) {
  1182. this._currentMeasure.top += this._top.getValue(this._host);
  1183. } else {
  1184. this._currentMeasure.top += parentHeight * this._top.getValue(this._host);
  1185. }
  1186. this._currentMeasure.left += x;
  1187. this._currentMeasure.top += y;
  1188. }
  1189. /** @hidden */
  1190. protected _preMeasure(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  1191. // Do nothing
  1192. }
  1193. /** @hidden */
  1194. protected _additionalProcessing(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  1195. // Do nothing
  1196. }
  1197. /** @hidden */
  1198. protected _clipForChildren(context: CanvasRenderingContext2D): void {
  1199. // DO nothing
  1200. }
  1201. private static _ClipMeasure = new Measure(0, 0, 0, 0);
  1202. private _tmpMeasureA = new Measure(0, 0, 0, 0);
  1203. private _clip(context: CanvasRenderingContext2D, invalidatedRectangle?: Nullable<Measure>) {
  1204. context.beginPath();
  1205. Control._ClipMeasure.copyFrom(this._currentMeasure);
  1206. if (invalidatedRectangle) {
  1207. // Rotate the invalidated rect into the control's space
  1208. invalidatedRectangle.transformToRef(this._invertTransformMatrix, this._tmpMeasureA);
  1209. // Get the intersection of the rect in context space and the current context
  1210. var intersection = new Measure(0, 0, 0, 0);
  1211. intersection.left = Math.max(this._tmpMeasureA.left, this._currentMeasure.left);
  1212. intersection.top = Math.max(this._tmpMeasureA.top, this._currentMeasure.top);
  1213. intersection.width = Math.min(this._tmpMeasureA.left + this._tmpMeasureA.width, this._currentMeasure.left + this._currentMeasure.width) - intersection.left;
  1214. intersection.height = Math.min(this._tmpMeasureA.top + this._tmpMeasureA.height, this._currentMeasure.top + this._currentMeasure.height) - intersection.top;
  1215. Control._ClipMeasure.copyFrom(intersection);
  1216. }
  1217. if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
  1218. var shadowOffsetX = this.shadowOffsetX;
  1219. var shadowOffsetY = this.shadowOffsetY;
  1220. var shadowBlur = this.shadowBlur;
  1221. var leftShadowOffset = Math.min(Math.min(shadowOffsetX, 0) - shadowBlur * 2, 0);
  1222. var rightShadowOffset = Math.max(Math.max(shadowOffsetX, 0) + shadowBlur * 2, 0);
  1223. var topShadowOffset = Math.min(Math.min(shadowOffsetY, 0) - shadowBlur * 2, 0);
  1224. var bottomShadowOffset = Math.max(Math.max(shadowOffsetY, 0) + shadowBlur * 2, 0);
  1225. context.rect(
  1226. Control._ClipMeasure.left + leftShadowOffset,
  1227. Control._ClipMeasure.top + topShadowOffset,
  1228. Control._ClipMeasure.width + rightShadowOffset - leftShadowOffset,
  1229. Control._ClipMeasure.height + bottomShadowOffset - topShadowOffset
  1230. );
  1231. } else {
  1232. context.rect(Control._ClipMeasure.left, Control._ClipMeasure.top, Control._ClipMeasure.width, Control._ClipMeasure.height);
  1233. }
  1234. context.clip();
  1235. }
  1236. /** @hidden */
  1237. public _render(context: CanvasRenderingContext2D, invalidatedRectangle?: Nullable<Measure>): boolean {
  1238. if (!this.isVisible || this.notRenderable || this._isClipped) {
  1239. this._isDirty = false;
  1240. return false;
  1241. }
  1242. context.save();
  1243. this._applyStates(context);
  1244. // Transform
  1245. this._transform(context);
  1246. // Clip
  1247. if (this.clipChildren) {
  1248. this._clip(context, invalidatedRectangle);
  1249. }
  1250. if (this.onBeforeDrawObservable.hasObservers()) {
  1251. this.onBeforeDrawObservable.notifyObservers(this);
  1252. }
  1253. if (this.useBitmapCache && !this._wasDirty && this._cacheData) {
  1254. context.putImageData(this._cacheData, this._currentMeasure.left, this._currentMeasure.top);
  1255. } else {
  1256. this._draw(context, invalidatedRectangle);
  1257. }
  1258. if (this.useBitmapCache && this._wasDirty) {
  1259. this._cacheData = context.getImageData(this._currentMeasure.left, this._currentMeasure.top, this._currentMeasure.width, this._currentMeasure.height);
  1260. }
  1261. this._renderHighlight(context);
  1262. if (this.onAfterDrawObservable.hasObservers()) {
  1263. this.onAfterDrawObservable.notifyObservers(this);
  1264. }
  1265. context.restore();
  1266. return true;
  1267. }
  1268. /** @hidden */
  1269. public _draw(context: CanvasRenderingContext2D, invalidatedRectangle?: Nullable<Measure>): void {
  1270. // Do nothing
  1271. }
  1272. /**
  1273. * Tests if a given coordinates belong to the current control
  1274. * @param x defines x coordinate to test
  1275. * @param y defines y coordinate to test
  1276. * @returns true if the coordinates are inside the control
  1277. */
  1278. public contains(x: number, y: number): boolean {
  1279. // Invert transform
  1280. this._invertTransformMatrix.transformCoordinates(x, y, this._transformedPosition);
  1281. x = this._transformedPosition.x;
  1282. y = this._transformedPosition.y;
  1283. // Check
  1284. if (x < this._currentMeasure.left) {
  1285. return false;
  1286. }
  1287. if (x > this._currentMeasure.left + this._currentMeasure.width) {
  1288. return false;
  1289. }
  1290. if (y < this._currentMeasure.top) {
  1291. return false;
  1292. }
  1293. if (y > this._currentMeasure.top + this._currentMeasure.height) {
  1294. return false;
  1295. }
  1296. if (this.isPointerBlocker) {
  1297. this._host._shouldBlockPointer = true;
  1298. }
  1299. return true;
  1300. }
  1301. /** @hidden */
  1302. public _processPicking(x: number, y: number, type: number, pointerId: number, buttonIndex: number): boolean {
  1303. if (!this._isEnabled) {
  1304. return false;
  1305. }
  1306. if (!this.isHitTestVisible || !this.isVisible || this._doNotRender) {
  1307. return false;
  1308. }
  1309. if (!this.contains(x, y)) {
  1310. return false;
  1311. }
  1312. this._processObservables(type, x, y, pointerId, buttonIndex);
  1313. return true;
  1314. }
  1315. /** @hidden */
  1316. public _onPointerMove(target: Control, coordinates: Vector2): void {
  1317. var canNotify: boolean = this.onPointerMoveObservable.notifyObservers(coordinates, -1, target, this);
  1318. if (canNotify && this.parent != null) { this.parent._onPointerMove(target, coordinates); }
  1319. }
  1320. /** @hidden */
  1321. public _onPointerEnter(target: Control): boolean {
  1322. if (!this._isEnabled) {
  1323. return false;
  1324. }
  1325. if (this._enterCount > 0) {
  1326. return false;
  1327. }
  1328. if (this._enterCount === -1) { // -1 is for touch input, we are now sure we are with a mouse or pencil
  1329. this._enterCount = 0;
  1330. }
  1331. this._enterCount++;
  1332. var canNotify: boolean = this.onPointerEnterObservable.notifyObservers(this, -1, target, this);
  1333. if (canNotify && this.parent != null) { this.parent._onPointerEnter(target); }
  1334. return true;
  1335. }
  1336. /** @hidden */
  1337. public _onPointerOut(target: Control): void {
  1338. if (!this._isEnabled || target === this) {
  1339. return;
  1340. }
  1341. this._enterCount = 0;
  1342. var canNotify: boolean = true;
  1343. if (!target.isAscendant(this)) {
  1344. canNotify = this.onPointerOutObservable.notifyObservers(this, -1, target, this);
  1345. }
  1346. if (canNotify && this.parent != null) { this.parent._onPointerOut(target); }
  1347. }
  1348. /** @hidden */
  1349. public _onPointerDown(target: Control, coordinates: Vector2, pointerId: number, buttonIndex: number): boolean {
  1350. // Prevent pointerout to lose control context.
  1351. // Event redundancy is checked inside the function.
  1352. this._onPointerEnter(this);
  1353. if (this._downCount !== 0) {
  1354. return false;
  1355. }
  1356. this._downCount++;
  1357. this._downPointerIds[pointerId] = true;
  1358. var canNotify: boolean = this.onPointerDownObservable.notifyObservers(new Vector2WithInfo(coordinates, buttonIndex), -1, target, this);
  1359. if (canNotify && this.parent != null) { this.parent._onPointerDown(target, coordinates, pointerId, buttonIndex); }
  1360. return true;
  1361. }
  1362. /** @hidden */
  1363. public _onPointerUp(target: Control, coordinates: Vector2, pointerId: number, buttonIndex: number, notifyClick: boolean): void {
  1364. if (!this._isEnabled) {
  1365. return;
  1366. }
  1367. this._downCount = 0;
  1368. delete this._downPointerIds[pointerId];
  1369. var canNotifyClick: boolean = notifyClick;
  1370. if (notifyClick && (this._enterCount > 0 || this._enterCount === -1)) {
  1371. canNotifyClick = this.onPointerClickObservable.notifyObservers(new Vector2WithInfo(coordinates, buttonIndex), -1, target, this);
  1372. }
  1373. var canNotify: boolean = this.onPointerUpObservable.notifyObservers(new Vector2WithInfo(coordinates, buttonIndex), -1, target, this);
  1374. if (canNotify && this.parent != null) { this.parent._onPointerUp(target, coordinates, pointerId, buttonIndex, canNotifyClick); }
  1375. }
  1376. /** @hidden */
  1377. public _forcePointerUp(pointerId: Nullable<number> = null) {
  1378. if (pointerId !== null) {
  1379. this._onPointerUp(this, Vector2.Zero(), pointerId, 0, true);
  1380. } else {
  1381. for (var key in this._downPointerIds) {
  1382. this._onPointerUp(this, Vector2.Zero(), +key as number, 0, true);
  1383. }
  1384. }
  1385. }
  1386. /** @hidden */
  1387. public _processObservables(type: number, x: number, y: number, pointerId: number, buttonIndex: number): boolean {
  1388. if (!this._isEnabled) {
  1389. return false;
  1390. }
  1391. this._dummyVector2.copyFromFloats(x, y);
  1392. if (type === PointerEventTypes.POINTERMOVE) {
  1393. this._onPointerMove(this, this._dummyVector2);
  1394. var previousControlOver = this._host._lastControlOver[pointerId];
  1395. if (previousControlOver && previousControlOver !== this) {
  1396. previousControlOver._onPointerOut(this);
  1397. }
  1398. if (previousControlOver !== this) {
  1399. this._onPointerEnter(this);
  1400. }
  1401. this._host._lastControlOver[pointerId] = this;
  1402. return true;
  1403. }
  1404. if (type === PointerEventTypes.POINTERDOWN) {
  1405. this._onPointerDown(this, this._dummyVector2, pointerId, buttonIndex);
  1406. this._host._registerLastControlDown(this, pointerId);
  1407. this._host._lastPickedControl = this;
  1408. return true;
  1409. }
  1410. if (type === PointerEventTypes.POINTERUP) {
  1411. if (this._host._lastControlDown[pointerId]) {
  1412. this._host._lastControlDown[pointerId]._onPointerUp(this, this._dummyVector2, pointerId, buttonIndex, true);
  1413. }
  1414. delete this._host._lastControlDown[pointerId];
  1415. return true;
  1416. }
  1417. return false;
  1418. }
  1419. private _prepareFont() {
  1420. if (!this._font && !this._fontSet) {
  1421. return;
  1422. }
  1423. if (this._style) {
  1424. this._font = this._style.fontStyle + " " + this._style.fontWeight + " " + this.fontSizeInPixels + "px " + this._style.fontFamily;
  1425. } else {
  1426. this._font = this._fontStyle + " " + this._fontWeight + " " + this.fontSizeInPixels + "px " + this._fontFamily;
  1427. }
  1428. this._fontOffset = Control._GetFontOffset(this._font);
  1429. }
  1430. /** Releases associated resources */
  1431. public dispose() {
  1432. this.onDirtyObservable.clear();
  1433. this.onBeforeDrawObservable.clear();
  1434. this.onAfterDrawObservable.clear();
  1435. this.onPointerDownObservable.clear();
  1436. this.onPointerEnterObservable.clear();
  1437. this.onPointerMoveObservable.clear();
  1438. this.onPointerOutObservable.clear();
  1439. this.onPointerUpObservable.clear();
  1440. this.onPointerClickObservable.clear();
  1441. if (this._styleObserver && this._style) {
  1442. this._style.onChangedObservable.remove(this._styleObserver);
  1443. this._styleObserver = null;
  1444. }
  1445. if (this.parent) {
  1446. this.parent.removeControl(this);
  1447. this.parent = null;
  1448. }
  1449. if (this._host) {
  1450. var index = this._host._linkedControls.indexOf(this);
  1451. if (index > -1) {
  1452. this.linkWithMesh(null);
  1453. }
  1454. }
  1455. }
  1456. // Statics
  1457. private static _HORIZONTAL_ALIGNMENT_LEFT = 0;
  1458. private static _HORIZONTAL_ALIGNMENT_RIGHT = 1;
  1459. private static _HORIZONTAL_ALIGNMENT_CENTER = 2;
  1460. private static _VERTICAL_ALIGNMENT_TOP = 0;
  1461. private static _VERTICAL_ALIGNMENT_BOTTOM = 1;
  1462. private static _VERTICAL_ALIGNMENT_CENTER = 2;
  1463. /** HORIZONTAL_ALIGNMENT_LEFT */
  1464. public static get HORIZONTAL_ALIGNMENT_LEFT(): number {
  1465. return Control._HORIZONTAL_ALIGNMENT_LEFT;
  1466. }
  1467. /** HORIZONTAL_ALIGNMENT_RIGHT */
  1468. public static get HORIZONTAL_ALIGNMENT_RIGHT(): number {
  1469. return Control._HORIZONTAL_ALIGNMENT_RIGHT;
  1470. }
  1471. /** HORIZONTAL_ALIGNMENT_CENTER */
  1472. public static get HORIZONTAL_ALIGNMENT_CENTER(): number {
  1473. return Control._HORIZONTAL_ALIGNMENT_CENTER;
  1474. }
  1475. /** VERTICAL_ALIGNMENT_TOP */
  1476. public static get VERTICAL_ALIGNMENT_TOP(): number {
  1477. return Control._VERTICAL_ALIGNMENT_TOP;
  1478. }
  1479. /** VERTICAL_ALIGNMENT_BOTTOM */
  1480. public static get VERTICAL_ALIGNMENT_BOTTOM(): number {
  1481. return Control._VERTICAL_ALIGNMENT_BOTTOM;
  1482. }
  1483. /** VERTICAL_ALIGNMENT_CENTER */
  1484. public static get VERTICAL_ALIGNMENT_CENTER(): number {
  1485. return Control._VERTICAL_ALIGNMENT_CENTER;
  1486. }
  1487. private static _FontHeightSizes: { [key: string]: { ascent: number, height: number, descent: number } } = {};
  1488. /** @hidden */
  1489. public static _GetFontOffset(font: string): { ascent: number, height: number, descent: number } {
  1490. if (Control._FontHeightSizes[font]) {
  1491. return Control._FontHeightSizes[font];
  1492. }
  1493. var text = document.createElement("span");
  1494. text.innerHTML = "Hg";
  1495. text.style.font = font;
  1496. var block = document.createElement("div");
  1497. block.style.display = "inline-block";
  1498. block.style.width = "1px";
  1499. block.style.height = "0px";
  1500. block.style.verticalAlign = "bottom";
  1501. var div = document.createElement("div");
  1502. div.appendChild(text);
  1503. div.appendChild(block);
  1504. document.body.appendChild(div);
  1505. var fontAscent = 0;
  1506. var fontHeight = 0;
  1507. try {
  1508. fontHeight = block.getBoundingClientRect().top - text.getBoundingClientRect().top;
  1509. block.style.verticalAlign = "baseline";
  1510. fontAscent = block.getBoundingClientRect().top - text.getBoundingClientRect().top;
  1511. } finally {
  1512. document.body.removeChild(div);
  1513. }
  1514. var result = { ascent: fontAscent, height: fontHeight, descent: fontHeight - fontAscent };
  1515. Control._FontHeightSizes[font] = result;
  1516. return result;
  1517. }
  1518. /**
  1519. * Creates a stack panel that can be used to render headers
  1520. * @param control defines the control to associate with the header
  1521. * @param text defines the text of the header
  1522. * @param size defines the size of the header
  1523. * @param options defines options used to configure the header
  1524. * @returns a new StackPanel
  1525. * @ignore
  1526. * @hidden
  1527. */
  1528. public static AddHeader: (control: Control, text: string, size: string | number, options: { isHorizontal: boolean, controlFirst: boolean }) => any = () => { };
  1529. /** @hidden */
  1530. protected static drawEllipse(x: number, y: number, width: number, height: number, context: CanvasRenderingContext2D): void {
  1531. context.translate(x, y);
  1532. context.scale(width, height);
  1533. context.beginPath();
  1534. context.arc(0, 0, 1, 0, 2 * Math.PI);
  1535. context.closePath();
  1536. context.scale(1 / width, 1 / height);
  1537. context.translate(-x, -y);
  1538. }
  1539. }