@mai3/phaser-sdk
Version:
A UI component library based on the Phaser game engine
78 lines (77 loc) • 3.51 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
import { Container } from "./Container";
var defaultConfig = {
barColor: 0x888888,
};
var ScrollBar = /** @class */ (function (_super) {
__extends(ScrollBar, _super);
function ScrollBar(scene, config) {
var _a, _b, _c, _d;
var _this = _super.call(this, scene) || this;
var mergedConfig = __assign(__assign({}, defaultConfig), config);
_this._direction = mergedConfig.direction || "y";
// Create a scrollbar track
_this._track = scene.add.rectangle(mergedConfig.x, mergedConfig.y, mergedConfig.width, mergedConfig.height, mergedConfig.backgroundColor);
_this._track.setOrigin(0, 0);
_this._thumb = scene.add.graphics();
_this._thumbWidth =
_this._direction === "y"
? mergedConfig.width || 0
: (mergedConfig.width || 0) * 0.3;
_this._thumbHeight =
_this._direction === "y"
? (mergedConfig.height || 0) * 0.3
: mergedConfig.height || 0;
// Draw a rounded rectangular slider
_this._thumb.fillStyle(mergedConfig.barColor);
var radius = _this._direction === "y" ? _this._thumbWidth / 2 : _this._thumbHeight / 2;
_this._thumb.fillRoundedRect((_a = mergedConfig.x) !== null && _a !== void 0 ? _a : 0, (_b = mergedConfig.y) !== null && _b !== void 0 ? _b : 0, (_c = _this._thumbWidth) !== null && _c !== void 0 ? _c : 0, (_d = _this._thumbHeight) !== null && _d !== void 0 ? _d : 0, radius);
_this.add(_this._track);
_this.add(_this._thumb);
return _this;
}
ScrollBar.prototype.updateThumbPosition = function (percent) {
var maxOffset = this._direction === "y"
? this._track.height - this._thumbHeight
: this._track.width - this._thumbWidth;
if (this._direction === "y") {
this._thumb.y = maxOffset * percent;
}
else {
this._thumb.x = maxOffset * percent;
}
};
ScrollBar.prototype.destroy = function (fromScene) {
var _a, _b;
_super.prototype.destroy.call(this, fromScene);
(_a = this._track) === null || _a === void 0 ? void 0 : _a.destroy(fromScene);
(_b = this._thumb) === null || _b === void 0 ? void 0 : _b.destroy(fromScene);
};
return ScrollBar;
}(Container));
export { ScrollBar };