Source: Menu.js

/*jslint plusplus: true, white: true, indent: 2, maxlen: 90 */
/**
 *  @author    Wolfgang Kowarschick
 *  @copyright 2013, Wolfgang Kowarschick
 *  <br/>
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted under the terms of the 
 *  Creative Commons License Attribution-NonCommercial-ShareAlike 3.0 Unported    
 *  (CC BY-NC-SA 3.0: http://creativecommons.org/licenses/by-nc-sa/3.0/). 
 */

// Define package $app$.game.snake, if that has not already be done.
(function($app$, document)
{ "use strict";
////////////////////////////////////////////////////////////////////////////////
// Class "Menu"
////////////////////////////////////////////////////////////////////////////////

//import
var Controller = $app$.game.snake.Controller;

/** 
 *  @class
 *  @name $app$.game.snake.Menu
 *  
 *  @param {Object} p_init 
 *         Should contain the attributes
 *         <code>buttonStartID</code> (the id of the start button element),
 *         <code>buttonPauseID</code> (the id of the pause button element), and
 *         <code>buttonStopID</code>  (the id of the stop button element).
 *  @param {$app$.game.snake.Controller} p_controller 
 *         The controller of the app. 
 */
function Menu(p_init, p_controller)
{ var l_button_start = document.getElementById(p_init.buttonStartID),
      l_button_pause = document.getElementById(p_init.buttonPauseID),
      l_button_stop  = document.getElementById(p_init.buttonStopID);
 
  // Do not use "click", as Firefox looses the focus when the "disabled"
  // status of buttons is changed by f_init_...
  // http://stackoverflow.com/questions/7392959/how-do-you-avoid-losing-focus-on-a-contenteditable-element-when-a-user-clicks-ou

  l_button_start
    .addEventListener("mousedown", function(){ p_controller.startGame(); }); 
  l_button_pause
    .addEventListener("mousedown", function(){ p_controller.pauseGame(); }); 
  l_button_stop
    .addEventListener("mousedown", function(){ p_controller.stopGame(); }); 

  function o_started()
  { l_button_start.disabled = true;
    l_button_pause.disabled = false;
    l_button_stop.disabled  = false;
  }
  function o_paused()
  { l_button_start.disabled = false;
    l_button_pause.disabled = true;
    l_button_stop.disabled  = false;
  }
  function o_stopped()
  { l_button_start.disabled = false;
    l_button_pause.disabled = true;
    l_button_stop.disabled  = true;
  }

  p_controller.addObserver(Controller.STARTED, o_started);
  p_controller.addObserver(Controller.PAUSED,  o_paused);
  p_controller.addObserver(Controller.STOPPED, o_stopped);
  o_stopped();  // When the game is launched, its state is "STOPPED".
}

$app$.game.snake.Menu = Menu;

////////////////////////////////////////////////////////////////////////////////
// End of Class "Menu"
////////////////////////////////////////////////////////////////////////////////
}(this.$app$, this.document)); 
W. Kowarschick © 2013 (CC BY-NC-SA 3.0: http://creativecommons.org/licenses/by-nc-sa/3.0/)
Documentation generated by JSDoc 3.3.0-dev on Sun Dec 15 2013 11:49:53 GMT+0100 (MEZ) using the DocStrap template.