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.

212 lines
6.4 KiB

  1. define(['sugar-web/activity/activity', "webL10n", 'activity/Board', 'activity/vanilla-state', 'activity/patterns', 'activity/shadeColor','sugar-web/env'], function (activity, l10n, Board, State, patterns, shadeColor, env) {
  2. requirejs(['domReady!'], function (doc) {
  3. activity.setup();
  4. env.getEnvironment(function(err, environment) {
  5. var defaultLanguage = (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) ? chrome.i18n.getUILanguage() : navigator.language;
  6. var language = environment.user ? environment.user.language : defaultLanguage;
  7. l10n.language.code = language;
  8. window.addEventListener('localized', function () {
  9. activity.getXOColor(function (err, color) {
  10. var dataStore = activity.getDatastoreObject();
  11. main(Board, State, patterns, color, shadeColor, l10n, dataStore);
  12. });
  13. });
  14. });
  15. });
  16. });
  17. function main(Board, State, patterns, color, shadeColor, l10n, dataStore) {
  18. var state = new State({
  19. boardState: [],
  20. generation: 0,
  21. playPauseIcon: 'play',
  22. shouldPlay: false
  23. });
  24. var randomPattern = patterns[0],
  25. gliderPattern = patterns[1],
  26. noPattern = patterns[2],
  27. blankPattern = patterns[3];
  28. var target = document.querySelector('.main canvas');
  29. var board = new Board(state.state.boardState, color.fill, '#FBF6F5', shadeColor(color.stroke, 10), color.stroke, 12, 12, 2, 2, target);
  30. document.querySelector('.generation-count').style.color = color.fill;
  31. document.querySelector('.generation-status').style.color = color.fill;
  32. document.querySelector('.generation-status').innerText = l10n.get('Generation');
  33. board.draw();
  34. var storeLocally = function storeLocally(state) {
  35. dataStore.setDataAsText({
  36. boardState: state.boardState,
  37. generation: state.generation
  38. });
  39. console.log('writing');
  40. dataStore.save(function (err) {
  41. if (err) {
  42. console.log('writing failed.');
  43. console.error(err);
  44. } else {
  45. console.log('writing saved.');
  46. }
  47. });
  48. };
  49. var generateGeneration = function generateGeneration() {
  50. if (state.state.shouldPlay) {
  51. var nextGenerationBoard = state.state.boardState.map(function (row, y) {
  52. return row.map(function (cell, x) {
  53. return transformCellByRule(cell, findNeighbours(x, y));
  54. });
  55. });
  56. state.set(function (prev) {
  57. return {
  58. boardState: nextGenerationBoard,
  59. generation: prev.generation + 1
  60. };
  61. });
  62. setTimeout(generateGeneration, 100);
  63. } else {
  64. return 0;
  65. }
  66. };
  67. var transformCellByRule = function transformCellByRule(cell, neighbours) {
  68. var cellIsAlive = cell === 1 || cell === 2;
  69. var aliveInNeighbour = neighbours.filter(function (cell) {
  70. return cell === 1 || cell === 2;
  71. });
  72. var numOfAliveNeighbours = aliveInNeighbour.length;
  73. if (cellIsAlive) {
  74. if (numOfAliveNeighbours < 2) {
  75. return 0;
  76. } else if (numOfAliveNeighbours === 2 || numOfAliveNeighbours === 3) {
  77. return 1;
  78. } else {
  79. return 0;
  80. }
  81. } else {
  82. if (numOfAliveNeighbours === 3) {
  83. return 2;
  84. } else {
  85. return 0;
  86. }
  87. }
  88. };
  89. var findNeighbours = function findNeighbours(x, y) {
  90. var leftX = x - 1 === -1 ? 49 : x - 1;
  91. var rightX = x + 1 === 50 ? 0 : x + 1;
  92. var topY = y - 1 === -1 ? 29 : y - 1;
  93. var bottomY = y + 1 === 30 ? 0 : y + 1;
  94. var boardState = state.state.boardState;
  95. var left = boardState[y][leftX];
  96. var right = boardState[y][rightX];
  97. var top = boardState[topY][x];
  98. var bottom = boardState[bottomY][x];
  99. var leftTop = boardState[topY][leftX];
  100. var leftBottom = boardState[bottomY][leftX];
  101. var rightTop = boardState[topY][rightX];
  102. var rightBottom = boardState[bottomY][rightX];
  103. return [left, right, top, bottom, leftTop, leftBottom, rightTop, rightBottom];
  104. };
  105. state.subscribe({
  106. boardState: ['.fake-selector', function (fakeElem, value, prevValue) {
  107. board.update(value);
  108. }],
  109. generation: ['.generation-count', 'innerText'],
  110. playPauseIcon: ['#play-pause', function (elem, value, prevValue) {
  111. elem.className = value + ' toolbutton';
  112. }],
  113. shouldPlay: ['.fake-selector', function (fakeElem, value, prevValue) {
  114. if (value) {
  115. generateGeneration();
  116. }
  117. }]
  118. });
  119. dataStore.loadAsText(function (err, metadata, data) {
  120. var boardState = data ? data.boardState : randomPattern();
  121. var generation = data ? data.generation : 0;
  122. state.set({
  123. boardState: boardState,
  124. generation: parseInt(generation)
  125. });
  126. });
  127. board.onClick(function (cellX, cellY) {
  128. state.set(function (prev) {
  129. var newState = [].concat(prev.boardState);
  130. var clickedCell = newState[cellY][cellX]
  131. if (clickedCell === 1 || clickedCell === 2) {
  132. newState[cellY][cellX] = 0;
  133. } else {
  134. newState[cellY][cellX] = 2;
  135. }
  136. return {
  137. boardState: newState
  138. };
  139. });
  140. });
  141. document.querySelector('#play-pause').addEventListener('click', function () {
  142. state.set(function (prev) {
  143. var iconToSet = prev.playPauseIcon === 'play' ? 'pause' : 'play';
  144. var togglePlay = prev.shouldPlay === true ? false : true;
  145. if (prev.shouldPlay) {
  146. storeLocally({
  147. boardState: state.state.boardState,
  148. generation: state.state.generation
  149. });
  150. }
  151. return {
  152. playPauseIcon: iconToSet,
  153. shouldPlay: togglePlay
  154. };
  155. });
  156. });
  157. document.querySelector('#stop-button').addEventListener('click', function() {
  158. storeLocally({
  159. boardState: state.state.boardState,
  160. generation: state.state.generation
  161. });
  162. })
  163. document.querySelector('#random').addEventListener('click', function () {
  164. state.set({
  165. boardState: randomPattern(),
  166. generation: 0
  167. });
  168. });
  169. document.querySelector('#glider').addEventListener('click', function () {
  170. state.set({
  171. boardState: glider(),
  172. generation: 0
  173. });
  174. });
  175. document.querySelector('#no').addEventListener('click', function () {
  176. state.set({
  177. boardState: no(),
  178. generation: 0
  179. });
  180. });
  181. document.querySelector('#clear').addEventListener('click', function () {
  182. state.set({
  183. boardState: blankPattern(),
  184. generation: 0,
  185. playPauseIcon: 'play',
  186. shouldPlay: false
  187. });
  188. });
  189. window.addEventListener('resize', function (e) {
  190. board.handleResize(window.innerWidth, state.state.boardState);
  191. });
  192. board.handleResize(window.innerWidth, state.state.boardState);
  193. }