Skip to content

Commit

Permalink
feat: Improve text related API
Browse files Browse the repository at this point in the history
  • Loading branch information
warxander committed Nov 15, 2023
1 parent 9399ac9 commit 2e6e425
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 68 deletions.
37 changes: 21 additions & 16 deletions src/core/painter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,35 +65,30 @@ export class Painter {
);
}

getTextWidth(): number {
if (this.textEntryIndex == -1) return 0;
getTextWidth(text: string, font: number, scale: number): number {
this.setText(text, font, scale);

BeginTextCommandGetWidth(this.getTextEntry());
return EndTextCommandGetWidth(true);
}

getTextLineCount(): number {
if (this.textEntryIndex == -1) return 0;
getTextLineCount(text: string, font: number, scale: number, w: number): number {
this.setText(text, font, scale, w);

BeginTextCommandLineCount(this.getTextEntry());
return EndTextCommandLineCount(this.position.x, this.position.y);
}

setText(font: number, scale: number, text: string) {
SetTextFont(font);
SetTextScale(1, scale);

++this.textEntryIndex;
AddTextEntry(this.getTextEntry(), text);
}
drawText(text: string, font: number, scale: number) {
this.setText(text, font, scale);
SetTextColour(this.color[0], this.color[1], this.color[2], this.color[3]);

setTextWidth(w: number) {
SetTextWrap(this.position.x, this.position.x + w);
BeginTextCommandDisplayText(this.getTextEntry());
EndTextCommandDisplayText(this.position.x, this.position.y);
}

drawText() {
if (this.textEntryIndex == -1) return;

drawMultilineText(text: string, font: number, scale: number, w: number) {
this.setText(text, font, scale, w);
SetTextColour(this.color[0], this.color[1], this.color[2], this.color[3]);

BeginTextCommandDisplayText(this.getTextEntry());
Expand All @@ -103,4 +98,14 @@ export class Painter {
private getTextEntry(): string {
return `VEIN_TEXT_ENTRY_${this.textEntryIndex}`;
}

private setText(text: string, font: number, scale: number, w: number | null = null) {
++this.textEntryIndex;
AddTextEntry(this.getTextEntry(), text);

SetTextFont(font);
SetTextScale(1, scale);

if (w) SetTextWrap(this.position.x, this.position.x + w);
}
}
25 changes: 13 additions & 12 deletions src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,11 @@ export interface IPainter {

drawSprite(dict: string, name: string, w: number, h: number): void;

setText(font: number, scale: number, text: string): void;
setTextWidth(w: number): void;
getTextWidth(): number;
drawText(): void;
getTextWidth(text: string, font: number, scale: number): number;
getTextLineCount(text: string, font: number, scale: number, w: number): number;

drawText(text: string, font: number, scale: number): void;
drawMultilineText(text: string, font: number, scale: number, w: number): void;
}

export function getUi(): IUi {
Expand Down Expand Up @@ -169,20 +170,20 @@ export function getUi(): IUi {
ui.getPainter().drawSprite(dict, name, w, h);
},

setText(font: number, scale: number, text: string) {
ui.getPainter().setText(font, scale, text);
getTextWidth(text: string, font: number, scale: number): number {
return ui.getPainter().getTextWidth(text, font, scale);
},

setTextWidth(w: number) {
ui.getPainter().setTextWidth(w);
getTextLineCount(text: string, font: number, scale: number, w: number): number {
return ui.getPainter().getTextLineCount(text, font, scale, w);
},

getTextWidth(): number {
return ui.getPainter().getTextWidth();
drawText(text: string, font: number, scale: number) {
ui.getPainter().drawText(text, font, scale);
},

drawText() {
ui.getPainter().drawText();
drawMultilineText(text: string, font: number, scale: number, w: number) {
ui.getPainter().drawMultilineText(text, font, scale, w);
}
};
},
Expand Down
6 changes: 2 additions & 4 deletions src/items/button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ export function button(text: string): boolean {
const font = buttonProperties.get<number>('font-family');
const scale = buttonProperties.get<number>('font-size');

painter.setText(font, scale, text);

const w = ui.tryGetItemWidth() ?? painter.getTextWidth() + style.button.spacing * 2;
const w = ui.tryGetItemWidth() ?? painter.getTextWidth(text, font, scale) + style.button.spacing * 2;
const h = style.item.height;

ui.beginItem(w, h);
Expand All @@ -25,7 +23,7 @@ export function button(text: string): boolean {

painter.setColor(properties.get<Color>('color'));
painter.move(style.button.spacing, (h - GetRenderedCharacterHeight(scale, font)) / 2 + style.item.textOffset);
painter.drawText();
painter.drawText(text, font, scale);

ui.endItem();

Expand Down
6 changes: 2 additions & 4 deletions src/items/checkbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@ export function checkBox(isChecked: boolean, text: string): boolean {
const font = checkBoxProperties.get<number>('font-family');
const scale = checkBoxProperties.get<number>('font-size');

painter.setText(font, scale, text);

const aspectRatio = GetAspectRatio(false);
const checkboxStyle = style.checkbox;
let cw = checkboxStyle.height / aspectRatio;

const w = ui.tryGetItemWidth() ?? cw + checkboxStyle.spacing + painter.getTextWidth();
const w = ui.tryGetItemWidth() ?? cw + checkboxStyle.spacing + painter.getTextWidth(text, font, scale);
const h = style.item.height;

ui.beginItem(w, h);
Expand Down Expand Up @@ -51,7 +49,7 @@ export function checkBox(isChecked: boolean, text: string): boolean {
checkboxStyle.height / aspectRatio + checkboxStyle.spacing * 2,
(h - GetRenderedCharacterHeight(scale, font)) / 2 + style.item.textOffset
);
painter.drawText();
painter.drawText(text, font, scale);

ui.endItem();

Expand Down
8 changes: 4 additions & 4 deletions src/items/collapsingheader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ export function collapsingHeader(isCollapsed: boolean, text: string): boolean {
const font = collapsingHeaderProperties.get<number>('font-family');
const scale = collapsingHeaderProperties.get<number>('font-size');

painter.setText(font, scale, text);

const w =
ui.tryGetItemWidth() ??
painter.getTextWidth() + style.collapsingHeader.spacing * 3 + style.collapsingHeader.spriteWidth;
painter.getTextWidth(text, font, scale) +
style.collapsingHeader.spacing * 3 +
style.collapsingHeader.spriteWidth;
const h = style.item.height;

ui.beginItem(w, h);
Expand All @@ -37,7 +37,7 @@ export function collapsingHeader(isCollapsed: boolean, text: string): boolean {
style.collapsingHeader.spriteWidth,
sh / 2 - GetRenderedCharacterHeight(scale, font) / 2 + style.item.textOffset
);
painter.drawText();
painter.drawText(text, font, scale);

ui.endItem();

Expand Down
6 changes: 2 additions & 4 deletions src/items/heading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@ export function heading(text: string) {
const font = properties.get<number>('font-family');
const scale = properties.get<number>('font-size');

painter.setText(font, scale, text);

const w = ui.tryGetItemWidth() ?? painter.getTextWidth();
const w = ui.tryGetItemWidth() ?? painter.getTextWidth(text, font, scale);
const h = style.item.height;

ui.beginItem(w, h);

painter.setColor(properties.get<Color>('color'));
painter.move(0, (h - GetRenderedCharacterHeight(scale, font)) / 2 + style.item.textOffset);
painter.drawText();
painter.drawText(text, font, scale);

ui.endItem();
}
7 changes: 3 additions & 4 deletions src/items/hyperlink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ export function hyperlink(url: string, urlText: string | null) {
const hyperlinkProperties = style.getProperties(id);
const font = hyperlinkProperties.get<number>('font-family');
const scale = hyperlinkProperties.get<number>('font-size');
const text = urlText ?? url;

painter.setText(font, scale, urlText ?? url);

const w = ui.tryGetItemWidth() ?? painter.getTextWidth();
const w = ui.tryGetItemWidth() ?? painter.getTextWidth(text, font, scale);
const h = style.item.height;

ui.beginItem(w, h);
Expand All @@ -30,7 +29,7 @@ export function hyperlink(url: string, urlText: string | null) {
const properties = isHovered ? style.getProperties(`${id}:hover`) : hyperlinkProperties;
painter.setColor(properties.get<Color>('color'));
painter.move(0, (h - GetRenderedCharacterHeight(scale, font)) / 2 + style.item.textOffset);
painter.drawText();
painter.drawText(text, font, scale);

ui.endItem();
}
6 changes: 2 additions & 4 deletions src/items/label.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@ export function label(text: string) {
const font = properties.get<number>('font-family');
const scale = properties.get<number>('font-size');

painter.setText(font, scale, text);

const w = ui.tryGetItemWidth() ?? painter.getTextWidth();
const w = ui.tryGetItemWidth() ?? painter.getTextWidth(text, font, scale);
const h = style.item.height;

ui.beginItem(w, h);

painter.setColor(properties.get<Color>('color'));
painter.move(0, (h - GetRenderedCharacterHeight(scale, font)) / 2 + style.item.textOffset);
painter.drawText();
painter.drawText(text, font, scale);

ui.endItem();
}
6 changes: 2 additions & 4 deletions src/items/selectable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ export function selectable(isSelected: boolean, text: string): boolean {
const font = selectableProperties.get<number>('font-family');
const scale = selectableProperties.get<number>('font-size');

painter.setText(font, scale, text);

const w = ui.tryGetItemWidth() ?? painter.getTextWidth() + style.selectable.spacing * 2;
const w = ui.tryGetItemWidth() ?? painter.getTextWidth(text, font, scale) + style.selectable.spacing * 2;
const h = style.item.height;

ui.beginItem(w, h);
Expand All @@ -31,7 +29,7 @@ export function selectable(isSelected: boolean, text: string): boolean {

painter.setColor(properties.get<Color>('color'));
painter.move(style.selectable.spacing, (h - GetRenderedCharacterHeight(scale, font)) / 2 + style.item.textOffset);
painter.drawText();
painter.drawText(text, font, scale);

ui.endItem();

Expand Down
7 changes: 3 additions & 4 deletions src/items/spritebutton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ export function spriteButton(dict: string, name: string, text: string): boolean
const font = spriteButtonProperties.get<number>('font-family');
const scale = spriteButtonProperties.get<number>('font-size');

painter.setText(font, scale, text);

const spriteButtonStyle = style.spriteButton;
const sw = spriteButtonStyle.spriteWidth;

const w =
ui.tryGetItemWidth() || painter.getTextWidth() + style.button.spacing * 2 + spriteButtonStyle.spacing + sw;
ui.tryGetItemWidth() ||
painter.getTextWidth(text, font, scale) + style.button.spacing * 2 + spriteButtonStyle.spacing + sw;
const h = style.item.height;

ui.beginItem(w, h);
Expand All @@ -39,7 +38,7 @@ export function spriteButton(dict: string, name: string, text: string): boolean
sw + spriteButtonStyle.spacing,
-so + (h - GetRenderedCharacterHeight(scale, font)) / 2 + style.item.textOffset
);
painter.drawText();
painter.drawText(text, font, scale);

ui.endItem();

Expand Down
8 changes: 2 additions & 6 deletions src/items/textarea.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ export function textArea(text: string, w: number) {
const font = properties.get<number>('font-family');
const scale = properties.get<number>('font-size');

painter.setTextWidth(w);
painter.setText(font, scale, text);
const lc = painter.getTextLineCount();
const lc = painter.getTextLineCount(text, font, scale, w);

const h = lc === 1 ? style.item.height : GetRenderedCharacterHeight(scale, font) * (lc + 1);

Expand All @@ -22,9 +20,7 @@ export function textArea(text: string, w: number) {
painter.setColor(properties.get<Color>('color'));
if (lc == 1) painter.move(0, style.item.textOffset);

painter.setTextWidth(w);
painter.setText(font, scale, text);
painter.drawText();
painter.drawMultilineText(text, font, scale, w);

ui.endItem();
}
3 changes: 1 addition & 2 deletions src/items/textedit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ export async function textEdit(
const font = textEditProperties.get<number>('font-family');
const scale = textEditProperties.get<number>('font-size');

painter.setText(font, scale, isSecretMode ? text.replace(/./g, '*') : text);
painter.move(style.textEdit.spacing, (h - GetRenderedCharacterHeight(scale, font)) / 2 + style.item.textOffset);
painter.drawText();
painter.drawText(isSecretMode ? text.replace(/./g, '*') : text, font, scale);

ui.endItem();

Expand Down

0 comments on commit 2e6e425

Please sign in to comment.