1 changed files with 73 additions and 0 deletions
@ -0,0 +1,73 @@ |
|||||
|
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; |
||||
Loading…
Reference in new issue