UI.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. "atomic component";
  2. var SFXR = require("SFXR");
  3. var component = function (self) {
  4. self.buttons = [];
  5. self.workQueue = [];
  6. self.sounds = [];
  7. self.createButton = function(name) {
  8. var button = new Atomic.UIButton();
  9. button.fontId = "Vera";
  10. button.fontSize = 30;
  11. button.text = name;
  12. button.gravity = Atomic.UI_GRAVITY.UI_GRAVITY_LEFT_RIGHT;
  13. button.layoutWidth = 350;
  14. button.layoutHeight = 50;
  15. button.opacity = 1.0;
  16. return button;
  17. }
  18. self.startGeneratingAtomicSound = function(name, doneCB, progressCB) {
  19. // Guard callbacks
  20. if (!doneCB) {
  21. doneCB = function() {};
  22. }
  23. if (!progressCB) {
  24. progressCB = function() {};
  25. }
  26. // Create the generator
  27. var sfxr = new SFXR();
  28. // Load the sound
  29. // (Because this is a resource file, we assume that this can't fail.)
  30. var settings = Atomic.cache.getFile("Sounds/" + name + ".sfs").readBinary();
  31. sfxr.LoadSettings(settings);
  32. sfxr.PlaySample();
  33. var numSamples = sfxr.NumSamples();
  34. var samplesGenerated = 0;
  35. // Create the buffer
  36. var buf = new Int16Array(numSamples);
  37. var update = function(timestep) {
  38. // Generate some of the samples (only a few, so that we don't go over 1 frame time).
  39. var numSamplesToGenerateThisFrame = 0x100;
  40. if ((numSamplesToGenerateThisFrame + samplesGenerated) > numSamples) {
  41. numSamplesToGenerateThisFrame = numSamples - samplesGenerated;
  42. }
  43. sfxr.SynthSamples(buf.buffer, numSamplesToGenerateThisFrame, samplesGenerated * 2);
  44. samplesGenerated += numSamplesToGenerateThisFrame;
  45. if (samplesGenerated === numSamples) {
  46. // Create the sound
  47. var sound = new Atomic.Sound();
  48. sound.setFormat(sfxr.wav_freq, true, false); // 16-bit Mono
  49. sound.setData(buf);
  50. progressCB(1.0);
  51. doneCB(sound);
  52. return false;
  53. } else {
  54. progressCB(samplesGenerated / numSamples);
  55. return true;
  56. }
  57. }
  58. return update;
  59. }
  60. self.start = function() {
  61. // SFXR filenames (assumed to be in resources)
  62. self.soundSettingFilenames = [
  63. "Coin",
  64. "Implosion",
  65. "Deflect"
  66. ];
  67. // Create a SoundSource for playing the sounds.
  68. self.soundSource = self.node.createComponent("SoundSource");
  69. self.soundSource.soundType = Atomic.SOUND_EFFECT;
  70. self.soundSource.gain = 1.0;
  71. // Get the view and layout setup.
  72. self.uiView = new Atomic.UIView();
  73. self.uiLayout = new Atomic.UILayout();
  74. self.uiLayout.rect = self.uiView.rect;
  75. self.uiLayout.axis = Atomic.UI_AXIS.UI_AXIS_Y;
  76. self.uiLayout.layoutPosition = Atomic.UI_LAYOUT_POSITION.UI_LAYOUT_POSITION_GRAVITY;
  77. self.uiView.addChild(self.uiLayout);
  78. self.soundSettingFilenames.forEach(function(name) {
  79. console.log("Loading \"" + name + "\"");
  80. var button = self.createButton(name);
  81. button.opacity = 0.1;
  82. self.uiLayout.addChild(button);
  83. self.buttons.push(button);
  84. var oneFrameOfWork = self.startGeneratingAtomicSound(name, function(sound) {
  85. // Make the button fully opaque.
  86. button.opacity = 1.0;
  87. button.text = name;
  88. console.log("Done loading \"" + name + "\"");
  89. // Make the button play the sound when it is clicked.
  90. button.onClick = function() {
  91. console.log("Playing \"" + name + "\"");
  92. self.soundSource.play(sound);
  93. };
  94. }, function(progress) {
  95. // Make the button as opaque as the progress.
  96. button.opacity = progress;
  97. button.text = name + " " + Math.floor(progress * 100) + "%";
  98. });
  99. self.workQueue.push(oneFrameOfWork);
  100. });
  101. }
  102. self.update = function(timeStep) {
  103. if (self.workQueue.length !== 0) {
  104. var more = self.workQueue[0](timeStep);
  105. if (!more) {
  106. self.workQueue.shift();
  107. }
  108. }
  109. }
  110. }
  111. exports.component = component;