control.ts 62 KB

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