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

73 lines
1.5 KiB

import Game from "@/core/Game";
type HorizontalAlign = "left" | "center" | "right" | number;
type VerticalAlign = "top" | "center" | "bottom" | number;
interface PositionOptions {
x?: number;
y?: number;
}
class Position {
private game: Game;
constructor() {
this.game = Game.getInstance();
}
get(
hAlign: HorizontalAlign,
vAlign: VerticalAlign,
options?: PositionOptions
): { x: number; y: number } {
const { width, height } = this.game.getInfo();
let x: number;
let y: number;
// 水平对齐
if (hAlign === "left") {
x = 0;
} else if (hAlign === "center") {
x = width / 2;
} else if (hAlign === "right") {
x = width;
} else {
x = hAlign;
}
// 垂直对齐
if (vAlign === "top") {
y = 0;
} else if (vAlign === "center") {
y = height / 2;
} else if (vAlign === "bottom") {
y = height;
} else {
y = vAlign;
}
// 添加偏移
if (options) {
x += options.x ?? 0;
y += options.y ?? 0;
}
return { x, y };
}
center(options?: PositionOptions): { x: number; y: number } {
return this.get("center", "center", options);
}
centerX(y: VerticalAlign, options?: PositionOptions): { x: number; y: number } {
return this.get("center", y, options);
}
centerY(x: HorizontalAlign, options?: PositionOptions): { x: number; y: number } {
return this.get(x, "center", options);
}
}
export const position = new Position();
export default position;