NumberText.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**
  2. * Created by juho on 2016-03-30.
  3. */
  4. import * as PIXI from './pixi';
  5. var NumberText = function (fileName, align, space, iconName, iconAlign, commaName) {
  6. this.CENTER = 'center';
  7. this.RIGHT = 'right';
  8. this.LEFT = 'left';
  9. this.value = 0;
  10. this.isChange = false;
  11. this.list = [];
  12. this.tmpList = [];
  13. this.fileName = fileName;
  14. this.align = align;
  15. this.space = space;
  16. this.iconName = iconName;
  17. this.iconAlign = iconAlign;
  18. this.commaName = commaName;
  19. this.icon = null;
  20. PIXI.Container.call(this);
  21. this.container = new PIXI.Container();
  22. this.addChild(this.container);
  23. this.setValue(0);
  24. };
  25. NumberText.constructor = NumberText;
  26. NumberText.prototype = Object.create(PIXI.Container.prototype);
  27. NumberText.prototype.setValue = function (value) {
  28. this.value = value;
  29. this.isChange = true;
  30. };
  31. NumberText.prototype.getNumberSprite = function (num) {
  32. var sprite, name;
  33. name = this.fileName + num + '.png';
  34. if (this.tmpList.length > 0) {
  35. sprite = this.tmpList.shift();
  36. sprite.texture = PIXI.Texture.fromFrame(name);
  37. } else {
  38. sprite = PIXI.Sprite.fromFrame(name);
  39. }
  40. return sprite;
  41. };
  42. NumberText.prototype.getCommaSprite = function () {
  43. var sprite, name = this.commaName + '.png';
  44. if (this.tmpList.length > 0) {
  45. sprite = this.tmpList.shift();
  46. sprite.texture = PIXI.Texture.fromFrame(name);
  47. } else {
  48. sprite = PIXI.Sprite.fromFrame(name);
  49. }
  50. return sprite;
  51. };
  52. NumberText.prototype.removeAll = function () {
  53. var txt, i = this.list.length;
  54. while (i--) {
  55. txt = this.list.shift();
  56. this.tmpList.push(txt);
  57. }
  58. if (this.container) this.container.removeChildren();
  59. };
  60. NumberText.prototype.updateTransform = function () {
  61. if (this.isChange) {
  62. this.removeAll();
  63. var numStr = (this.commaName) ? Util.comma(this.value) : this.value.toString();
  64. var index = 0, txt, str,
  65. space = 0,
  66. i = numStr.length;
  67. if (this.iconName) {
  68. if (!this.icon) this.icon = PIXI.Sprite.fromFrame(this.iconName);
  69. this.container.addChild(this.icon);
  70. if (this.iconAlign == 'left') {
  71. space += this.icon.width + this.space;
  72. }
  73. }
  74. while (i--) {
  75. str = numStr.substr(index, 1);
  76. if (str == '.') {
  77. txt = this.getCommaSprite();
  78. } else {
  79. txt = this.getNumberSprite(parseInt(str) + 1);
  80. }
  81. txt.x = space;
  82. space += txt.width;
  83. if (i > 0) space += this.space;
  84. index++;
  85. this.container.addChild(txt);
  86. this.list[i] = txt;
  87. }
  88. if (this.iconAlign == 'right') {
  89. this.icon.x = space;
  90. space += this.icon.width + this.space;
  91. }
  92. if (this.align == this.CENTER) {
  93. this.container.x = -space / 2;
  94. } else if (this.align == this.RIGHT) {
  95. this.container.x = -space;
  96. }
  97. this.isChange = false;
  98. }
  99. PIXI.Container.prototype.updateTransform.call(this);
  100. };
  101. module.exports = NumberText