control.ts 62 KB

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