control.ts 63 KB

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