<commit_before>//▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
/**
 * Pixelbox main framework module
 * 
 * @author <NAME>
 */

var assetLoader  = require('assetLoader');
var AudioManager = require('audio-manager');

var PIXEL_SIZE    = 4;
var SCREEN_WIDTH  = 128;
var SCREEN_HEIGHT = 128;

var PALETTE_COLORS = [
	'#000000', '#1D2B53', '#008751', '#AB5236',
	'#7E2553', '#5F574F', '#29ADFF', '#00E756',
	'#FFA300', '#FF77A8', '#C2C3C7', '#83769C',
	'#FFFF27', '#FFCCAA', '#FFF1E8', '#FF004D'
];

//▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
// built-in modules

window.EventEmitter = require('EventEmitter');
window.inherits = function (Child, Parent) {
	Child.prototype = Object.create(Parent.prototype, {
		constructor: {
			value: Child,
			enumerable: false,
			writable: true,
			configurable: true
		}
	});
};

//▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
// audio

var audioManager = window.audioManager = new AudioManager(['sfx']);
audioManager.settings.audioPath = 'audio/';
audioManager.settings.defaultFade = 0.3;

audioManager.init();
audioManager.setVolume('sfx', 1.0);

window.sfx = function (soundId, volume, panoramic, pitch) {
	audioManager.playSound('sfx', soundId, volume, panoramic, pitch);
};

window.music = function (soundId, volume) {
	if (!soundId) {
		audioManager.stopLoopSound('sfx');
		return;
	}
	audioManager.playLoopSound('sfx', soundId, volume);
};

//▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
// controls

var button   = window.btn  = { up: false, down: false, left: false, right: false, A: false, B: false };
var bpress   = window.btnp = { up: false, down: false, left: false, right: false, A: false, B: false };
var brelease = window.btnr = { up: false, down: false, left: false, right: false, A: false, B: false };

function resetControlTriggers() {
	bpress.up      = false;
	bpress.down    = false;
	bpress.left    = false;
	bpress.right   = false;
	bpress.A       = false;
	bpress.B       = false;
	brelease.up    = false;
	brelease.down  = false;
	brelease.left  = false;
	brelease.right = false;
	brelease.A     = false;
	brelease.B     = false;
}

var keyMap = {
	37: 'left',
	38: 'up',
	39: 'right',
	40: 'down',
	32: 'A',
	81: 'B'
};

function keyChange(keyCode, isPressed) {
	var key = keyMap[keyCode];
	if (!key) return;
	if ( isPressed && !button[key])   bpress[key] = true;
	if (!isPressed &&  button[key]) brelease[key] = true;
	button[key] = isPressed;
}

window.addEventListener('keydown', function onKeyPressed(e) { keyChange(e.keyCode, true);  });
window.addEventListener('keyup',   function onKeyRelease(e) { keyChange(e.keyCode, false); });


//▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
// Texture

function createCanvas(w, h) {
	var canvas = document.createElement('canvas');
	canvas.width  = w;
	canvas.height = h;
	return canvas;
}

function Texture(w, h) {
	this.canvas  = createCanvas(w, h);
	this.ctx     = this.canvas.getContext('2d');
	this._cursor = { i: 0, j: 0 };
	this._paper  = 0;
	this._pen    = 1;

	// TODO camera offset

	this.ctx.fillStyle   = PALETTE_COLORS[0];
	this.ctx.strokeStyle = PALETTE_COLORS[1];

}

Texture.prototype._isTexture = true;

window.texture = function (img) {
	var texture = new Texture(img.width, img.height);
	texture.ctx.drawImage(img, 0, 0);
	return texture;
}

var currentSpritesheet = new Texture(128, 128);
var textCharset = new Texture(128 * 3, 5 * PALETTE_COLORS.length);

Texture.prototype.sprite = function (sprite, x, y, flipH, flipV) {
	var sx = sprite % 16;
	var sy = ~~(sprite / 16);
	this.ctx.drawImage(currentSpritesheet, sx * 8, sy * 8, 8, 8, ~~x, ~~y, 8, 8);
	return this;
};

Texture.prototype.draw = function (img, x, y) {
	if (img._isTexture) {
		this.ctx.drawImage(img.canvas, ~~x, ~~y);
	} else {
		this.ctx.drawImage(img, ~~x, ~~y);
	}
	return this;
};

Texture.prototype.clear = function () {
	this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
	return this;
};

//▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
// colors

Texture.prototype.pen = function (p) {
	this._pen = p % PALETTE_COLORS.length;
	this.ctx.strokeStyle = PALETTE_COLORS[this._pen];
	return this;
};

Texture.prototype.paper = function (p) {
	this._paper = p % PALETTE_COLORS.length;
	this.ctx.fillStyle = PALETTE_COLORS[this._paper];
	return this;
};

//▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
// shape
<commit_msg>Texture.cls<commit_after>//▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
/**
 * Pixelbox main framework module
 * 
 * @author <NAME>
 */

