You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

246 lines
7.8 KiB
JavaScript

3 years ago
//=============================================================================
// Salted Fish Plugins - Sprites
3 years ago
// SF_Sprites.js
//=============================================================================
"use strict";
var Imported = Imported || {};
Imported.SF_Sprites = true;
var SF_Plugins = SF_Plugins || {};
//=============================================================================
/*:
* @plugindesc sprite base for salted fish plugins
* @author Salted Fish
*
* @help
* ============================================================================
* Requirements
* ============================================================================
*
* This plugin requires the following plugins:
* SF_Core
*/
3 years ago
//=============================================================================
(function () {
var SF_Sprites = {};
SF_Plugins.Sprites = SF_Sprites;
SF_Sprites.version = 1.0;
//=============================================================================
// Sprite_SFBase
//=============================================================================
function Sprite_SFBase() {
this.initialize.apply(this, arguments);
}
SF_Sprites.Sprite_SFBase = Sprite_SFBase;
window.Sprite_SFBase = Sprite_SFBase;
Sprite_SFBase.prototype = Object.create(Sprite.prototype);
Sprite_SFBase.prototype.constructor = Sprite_SFBase;
Sprite_SFBase.prototype.initialize = function () {
Sprite.prototype.initialize.apply(this, arguments);
this._active = true;
};
3 years ago
Sprite_SFBase.prototype.activate = function () {
this._active = true;
};
3 years ago
Sprite_SFBase.prototype.deactivate = function () {
this._active = false;
};
3 years ago
Sprite_SFBase.prototype.isActive = function () {
return this._active;
};
3 years ago
Sprite_SFBase.prototype.canUpdate = function () {
return this.isActive() && this.visible && this.worldVisible;
};
3 years ago
//=============================================================================
// Sprite_ButtonBase
//=============================================================================
function Sprite_ButtonBase() {
this.initialize.apply(this, arguments);
}
SF_Sprites.Sprite_ButtonBase = Sprite_ButtonBase;
window.Sprite_ButtonBase = Sprite_ButtonBase;
Sprite_ButtonBase.prototype = Object.create(Sprite_SFBase.prototype);
Sprite_ButtonBase.prototype.constructor = Sprite_ButtonBase;
Sprite_ButtonBase.prototype.initialize = function () {
Sprite_SFBase.prototype.initialize.apply(this, arguments);
this._state = "pointer-out"; // pointer-over pointer-out pointer-down
};
3 years ago
Sprite_ButtonBase.prototype.onPointerOver = function () {
// override
};
3 years ago
Sprite_ButtonBase.prototype.onPointerMove = function () {
// override
};
3 years ago
Sprite_ButtonBase.prototype.onPointerEnter = function () {
// override
};
3 years ago
Sprite_ButtonBase.prototype.onPointerLeave = function () {
// override
};
3 years ago
Sprite_ButtonBase.prototype.onPointerDown = function () {
// override
};
3 years ago
Sprite_ButtonBase.prototype.onPointerUp = function () {
// override
};
3 years ago
Sprite_ButtonBase.prototype.onClick = function () {
// override
};
3 years ago
Sprite_ButtonBase.prototype.releasePointer = function () {
Sprite_SFBase.prototype.releasePointer.call(this);
this.setState("pointer-out");
};
3 years ago
Sprite_ButtonBase.prototype.update = function () {
if (this.canUpdate()) {
Sprite_SFBase.prototype.update.call(this);
if (this._state !== "pointer-out") {
3 years ago
this.onPointerOver();
}
this.updatePointerEvent();
}
};
3 years ago
Sprite_ButtonBase.prototype.updatePointerEvent = function () {
var pointer = this.getPointer();
if (pointer) {
var events = pointer.getEvents();
var inFrame = this.containsPoint(new Point(pointer.x, pointer.y));
if (inFrame) {
pointer.setUser(this);
if (this._state === "pointer-out") {
this.setState("pointer-over");
3 years ago
}
for (var i = 0; i < events.length; i++) {
var event = events[i];
if (event === "pointer-down") {
this.setState("pointer-down");
} else if (event === "pointer-up") {
this.setState("pointer-over");
} else if (event === "pointer-move") {
3 years ago
this.onPointerMove();
} else if (event === "pointer-cancel") {
this.setState("pointer-out");
3 years ago
}
}
} else {
pointer.removeUser(this);
this.setState("pointer-out");
3 years ago
}
} else if (this._state !== "pointer-out") {
this.setState("pointer-out");
3 years ago
}
};
3 years ago
Sprite_ButtonBase.prototype.setState = function (state) {
if (this._state !== state) {
if (state === "pointer-out") {
if (this._state === "pointer-down") {
this._state = "pointer-over";
3 years ago
this.onPointerUp();
}
if (this._state === "pointer-over") {
this._state = "pointer-out";
3 years ago
this.onPointerLeave();
}
} else if (state === "pointer-down") {
if (this._state === "pointer-out") {
this._state = "pointer-over";
3 years ago
this.onPointerEnter();
}
if (this._state === "pointer-over") {
this._state = "pointer-down";
3 years ago
this.onPointerDown();
}
} else if (state === "pointer-over") {
if (this._state === "pointer-out") {
this._state = "pointer-over";
3 years ago
this.onPointerEnter();
}
if (this._state === "pointer-down") {
this._state = "pointer-over";
3 years ago
this.onPointerUp();
this.onClick();
}
} else {
SF_Plugins.SF_Core.Utils.error("Sprite_ButtonBase.setState: unknown state: " + state);
3 years ago
}
}
};
3 years ago
//=============================================================================
// Sprite_SFButton
//=============================================================================
function Sprite_SFButton() {
this.initialize.apply(this, arguments);
}
SF_Sprites.Sprite_SFButton = Sprite_SFButton;
window.Sprite_SFButton = Sprite_SFButton;
Sprite_SFButton.prototype = Object.create(Sprite_ButtonBase.prototype);
Sprite_SFButton.prototype.constructor = Sprite_SFButton;
Sprite_SFButton.prototype.initialize = function () {
Sprite_ButtonBase.prototype.initialize.apply(this, arguments);
this._coldBitmap = null;
this._hotBitmap = null;
this._clickHandler = null;
};
3 years ago
Sprite_SFButton.prototype.setColdBitmap = function (bitmap) {
this._coldBitmap = bitmap;
};
3 years ago
Sprite_SFButton.prototype.setHotBitmap = function (bitmap) {
this._hotBitmap = bitmap;
};
3 years ago
Sprite_SFButton.prototype.setClickHandler = function (handler) {
this._clickHandler = handler;
};
3 years ago
Sprite_SFButton.prototype.onClick = function () {
if (this._clickHandler) {
this._clickHandler();
}
};
3 years ago
Sprite_SFButton.prototype.refresh = function () {
this.bitmap = this._coldBitmap;
};
3 years ago
Sprite_SFButton.prototype.onPointerEnter = function () {
this.bitmap = this._hotBitmap;
};
3 years ago
Sprite_SFButton.prototype.onPointerLeave = function () {
this.bitmap = this._coldBitmap;
};
})();