2012-09-16 15:50:40 +04:00
|
|
|
(function () {
|
|
|
|
var ns = $.namespace("pskl.controller");
|
|
|
|
|
2013-11-19 10:40:35 +04:00
|
|
|
ns.PaletteController = function () {
|
|
|
|
this.primaryColor = Constants.DEFAULT_PEN_COLOR;
|
|
|
|
this.secondaryColor = Constants.TRANSPARENT_COLOR;
|
|
|
|
};
|
2013-09-22 23:02:43 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
ns.PaletteController.prototype.init = function() {
|
|
|
|
var transparentColorPalette = $(".palette-color[data-color=TRANSPARENT]");
|
|
|
|
transparentColorPalette.mouseup($.proxy(this.onPaletteColorClick_, this));
|
|
|
|
|
2013-11-19 10:40:35 +04:00
|
|
|
$.subscribe(Events.SELECT_PRIMARY_COLOR, this.onColorSelected_.bind(this, {isPrimary:true}));
|
|
|
|
$.subscribe(Events.SELECT_SECONDARY_COLOR, this.onColorSelected_.bind(this, {isPrimary:false}));
|
2013-09-22 23:02:43 +04:00
|
|
|
|
2013-11-20 02:46:33 +04:00
|
|
|
pskl.app.shortcutService.addShortcut('X', this.swapColors.bind(this));
|
|
|
|
pskl.app.shortcutService.addShortcut('D', this.resetColors.bind(this));
|
2013-09-22 23:02:43 +04:00
|
|
|
|
2013-11-21 03:44:46 +04:00
|
|
|
var spectrumCfg = {
|
|
|
|
showPalette: true,
|
2013-11-27 02:49:55 +04:00
|
|
|
showButtons: false,
|
2013-11-21 03:44:46 +04:00
|
|
|
palette: [
|
|
|
|
['rgba(0,0,0,0)']
|
|
|
|
],
|
|
|
|
clickoutFiresChange : true,
|
2013-11-27 02:49:55 +04:00
|
|
|
|
2013-11-21 03:44:46 +04:00
|
|
|
beforeShow : function(tinycolor) {
|
|
|
|
tinycolor.setAlpha(1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-09-22 23:02:43 +04:00
|
|
|
// Initialize colorpickers:
|
|
|
|
var colorPicker = $('#color-picker');
|
2013-11-21 03:44:46 +04:00
|
|
|
colorPicker.spectrum($.extend({color: Constants.DEFAULT_PEN_COLOR}, spectrumCfg));
|
2013-09-22 23:02:43 +04:00
|
|
|
colorPicker.change({isPrimary : true}, $.proxy(this.onPickerChange_, this));
|
2013-11-28 02:43:59 +04:00
|
|
|
this.setTitleOnPicker_(Constants.DEFAULT_PEN_COLOR, colorPicker);
|
2013-09-22 23:02:43 +04:00
|
|
|
|
|
|
|
var secondaryColorPicker = $('#secondary-color-picker');
|
2013-11-21 03:44:46 +04:00
|
|
|
secondaryColorPicker.spectrum($.extend({color: Constants.TRANSPARENT_COLOR}, spectrumCfg));
|
2013-09-22 23:02:43 +04:00
|
|
|
secondaryColorPicker.change({isPrimary : false}, $.proxy(this.onPickerChange_, this));
|
2013-11-28 02:43:59 +04:00
|
|
|
this.setTitleOnPicker_(Constants.TRANSPARENT_COLOR, secondaryColorPicker);
|
|
|
|
|
2012-09-16 15:50:40 +04:00
|
|
|
};
|
2012-09-11 02:52:09 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2012-09-16 15:50:40 +04:00
|
|
|
ns.PaletteController.prototype.onPickerChange_ = function(evt, isPrimary) {
|
|
|
|
var inputPicker = $(evt.target);
|
2013-04-09 03:32:30 +04:00
|
|
|
if(evt.data.isPrimary) {
|
2013-11-19 10:40:35 +04:00
|
|
|
this.setPrimaryColor(inputPicker.val());
|
2013-09-22 23:02:43 +04:00
|
|
|
} else {
|
2013-11-19 10:40:35 +04:00
|
|
|
this.setSecondaryColor(inputPicker.val());
|
2013-04-09 03:32:30 +04:00
|
|
|
}
|
2012-09-16 15:50:40 +04:00
|
|
|
};
|
2012-09-16 19:25:51 +04:00
|
|
|
|
2013-11-19 10:40:35 +04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.PaletteController.prototype.onColorSelected_ = function(args, evt, color) {
|
|
|
|
var inputPicker = $(evt.target);
|
|
|
|
if(args.isPrimary) {
|
|
|
|
this.setPrimaryColor(color);
|
|
|
|
} else {
|
|
|
|
this.setSecondaryColor(color);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.PaletteController.prototype.setPrimaryColor = function (color) {
|
|
|
|
this.primaryColor = color;
|
|
|
|
this.updateColorPicker_(color, $('#color-picker'));
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.PaletteController.prototype.setSecondaryColor = function (color) {
|
|
|
|
this.secondaryColor = color;
|
|
|
|
this.updateColorPicker_(color, $('#secondary-color-picker'));
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.PaletteController.prototype.getPrimaryColor = function () {
|
|
|
|
return this.primaryColor;
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.PaletteController.prototype.getSecondaryColor = function () {
|
|
|
|
return this.secondaryColor;
|
|
|
|
};
|
|
|
|
|
2013-11-20 02:46:33 +04:00
|
|
|
ns.PaletteController.prototype.swapColors = function () {
|
2013-11-19 10:40:35 +04:00
|
|
|
var primaryColor = this.getPrimaryColor();
|
|
|
|
this.setPrimaryColor(this.getSecondaryColor());
|
|
|
|
this.setSecondaryColor(primaryColor);
|
2013-11-19 02:53:12 +04:00
|
|
|
};
|
|
|
|
|
2013-11-20 02:46:33 +04:00
|
|
|
ns.PaletteController.prototype.resetColors = function () {
|
|
|
|
this.setPrimaryColor(Constants.DEFAULT_PEN_COLOR);
|
|
|
|
this.setSecondaryColor(Constants.TRANSPARENT_COLOR);
|
|
|
|
};
|
|
|
|
|
2012-09-16 15:50:40 +04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.PaletteController.prototype.onPaletteColorClick_ = function (event) {
|
|
|
|
var selectedColor = $(event.target).data("color");
|
2013-04-09 03:32:30 +04: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 15:50:40 +04: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
|
2013-11-21 03:44:46 +04:00
|
|
|
// tool for customized deletions.
|
2012-09-16 15:50:40 +04:00
|
|
|
// 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:
|
2013-11-21 03:44:46 +04:00
|
|
|
colorPicker.spectrum("set", Constants.TRANSPARENT_COLOR);
|
2012-09-16 15:50:40 +04:00
|
|
|
colorPicker.val(Constants.TRANSPARENT_COLOR);
|
|
|
|
} else {
|
2013-11-21 03:44:46 +04:00
|
|
|
colorPicker.spectrum("set", color);
|
2012-09-16 15:50:40 +04:00
|
|
|
}
|
2013-11-28 02:43:59 +04:00
|
|
|
this.setTitleOnPicker_(color, colorPicker);
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.PaletteController.prototype.setTitleOnPicker_ = function (title, colorPicker) {
|
|
|
|
var spectrumInputSelector = '.sp-replacer';
|
|
|
|
colorPicker.next(spectrumInputSelector).attr('title', title);
|
2012-09-16 15:50:40 +04:00
|
|
|
};
|
2012-09-04 00:30:53 +04:00
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
|
|
|