2012-09-16 11:50:40 +00:00
|
|
|
(function () {
|
|
|
|
var ns = $.namespace("pskl.controller");
|
|
|
|
|
|
|
|
ns.PaletteController = function () {
|
|
|
|
this.paletteRoot = null;
|
|
|
|
this.paletteColors = [];
|
|
|
|
};
|
2012-09-10 22:52:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2012-09-16 11:50:40 +00:00
|
|
|
ns.PaletteController.prototype.onPickerChange_ = function(evt, isPrimary) {
|
|
|
|
var inputPicker = $(evt.target);
|
2013-04-08 23:32:30 +00:00
|
|
|
if(evt.data.isPrimary) {
|
|
|
|
$.publish(Events.PRIMARY_COLOR_SELECTED, [inputPicker.val()]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$.publish(Events.SECONDARY_COLOR_SELECTED, [inputPicker.val()]);
|
|
|
|
}
|
2012-09-16 11:50:40 +00:00
|
|
|
};
|
2013-04-09 05:24:07 +00:00
|
|
|
|
2012-09-16 11:50:40 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.PaletteController.prototype.addColorToPalette_ = function (color) {
|
|
|
|
if (this.paletteColors.indexOf(color) == -1 && color != Constants.TRANSPARENT_COLOR) {
|
|
|
|
this.paletteColors.push(color);
|
|
|
|
}
|
|
|
|
};
|
2012-09-03 20:30:53 +00:00
|
|
|
|
2012-09-16 15:25:51 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.PaletteController.prototype.addColorsToPalette_ = function (colors) {
|
|
|
|
for(var color in colors) {
|
|
|
|
this.addColorToPalette_(color);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-09-16 11:50:40 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.PaletteController.prototype.onPaletteColorClick_ = function (event) {
|
|
|
|
var selectedColor = $(event.target).data("color");
|
2013-04-08 23:32:30 +00:00
|
|
|
var isLeftClick = (event.which == 1);
|
|
|
|
var isRightClick = (event.which == 3);
|
|
|
|
if (isLeftClick) {
|
|
|
|
$.publish(Events.PRIMARY_COLOR_SELECTED, [selectedColor]);
|
|
|
|
} else if (isRightClick) {
|
|
|
|
$.publish(Events.SECONDARY_COLOR_SELECTED, [selectedColor]);
|
2012-09-16 11:50:40 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.PaletteController.prototype.updateColorPicker_ = function (color, colorPicker) {
|
|
|
|
if (color == Constants.TRANSPARENT_COLOR) {
|
|
|
|
// We can set the current palette color to transparent.
|
|
|
|
// You can then combine this transparent color with an advanced
|
|
|
|
// tool for customized deletions.
|
|
|
|
// Eg: bucket + transparent: Delete a colored area
|
|
|
|
// Stroke + transparent: hollow out the equivalent of a stroke
|
|
|
|
|
|
|
|
// The colorpicker can't be set to a transparent state.
|
|
|
|
// We set its background to white and insert the
|
|
|
|
// string "TRANSPARENT" to mimic this state:
|
|
|
|
colorPicker[0].color.fromString("#fff");
|
|
|
|
colorPicker.val(Constants.TRANSPARENT_COLOR);
|
|
|
|
} else {
|
|
|
|
colorPicker[0].color.fromString(color);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
ns.PaletteController.prototype.init = function(framesheet) {
|
2012-09-10 22:52:09 +00:00
|
|
|
|
2012-09-16 11:50:40 +00:00
|
|
|
this.paletteRoot = $("#palette");
|
|
|
|
this.framesheet = framesheet;
|
2012-09-03 20:30:53 +00:00
|
|
|
|
2012-09-16 11:50:40 +00:00
|
|
|
// Initialize palette:
|
2012-09-16 15:25:51 +00:00
|
|
|
this.addColorsToPalette_(this.framesheet.getUsedColors());
|
2012-09-03 20:30:53 +00:00
|
|
|
|
2012-09-16 15:25:51 +00:00
|
|
|
$.subscribe(Events.FRAMESHEET_RESET, $.proxy(function(evt) {
|
|
|
|
this.addColorsToPalette_(this.framesheet.getUsedColors());
|
2012-09-16 11:50:40 +00:00
|
|
|
}, this));
|
2012-09-03 20:30:53 +00:00
|
|
|
|
2012-09-16 11:50:40 +00:00
|
|
|
this.paletteRoot.mouseup($.proxy(this.onPaletteColorClick_, this));
|
|
|
|
|
2013-04-08 23:32:30 +00:00
|
|
|
$.subscribe(Events.PRIMARY_COLOR_UPDATED, $.proxy(function(evt, color) {
|
|
|
|
this.updateColorPicker_(color, $('#color-picker'));
|
|
|
|
this.addColorToPalette_(color);
|
|
|
|
}, this));
|
|
|
|
|
|
|
|
$.subscribe(Events.SECONDARY_COLOR_UPDATED, $.proxy(function(evt, color) {
|
|
|
|
this.updateColorPicker_(color, $('#secondary-color-picker'));
|
2012-09-16 11:50:40 +00:00
|
|
|
this.addColorToPalette_(color);
|
|
|
|
}, this));
|
2012-09-03 20:30:53 +00:00
|
|
|
|
2012-09-16 11:50:40 +00:00
|
|
|
// Initialize colorpickers:
|
|
|
|
var colorPicker = $('#color-picker');
|
|
|
|
colorPicker.val(Constants.DEFAULT_PEN_COLOR);
|
|
|
|
colorPicker.change({isPrimary : true}, $.proxy(this.onPickerChange_, this));
|
2012-09-06 20:37:19 +00:00
|
|
|
|
|
|
|
|
2012-09-16 11:50:40 +00:00
|
|
|
var secondaryColorPicker = $('#secondary-color-picker');
|
|
|
|
secondaryColorPicker.val(Constants.TRANSPARENT_COLOR);
|
|
|
|
secondaryColorPicker.change({isPrimary : false}, $.proxy(this.onPickerChange_, this));
|
2012-09-03 20:30:53 +00:00
|
|
|
|
2012-09-10 22:52:09 +00:00
|
|
|
};
|
2012-09-03 20:30:53 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
|
|
|