From 2cb38358c8946219a5bddc44029880a36af77e59 Mon Sep 17 00:00:00 2001 From: warxander Date: Mon, 13 Nov 2023 16:27:14 +0300 Subject: [PATCH] refactor: Store `Style` as `Context` member to simplify `windowSpacing` management --- src/core/context.ts | 10 +++++++-- src/core/painter.ts | 40 ++++++++++++----------------------- src/exports.ts | 4 ++-- src/items/button.ts | 2 +- src/items/checkbox.ts | 2 +- src/items/collapsingheader.ts | 2 +- src/items/heading.ts | 2 +- src/items/hyperlink.ts | 2 +- src/items/label.ts | 2 +- src/items/progressbar.ts | 2 +- src/items/selectable.ts | 2 +- src/items/separator.ts | 2 +- src/items/slider.ts | 2 +- src/items/spacing.ts | 2 +- src/items/spritebutton.ts | 2 +- src/items/textarea.ts | 2 +- src/items/textedit.ts | 2 +- 17 files changed, 38 insertions(+), 44 deletions(-) diff --git a/src/core/context.ts b/src/core/context.ts index 3629146..f446521 100644 --- a/src/core/context.ts +++ b/src/core/context.ts @@ -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) {} @@ -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[] = []; @@ -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) { @@ -144,4 +146,8 @@ export class Context { getPainter(): Painter { return this.painter; } + + getStyle(): Style { + return this.style; + } } diff --git a/src/core/painter.ts b/src/core/painter.ts index 8c8dd93..21d8466 100644 --- a/src/core/painter.ts +++ b/src/core/painter.ts @@ -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; @@ -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(); @@ -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 ); @@ -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(); @@ -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; @@ -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 ); @@ -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; @@ -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; @@ -204,10 +196,6 @@ export class Painter { this.position.y += y; } - getStyle(): Style { - return this.style; - } - setColor(color: Color) { this.color = color; } @@ -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('window', 'color')); + this.setColor(this.context.getStyle().getProperty('window', 'color')); this.drawRect(w, h); } diff --git a/src/exports.ts b/src/exports.ts index 3df18bd..9f10073 100644 --- a/src/exports.ts +++ b/src/exports.ts @@ -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 */ diff --git a/src/items/button.ts b/src/items/button.ts index 2cd0141..55973b7 100644 --- a/src/items/button.ts +++ b/src/items/button.ts @@ -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); diff --git a/src/items/checkbox.ts b/src/items/checkbox.ts index d73a12e..ac66396 100644 --- a/src/items/checkbox.ts +++ b/src/items/checkbox.ts @@ -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); diff --git a/src/items/collapsingheader.ts b/src/items/collapsingheader.ts index fbaf51b..4838c5a 100644 --- a/src/items/collapsingheader.ts +++ b/src/items/collapsingheader.ts @@ -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); diff --git a/src/items/heading.ts b/src/items/heading.ts index 28fa704..4d779fb 100644 --- a/src/items/heading.ts +++ b/src/items/heading.ts @@ -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('font-family'); diff --git a/src/items/hyperlink.ts b/src/items/hyperlink.ts index e5eb1ec..0e84b01 100644 --- a/src/items/hyperlink.ts +++ b/src/items/hyperlink.ts @@ -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); diff --git a/src/items/label.ts b/src/items/label.ts index 4071b50..e923f28 100644 --- a/src/items/label.ts +++ b/src/items/label.ts @@ -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('font-family'); diff --git a/src/items/progressbar.ts b/src/items/progressbar.ts index 6600fbd..8007bac 100644 --- a/src/items/progressbar.ts +++ b/src/items/progressbar.ts @@ -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); diff --git a/src/items/selectable.ts b/src/items/selectable.ts index 3ec6527..01c6e8a 100644 --- a/src/items/selectable.ts +++ b/src/items/selectable.ts @@ -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); diff --git a/src/items/separator.ts b/src/items/separator.ts index a73348d..7adb7d6 100644 --- a/src/items/separator.ts +++ b/src/items/separator.ts @@ -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; diff --git a/src/items/slider.ts b/src/items/slider.ts index 2c5ae1e..fbda1eb 100644 --- a/src/items/slider.ts +++ b/src/items/slider.ts @@ -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; diff --git a/src/items/spacing.ts b/src/items/spacing.ts index fe29de3..1313ec3 100644 --- a/src/items/spacing.ts +++ b/src/items/spacing.ts @@ -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; diff --git a/src/items/spritebutton.ts b/src/items/spritebutton.ts index 629590f..c5d8fab 100644 --- a/src/items/spritebutton.ts +++ b/src/items/spritebutton.ts @@ -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); diff --git a/src/items/textarea.ts b/src/items/textarea.ts index 652772e..3174ec4 100644 --- a/src/items/textarea.ts +++ b/src/items/textarea.ts @@ -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('font-family'); diff --git a/src/items/textedit.ts b/src/items/textedit.ts index 24ab1dd..48c0ed4 100644 --- a/src/items/textedit.ts +++ b/src/items/textedit.ts @@ -14,7 +14,7 @@ export async function textEdit( isSecretMode: boolean ): Promise { const painter = context.getPainter(); - const style = painter.getStyle(); + const style = context.getStyle(); const _keyboardTitleEntry = 'VEIN_EDIT_KEYBOARD_TITLE';