valueAndUnit.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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, 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. if (host.idealWidth) { // horizontal
  27. return (this._value * host.getSize().width) / host.idealWidth;
  28. }
  29. if (host.idealHeight) { // vertical
  30. return (this._value * host.getSize().height) / host.idealHeight;
  31. }
  32. }
  33. return this._value;
  34. }
  35. public toString(host: AdvancedDynamicTexture): string {
  36. switch (this.unit) {
  37. case ValueAndUnit.UNITMODE_PERCENTAGE:
  38. return (this.getValue(host) * 100) + "%";
  39. case ValueAndUnit.UNITMODE_PIXEL:
  40. return this.getValue(host) + "px";
  41. }
  42. return this.unit.toString();
  43. }
  44. public fromString(source: string | number ): boolean {
  45. var match = ValueAndUnit._Regex.exec(source.toString());
  46. if (!match || match.length === 0) {
  47. return false;
  48. }
  49. var sourceValue = parseFloat(match[1]);
  50. var sourceUnit = this.unit;
  51. if (!this.negativeValueAllowed) {
  52. if (sourceValue < 0) {
  53. sourceValue = 0;
  54. }
  55. }
  56. if (match.length === 4) {
  57. switch(match[3]) {
  58. case "px":
  59. sourceUnit = ValueAndUnit.UNITMODE_PIXEL;
  60. break;
  61. case "%":
  62. sourceUnit = ValueAndUnit.UNITMODE_PERCENTAGE;
  63. sourceValue /= 100.0;
  64. break;
  65. }
  66. }
  67. if (sourceValue === this._value && sourceUnit === this.unit) {
  68. return false;
  69. }
  70. this._value = sourceValue;
  71. this.unit = sourceUnit;
  72. return true;
  73. }
  74. // Static
  75. private static _Regex = /(^-?\d*(\.\d+)?)(%|px)?/;
  76. private static _UNITMODE_PERCENTAGE = 0;
  77. private static _UNITMODE_PIXEL = 1;
  78. public static get UNITMODE_PERCENTAGE(): number {
  79. return ValueAndUnit._UNITMODE_PERCENTAGE;
  80. }
  81. public static get UNITMODE_PIXEL(): number {
  82. return ValueAndUnit._UNITMODE_PIXEL;
  83. }
  84. }
  85. }