not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
3.8 KiB

  1. function AbacusBead(x,y,blockcol,abacus,column,i,value,beadheight){
  2. if (beadheight === undefined) beadheight=-1;
  3. var Color = net.brehaut.Color;
  4. this.bead = null;
  5. this.originaly = null;
  6. this.index = i;
  7. this.value = value;
  8. this.text = null;
  9. this.containerbead = null;
  10. this.age = 4;
  11. this.y = y;
  12. this.updateY = function(y){
  13. this.containerbead.y = y;
  14. this.y = y;
  15. }
  16. this.resetAge = function(){
  17. this.age = -1;
  18. }
  19. this.forceAge = function(age){
  20. this.age = age-1;
  21. console.log(this.age);
  22. this.updateAge();
  23. }
  24. this.updateAge = function(){
  25. // HACK: On iOS, Android and Safari, remove age handling to avoid black beads (saturation not supported)
  26. var ua = navigator.userAgent.toLowerCase();
  27. if ((ua.indexOf('safari') != -1 && ua.indexOf('chrome') == -1) || /Android/i.test(ua) || ua.match(/ipad|iphone|ipod/g)) {
  28. return;
  29. }
  30. this.age+=1;
  31. if (this.age<=0){
  32. this.redraw("#FFF");
  33. this.age = 0;
  34. } else if (this.age<3){
  35. var col = Color(blockcol);
  36. col = col.setSaturation(col.getSaturation()/3);
  37. col.s = col.s/2;
  38. console.log(col.toRGB());
  39. this.redraw(col.toRGB());
  40. } else if (this.age==3){
  41. this.redraw(blockcol);
  42. }
  43. }
  44. this.redraw = function(colour){
  45. var bh;
  46. if (beadheight==-1){
  47. bh = abacus.blockHeight;
  48. } else {
  49. bh = beadheight;
  50. }
  51. this.bead.graphics.clear().beginStroke("#000000").setStrokeStyle(abacus.blockWidth/25).beginFill(colour).drawRoundRect(-1*abacus.blockWidth/2,0,abacus.blockWidth,bh,abacus.blockHeight/2);
  52. }
  53. this.drawBead = function(colour){
  54. if (colour === undefined) colour=blockcol;
  55. var bh;
  56. if (beadheight==-1){
  57. bh = abacus.blockHeight;
  58. } else {
  59. bh = beadheight;
  60. }
  61. this.bead = new createjs.Shape();
  62. this.bead.graphics.beginStroke("#000000");
  63. this.bead.graphics.setStrokeStyle(abacus.blockWidth/25);
  64. this.bead.graphics.beginFill(colour).drawRoundRect(-1*abacus.blockWidth/2,0,abacus.blockWidth,bh,abacus.blockHeight/2);
  65. this.bead.x = 0;
  66. this.bead.y = 0;
  67. this.containerbead = new createjs.Container();
  68. this.containerbead.addChild(this.bead);
  69. this.containerbead.x = x;
  70. this.containerbead.y = this.y;
  71. abacus.stage.addChild(this.containerbead);
  72. }
  73. this.updateIndex = function(i){
  74. var oldi = this.index;
  75. this.index = i;
  76. if (this.index!=oldi){
  77. this.resetAge();
  78. }
  79. }
  80. this.addClickListeners = function(){
  81. var th = this;
  82. var col = column;
  83. var previous = "down";
  84. this.bead.on("mousedown", function(evt) {
  85. th.originaly = evt.stageY;
  86. });
  87. this.bead.on("pressup", function(evt) {
  88. if (th.originaly<evt.stageY){
  89. col.shuntRight(th.index);
  90. }
  91. else if (th.originaly>evt.stageY){
  92. col.shuntLeft(th.index);
  93. }
  94. });
  95. this.bead.addEventListener("click", function(evt){
  96. if (previous === "down") {
  97. col.shuntLeft(th.index);
  98. previous = "up";
  99. }
  100. else if (previous === "up") {
  101. col.shuntRight(th.index);
  102. previous = "down";
  103. }
  104. });
  105. }
  106. this.updateValue = function(on,positive){
  107. if (positive === undefined) positive=true;
  108. if (on==true&&this.value.toFraction(true).length<=4){
  109. if (positive==false){
  110. this.text.text = "-"+this.value.toFraction(true);
  111. } else {
  112. this.text.text = this.value.toFraction(true);
  113. }
  114. } else {
  115. this.text.text = "";
  116. }
  117. }
  118. this.drawText = function(){
  119. var usecol;
  120. if (blockcol=="#000"||blockcol=="#000000"){
  121. usecol = "#FFF";
  122. } else {
  123. usecol = "#000";
  124. }
  125. var text = new createjs.Text("",(abacus.blockWidth/3).toString()+"px Arial", usecol);
  126. text.set({
  127. textAlign: 'center'
  128. });
  129. text.x = 0;
  130. var bh;
  131. if (beadheight==-1){
  132. bh = abacus.blockHeight/3;
  133. } else {
  134. bh = beadheight/2-abacus.blockHeight/3;
  135. }
  136. text.y = bh;
  137. this.containerbead.addChild(text);
  138. this.text = text;
  139. }
  140. this.init = function(){
  141. this.drawBead();
  142. this.addClickListeners();
  143. this.drawText();
  144. }
  145. }