Skip to content

Commit

Permalink
feat: Improve performance in some cases
Browse files Browse the repository at this point in the history
* Text parameter is empty
* Zero value parameter for progressBar
  • Loading branch information
warxander committed Feb 4, 2024
1 parent f29660f commit 99b6086
Show file tree
Hide file tree
Showing 17 changed files with 135 additions and 142 deletions.
22 changes: 13 additions & 9 deletions src/core/painter.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import { Color, Vector2 } from './types';

export class TextData {
constructor(public text: string, public font: number, public scale: number, public width?: number) {}
}
import { Color, TextData, Vector2 } from './types';

export class Painter {
private position: Vector2;
Expand Down Expand Up @@ -32,13 +28,17 @@ export class Painter {
}

drawRect(w: number, h: number) {
if (w === 0 || h === 0) return;

const sw = w * this.scale;
const sh = h * this.scale;

DrawRect(this.position.x + sw / 2, this.position.y + sh / 2, sw, sh, ...this.color);
}

drawSprite(dict: string, name: string, w: number, h: number, heading?: number) {
if (dict.length === 0 || name.length === 0 || w === 0 || h === 0) return;

const sw = w * this.scale;
const sh = h * this.scale;

Expand All @@ -50,20 +50,26 @@ export class Painter {
}

getTextWidth(textData: TextData): number {
if (textData.isEmpty()) return 0;

this.setText(textData, false);

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

getTextLineCount(textData: TextData): number {
if (textData.isEmpty()) return 0;

this.setWrappedText(textData, false);

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

drawText(textData: TextData) {
if (textData.isEmpty()) return 0;

this.setText(textData, true);
SetTextColour(...this.color);

Expand All @@ -72,6 +78,8 @@ export class Painter {
}

drawMultilineText(textData: TextData) {
if (textData.isEmpty()) return 0;

this.setWrappedText(textData, true);
SetTextColour(...this.color);

Expand All @@ -84,8 +92,6 @@ export class Painter {
}

private setText(textData: TextData, useScaling: boolean) {
if (textData.text.length === 0) return;

++this.textEntryIndex;
AddTextEntry(this.getTextEntry(), textData.text);

Expand All @@ -94,8 +100,6 @@ export class Painter {
}

private setWrappedText(textData: TextData, useScaling: boolean) {
if (textData.text.length === 0) return;

++this.textEntryIndex;
AddTextEntry(this.getTextEntry(), textData.text);

Expand Down
10 changes: 10 additions & 0 deletions src/core/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
export type Color = [number, number, number, number];
export type Image = [string, string];

export class TextData {
static readonly Empty = new TextData('', -1, -1);

constructor(public text: string, public font: number, public scale: number, public width?: number) {}

isEmpty(): boolean {
return this.text.length === 0;
}
}

export class Vector2 {
constructor(public x: number = 0, public y: number = 0) {}
}
Expand Down
16 changes: 14 additions & 2 deletions src/core/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import { Frame } from './frame';
import { Color, Image } from './types';
import { Color, Image, TextData } from './types';

export function createTextData(text: string, selector: string, w?: number): TextData {
if (text.length === 0) return TextData.Empty;

const style = Frame.getStyle();
return new TextData(
text,
style.getPropertyAs<number>(selector, 'font-family'),
style.getPropertyAs<number>(selector, 'font-size'),
w
);
}

export function drawItemBackground(frame: Frame, selector: string, w: number, h: number) {
const style = Frame.getStyle();
Expand All @@ -16,7 +28,7 @@ export function drawItemBackground(frame: Frame, selector: string, w: number, h:
}
}

export function getDefaultStyleSelectorState(frame: Frame): string | undefined {
export function getStyleSelectorState(frame: Frame): string | undefined {
return frame.isItemDisabled()
? 'disabled'
: frame.isItemPressed()
Expand Down
Loading

0 comments on commit 99b6086

Please sign in to comment.