Skip to content

Commit

Permalink
refactor: Store Style as Context member to simplify `windowSpacin…
Browse files Browse the repository at this point in the history
…g` management
  • Loading branch information
warxander committed Nov 13, 2023
1 parent 7842e2b commit 2cb3835
Show file tree
Hide file tree
Showing 17 changed files with 38 additions and 44 deletions.
10 changes: 8 additions & 2 deletions src/core/context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Rect, Vector2 } from '../exports';
import { Input, InputKey } from './input';
import { Painter } from './painter';
import { Style } from './style';

class ItemState {
constructor(public id: string | undefined = undefined, public width: number | undefined = undefined) {}
Expand All @@ -23,6 +24,7 @@ class WindowState {
export class Context {
private input = new Input();
private painter = new Painter(this);
private style = new Style();
private nextWindowState = new WindowState();
private nextItemState = new ItemState();
private itemWidthStack: number[] = [];
Expand Down Expand Up @@ -67,8 +69,8 @@ export class Context {
return this.nextWindowState.id;
}

getWindowSpacing(): Vector2 | undefined {
return this.nextWindowState.spacing;
getWindowSpacing(): Vector2 {
return this.nextWindowState.spacing ?? this.style.window.spacing;
}

beginWindow(x: number, y: number) {
Expand Down Expand Up @@ -144,4 +146,8 @@ export class Context {
getPainter(): Painter {
return this.painter;
}

getStyle(): Style {
return this.style;
}
}
40 changes: 14 additions & 26 deletions src/core/painter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export enum MouseCursor {
}

export class Painter {
private style = new Style();
private position = new Vector2();
private color: Color = [0, 0, 0, 255];
private mouseCursor: MouseCursor = MouseCursor.Normal;
Expand All @@ -34,7 +33,6 @@ export class Painter {
private rowState: RowState | null = null;
private windowRect = new Rect();
private windowDragPosition: Vector2 | null = null;
private windowSpacing = new Vector2();
private isFirstItem = true;
private itemRect = new Rect();

Expand All @@ -50,15 +48,11 @@ export class Painter {
this.windowRect.position = new Vector2(x, y);
this.setPosition(this.windowRect.position.x, this.windowRect.position.y);

const windowSpacing = this.context.getWindowSpacing();
this.windowSpacing.x = windowSpacing !== undefined ? windowSpacing.x : this.style.window.spacing.x;
this.windowSpacing.y = windowSpacing !== undefined ? windowSpacing.y : this.style.window.spacing.y;

if (!this.context.isWindowNoDrag()) this.beginWindowDrag();

if (!this.context.isWindowNoBackground())
this.drawItemBackground(
this.style.getProperties(this.context.getWindowId() ?? 'window'),
this.context.getStyle().getProperties(this.context.getWindowId() ?? 'window'),
this.windowRect.size.x,
this.windowRect.size.y
);
Expand All @@ -68,8 +62,8 @@ export class Painter {
if (!this.context.isWindowNoDrag()) this.endWindowDrag();

this.windowRect.size = new Vector2(
this.isFirstItem ? 0 : this.contentSize.x + this.style.window.margins.x * 2,
this.isFirstItem ? 0 : this.contentSize.y + this.style.window.margins.y * 2
this.isFirstItem ? 0 : this.contentSize.x + this.context.getStyle().window.margins.x * 2,
this.isFirstItem ? 0 : this.contentSize.y + this.context.getStyle().window.margins.y * 2
);

SetMouseCursorActiveThisFrame();
Expand All @@ -87,9 +81,10 @@ export class Painter {
const mousePosition = input.getMousePosition();

if (
!new Rect(this.position, new Vector2(this.windowRect.size.x, this.style.window.margins.y)).contains(
mousePosition
)
!new Rect(
this.position,
new Vector2(this.windowRect.size.x, this.context.getStyle().window.margins.y)
).contains(mousePosition)
)
return;

Expand Down Expand Up @@ -136,7 +131,7 @@ export class Painter {
);

this.setPosition(
this.windowRect.position.x + this.style.window.margins.x,
this.windowRect.position.x + this.context.getStyle().window.margins.x,
this.position.y + this.rowState.size.y
);

Expand All @@ -148,16 +143,17 @@ export class Painter {
}

beginItem(w: number, h: number) {
if (this.isFirstItem) this.move(this.style.window.margins.x, this.style.window.margins.y);
if (this.isFirstItem)
this.move(this.context.getStyle().window.margins.x, this.context.getStyle().window.margins.y);
else {
let ho = 0;
if (this.rowState && !this.rowState.isFirstItem) {
ho = this.windowSpacing.x;
ho = this.context.getWindowSpacing().x;
this.rowState.size.x += ho;
}

let vo = 0;
if (!this.rowState || this.rowState.isFirstItem) vo = this.windowSpacing.y;
if (!this.rowState || this.rowState.isFirstItem) vo = this.context.getWindowSpacing().y;

this.contentSize.x += ho;
this.contentSize.y += vo;
Expand Down Expand Up @@ -190,10 +186,6 @@ export class Painter {
return this.itemRect;
}

getWindowSpacing(): Vector2 {
return this.windowSpacing;
}

setPosition(x: number, y: number) {
this.position.x = x;
this.position.y = y;
Expand All @@ -204,10 +196,6 @@ export class Painter {
this.position.y += y;
}

getStyle(): Style {
return this.style;
}

setColor(color: Color) {
this.color = color;
}
Expand Down Expand Up @@ -288,11 +276,11 @@ export class Painter {
EndTextCommandDisplayText(this.position.x, this.position.y);
}

drawDebug(w: number, h = this.style.item.height) {
drawDebug(w: number, h = this.context.getStyle().item.height) {
if (!this.context.isDebugEnabled()) return;

this.setPosition(this.itemRect.position.x, this.itemRect.position.y);
this.setColor(this.style.getProperty<Color>('window', 'color'));
this.setColor(this.context.getStyle().getProperty<Color>('window', 'color'));
this.drawRect(w, h);
}

Expand Down
4 changes: 2 additions & 2 deletions src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,11 @@ export function popItemWidth() {
}

export function setStyleSheet(styleSheet: string) {
context.getPainter().getStyle().setSheet(styleSheet);
context.getStyle().setSheet(styleSheet);
}

export function useDefaultStyle() {
context.getPainter().getStyle().useDefault();
context.getStyle().useDefault();
}

/** Used as a selector name */
Expand Down
2 changes: 1 addition & 1 deletion src/items/button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Color } from '../exports';

export function button(text: string): boolean {
const painter = context.getPainter();
const style = painter.getStyle();
const style = context.getStyle();

const id = context.tryGetItemId() ?? 'button';
const buttonProperties = style.getProperties(id);
Expand Down
2 changes: 1 addition & 1 deletion src/items/checkbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Color } from '../exports';

export function checkBox(isChecked: boolean, text: string): boolean {
const painter = context.getPainter();
const style = painter.getStyle();
const style = context.getStyle();

const id = context.tryGetItemId() ?? 'check-box';
const checkBoxProperties = style.getProperties(id);
Expand Down
2 changes: 1 addition & 1 deletion src/items/collapsingheader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Color, context } from '../exports';

export function collapsingHeader(isCollapsed: boolean, text: string): boolean {
const painter = context.getPainter();
const style = painter.getStyle();
const style = context.getStyle();

const id = context.tryGetItemId() ?? 'collapsing-header';
const collapsingHeaderProperties = style.getProperties(id);
Expand Down
2 changes: 1 addition & 1 deletion src/items/heading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Color } from '../exports';

export function heading(text: string) {
const painter = context.getPainter();
const style = painter.getStyle();
const style = context.getStyle();

const properties = style.getProperties(context.tryGetItemId() ?? 'heading');
const font = properties.get<number>('font-family');
Expand Down
2 changes: 1 addition & 1 deletion src/items/hyperlink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Color } from '../exports';

export function hyperlink(url: string, urlText: string | null) {
const painter = context.getPainter();
const style = painter.getStyle();
const style = context.getStyle();

const id = context.tryGetItemId() ?? 'hyperlink';
const hyperlinkProperties = style.getProperties(id);
Expand Down
2 changes: 1 addition & 1 deletion src/items/label.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Color } from '../exports';

export function label(text: string) {
const painter = context.getPainter();
const style = painter.getStyle();
const style = context.getStyle();

const properties = style.getProperties(context.tryGetItemId() ?? 'label');
const font = properties.get<number>('font-family');
Expand Down
2 changes: 1 addition & 1 deletion src/items/progressbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Color } from '../exports';

export function progressBar(min: number, value: number, max: number, w: number) {
const painter = context.getPainter();
const style = painter.getStyle();
const style = context.getStyle();

context.beginItem(w, style.item.height);

Expand Down
2 changes: 1 addition & 1 deletion src/items/selectable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Color } from '../exports';

export function selectable(isSelected: boolean, text: string): boolean {
const painter = context.getPainter();
const style = painter.getStyle();
const style = context.getStyle();

const id = context.tryGetItemId() ?? 'selectable';
const selectableProperties = style.getProperties(id);
Expand Down
2 changes: 1 addition & 1 deletion src/items/separator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Color } from '../exports';

export function separator(w: number) {
const painter = context.getPainter();
const style = painter.getStyle();
const style = context.getStyle();

const h = style.item.height;

Expand Down
2 changes: 1 addition & 1 deletion src/items/slider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface ISliderResult {
export function slider(min: number, value: number, max: number, w: number): ISliderResult {
const input = context.getInput();
const painter = context.getPainter();
const style = painter.getStyle();
const style = context.getStyle();

const h = style.item.height;

Expand Down
2 changes: 1 addition & 1 deletion src/items/spacing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function spacing(count = 1) {

const isRowMode = painter.isRowMode();

const windowSpacing = painter.getWindowSpacing();
const windowSpacing = context.getWindowSpacing();
const w = isRowMode ? windowSpacing.x * count : 0;
const h = isRowMode ? 0 : windowSpacing.y * count;

Expand Down
2 changes: 1 addition & 1 deletion src/items/spritebutton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Color } from '../exports';

export function spriteButton(dict: string, name: string, text: string): boolean {
const painter = context.getPainter();
const style = painter.getStyle();
const style = context.getStyle();

const id = context.tryGetItemId() ?? 'sprite-button';
const spriteButtonProperties = style.getProperties(id);
Expand Down
2 changes: 1 addition & 1 deletion src/items/textarea.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Color } from '../exports';

export function textArea(text: string, w: number) {
const painter = context.getPainter();
const style = painter.getStyle();
const style = context.getStyle();

const properties = style.getProperties(context.tryGetItemId() ?? 'text-area');
const font = properties.get<number>('font-family');
Expand Down
2 changes: 1 addition & 1 deletion src/items/textedit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export async function textEdit(
isSecretMode: boolean
): Promise<ITextEditResult> {
const painter = context.getPainter();
const style = painter.getStyle();
const style = context.getStyle();

const _keyboardTitleEntry = 'VEIN_EDIT_KEYBOARD_TITLE';

Expand Down

0 comments on commit 2cb3835

Please sign in to comment.