valueAndUnit.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON.GUI {
  3. /**
  4. * Class used to specific a value and its associated unit
  5. */
  6. export class ValueAndUnit {
  7. private _value = 1;
  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. }
  26. /** Gets a boolean indicating if the value is a percentage */
  27. public get isPercentage(): boolean {
  28. return this.unit === ValueAndUnit.UNITMODE_PERCENTAGE;
  29. }
  30. /** Gets a boolean indicating if the value is store as pixel */
  31. public get isPixel(): boolean {
  32. return this.unit === ValueAndUnit.UNITMODE_PIXEL;
  33. }
  34. /** Gets direct internal value */
  35. public get internalValue(): number {
  36. return this._value;
  37. }
  38. /**
  39. * Gets value as pixel
  40. * @param host defines the root host
  41. * @param refValue defines the reference value for percentages
  42. * @returns the value as pixel
  43. */
  44. public getValueInPixel(host: AdvancedDynamicTexture, refValue: number): number {
  45. if (this.isPixel) {
  46. return this.getValue(host);
  47. }
  48. return this.getValue(host) * refValue;
  49. }
  50. /**
  51. * Gets the value accordingly to its unit
  52. * @param host defines the root host
  53. * @returns the value
  54. */
  55. public getValue(host: AdvancedDynamicTexture): number {
  56. if (host && !this.ignoreAdaptiveScaling && this.unit !== ValueAndUnit.UNITMODE_PERCENTAGE) {
  57. var width: number = 0;
  58. var height: number = 0;
  59. if (host.idealWidth) {
  60. width = (this._value * host.getSize().width) / host.idealWidth;
  61. }
  62. if (host.idealHeight) {
  63. height = (this._value * host.getSize().height) / host.idealHeight;
  64. }
  65. if (host.useSmallestIdeal && host.idealWidth && host.idealHeight) {
  66. return window.innerWidth < window.innerHeight ? width : height;
  67. }
  68. if (host.idealWidth) { // horizontal
  69. return width;
  70. }
  71. if (host.idealHeight) { // vertical
  72. return height;
  73. }
  74. }
  75. return this._value;
  76. }
  77. /**
  78. * Gets a string representation of the value
  79. * @param host defines the root host
  80. * @returns a string
  81. */
  82. public toString(host: AdvancedDynamicTexture): string {
  83. switch (this.unit) {
  84. case ValueAndUnit.UNITMODE_PERCENTAGE:
  85. return (this.getValue(host) * 100) + "%";
  86. case ValueAndUnit.UNITMODE_PIXEL:
  87. return this.getValue(host) + "px";
  88. }
  89. return this.unit.toString();
  90. }
  91. /**
  92. * Store a value parsed from a string
  93. * @param source defines the source string
  94. * @returns true if the value was successfully parsed
  95. */
  96. public fromString(source: string | number ): boolean {
  97. var match = ValueAndUnit._Regex.exec(source.toString());
  98. if (!match || match.length === 0) {
  99. return false;
  100. }
  101. var sourceValue = parseFloat(match[1]);
  102. var sourceUnit = this.unit;
  103. if (!this.negativeValueAllowed) {
  104. if (sourceValue < 0) {
  105. sourceValue = 0;
  106. }
  107. }
  108. if (match.length === 4) {
  109. switch(match[3]) {
  110. case "px":
  111. sourceUnit = ValueAndUnit.UNITMODE_PIXEL;
  112. break;
  113. case "%":
  114. sourceUnit = ValueAndUnit.UNITMODE_PERCENTAGE;
  115. sourceValue /= 100.0;
  116. break;
  117. }
  118. }
  119. if (sourceValue === this._value && sourceUnit === this.unit) {
  120. return false;
  121. }
  122. this._value = sourceValue;
  123. this.unit = sourceUnit;
  124. return true;
  125. }
  126. // Static
  127. private static _Regex = /(^-?\d*(\.\d+)?)(%|px)?/;
  128. private static _UNITMODE_PERCENTAGE = 0;
  129. private static _UNITMODE_PIXEL = 1;
  130. /** UNITMODE_PERCENTAGE */
  131. public static get UNITMODE_PERCENTAGE(): number {
  132. return ValueAndUnit._UNITMODE_PERCENTAGE;
  133. }
  134. /** UNITMODE_PIXEL */
  135. public static get UNITMODE_PIXEL(): number {
  136. return ValueAndUnit._UNITMODE_PIXEL;
  137. }
  138. }
  139. }