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.

192 lines
5.1 KiB

  1. function PosNegColumn(x,starty,endy,blocks,blocksheight,colcols,blockcols,abacus,value,aging,rods){
  2. if (aging === undefined) aging=true;
  3. if (rods === undefined) rods=false;
  4. this.elements = [];
  5. this.colWidthScale = 8/33;
  6. this.blockcols = blockcols;
  7. this.value = value;
  8. this.nullheight = 0;
  9. this.abaheight = 0;
  10. this.drawColumn = function(){
  11. //grey: A0A0A0, stroke 6B6B6B
  12. var width = this.colWidthScale*abacus.blockWidth;
  13. var height = endy-starty;
  14. //console.log(width);
  15. //console.log(height);
  16. //console.log(x);
  17. //console.log(starty);
  18. var rect = new createjs.Shape();
  19. rect.graphics.beginFill(this.blockcols[0]).drawRect(-1*(width/2),0,width,height);
  20. rect.graphics.beginStroke(this.blockcols[1]);
  21. rect.graphics.setStrokeStyle(width/8);
  22. rect.graphics.moveTo(-1*(width/2),0);
  23. rect.graphics.lineTo(-1*(width/2),height);
  24. rect.graphics.moveTo((width/2),0);
  25. rect.graphics.lineTo((width/2),height);
  26. rect.x = x;
  27. rect.y = starty;
  28. abacus.stage.addChild(rect);
  29. }
  30. this.updateY = function(){
  31. var bmargin = (abacus.verticalMargin*abacus.blockHeight);
  32. var start = starty+(bmargin/2);
  33. var incr = abacus.blockHeight+bmargin;
  34. for (var i = 0; i<blocksheight; i++){
  35. if (this.elements[i]!=null){
  36. this.elements[i].updateIndex(i);
  37. this.elements[i].updateY(start);
  38. if (this.elements.indexOf(null)>i){
  39. this.elements[i].updateValue(true,true);
  40. } else if (this.elements.lastIndexOf(null)<i){
  41. this.elements[i].updateValue(true,false);
  42. } else {
  43. this.elements[i].updateValue(false);
  44. }
  45. if (rods==false){
  46. start+=incr;
  47. } else {
  48. start+=this.abaheight;
  49. }
  50. } else {
  51. start+=this.nullheight;
  52. }
  53. }
  54. //console.log(this.howManyInUse());
  55. abacus.updateTextItems();
  56. }
  57. this.initElements = function(){
  58. this.elements = [];
  59. if (rods==false){
  60. this.nullheight = endy-starty-blocks*(abacus.blockHeight+(abacus.verticalMargin*abacus.blockHeight));
  61. this.nullheight=this.nullheight/2;
  62. } else {
  63. this.nullheight = abacus.blockHeight;
  64. }
  65. console.log(endy-starty-this.nullheight);
  66. console.log(blocks);
  67. this.abaheight = (endy-starty-this.nullheight*2)/blocks;
  68. console.log(this.abaheight);
  69. var block = blocksheight-blocks;
  70. this.elements.push(null);
  71. for (var i = 0; i<blocks; i++){
  72. console.log("make bead");
  73. if (rods==false){
  74. var b = new AbacusBead(x,starty,colcols,abacus,this,i,value);
  75. } else {
  76. var b = new AbacusBead(x,starty,colcols,abacus,this,i,value,this.abaheight);
  77. }
  78. b.init();
  79. console.log(b);
  80. this.elements.push(b);
  81. }
  82. this.elements.push(null);
  83. }
  84. this.shuntLeft = function(index){
  85. if (this.elements.lastIndexOf(null,index)!=-1){
  86. console.log("shuntLeft");
  87. var placeindex = this.elements.lastIndexOf(null,index);
  88. var startindex = placeindex+1;
  89. var length = index-startindex+1;
  90. var movearray = this.elements.splice(startindex,length);
  91. //for (var i = 0; i<movearray.length; i++){
  92. // movearray[i].resetAge();
  93. //}
  94. var args = [placeindex, 0].concat(movearray);
  95. Array.prototype.splice.apply(this.elements, args);
  96. this.updateY();
  97. }
  98. }
  99. this.shuntRight = function(index){
  100. if (this.elements.indexOf(null,index)!=-1){
  101. console.log("start");
  102. console.log(this.elements);
  103. var placeindex = this.elements.indexOf(null,index)+1;
  104. var endindex = placeindex-2;
  105. var length = endindex-index+1;
  106. var movearray = this.elements.splice(index,length);
  107. console.log(movearray);
  108. //for (var i = 0; i<movearray.length; i++){
  109. // movearray[i].resetAge();
  110. //}
  111. console.log(this.elements);
  112. var args = [placeindex-length, 0].concat(movearray);
  113. Array.prototype.splice.apply(this.elements, args);
  114. this.updateY();
  115. //console.log(this.elements);
  116. }
  117. }
  118. this.howManyInUse = function(){
  119. var arr = [];
  120. var index = this.elements.indexOf(null);
  121. if (index!=0){
  122. arr.push(this.elements.slice(0,index));
  123. } else {
  124. arr.push([]);
  125. }
  126. var index = this.elements.lastIndexOf(null);
  127. if (index!=this.elements.length-1){
  128. arr.push(this.elements.slice(index+1,this.elements.length));
  129. } else {
  130. arr.push([]);
  131. }
  132. return arr;
  133. }
  134. this.updateAges = function(){
  135. for (var i = 0; i<this.elements.length; i++){
  136. if (this.elements[i]!=null&&aging==true){
  137. this.elements[i].updateAge();
  138. }
  139. }
  140. }
  141. this.restore = function(arr){
  142. var newelements = [];
  143. for (var i = 0; i<this.elements.length; i++){
  144. if (this.elements[i]!=null){
  145. newelements.push(this.elements[i]);
  146. }
  147. }
  148. this.elements = newelements;
  149. for (var i = 0; i<arr.length; i++){
  150. if (arr[i] == null){
  151. this.elements.splice(i, 0, null);
  152. }
  153. }
  154. //console.log(this.elements);
  155. this.updateY();
  156. }
  157. this.restoreAge = function(arr){
  158. if (aging==true){
  159. for (var i = 0; i<this.elements.length; i++){
  160. if (this.elements[i]!=null){
  161. this.elements[i].forceAge(arr[i]);
  162. }
  163. }
  164. }
  165. }
  166. this.save = function(){
  167. var arr = [];
  168. for (var i = 0; i<this.elements.length; i++){
  169. if (this.elements[i]==null){
  170. arr.push(null);
  171. } else {
  172. arr.push(this.elements[i].age);
  173. }
  174. }
  175. return arr;
  176. }
  177. this.init = function(){
  178. this.drawColumn();
  179. this.initElements();
  180. this.updateY();
  181. }
  182. }