mirror of
https://github.com/piskelapp/piskel.git
synced 2023-08-10 21:12:52 +03:00
Create MouseStateService and integrate
This commit is contained in:
@ -41,6 +41,9 @@
|
|||||||
this.selectedColorsService = new pskl.service.SelectedColorsService();
|
this.selectedColorsService = new pskl.service.SelectedColorsService();
|
||||||
this.selectedColorsService.init();
|
this.selectedColorsService.init();
|
||||||
|
|
||||||
|
this.mouseStateService = new pskl.service.MouseStateService();
|
||||||
|
this.mouseStateService.init();
|
||||||
|
|
||||||
this.paletteController = new pskl.controller.PaletteController();
|
this.paletteController = new pskl.controller.PaletteController();
|
||||||
this.paletteController.init();
|
this.paletteController.init();
|
||||||
|
|
||||||
|
@ -47,9 +47,6 @@
|
|||||||
this.isClicked = false;
|
this.isClicked = false;
|
||||||
this.previousMousemoveTime = 0;
|
this.previousMousemoveTime = 0;
|
||||||
this.currentToolBehavior = null;
|
this.currentToolBehavior = null;
|
||||||
|
|
||||||
// State of clicked button (need to be stateful here, see comment in getCurrentColor_)
|
|
||||||
this.currentMouseButton_ = Constants.LEFT_BUTTON;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
ns.DrawingController.prototype.init = function () {
|
ns.DrawingController.prototype.init = function () {
|
||||||
@ -145,7 +142,6 @@
|
|||||||
var coords = this.getSpriteCoordinates(event.clientX, event.clientY);
|
var coords = this.getSpriteCoordinates(event.clientX, event.clientY);
|
||||||
|
|
||||||
this.isClicked = true;
|
this.isClicked = true;
|
||||||
this.setCurrentButton(event);
|
|
||||||
|
|
||||||
if (event.button === Constants.MIDDLE_BUTTON) {
|
if (event.button === Constants.MIDDLE_BUTTON) {
|
||||||
this.dragHandler.startDrag(event.clientX, event.clientY);
|
this.dragHandler.startDrag(event.clientX, event.clientY);
|
||||||
@ -191,12 +187,10 @@
|
|||||||
var currentFrame = this.piskelController.getCurrentFrame();
|
var currentFrame = this.piskelController.getCurrentFrame();
|
||||||
|
|
||||||
if (this.isClicked) {
|
if (this.isClicked) {
|
||||||
if (this.currentMouseButton_ == Constants.MIDDLE_BUTTON) {
|
if (pskl.app.mouseStateService.isMiddleButtonPressed()) {
|
||||||
this.dragHandler.updateDrag(x, y);
|
this.dragHandler.updateDrag(x, y);
|
||||||
} else {
|
} else {
|
||||||
$.publish(Events.MOUSE_EVENT, [event, this]);
|
$.publish(Events.MOUSE_EVENT, [event, this]);
|
||||||
// Warning : do not call setCurrentButton here
|
|
||||||
// mousemove do not have the correct mouse button information on all browsers
|
|
||||||
this.currentToolBehavior.moveToolAt(
|
this.currentToolBehavior.moveToolAt(
|
||||||
coords.x | 0,
|
coords.x | 0,
|
||||||
coords.y | 0,
|
coords.y | 0,
|
||||||
@ -269,9 +263,8 @@
|
|||||||
// of the drawing canvas.
|
// of the drawing canvas.
|
||||||
|
|
||||||
this.isClicked = false;
|
this.isClicked = false;
|
||||||
this.setCurrentButton(event);
|
|
||||||
|
|
||||||
if (event.button === Constants.MIDDLE_BUTTON) {
|
if (pskl.app.mouseStateService.isMiddleButtonPressed()) {
|
||||||
if (this.dragHandler.isDragging()) {
|
if (this.dragHandler.isDragging()) {
|
||||||
this.dragHandler.stopDrag();
|
this.dragHandler.stopDrag();
|
||||||
} else if (frame.containsPixel(coords.x, coords.y)) {
|
} else if (frame.containsPixel(coords.x, coords.y)) {
|
||||||
@ -306,10 +299,6 @@
|
|||||||
return this.renderer.reverseCoordinates(spriteX, spriteY);
|
return this.renderer.reverseCoordinates(spriteX, spriteY);
|
||||||
};
|
};
|
||||||
|
|
||||||
ns.DrawingController.prototype.setCurrentButton = function (event) {
|
|
||||||
this.currentMouseButton_ = event.button;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
@ -320,9 +309,9 @@
|
|||||||
// on a mouse move event
|
// on a mouse move event
|
||||||
// This always matches a LEFT mouse button which is __really__ not helpful
|
// This always matches a LEFT mouse button which is __really__ not helpful
|
||||||
|
|
||||||
if (this.currentMouseButton_ == Constants.RIGHT_BUTTON) {
|
if (pskl.app.mouseStateService.isRightButtonPressed()) {
|
||||||
return pskl.app.selectedColorsService.getSecondaryColor();
|
return pskl.app.selectedColorsService.getSecondaryColor();
|
||||||
} else if (this.currentMouseButton_ == Constants.LEFT_BUTTON) {
|
} else if (pskl.app.mouseStateService.isLeftButtonPressed()) {
|
||||||
return pskl.app.selectedColorsService.getPrimaryColor();
|
return pskl.app.selectedColorsService.getPrimaryColor();
|
||||||
} else {
|
} else {
|
||||||
return Constants.DEFAULT_PEN_COLOR;
|
return Constants.DEFAULT_PEN_COLOR;
|
||||||
|
39
src/js/service/MouseStateService.js
Normal file
39
src/js/service/MouseStateService.js
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
(function () {
|
||||||
|
var ns = $.namespace('pskl.service');
|
||||||
|
|
||||||
|
var BUTTON_UNSET = null;
|
||||||
|
|
||||||
|
ns.MouseStateService = function () {
|
||||||
|
this.lastButtonPressed_ = BUTTON_UNSET;
|
||||||
|
};
|
||||||
|
|
||||||
|
ns.MouseStateService.prototype.init = function () {
|
||||||
|
$.subscribe(Events.MOUSE_EVENT, this.onMouseEvent_.bind(this));
|
||||||
|
};
|
||||||
|
|
||||||
|
ns.MouseStateService.prototype.isLeftButtonPressed = function () {
|
||||||
|
return this.isMouseButtonPressed_(Constants.LEFT_BUTTON);
|
||||||
|
};
|
||||||
|
|
||||||
|
ns.MouseStateService.prototype.isRightButtonPressed = function () {
|
||||||
|
return this.isMouseButtonPressed_(Constants.RIGHT_BUTTON);
|
||||||
|
};
|
||||||
|
|
||||||
|
ns.MouseStateService.prototype.isMiddleButtonPressed = function () {
|
||||||
|
return this.isMouseButtonPressed_(Constants.MIDDLE_BUTTON);
|
||||||
|
};
|
||||||
|
|
||||||
|
ns.MouseStateService.prototype.isMouseButtonPressed_ = function (mouseButton) {
|
||||||
|
return this.lastButtonPressed_ != BUTTON_UNSET && this.lastButtonPressed_ == mouseButton;
|
||||||
|
}
|
||||||
|
|
||||||
|
ns.MouseStateService.prototype.onMouseEvent_ = function(evt, mouseEvent) {
|
||||||
|
if (mouseEvent.type == 'mousedown') {
|
||||||
|
this.lastButtonPressed_ = mouseEvent.button;
|
||||||
|
} else if (mouseEvent.type == 'mouseup') {
|
||||||
|
this.lastButtonPressed_ = BUTTON_UNSET;
|
||||||
|
}
|
||||||
|
// Warning : do not call setCurrentButton here
|
||||||
|
// mousemove do not have the correct mouse button information on all browsers
|
||||||
|
};
|
||||||
|
})();
|
@ -162,6 +162,7 @@
|
|||||||
"js/service/CurrentColorsService.js",
|
"js/service/CurrentColorsService.js",
|
||||||
"js/service/FileDropperService.js",
|
"js/service/FileDropperService.js",
|
||||||
"js/service/SelectedColorsService.js",
|
"js/service/SelectedColorsService.js",
|
||||||
|
"js/service/MouseStateService.js",
|
||||||
|
|
||||||
// Tools
|
// Tools
|
||||||
"js/tools/ToolsHelper.js",
|
"js/tools/ToolsHelper.js",
|
||||||
|
Reference in New Issue
Block a user