var assetLoader  = require('assetLoader');
var AudioManager = require('audio-manager');

var PIXEL_SIZE    = 4;
var SCREEN_WIDTH  = 128;
var SCREEN_HEIGHT = 128;

var PALETTE_COLORS = [
	'#000000', '#1D2B53', '#008751', '#AB5236',
	'#7E2553', '#5F574F', '#29ADFF', '#00E756',
	'#FFA300', '#FF77A8', '#C2C3C7', '#83769C',
	'#FFFF27', '#FFCCAA', '#FFF1E8', '#FF004D'
];

//▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
// built-in modules

window.EventEmitter = require('EventEmitter');
window.inherits = function (Child, Parent) {
	Child.prototype = Object.create(Parent.prototype, {
		constructor: {
			value: Child,
			enumerable: false,
			writable: true,
			configurable: true
		}
	});
};

//▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
// audio

var audioManager = window.audioManager = new AudioManager(['sfx']);
audioManager.settings.audioPath = 'audio/';
audioManager.settings.defaultFade = 0.3;

audioManager.init();
audioManager.setVolume('sfx', 1.0);

window.sfx = function (soundId, volume, panoramic, pitch) {
	audioManager.playSound('sfx', soundId, volume, panoramic, pitch);
};

window.music = function (soundId, volume) {
	if (!soundId) {
		audioManager.stopLoopSound('sfx');
		return;
	}
	audioManager.playLoopSound('sfx', soundId, volume);
};

//▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
// controls

var button   = window.btn  = { up: false, down: false, left: false, right: false, A: false, B: false };
var bpress   = window.btnp = { up: false, down: false, left: false, right: false, A: false, B: false };
var brelease = window.btnr = { up: false, down: false, left: false, right: false, A: false, B: false };

function resetControlTriggers() {
	bpress.up      = false;
	bpress.down    = false;
	bpress.left    = false;
	bpress.right   = false;
	bpress.A       = false;
	bpress.B       = false;
	brelease.up    = false;
	brelease.down  = false;
	brelease.left  = false;
	brelease.right = false;
	brelease.A     = false;
	brelease.B     = false;
}

var keyMap = {
	37: 'left',
	38: 'up',
	39: 'right',
	40: 'down',
	32: 'A',
	81: 'B'
};

function keyChange(keyCode, isPressed) {
	var key = keyMap[keyCode];
	if (!key) return;
	if ( isPressed && !button[key])   bpress[key] = true;
	if (!isPressed &&  button[key]) brelease[key] = true;
	button[key] = isPressed;
}

window.addEventListener('keydown', function onKeyPressed(e) { keyChange(e.keyCode, true);  });
window.addEventListener('keyup',   function onKeyRelease(e) { keyChange(e.keyCode, false); });


//▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
// Texture

function createCanvas(w, h) {
	var canvas = document.createElement('canvas');
	canvas.width  = w;
	canvas.height = h;
	return canvas;
}

function Texture(w, h) {
	this.canvas  = createCanvas(w, h);
	this.ctx     = this.canvas.getContext('2d');
	this._cursor = { i: 0, j: 0 };
	this._paper  = 0;
	this._pen    = 1;

	// TODO camera offset

	this.ctx.fillStyle   = PALETTE_COLORS[0];
	this.ctx.strokeStyle = PALETTE_COLORS[1];

}

Texture.prototype._isTexture = true;

window.texture = function (img) {
	var texture = new Texture(img.width, img.height);
	texture.ctx.drawImage(img, 0, 0);
	return texture;
}

var currentSpritesheet = new Texture(128, 128);
var textCharset = new Texture(128 * 3, 5 * PALETTE_COLORS.length);

Texture.prototype.sprite = function (sprite, x, y, flipH, flipV) {
	var sx = sprite % 16;
	var sy = ~~(sprite / 16);
	this.ctx.drawImage(currentSpritesheet, sx * 8, sy * 8, 8, 8, ~~x, ~~y, 8, 8);
	return this;
};

Texture.prototype.draw = function (img, x, y) {
	if (img._isTexture) {
		this.ctx.drawImage(img.canvas, ~~x, ~~y);
	} else {
		this.ctx.drawImage(img, ~~x, ~~y);
	}
	return this;
};

Texture.prototype.clear = function () {
	this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
	return this;
};

Texture.prototype.cls = Texture.prototype.clear;

//▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
// colors

Texture.prototype.pen = function (p) {
	this._pen = p % PALETTE_COLORS.length;
	this.ctx.strokeStyle = PALETTE_COLORS[this._pen];
	return this;
};

Texture.prototype.paper = function (p) {
	this._paper = p % PALETTE_COLORS.length;
	this.ctx.fillStyle = PALETTE_COLORS[this._paper];
	return this;
};

//▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
// shape

{'fraction_non_alphanumeric': 0.2672695576378296, 'fraction_numerical': 0.02440618871213772, 'mean_word_length': 4.596951219512195, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4777987', 'n_tokens_mistral': 4334, 'n_tokens_neox': 5225, 'n_words': 712}