control.ts 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971
  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. /** @hidden */
  900. public _getDescendants(results: Control[], directDescendantsOnly: boolean = false, predicate?: (control: Control) => boolean): void {
  901. // Do nothing by default
  902. }
  903. /**
  904. * Will return all controls that have this control as ascendant
  905. * @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
  906. * @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
  907. * @return all child controls
  908. */
  909. public getDescendants(directDescendantsOnly?: boolean, predicate?: (control: Control) => boolean): Control[] {
  910. var results = new Array<Control>();
  911. this._getDescendants(results, directDescendantsOnly, predicate);
  912. return results;
  913. }
  914. /**
  915. * Link current control with a target mesh
  916. * @param mesh defines the mesh to link with
  917. * @see http://doc.babylonjs.com/how_to/gui#tracking-positions
  918. */
  919. public linkWithMesh(mesh: Nullable<AbstractMesh>): void {
  920. if (!this._host || this.parent && this.parent !== this._host._rootContainer) {
  921. if (mesh) {
  922. Tools.Error("Cannot link a control to a mesh if the control is not at root level");
  923. }
  924. return;
  925. }
  926. var index = this._host._linkedControls.indexOf(this);
  927. if (index !== -1) {
  928. this._linkedMesh = mesh;
  929. if (!mesh) {
  930. this._host._linkedControls.splice(index, 1);
  931. }
  932. return;
  933. } else if (!mesh) {
  934. return;
  935. }
  936. this.horizontalAlignment = Control.HORIZONTAL_ALIGNMENT_LEFT;
  937. this.verticalAlignment = Control.VERTICAL_ALIGNMENT_TOP;
  938. this._linkedMesh = mesh;
  939. this._host._linkedControls.push(this);
  940. }
  941. /** @hidden */
  942. public _moveToProjectedPosition(projectedPosition: Vector3): void {
  943. let oldLeft = this._left.getValue(this._host);
  944. let oldTop = this._top.getValue(this._host);
  945. var newLeft = ((projectedPosition.x + this._linkOffsetX.getValue(this._host)) - this._currentMeasure.width / 2);
  946. var newTop = ((projectedPosition.y + this._linkOffsetY.getValue(this._host)) - this._currentMeasure.height / 2);
  947. if (this._left.ignoreAdaptiveScaling && this._top.ignoreAdaptiveScaling) {
  948. if (Math.abs(newLeft - oldLeft) < 0.5) {
  949. newLeft = oldLeft;
  950. }
  951. if (Math.abs(newTop - oldTop) < 0.5) {
  952. newTop = oldTop;
  953. }
  954. }
  955. this.left = newLeft + "px";
  956. this.top = newTop + "px";
  957. this._left.ignoreAdaptiveScaling = true;
  958. this._top.ignoreAdaptiveScaling = true;
  959. this._markAsDirty();
  960. }
  961. /** @hidden */
  962. public _offsetLeft(offset: number) {
  963. this._isDirty = true;
  964. this._currentMeasure.left += offset;
  965. }
  966. /** @hidden */
  967. public _offsetTop(offset: number) {
  968. this._isDirty = true;
  969. this._currentMeasure.top += offset;
  970. }
  971. /** @hidden */
  972. public _markMatrixAsDirty(): void {
  973. this._isMatrixDirty = true;
  974. this._flagDescendantsAsMatrixDirty();
  975. }
  976. /** @hidden */
  977. public _flagDescendantsAsMatrixDirty(): void {
  978. // No child
  979. }
  980. /** @hidden */
  981. public _intersectsRect(rect: Measure) {
  982. // Rotate the control's current measure into local space and check if it intersects the passed in rectangle
  983. this._currentMeasure.transformToRef(this._transformMatrix, this._tmpMeasureA);
  984. if (this._tmpMeasureA.left >= rect.left + rect.width) {
  985. return false;
  986. }
  987. if (this._tmpMeasureA.top >= rect.top + rect.height) {
  988. return false;
  989. }
  990. if (this._tmpMeasureA.left + this._tmpMeasureA.width <= rect.left) {
  991. return false;
  992. }
  993. if (this._tmpMeasureA.top + this._tmpMeasureA.height <= rect.top) {
  994. return false;
  995. }
  996. return true;
  997. }
  998. /** @hidden */
  999. protected invalidateRect() {
  1000. this._transform();
  1001. if (this.host && this.host.useInvalidateRectOptimization) {
  1002. // Rotate by transform to get the measure transformed to global space
  1003. this._currentMeasure.transformToRef(this._transformMatrix, this._tmpMeasureA);
  1004. // get the boudning box of the current measure and last frames measure in global space and invalidate it
  1005. // the previous measure is used to properly clear a control that is scaled down
  1006. Measure.CombineToRef(this._tmpMeasureA, this._prevCurrentMeasureTransformedIntoGlobalSpace, this._tmpMeasureA);
  1007. if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
  1008. // Expand rect based on shadows
  1009. var shadowOffsetX = this.shadowOffsetX;
  1010. var shadowOffsetY = this.shadowOffsetY;
  1011. var shadowBlur = this.shadowBlur;
  1012. var leftShadowOffset = Math.min(Math.min(shadowOffsetX, 0) - shadowBlur * 2, 0);
  1013. var rightShadowOffset = Math.max(Math.max(shadowOffsetX, 0) + shadowBlur * 2, 0);
  1014. var topShadowOffset = Math.min(Math.min(shadowOffsetY, 0) - shadowBlur * 2, 0);
  1015. var bottomShadowOffset = Math.max(Math.max(shadowOffsetY, 0) + shadowBlur * 2, 0);
  1016. this.host.invalidateRect(
  1017. Math.floor(this._tmpMeasureA.left + leftShadowOffset),
  1018. Math.floor(this._tmpMeasureA.top + topShadowOffset),
  1019. Math.ceil(this._tmpMeasureA.left + this._tmpMeasureA.width + rightShadowOffset),
  1020. Math.ceil(this._tmpMeasureA.top + this._tmpMeasureA.height + bottomShadowOffset),
  1021. );
  1022. } else {
  1023. this.host.invalidateRect(
  1024. Math.floor(this._tmpMeasureA.left),
  1025. Math.floor(this._tmpMeasureA.top),
  1026. Math.ceil(this._tmpMeasureA.left + this._tmpMeasureA.width),
  1027. Math.ceil(this._tmpMeasureA.top + this._tmpMeasureA.height),
  1028. );
  1029. }
  1030. }
  1031. }
  1032. /** @hidden */
  1033. public _markAsDirty(force = false): void {
  1034. if (!this._isVisible && !force) {
  1035. return;
  1036. }
  1037. this._isDirty = true;
  1038. // Redraw only this rectangle
  1039. if (this._host) {
  1040. this._host.markAsDirty();
  1041. }
  1042. }
  1043. /** @hidden */
  1044. public _markAllAsDirty(): void {
  1045. this._markAsDirty();
  1046. if (this._font) {
  1047. this._prepareFont();
  1048. }
  1049. }
  1050. /** @hidden */
  1051. public _link(host: AdvancedDynamicTexture): void {
  1052. this._host = host;
  1053. if (this._host) {
  1054. this.uniqueId = this._host.getScene()!.getUniqueId();
  1055. }
  1056. }
  1057. /** @hidden */
  1058. protected _transform(context?: CanvasRenderingContext2D): void {
  1059. if (!this._isMatrixDirty && this._scaleX === 1 && this._scaleY === 1 && this._rotation === 0) {
  1060. return;
  1061. }
  1062. // postTranslate
  1063. var offsetX = this._currentMeasure.width * this._transformCenterX + this._currentMeasure.left;
  1064. var offsetY = this._currentMeasure.height * this._transformCenterY + this._currentMeasure.top;
  1065. if (context) {
  1066. context.translate(offsetX, offsetY);
  1067. // rotate
  1068. context.rotate(this._rotation);
  1069. // scale
  1070. context.scale(this._scaleX, this._scaleY);
  1071. // preTranslate
  1072. context.translate(-offsetX, -offsetY);
  1073. }
  1074. // Need to update matrices?
  1075. if (this._isMatrixDirty || this._cachedOffsetX !== offsetX || this._cachedOffsetY !== offsetY) {
  1076. this._cachedOffsetX = offsetX;
  1077. this._cachedOffsetY = offsetY;
  1078. this._isMatrixDirty = false;
  1079. this._flagDescendantsAsMatrixDirty();
  1080. Matrix2D.ComposeToRef(-offsetX, -offsetY, this._rotation, this._scaleX, this._scaleY, this.parent ? this.parent._transformMatrix : null, this._transformMatrix);
  1081. this._transformMatrix.invertToRef(this._invertTransformMatrix);
  1082. }
  1083. }
  1084. /** @hidden */
  1085. public _renderHighlight(context: CanvasRenderingContext2D): void {
  1086. if (!this.isHighlighted) {
  1087. return;
  1088. }
  1089. context.save();
  1090. context.strokeStyle = "#4affff";
  1091. context.lineWidth = 2;
  1092. this._renderHighlightSpecific(context);
  1093. context.restore();
  1094. }
  1095. /** @hidden */
  1096. public _renderHighlightSpecific(context: CanvasRenderingContext2D): void {
  1097. context.strokeRect(this._currentMeasure.left, this._currentMeasure.top, this._currentMeasure.width, this._currentMeasure.height);
  1098. }
  1099. /** @hidden */
  1100. protected _applyStates(context: CanvasRenderingContext2D): void {
  1101. if (this._isFontSizeInPercentage) {
  1102. this._fontSet = true;
  1103. }
  1104. if (this._fontSet) {
  1105. this._prepareFont();
  1106. this._fontSet = false;
  1107. }
  1108. if (this._font) {
  1109. context.font = this._font;
  1110. }
  1111. if (this._color) {
  1112. context.fillStyle = this._color;
  1113. }
  1114. if (Control.AllowAlphaInheritance) {
  1115. context.globalAlpha *= this._alpha;
  1116. } else if (this._alphaSet) {
  1117. context.globalAlpha = this.parent ? this.parent.alpha * this._alpha : this._alpha;
  1118. }
  1119. }
  1120. /** @hidden */
  1121. public _layout(parentMeasure: Measure, context: CanvasRenderingContext2D): boolean {
  1122. if (!this.isDirty && (!this.isVisible || this.notRenderable)) {
  1123. return false;
  1124. }
  1125. if (this._isDirty || !this._cachedParentMeasure.isEqualsTo(parentMeasure)) {
  1126. this._currentMeasure.transformToRef(this._transformMatrix, this._prevCurrentMeasureTransformedIntoGlobalSpace);
  1127. context.save();
  1128. this._applyStates(context);
  1129. let rebuildCount = 0;
  1130. do {
  1131. this._rebuildLayout = false;
  1132. this._processMeasures(parentMeasure, context);
  1133. rebuildCount++;
  1134. }
  1135. while (this._rebuildLayout && rebuildCount < 3);
  1136. if (rebuildCount >= 3) {
  1137. Logger.Error(`Layout cycle detected in GUI (Control name=${this.name}, uniqueId=${this.uniqueId})`);
  1138. }
  1139. context.restore();
  1140. this.invalidateRect();
  1141. this._evaluateClippingState(parentMeasure);
  1142. }
  1143. this._wasDirty = this._isDirty;
  1144. this._isDirty = false;
  1145. return true;
  1146. }
  1147. /** @hidden */
  1148. protected _processMeasures(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  1149. this._currentMeasure.copyFrom(parentMeasure);
  1150. // Let children take some pre-measurement actions
  1151. this._preMeasure(parentMeasure, context);
  1152. this._measure();
  1153. this._computeAlignment(parentMeasure, context);
  1154. // Convert to int values
  1155. this._currentMeasure.left = this._currentMeasure.left | 0;
  1156. this._currentMeasure.top = this._currentMeasure.top | 0;
  1157. this._currentMeasure.width = this._currentMeasure.width | 0;
  1158. this._currentMeasure.height = this._currentMeasure.height | 0;
  1159. // Let children add more features
  1160. this._additionalProcessing(parentMeasure, context);
  1161. this._cachedParentMeasure.copyFrom(parentMeasure);
  1162. if (this.onDirtyObservable.hasObservers()) {
  1163. this.onDirtyObservable.notifyObservers(this);
  1164. }
  1165. }
  1166. protected _evaluateClippingState(parentMeasure: Measure) {
  1167. if (this.parent && this.parent.clipChildren) {
  1168. // Early clip
  1169. if (this._currentMeasure.left > parentMeasure.left + parentMeasure.width) {
  1170. this._isClipped = true;
  1171. return;
  1172. }
  1173. if (this._currentMeasure.left + this._currentMeasure.width < parentMeasure.left) {
  1174. this._isClipped = true;
  1175. return;
  1176. }
  1177. if (this._currentMeasure.top > parentMeasure.top + parentMeasure.height) {
  1178. this._isClipped = true;
  1179. return;
  1180. }
  1181. if (this._currentMeasure.top + this._currentMeasure.height < parentMeasure.top) {
  1182. this._isClipped = true;
  1183. return;
  1184. }
  1185. }
  1186. this._isClipped = false;
  1187. }
  1188. /** @hidden */
  1189. public _measure(): void {
  1190. // Width / Height
  1191. if (this._width.isPixel) {
  1192. this._currentMeasure.width = this._width.getValue(this._host);
  1193. } else {
  1194. this._currentMeasure.width *= this._width.getValue(this._host);
  1195. }
  1196. if (this._height.isPixel) {
  1197. this._currentMeasure.height = this._height.getValue(this._host);
  1198. } else {
  1199. this._currentMeasure.height *= this._height.getValue(this._host);
  1200. }
  1201. }
  1202. /** @hidden */
  1203. protected _computeAlignment(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  1204. var width = this._currentMeasure.width;
  1205. var height = this._currentMeasure.height;
  1206. var parentWidth = parentMeasure.width;
  1207. var parentHeight = parentMeasure.height;
  1208. // Left / top
  1209. var x = 0;
  1210. var y = 0;
  1211. switch (this.horizontalAlignment) {
  1212. case Control.HORIZONTAL_ALIGNMENT_LEFT:
  1213. x = 0;
  1214. break;
  1215. case Control.HORIZONTAL_ALIGNMENT_RIGHT:
  1216. x = parentWidth - width;
  1217. break;
  1218. case Control.HORIZONTAL_ALIGNMENT_CENTER:
  1219. x = (parentWidth - width) / 2;
  1220. break;
  1221. }
  1222. switch (this.verticalAlignment) {
  1223. case Control.VERTICAL_ALIGNMENT_TOP:
  1224. y = 0;
  1225. break;
  1226. case Control.VERTICAL_ALIGNMENT_BOTTOM:
  1227. y = parentHeight - height;
  1228. break;
  1229. case Control.VERTICAL_ALIGNMENT_CENTER:
  1230. y = (parentHeight - height) / 2;
  1231. break;
  1232. }
  1233. if (this._paddingLeft.isPixel) {
  1234. this._currentMeasure.left += this._paddingLeft.getValue(this._host);
  1235. this._currentMeasure.width -= this._paddingLeft.getValue(this._host);
  1236. } else {
  1237. this._currentMeasure.left += parentWidth * this._paddingLeft.getValue(this._host);
  1238. this._currentMeasure.width -= parentWidth * this._paddingLeft.getValue(this._host);
  1239. }
  1240. if (this._paddingRight.isPixel) {
  1241. this._currentMeasure.width -= this._paddingRight.getValue(this._host);
  1242. } else {
  1243. this._currentMeasure.width -= parentWidth * this._paddingRight.getValue(this._host);
  1244. }
  1245. if (this._paddingTop.isPixel) {
  1246. this._currentMeasure.top += this._paddingTop.getValue(this._host);
  1247. this._currentMeasure.height -= this._paddingTop.getValue(this._host);
  1248. } else {
  1249. this._currentMeasure.top += parentHeight * this._paddingTop.getValue(this._host);
  1250. this._currentMeasure.height -= parentHeight * this._paddingTop.getValue(this._host);
  1251. }
  1252. if (this._paddingBottom.isPixel) {
  1253. this._currentMeasure.height -= this._paddingBottom.getValue(this._host);
  1254. } else {
  1255. this._currentMeasure.height -= parentHeight * this._paddingBottom.getValue(this._host);
  1256. }
  1257. if (this._left.isPixel) {
  1258. this._currentMeasure.left += this._left.getValue(this._host);
  1259. } else {
  1260. this._currentMeasure.left += parentWidth * this._left.getValue(this._host);
  1261. }
  1262. if (this._top.isPixel) {
  1263. this._currentMeasure.top += this._top.getValue(this._host);
  1264. } else {
  1265. this._currentMeasure.top += parentHeight * this._top.getValue(this._host);
  1266. }
  1267. this._currentMeasure.left += x;
  1268. this._currentMeasure.top += y;
  1269. }
  1270. /** @hidden */
  1271. protected _preMeasure(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  1272. // Do nothing
  1273. }
  1274. /** @hidden */
  1275. protected _additionalProcessing(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  1276. // Do nothing
  1277. }
  1278. /** @hidden */
  1279. protected _clipForChildren(context: CanvasRenderingContext2D): void {
  1280. // DO nothing
  1281. }
  1282. private static _ClipMeasure = new Measure(0, 0, 0, 0);
  1283. private _tmpMeasureA = new Measure(0, 0, 0, 0);
  1284. private _clip(context: CanvasRenderingContext2D, invalidatedRectangle?: Nullable<Measure>) {
  1285. context.beginPath();
  1286. Control._ClipMeasure.copyFrom(this._currentMeasure);
  1287. if (invalidatedRectangle) {
  1288. // Rotate the invalidated rect into the control's space
  1289. invalidatedRectangle.transformToRef(this._invertTransformMatrix, this._tmpMeasureA);
  1290. // Get the intersection of the rect in context space and the current context
  1291. var intersection = new Measure(0, 0, 0, 0);
  1292. intersection.left = Math.max(this._tmpMeasureA.left, this._currentMeasure.left);
  1293. intersection.top = Math.max(this._tmpMeasureA.top, this._currentMeasure.top);
  1294. intersection.width = Math.min(this._tmpMeasureA.left + this._tmpMeasureA.width, this._currentMeasure.left + this._currentMeasure.width) - intersection.left;
  1295. intersection.height = Math.min(this._tmpMeasureA.top + this._tmpMeasureA.height, this._currentMeasure.top + this._currentMeasure.height) - intersection.top;
  1296. Control._ClipMeasure.copyFrom(intersection);
  1297. }
  1298. if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
  1299. var shadowOffsetX = this.shadowOffsetX;
  1300. var shadowOffsetY = this.shadowOffsetY;
  1301. var shadowBlur = this.shadowBlur;
  1302. var leftShadowOffset = Math.min(Math.min(shadowOffsetX, 0) - shadowBlur * 2, 0);
  1303. var rightShadowOffset = Math.max(Math.max(shadowOffsetX, 0) + shadowBlur * 2, 0);
  1304. var topShadowOffset = Math.min(Math.min(shadowOffsetY, 0) - shadowBlur * 2, 0);
  1305. var bottomShadowOffset = Math.max(Math.max(shadowOffsetY, 0) + shadowBlur * 2, 0);
  1306. context.rect(
  1307. Control._ClipMeasure.left + leftShadowOffset,
  1308. Control._ClipMeasure.top + topShadowOffset,
  1309. Control._ClipMeasure.width + rightShadowOffset - leftShadowOffset,
  1310. Control._ClipMeasure.height + bottomShadowOffset - topShadowOffset
  1311. );
  1312. } else {
  1313. context.rect(Control._ClipMeasure.left, Control._ClipMeasure.top, Control._ClipMeasure.width, Control._ClipMeasure.height);
  1314. }
  1315. context.clip();
  1316. }
  1317. /** @hidden */
  1318. public _render(context: CanvasRenderingContext2D, invalidatedRectangle?: Nullable<Measure>): boolean {
  1319. if (!this.isVisible || this.notRenderable || this._isClipped) {
  1320. this._isDirty = false;
  1321. return false;
  1322. }
  1323. context.save();
  1324. this._applyStates(context);
  1325. // Transform
  1326. this._transform(context);
  1327. // Clip
  1328. if (this.clipContent) {
  1329. this._clip(context, invalidatedRectangle);
  1330. }
  1331. if (this.onBeforeDrawObservable.hasObservers()) {
  1332. this.onBeforeDrawObservable.notifyObservers(this);
  1333. }
  1334. if (this.useBitmapCache && !this._wasDirty && this._cacheData) {
  1335. context.putImageData(this._cacheData, this._currentMeasure.left, this._currentMeasure.top);
  1336. } else {
  1337. this._draw(context, invalidatedRectangle);
  1338. }
  1339. if (this.useBitmapCache && this._wasDirty) {
  1340. this._cacheData = context.getImageData(this._currentMeasure.left, this._currentMeasure.top, this._currentMeasure.width, this._currentMeasure.height);
  1341. }
  1342. this._renderHighlight(context);
  1343. if (this.onAfterDrawObservable.hasObservers()) {
  1344. this.onAfterDrawObservable.notifyObservers(this);
  1345. }
  1346. context.restore();
  1347. return true;
  1348. }
  1349. /** @hidden */
  1350. public _draw(context: CanvasRenderingContext2D, invalidatedRectangle?: Nullable<Measure>): void {
  1351. // Do nothing
  1352. }
  1353. /**
  1354. * Tests if a given coordinates belong to the current control
  1355. * @param x defines x coordinate to test
  1356. * @param y defines y coordinate to test
  1357. * @returns true if the coordinates are inside the control
  1358. */
  1359. public contains(x: number, y: number): boolean {
  1360. // Invert transform
  1361. this._invertTransformMatrix.transformCoordinates(x, y, this._transformedPosition);
  1362. x = this._transformedPosition.x;
  1363. y = this._transformedPosition.y;
  1364. // Check
  1365. if (x < this._currentMeasure.left) {
  1366. return false;
  1367. }
  1368. if (x > this._currentMeasure.left + this._currentMeasure.width) {
  1369. return false;
  1370. }
  1371. if (y < this._currentMeasure.top) {
  1372. return false;
  1373. }
  1374. if (y > this._currentMeasure.top + this._currentMeasure.height) {
  1375. return false;
  1376. }
  1377. if (this.isPointerBlocker) {
  1378. this._host._shouldBlockPointer = true;
  1379. }
  1380. return true;
  1381. }
  1382. /** @hidden */
  1383. public _processPicking(x: number, y: number, type: number, pointerId: number, buttonIndex: number): boolean {
  1384. if (!this._isEnabled) {
  1385. return false;
  1386. }
  1387. if (!this.isHitTestVisible || !this.isVisible || this._doNotRender) {
  1388. return false;
  1389. }
  1390. if (!this.contains(x, y)) {
  1391. return false;
  1392. }
  1393. this._processObservables(type, x, y, pointerId, buttonIndex);
  1394. return true;
  1395. }
  1396. /** @hidden */
  1397. public _onPointerMove(target: Control, coordinates: Vector2, pointerId: number): void {
  1398. var canNotify: boolean = this.onPointerMoveObservable.notifyObservers(coordinates, -1, target, this);
  1399. if (canNotify && this.parent != null) { this.parent._onPointerMove(target, coordinates, pointerId); }
  1400. }
  1401. /** @hidden */
  1402. public _onPointerEnter(target: Control): boolean {
  1403. if (!this._isEnabled) {
  1404. return false;
  1405. }
  1406. if (this._enterCount > 0) {
  1407. return false;
  1408. }
  1409. if (this._enterCount === -1) { // -1 is for touch input, we are now sure we are with a mouse or pencil
  1410. this._enterCount = 0;
  1411. }
  1412. this._enterCount++;
  1413. var canNotify: boolean = this.onPointerEnterObservable.notifyObservers(this, -1, target, this);
  1414. if (canNotify && this.parent != null) { this.parent._onPointerEnter(target); }
  1415. return true;
  1416. }
  1417. /** @hidden */
  1418. public _onPointerOut(target: Control, force = false): void {
  1419. if (!force && (!this._isEnabled || target === this)) {
  1420. return;
  1421. }
  1422. this._enterCount = 0;
  1423. var canNotify: boolean = true;
  1424. if (!target.isAscendant(this)) {
  1425. canNotify = this.onPointerOutObservable.notifyObservers(this, -1, target, this);
  1426. }
  1427. if (canNotify && this.parent != null) {
  1428. this.parent._onPointerOut(target, force);
  1429. }
  1430. }
  1431. /** @hidden */
  1432. public _onPointerDown(target: Control, coordinates: Vector2, pointerId: number, buttonIndex: number): boolean {
  1433. // Prevent pointerout to lose control context.
  1434. // Event redundancy is checked inside the function.
  1435. this._onPointerEnter(this);
  1436. if (this._downCount !== 0) {
  1437. return false;
  1438. }
  1439. this._downCount++;
  1440. this._downPointerIds[pointerId] = true;
  1441. var canNotify: boolean = this.onPointerDownObservable.notifyObservers(new Vector2WithInfo(coordinates, buttonIndex), -1, target, this);
  1442. if (canNotify && this.parent != null) { this.parent._onPointerDown(target, coordinates, pointerId, buttonIndex); }
  1443. return true;
  1444. }
  1445. /** @hidden */
  1446. public _onPointerUp(target: Control, coordinates: Vector2, pointerId: number, buttonIndex: number, notifyClick: boolean): void {
  1447. if (!this._isEnabled) {
  1448. return;
  1449. }
  1450. this._downCount = 0;
  1451. delete this._downPointerIds[pointerId];
  1452. var canNotifyClick: boolean = notifyClick;
  1453. if (notifyClick && (this._enterCount > 0 || this._enterCount === -1)) {
  1454. canNotifyClick = this.onPointerClickObservable.notifyObservers(new Vector2WithInfo(coordinates, buttonIndex), -1, target, this);
  1455. }
  1456. var canNotify: boolean = this.onPointerUpObservable.notifyObservers(new Vector2WithInfo(coordinates, buttonIndex), -1, target, this);
  1457. if (canNotify && this.parent != null) { this.parent._onPointerUp(target, coordinates, pointerId, buttonIndex, canNotifyClick); }
  1458. }
  1459. /** @hidden */
  1460. public _forcePointerUp(pointerId: Nullable<number> = null) {
  1461. if (pointerId !== null) {
  1462. this._onPointerUp(this, Vector2.Zero(), pointerId, 0, true);
  1463. } else {
  1464. for (var key in this._downPointerIds) {
  1465. this._onPointerUp(this, Vector2.Zero(), +key as number, 0, true);
  1466. }
  1467. }
  1468. }
  1469. /** @hidden */
  1470. public _processObservables(type: number, x: number, y: number, pointerId: number, buttonIndex: number): boolean {
  1471. if (!this._isEnabled) {
  1472. return false;
  1473. }
  1474. this._dummyVector2.copyFromFloats(x, y);
  1475. if (type === PointerEventTypes.POINTERMOVE) {
  1476. this._onPointerMove(this, this._dummyVector2, pointerId);
  1477. var previousControlOver = this._host._lastControlOver[pointerId];
  1478. if (previousControlOver && previousControlOver !== this) {
  1479. previousControlOver._onPointerOut(this);
  1480. }
  1481. if (previousControlOver !== this) {
  1482. this._onPointerEnter(this);
  1483. }
  1484. this._host._lastControlOver[pointerId] = this;
  1485. return true;
  1486. }
  1487. if (type === PointerEventTypes.POINTERDOWN) {
  1488. this._onPointerDown(this, this._dummyVector2, pointerId, buttonIndex);
  1489. this._host._registerLastControlDown(this, pointerId);
  1490. this._host._lastPickedControl = this;
  1491. return true;
  1492. }
  1493. if (type === PointerEventTypes.POINTERUP) {
  1494. if (this._host._lastControlDown[pointerId]) {
  1495. this._host._lastControlDown[pointerId]._onPointerUp(this, this._dummyVector2, pointerId, buttonIndex, true);
  1496. }
  1497. delete this._host._lastControlDown[pointerId];
  1498. return true;
  1499. }
  1500. return false;
  1501. }
  1502. private _prepareFont() {
  1503. if (!this._font && !this._fontSet) {
  1504. return;
  1505. }
  1506. if (this._style) {
  1507. this._font = this._style.fontStyle + " " + this._style.fontWeight + " " + this.fontSizeInPixels + "px " + this._style.fontFamily;
  1508. } else {
  1509. this._font = this._fontStyle + " " + this._fontWeight + " " + this.fontSizeInPixels + "px " + this._fontFamily;
  1510. }
  1511. this._fontOffset = Control._GetFontOffset(this._font);
  1512. }
  1513. /** Releases associated resources */
  1514. public dispose() {
  1515. this.onDirtyObservable.clear();
  1516. this.onBeforeDrawObservable.clear();
  1517. this.onAfterDrawObservable.clear();
  1518. this.onPointerDownObservable.clear();
  1519. this.onPointerEnterObservable.clear();
  1520. this.onPointerMoveObservable.clear();
  1521. this.onPointerOutObservable.clear();
  1522. this.onPointerUpObservable.clear();
  1523. this.onPointerClickObservable.clear();
  1524. if (this._styleObserver && this._style) {
  1525. this._style.onChangedObservable.remove(this._styleObserver);
  1526. this._styleObserver = null;
  1527. }
  1528. if (this.parent) {
  1529. this.parent.removeControl(this);
  1530. this.parent = null;
  1531. }
  1532. if (this._host) {
  1533. var index = this._host._linkedControls.indexOf(this);
  1534. if (index > -1) {
  1535. this.linkWithMesh(null);
  1536. }
  1537. }
  1538. }
  1539. // Statics
  1540. private static _HORIZONTAL_ALIGNMENT_LEFT = 0;
  1541. private static _HORIZONTAL_ALIGNMENT_RIGHT = 1;
  1542. private static _HORIZONTAL_ALIGNMENT_CENTER = 2;
  1543. private static _VERTICAL_ALIGNMENT_TOP = 0;
  1544. private static _VERTICAL_ALIGNMENT_BOTTOM = 1;
  1545. private static _VERTICAL_ALIGNMENT_CENTER = 2;
  1546. /** HORIZONTAL_ALIGNMENT_LEFT */
  1547. public static get HORIZONTAL_ALIGNMENT_LEFT(): number {
  1548. return Control._HORIZONTAL_ALIGNMENT_LEFT;
  1549. }
  1550. /** HORIZONTAL_ALIGNMENT_RIGHT */
  1551. public static get HORIZONTAL_ALIGNMENT_RIGHT(): number {
  1552. return Control._HORIZONTAL_ALIGNMENT_RIGHT;
  1553. }
  1554. /** HORIZONTAL_ALIGNMENT_CENTER */
  1555. public static get HORIZONTAL_ALIGNMENT_CENTER(): number {
  1556. return Control._HORIZONTAL_ALIGNMENT_CENTER;
  1557. }
  1558. /** VERTICAL_ALIGNMENT_TOP */
  1559. public static get VERTICAL_ALIGNMENT_TOP(): number {
  1560. return Control._VERTICAL_ALIGNMENT_TOP;
  1561. }
  1562. /** VERTICAL_ALIGNMENT_BOTTOM */
  1563. public static get VERTICAL_ALIGNMENT_BOTTOM(): number {
  1564. return Control._VERTICAL_ALIGNMENT_BOTTOM;
  1565. }
  1566. /** VERTICAL_ALIGNMENT_CENTER */
  1567. public static get VERTICAL_ALIGNMENT_CENTER(): number {
  1568. return Control._VERTICAL_ALIGNMENT_CENTER;
  1569. }
  1570. private static _FontHeightSizes: { [key: string]: { ascent: number, height: number, descent: number } } = {};
  1571. /** @hidden */
  1572. public static _GetFontOffset(font: string): { ascent: number, height: number, descent: number } {
  1573. if (Control._FontHeightSizes[font]) {
  1574. return Control._FontHeightSizes[font];
  1575. }
  1576. var text = document.createElement("span");
  1577. text.innerHTML = "Hg";
  1578. text.style.font = font;
  1579. var block = document.createElement("div");
  1580. block.style.display = "inline-block";
  1581. block.style.width = "1px";
  1582. block.style.height = "0px";
  1583. block.style.verticalAlign = "bottom";
  1584. var div = document.createElement("div");
  1585. div.appendChild(text);
  1586. div.appendChild(block);
  1587. document.body.appendChild(div);
  1588. var fontAscent = 0;
  1589. var fontHeight = 0;
  1590. try {
  1591. fontHeight = block.getBoundingClientRect().top - text.getBoundingClientRect().top;
  1592. block.style.verticalAlign = "baseline";
  1593. fontAscent = block.getBoundingClientRect().top - text.getBoundingClientRect().top;
  1594. } finally {
  1595. document.body.removeChild(div);
  1596. }
  1597. var result = { ascent: fontAscent, height: fontHeight, descent: fontHeight - fontAscent };
  1598. Control._FontHeightSizes[font] = result;
  1599. return result;
  1600. }
  1601. /**
  1602. * Creates a stack panel that can be used to render headers
  1603. * @param control defines the control to associate with the header
  1604. * @param text defines the text of the header
  1605. * @param size defines the size of the header
  1606. * @param options defines options used to configure the header
  1607. * @returns a new StackPanel
  1608. * @ignore
  1609. * @hidden
  1610. */
  1611. public static AddHeader: (control: Control, text: string, size: string | number, options: { isHorizontal: boolean, controlFirst: boolean }) => any = () => { };
  1612. /** @hidden */
  1613. protected static drawEllipse(x: number, y: number, width: number, height: number, context: CanvasRenderingContext2D): void {
  1614. context.translate(x, y);
  1615. context.scale(width, height);
  1616. context.beginPath();
  1617. context.arc(0, 0, 1, 0, 2 * Math.PI);
  1618. context.closePath();
  1619. context.scale(1 / width, 1 / height);
  1620. context.translate(-x, -y);
  1621. }
  1622. }
  1623. _TypeStore.RegisteredTypes["BABYLON.GUI.Control"] = Control;