valueAndUnit.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /// <reference path="../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON.GUI {
  3. export class ValueAndUnit {
  4. private _value = 1;
  5. public ignoreAdaptiveScaling = false;
  6. public constructor(value: number, public unit = ValueAndUnit.UNITMODE_PIXEL, public negativeValueAllowed = true) {
  7. this._value = value;
  8. }
  9. public get isPercentage(): boolean {
  10. return this.unit === ValueAndUnit.UNITMODE_PERCENTAGE;
  11. }
  12. public get isPixel(): boolean {
  13. return this.unit === ValueAndUnit.UNITMODE_PIXEL;
  14. }
  15. public get internalValue(): number {
  16. return this._value;
  17. }
  18. public getValueInPixel(host: AdvancedDynamicTexture, refValue: number): number {
  19. if (this.isPixel) {
  20. return this.getValue(host);
  21. }
  22. return this.getValue(host) * refValue;
  23. }
  24. public getValue(host: AdvancedDynamicTexture): number {
  25. if (host && !this.ignoreAdaptiveScaling && this.unit !== ValueAndUnit.UNITMODE_PERCENTAGE) {
  26. var width: number = 0;
  27. var height: number = 0;
  28. if (host.idealWidth) {
  29. width = (this._value * host.getSize().width) / host.idealWidth;
  30. }
  31. if (host.idealHeight) {
  32. height = (this._value * host.getSize().height) / host.idealHeight;
  33. }
  34. if (host.useSmallestIdeal && host.idealWidth && host.idealHeight) {
  35. return window.innerWidth < window.innerHeight ? width : height;
  36. }
  37. if (host.idealWidth) { // horizontal
  38. return width;
  39. }
  40. if (host.idealHeight) { // vertical
  41. return height;
  42. }
  43. }
  44. return this._value;
  45. }
  46. public toString(host: AdvancedDynamicTexture): string {
  47. switch (this.unit) {
  48. case ValueAndUnit.UNITMODE_PERCENTAGE:
  49. return (this.getValue(host) * 100) + "%";
  50. case ValueAndUnit.UNITMODE_PIXEL:
  51. return this.getValue(host) + "px";
  52. }
  53. return this.unit.toString();
  54. }
  55. public fromString(source: string | number ): boolean {
  56. var match = ValueAndUnit._Regex.exec(source.toString());
  57. if (!match || match.length === 0) {
  58. return false;
  59. }
  60. var sourceValue = parseFloat(match[1]);
  61. var sourceUnit = this.unit;
  62. if (!this.negativeValueAllowed) {
  63. if (sourceValue < 0) {
  64. sourceValue = 0;
  65. }
  66. }
  67. if (match.length === 4) {
  68. switch(match[3]) {
  69. case "px":
  70. sourceUnit = ValueAndUnit.UNITMODE_PIXEL;
  71. break;
  72. case "%":
  73. sourceUnit = ValueAndUnit.UNITMODE_PERCENTAGE;
  74. sourceValue /= 100.0;
  75. break;
  76. }
  77. }
  78. if (sourceValue === this._value && sourceUnit === this.unit) {
  79. return false;
  80. }
  81. this._value = sourceValue;
  82. this.unit = sourceUnit;
  83. return true;
  84. }
  85. // Static
  86. private static _Regex = /(^-?\d*(\.\d+)?)(%|px)?/;
  87. private static _UNITMODE_PERCENTAGE = 0;
  88. private static _UNITMODE_PIXEL = 1;
  89. public static get UNITMODE_PERCENTAGE(): number {
  90. return ValueAndUnit._UNITMODE_PERCENTAGE;
  91. }
  92. public static get UNITMODE_PIXEL(): number {
  93. return ValueAndUnit._UNITMODE_PIXEL;
  94. }
  95. }
  96. }