/*jslint plusplus: true, white: true, indent: 2, maxlen: 90 */
/*global $wk$*/
/**
* @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/).
*/
$wk$(this.document,
function(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.
*/
$wk$.WKcClass(
{ name: "$app$.game.snake.Menu",
methods:
{ init:
function (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".
}
}
});
////////////////////////////////////////////////////////////////////////////////
// End of Class "Menu"
////////////////////////////////////////////////////////////////////////////////
});