valueAndUnit.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import { AdvancedDynamicTexture } from "./advancedDynamicTexture";
  2. /**
  3. * Class used to specific a value and its associated unit
  4. */
  5. export class ValueAndUnit {
  6. private _value = 1;
  7. private _originalUnit: number;
  8. /**
  9. * Gets or sets a value indicating that this value will not scale accordingly with adaptive scaling property
  10. * @see http://doc.babylonjs.com/how_to/gui#adaptive-scaling
  11. */
  12. public ignoreAdaptiveScaling = false;
  13. /**
  14. * Creates a new ValueAndUnit
  15. * @param value defines the value to store
  16. * @param unit defines the unit to store
  17. * @param negativeValueAllowed defines a boolean indicating if the value can be negative
  18. */
  19. public constructor(value: number,
  20. /** defines the unit to store */
  21. public unit = ValueAndUnit.UNITMODE_PIXEL,
  22. /** defines a boolean indicating if the value can be negative */
  23. public negativeValueAllowed = true) {
  24. this._value = value;
  25. this._originalUnit = unit;
  26. }
  27. /** Gets a boolean indicating if the value is a percentage */
  28. public get isPercentage(): boolean {
  29. return this.unit === ValueAndUnit.UNITMODE_PERCENTAGE;
  30. }
  31. /** Gets a boolean indicating if the value is store as pixel */
  32. public get isPixel(): boolean {
  33. return this.unit === ValueAndUnit.UNITMODE_PIXEL;
  34. }
  35. /** Gets direct internal value */
  36. public get internalValue(): number {
  37. return this._value;
  38. }
  39. /**
  40. * Gets value as pixel
  41. * @param host defines the root host
  42. * @param refValue defines the reference value for percentages
  43. * @returns the value as pixel
  44. */
  45. public getValueInPixel(host: AdvancedDynamicTexture, refValue: number): number {
  46. if (this.isPixel) {
  47. return this.getValue(host);
  48. }
  49. return this.getValue(host) * refValue;
  50. }
  51. /**
  52. * Update the current value and unit. This should be done cautiously as the GUi won't be marked as dirty with this function.
  53. * @param value defines the value to store
  54. * @param unit defines the unit to store
  55. * @returns the current ValueAndUnit
  56. */
  57. public updateInPlace(value: number, unit = ValueAndUnit.UNITMODE_PIXEL): ValueAndUnit {
  58. this._value = value;
  59. this.unit = unit;
  60. return this;
  61. }
  62. /**
  63. * Gets the value accordingly to its unit
  64. * @param host defines the root host
  65. * @returns the value
  66. */
  67. public getValue(host: AdvancedDynamicTexture): number {
  68. if (host && !this.ignoreAdaptiveScaling && this.unit !== ValueAndUnit.UNITMODE_PERCENTAGE) {
  69. var width: number = 0;
  70. var height: number = 0;
  71. if (host.idealWidth) {
  72. width = (this._value * host.getSize().width) / host.idealWidth;
  73. }
  74. if (host.idealHeight) {
  75. height = (this._value * host.getSize().height) / host.idealHeight;
  76. }
  77. if (host.useSmallestIdeal && host.idealWidth && host.idealHeight) {
  78. return window.innerWidth < window.innerHeight ? width : height;
  79. }
  80. if (host.idealWidth) { // horizontal
  81. return width;
  82. }
  83. if (host.idealHeight) { // vertical
  84. return height;
  85. }
  86. }
  87. return this._value;
  88. }
  89. /**
  90. * Gets a string representation of the value
  91. * @param host defines the root host
  92. * @returns a string
  93. */
  94. public toString(host: AdvancedDynamicTexture): string {
  95. switch (this.unit) {
  96. case ValueAndUnit.UNITMODE_PERCENTAGE:
  97. return (this.getValue(host) * 100) + "%";
  98. case ValueAndUnit.UNITMODE_PIXEL:
  99. return this.getValue(host) + "px";
  100. }
  101. return this.unit.toString();
  102. }
  103. /**
  104. * Store a value parsed from a string
  105. * @param source defines the source string
  106. * @returns true if the value was successfully parsed
  107. */
  108. public fromString(source: string | number): boolean {
  109. var match = ValueAndUnit._Regex.exec(source.toString());
  110. if (!match || match.length === 0) {
  111. return false;
  112. }
  113. var sourceValue = parseFloat(match[1]);
  114. var sourceUnit = this._originalUnit;
  115. if (!this.negativeValueAllowed) {
  116. if (sourceValue < 0) {
  117. sourceValue = 0;
  118. }
  119. }
  120. if (match.length === 4) {
  121. switch (match[3]) {
  122. case "px":
  123. sourceUnit = ValueAndUnit.UNITMODE_PIXEL;
  124. break;
  125. case "%":
  126. sourceUnit = ValueAndUnit.UNITMODE_PERCENTAGE;
  127. sourceValue /= 100.0;
  128. break;
  129. }
  130. }
  131. if (sourceValue === this._value && sourceUnit === this.unit) {
  132. return false;
  133. }
  134. this._value = sourceValue;
  135. this.unit = sourceUnit;
  136. return true;
  137. }
  138. // Static
  139. private static _Regex = /(^-?\d*(\.\d+)?)(%|px)?/;
  140. private static _UNITMODE_PERCENTAGE = 0;
  141. private static _UNITMODE_PIXEL = 1;
  142. /** UNITMODE_PERCENTAGE */
  143. public static get UNITMODE_PERCENTAGE(): number {
  144. return ValueAndUnit._UNITMODE_PERCENTAGE;
  145. }
  146. /** UNITMODE_PIXEL */
  147. public static get UNITMODE_PIXEL(): number {
  148. return ValueAndUnit._UNITMODE_PIXEL;
  149. }
  150. }