control.ts 59 KB

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