Files
Zos/Skills/@be/be/hex-hitarea.js

54 lines
2.4 KiB
JavaScript

"use strict";
// Lightweight monkey-patch to enable polygon (hex) hit areas and per-button polygon configs.
exports.patch = function (jibo) {
try {
if (!jibo || !jibo.face || !jibo.face.views) return;
const MenuButton = jibo.face.views.MenuButton;
const Element = jibo.face.views.Element;
if (MenuButton && MenuButton.prototype && MenuButton.prototype.setupHitArea) {
const origSetup = MenuButton.prototype.setupHitArea;
MenuButton.prototype.setupHitArea = function (bounds) {
if (!bounds && !this._hitArea && this._dimensions) {
// default: regular flat-top hexagon centered in the button bounds
const w = this._dimensions.width;
const h = this._dimensions.height;
const cx = w / 2;
const cy = h / 2;
const r = Math.min(w, h) / 2;
const pts = [];
for (let i = 0; i < 6; i++) {
const angle = (Math.PI / 180) * (60 * i - 30);
pts.push(cx + r * Math.cos(angle), cy + r * Math.sin(angle));
}
bounds = new PIXI.Polygon(pts);
}
return origSetup.call(this, bounds);
};
}
if (Element && Element.prototype && Element.prototype.assignConfig) {
const origAssign = Element.prototype.assignConfig;
Element.prototype.assignConfig = function (configData) {
origAssign.call(this, configData);
if (configData && configData.hitAreaPolygon && Array.isArray(configData.hitAreaPolygon)) {
// hitAreaPolygon should be an array of numbers [x1,y1,x2,y2,...]
try {
this._hitArea = new PIXI.Polygon(configData.hitAreaPolygon.slice());
// If the display already exists, apply immediately
if (this.display) this.display.hitArea = this._hitArea;
}
catch (e) {
// swallow errors - leave original behavior intact
if (jibo && jibo.log && jibo.log.warn) jibo.log.warn('Invalid hitAreaPolygon:', e);
}
}
};
}
}
catch (e) {
if (jibo && jibo.log && jibo.log.warn) jibo.log.warn('hex-hitarea patch error', e);
}
};