added support for vAlign modification for Labels

This commit is contained in:
Joe Weaver 2021-04-05 11:13:57 -04:00
parent 14c029e31f
commit eb13a2d165

View File

@ -72,6 +72,10 @@ export default class Label extends UIElement{
this.hAlign = align; this.hAlign = align;
} }
setVAlign(align: string): void {
this.vAlign = align;
}
/** /**
* Calculate the offset of the text - this is used for rendering text with different alignments * Calculate the offset of the text - this is used for rendering text with different alignments
* @param ctx The rendering context * @param ctx The rendering context
@ -83,16 +87,16 @@ export default class Label extends UIElement{
let offset = new Vec2(0, 0); let offset = new Vec2(0, 0);
let hDiff = this.size.x - textWidth; let hDiff = this.size.x - textWidth;
if(this.hAlign === "center"){ if(this.hAlign === HAlign.CENTER){
offset.x = hDiff/2; offset.x = hDiff/2;
} else if (this.hAlign === "right"){ } else if (this.hAlign === HAlign.RIGHT){
offset.x = hDiff; offset.x = hDiff;
} }
if(this.vAlign === "top"){ if(this.vAlign === VAlign.TOP){
ctx.textBaseline = "top"; ctx.textBaseline = "top";
offset.y = 0; offset.y = 0;
} else if (this.vAlign === "bottom"){ } else if (this.vAlign === VAlign.BOTTOM){
ctx.textBaseline = "bottom"; ctx.textBaseline = "bottom";
offset.y = this.size.y; offset.y = this.size.y;
} else { } else {
@ -133,4 +137,16 @@ export default class Label extends UIElement{
sizeToText(): void { sizeToText(): void {
this.sizeAssigned = false; this.sizeAssigned = false;
} }
}
export enum VAlign {
TOP = "top",
CENTER = "center",
BOTTOM = "bottom"
}
export enum HAlign {
LEFT = "left",
CENTER = "center",
RIGHT = "right"
} }