/*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/).
*/
(function($wk$, document)
{ "use strict";
/**
* @class
* @name $wk$.Terminal2
* @param {String} p_terminal_id
* The identifier of the canvas element where the lines are to be
* printed.
* @param {JSON} p_init
* A JSON object that contains initial values for the terminal
* (i.e. canvas):
* <code>width</code>, <code>height</code>,
* <code>fontFamily</code>, <code>fontSize</code>,
* <code>fontWeight</code>, <code>lineHeight</code>,
* <code>marginTop</code>, <code>marginLeft</code>.
*/
var Terminal =
function(p_terminal_id, p_init)
{ var l_key = null,
l_terminal = document.getElementById(p_terminal_id);
// Default values
this.width = 600;
this.height = 600;
this.fontFamily = "'Times New Roman', Times, serif";
this.fontSize = 20;
this.fontWeight = "bold";
this.lineHeight = 25;
this.marginTop = 0;
this.marginLeft = 10;
// Override defaults if wished
/*jslint forin: true*/
for (l_key in p_init)
{ this[l_key] = p_init[l_key]; }
// All lines of text to be printed onto the canvas.
this.v_lines = [];
// The number of lines fitting on the canvas.
this.v_canvas_length =
Math.floor((this.height-this.marginTop-this.marginBottom)/this.lineHeight);
l_terminal.width = this.width;
l_terminal.height = this.height;
this.v_terminal = l_terminal;
this.v_context = l_terminal.getContext("2d");
};
Terminal.prototype =
{ /**
* Prints a line on the terminal.
* @method
* @name $wk$.Terminal2#println
* @param {String[]} arguments The strings to be printed.
*/
println:
function()
{ var i, n,
l_text = "",
l_sep = "";
for (i = 0, n = arguments.length; i < n; i++)
{ l_text += l_sep + arguments[i];
l_sep = ", ";
}
this.v_lines.push(l_text);
if (this.v_lines.length > this.v_max_line_numbers)
{ this.v_lines.shift(); }
this.v_context
.clearRect(0, 0, this.v_terminal.width, this.v_terminal.height);
this.v_context.font
= this.fontWeight + " "
+ this.fontSize + "px" + " "
+ this.fontFSamily;
for (i = 0, n = this.v_lines.length; i < n; i++)
{ this.v_context.fillText(this.v_lines[i],
this.marginLeft,
this.marginTop+(i+1)*this.lineHeight
);
}
}
};
$wk$.Terminal2 = Terminal;
}(this.$wk$, this.document));