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.

122 lines
4.0 KiB

  1. define(["sugar-web/activity/activity","sugar-web/graphics/radiobuttonsgroup","gearsketch_main"], function (activity,radioButtonsGroup) {
  2. // Manipulate the DOM only when it is ready.
  3. requirejs(['domReady!'], function (doc) {
  4. var gearSketch;
  5. // Initialize the activity.
  6. activity.setup();
  7. var stopButton = document.getElementById("stop-button");
  8. stopButton.addEventListener('click', function (event) {
  9. console.log("writing...");
  10. var jsonData = JSON.stringify(gearSketch.board);
  11. activity.getDatastoreObject().setDataAsText(jsonData);
  12. activity.getDatastoreObject().save(function (error) {
  13. if (error === null) {
  14. console.log("write done.");
  15. }
  16. else {
  17. console.log("write failed.");
  18. }
  19. });
  20. });
  21. gearSketch = new window.gearsketch.GearSketch(false);
  22. // Read from the datastore
  23. var datastoreObject = activity.getDatastoreObject();
  24. function onLoaded(error, metadata, jsonData) {
  25. if (error === null) {
  26. gearSketch.board = window.gearsketch.model.Board.
  27. fromObject(JSON.parse(jsonData));
  28. console.log("read done.");
  29. }
  30. else {
  31. console.log("read failed.");
  32. }
  33. }
  34. datastoreObject.loadAsText(onLoaded);
  35. var radioButtons;
  36. var gearButton = document.getElementById("gear-button");
  37. var chainButton = document.getElementById("chain-button");
  38. var momentumButton = document.getElementById("momentum-button");
  39. var playButton = document.getElementById("play-button");
  40. var buttonNames = {
  41. "gearButton": gearButton,
  42. "chainButton": chainButton,
  43. "momentumButton": momentumButton,
  44. "playButton": playButton
  45. };
  46. gearSketch.selectButton = function (buttonName) {
  47. return this.selectedButton = buttonName;
  48. }
  49. // Gear button.
  50. gearButton.addEventListener('click', function (event) {
  51. if (gearSketch.isDemoPlaying) {
  52. gearSketch.stopDemo();
  53. }
  54. gearSketch.selectButton("gearButton");
  55. });
  56. // Chain button.
  57. chainButton.addEventListener('click', function (event) {
  58. if (gearSketch.isDemoPlaying) {
  59. gearSketch.stopDemo();
  60. }
  61. gearSketch.selectButton("chainButton");
  62. });
  63. // Momentum button.
  64. momentumButton.addEventListener('click', function (event) {
  65. if (gearSketch.isDemoPlaying) {
  66. gearSketch.stopDemo();
  67. }
  68. gearSketch.selectButton("momentumButton");
  69. });
  70. // Play button.
  71. playButton.addEventListener('click', function (event) {
  72. if (gearSketch.isDemoPlaying) {
  73. gearSketch.stopDemo();
  74. }
  75. if(gearSketch.selectedButton == "playButton"){
  76. gearSketch.selectButton(null);
  77. }
  78. else{
  79. gearSketch.selectButton("playButton");
  80. }
  81. });
  82. radioButtons = new radioButtonsGroup.RadioButtonsGroup(
  83. [gearButton, chainButton, momentumButton, playButton]);
  84. // Clear button.
  85. var clearButton = document.getElementById("clear-button");
  86. clearButton.addEventListener('click', function (event) {
  87. if (gearSketch.isDemoPlaying) {
  88. gearSketch.showButtons = false;
  89. gearSketch.stopDemo();
  90. return;
  91. }
  92. gearSketch.board.clear();
  93. });
  94. // Help button.
  95. var helpButton = document.getElementById("help-button");
  96. helpButton.addEventListener('click', function (event) {
  97. if (gearSketch.isDemoPlaying) {
  98. gearSketch.stopDemo();
  99. return;
  100. }
  101. gearSketch.playDemo();
  102. });
  103. });
  104. });