Visualize Your Ideas With Code
refactor: better surface handling
aarthificial3b58069d36db
diff --git a/src/animations/surfaceTransition.ts b/src/animations/surfaceTransition.tsindex 141b68c6..7f61edea 100644--- a/src/animations/surfaceTransition.ts+++ b/src/animations/surfaceTransition.ts@@ -73,10 +73,10 @@ export function surfaceTransition(fromSurfaceOriginal: Surface) {target.setMask({...from,...value.rectArc(from, to, config.reverse),- radius: value.easeInOutCubic(from.radius, to.radius),+ radius: value.easeInOutCubic(from.radius, target.radius()),color: value.color(from.color,- target.getChild().getColor(),+ target.background(),value.easeInOutQuint(),),});@@ -100,10 +100,10 @@ export function surfaceTransition(fromSurfaceOriginal: Surface) {fromSurface.setMask({...from,...value.rectArc(from, to, config.reverse),- radius: value.easeInOutCubic(from.radius, to.radius),+ radius: value.easeInOutCubic(from.radius, target.radius()),color: value.color(from.color,- target.getChild().getColor(),+ target.background(),value.easeInOutQuint(),),});diff --git a/src/components/ColorPicker.ts b/src/components/ColorPicker.tsxsimilarity index 51%rename from src/components/ColorPicker.tsrename to src/components/ColorPicker.tsxindex 40caf415..d1a22c3f 100644--- a/src/components/ColorPicker.ts+++ b/src/components/ColorPicker.tsx@@ -1,14 +1,18 @@-import {LinearLayout, LinearLayoutConfig} from 'MC/components/LinearLayout';+import {LinearLayout, LinearLayoutConfig} from './LinearLayout';import {Rect} from 'konva/lib/shapes/Rect';-import {Range, RangeConfig} from 'MC/components/Range';-import {Node} from 'konva/lib/Node';-import {AnimatedGetSet, getset} from '../decorators';-import mixColor, {parseColor} from 'mix-color';+import {Range, RangeConfig} from './Range';+import {AnimatedGetSet, getset, KonvaNode} from '../decorators';+import {parseColor} from 'mix-color';import {TimeTween} from '../animations';+import {Surface} from './Surface';+import {Origin} from '../types';+import {Style} from '../styles';+import {GetSet} from 'konva/lib/types';export interface ColorPickerConfig extends LinearLayoutConfig {previewColor?: string;dissolve?: number;+ style?: Style;}const colorRangeConfig: RangeConfig = {@@ -20,7 +24,10 @@ const colorRangeConfig: RangeConfig = {value: 0,};-export class ColorPicker extends LinearLayout {+@KonvaNode()+export class ColorPicker extends Surface {+ @getset(null)+ public style: GetSet<ColorPickerConfig['style'], this>;@getset('#000000', ColorPicker.prototype.updateColor)public previewColor: AnimatedGetSet<ColorPickerConfig['previewColor'], this>;@getset(0, ColorPicker.prototype.updateDissolve)@@ -35,49 +42,58 @@ export class ColorPicker extends LinearLayout {public constructor(config?: ColorPickerConfig) {super(config);- this.preview = new Rect({- width: 360,- height: 200,- fill: 'yellow',- cornerRadius: [8, 8, 0, 0],- });- this.r = new Range({- ...colorRangeConfig,- label: 'R:',- margin: [40, 10, 10],- });- this.g = new Range({- ...colorRangeConfig,- label: 'G:',- });- this.b = new Range({- ...colorRangeConfig,- label: 'B:',- });- this.a = new Range({- ...colorRangeConfig,- label: 'A:',- margin: [10, 10, 40],- });+ this.setChild(+ <LinearLayout origin={Origin.Top}>+ <Rect+ ref={[this, 'preview']}+ width={360}+ height={200}+ fill={'yellow'}+ cornerRadius={[8, 8, 0, 0]}+ />+ <Range+ ref={[this, 'r']}+ {...colorRangeConfig}+ label={'R:'}+ margin={[40, 10, 10]}+ />+ <Range ref={[this, 'g']} {...colorRangeConfig} label={'G:'} />+ <Range ref={[this, 'b']} {...colorRangeConfig} label={'B:'} />+ <Range+ ref={[this, 'a']}+ {...colorRangeConfig}+ label={'A:'}+ margin={[10, 10, 40]}+ />+ </LinearLayout>,+ );- this.add(this.preview, this.r, this.g, this.b, this.a);this.updateColor();this.updateDissolve();}private updateColor() {- const color = parseColor(this.previewColor());+ const style = this.style();+ const preview = this.previewColor();++ this.style({+ ...style,+ foreground: preview,+ });+ const color = parseColor(preview);color.a = Math.round(color.a * 255);this.r.value(color.r);this.g.value(color.g);this.b.value(color.b);this.a.value(color.a);- this.r.foregroundColor(this.previewColor());- this.g.foregroundColor(this.previewColor());- this.b.foregroundColor(this.previewColor());- this.a.foregroundColor(this.previewColor());- this.preview.fill(this.previewColor());+ this.preview.fill(preview);+ }++ public setStyle(value: Style): this {+ this.attrs.style = value;+ this.background(value.background);+ return this;}private updateDissolve() {@@ -92,18 +108,6 @@ export class ColorPicker extends LinearLayout {this.fireLayoutChange();}- getColor(): string {- return mixColor(- super.getColor(),- this.previewColor(),- TimeTween.clampRemap(0.5, 1, 0, 1, this.dissolve()),- );- }-- clone(obj?: any): this {- return Node.prototype.clone.call(this, obj);- }-protected handleLayoutChange() {if (this.preview) {const rangeWidth = this.preview.width() - 80;diff --git a/src/components/Connection.ts b/src/components/Connection.tsindex 0ecd5d4f..b05efa58 100644--- a/src/components/Connection.ts+++ b/src/components/Connection.ts@@ -7,7 +7,7 @@ import {Direction} from '../types';import {Context} from 'konva/lib/Context';import {TimeTween} from '../animations';import {Project} from '../Project';-import {LAYOUT_CHANGE_EVENT} from "MC/components/ILayoutNode";+import {LAYOUT_CHANGE_EVENT} from "./ILayoutNode";export interface ConnectionPoint {node: Node;diff --git a/src/components/ILayoutNode.ts b/src/components/ILayoutNode.tsindex 21146a39..9c934d39 100644--- a/src/components/ILayoutNode.ts+++ b/src/components/ILayoutNode.ts@@ -6,8 +6,8 @@ import {originPosition,} from '../types';import {Node} from 'konva/lib/Node';-import {LayoutGroup} from 'MC/components/LayoutGroup';-import {LayoutShape} from 'MC/components/LayoutShape';+import {LayoutGroup} from './LayoutGroup';+import {LayoutShape} from './LayoutShape';import {IRect, Vector2d} from 'konva/lib/types';import {Shape} from 'konva/lib/Shape';import {Group} from 'konva/lib/Group';@@ -15,18 +15,14 @@ import {Group} from 'konva/lib/Group';export const LAYOUT_CHANGE_EVENT = 'layoutChange';export interface LayoutAttrs {- radius: number;margin: PossibleSpacing;padd: PossibleSpacing;- color: string;origin: Origin;}export interface ILayoutNode {- getRadius(): number;getMargin(): Spacing;getPadd(): Spacing;- getColor(): string;getOrigin(): Origin;getOriginOffset(custom?: Partial<LayoutAttrs>): Vector2d;getOriginDelta(newOrigin: Origin, custom?: Partial<LayoutAttrs>): Vector2d;diff --git a/src/components/LayoutGroup.ts b/src/components/LayoutGroup.tsindex f89a3049..f376f524 100644--- a/src/components/LayoutGroup.ts+++ b/src/components/LayoutGroup.ts@@ -27,16 +27,6 @@ export abstract class LayoutGroup extends Group implements ILayoutNode {public abstract getLayoutSize(custom?: LayoutGroupConfig): Size;- public setRadius(value: number): this {- this.attrs.radius = value;- this.fireLayoutChange();- return this;- }-- public getRadius(): number {- return this.attrs.radius ?? 0;- }-public setMargin(value: PossibleSpacing): this {this.attrs.margin = new Spacing(value);this.fireLayoutChange();@@ -57,16 +47,6 @@ export abstract class LayoutGroup extends Group implements ILayoutNode {return this.attrs.padd ?? new Spacing();}- public setColor(value: string): this {- this.attrs.color = value;- this.fireLayoutChange();- return this;- }-- public getColor(): string {- return this.attrs.color ?? '#000';- }-public setOrigin(value: Origin): this {if (!isInsideLayout(this)) {this.move(this.getOriginDelta(value));diff --git a/src/components/LayoutShape.ts b/src/components/LayoutShape.tsindex 3a777dcd..0243a781 100644--- a/src/components/LayoutShape.ts+++ b/src/components/LayoutShape.ts@@ -30,16 +30,6 @@ export abstract class LayoutShape extends Shape implements ILayoutNode {return padding.expand(this.getSize());}- public setRadius(value: number): this {- this.attrs.radius = value;- this.fireLayoutChange();- return this;- }-- public getRadius(): number {- return this.attrs.radius ?? 0;- }-public setMargin(value: PossibleSpacing): this {this.attrs.margin = new Spacing(value);this.fireLayoutChange();@@ -60,16 +50,6 @@ export abstract class LayoutShape extends Shape implements ILayoutNode {return this.attrs.padd ?? new Spacing();}- public setColor(value: string): this {- this.attrs.color = value;- this.fireLayoutChange();- return this;- }-- public getColor(): string {- return this.attrs.color ?? '#000';- }-public setOrigin(value: Origin): this {if (!isInsideLayout(this)) {this.move(this.getOriginDelta(value));diff --git a/src/components/LayoutText.ts b/src/components/LayoutText.tsindex 44ebb18e..f556414b 100644--- a/src/components/LayoutText.ts+++ b/src/components/LayoutText.ts@@ -22,8 +22,6 @@ export class LayoutText extends Text implements ILayoutNode {public constructor(config?: LayoutTextConfig) {super({- color: '#c0b3a3',- radius: 40,padd: new Spacing(30),align: 'center',verticalAlign: 'middle',@@ -51,16 +49,6 @@ export class LayoutText extends Text implements ILayoutNode {};}- public setRadius(value: number): this {- this.attrs.radius = value;- this.fireLayoutChange();- return this;- }-- public getRadius(): number {- return this.attrs.radius ?? 0;- }-public setMargin(value: PossibleSpacing): this {this.attrs.margin = new Spacing(value);this.fireLayoutChange();@@ -81,16 +69,6 @@ export class LayoutText extends Text implements ILayoutNode {return this.attrs.padd ?? new Spacing();}- public setColor(value: string): this {- this.attrs.color = value;- this.fireLayoutChange();- return this;- }-- public getColor(): string {- return this.attrs.color ?? '#000';- }-public setMinWidth(value: number): this {this.attrs.minWidth = value;this.fireLayoutChange();diff --git a/src/components/LinearLayout.ts b/src/components/LinearLayout.tsindex f7149620..046d9258 100644--- a/src/components/LinearLayout.ts+++ b/src/components/LinearLayout.ts@@ -1,18 +1,21 @@import {Group} from 'konva/lib/Group';import {GetSet, IRect} from 'konva/lib/types';-import {Factory} from 'konva/lib/Factory';import {Shape} from 'konva/lib/Shape';import {LayoutGroup, LayoutGroupConfig} from './LayoutGroup';import {Center, Origin, Size, Spacing} from '../types';import {Container} from 'konva/lib/Container';-import {getClientRect, getOriginDelta, isLayoutNode} from "MC/components/ILayoutNode";+import {getClientRect, getOriginDelta, isLayoutNode} from "./ILayoutNode";+import {getset, KonvaNode} from "../decorators";export interface LinearLayoutConfig extends LayoutGroupConfig {direction?: Center;}+@KonvaNode()export class LinearLayout extends LayoutGroup {+ @getset(Center.Vertical, LinearLayout.prototype.handleLayoutChange)public direction: GetSet<Center, this>;+private contentSize: Size;constructor(config?: LinearLayoutConfig) {@@ -78,12 +81,3 @@ export class LinearLayout extends LayoutGroup {return getClientRect(this, config);}}--Factory.addGetterSetter(- LinearLayout,- 'direction',- Center.Vertical,- undefined,- // @ts-ignore- LinearLayout.prototype.recalculate,-);diff --git a/src/components/Range.ts b/src/components/Range.tsindex c0dfbab6..9d5bf111 100644--- a/src/components/Range.ts+++ b/src/components/Range.ts@@ -1,33 +1,29 @@import {LayoutShape, LayoutShapeConfig} from './LayoutShape';import {Context} from 'konva/lib/Context';-import {AnimatedGetSet, getset, KonvaNode} from 'MC/decorators';-import {CanvasHelper} from 'MC/helpers';-import {TimeTween} from 'MC/animations';+import {AnimatedGetSet, getset, KonvaNode} from '../decorators';+import {CanvasHelper} from '../helpers';+import {TimeTween} from '../animations';+import {GetSet} from "konva/lib/types";+import {getFontColor, getStyle, Style} from "../styles";export interface RangeConfig extends LayoutShapeConfig {range?: [number, number];value?: number;precision?: number;- backgroundColor?: string;- foregroundColor?: string;- textColor?: string;label?: string;+ style?: Style;}@KonvaNode()export class Range extends LayoutShape {+ @getset(null)+ public style: GetSet<RangeConfig['style'], this>;@getset([0, 1])public range: AnimatedGetSet<RangeConfig['range'], this>;@getset(0.5)public value: AnimatedGetSet<RangeConfig['value'], this>;@getset(0)public precision: AnimatedGetSet<RangeConfig['precision'], this>;- @getset('#141414')- public backgroundColor: AnimatedGetSet<RangeConfig['backgroundColor'], this>;- @getset('rgba(255, 255, 255, 0.24)')- public foregroundColor: AnimatedGetSet<RangeConfig['foregroundColor'], this>;- @getset('rgba(255, 255, 255, 0.54')- public textColor: AnimatedGetSet<RangeConfig['textColor'], this>;@getset(null)public label: AnimatedGetSet<RangeConfig['label'], this>;@@ -36,6 +32,8 @@ export class Range extends LayoutShape {}public _sceneFunc(context: Context) {+ const style = getStyle(this);+const ctx = context._context;const size = this.getSize();const value = this.value();@@ -46,6 +44,10 @@ export class Range extends LayoutShape {minimumFractionDigits: precision,maximumFractionDigits: precision,});+ const minText = range[0].toLocaleString('en-EN', {+ minimumFractionDigits: precision,+ maximumFractionDigits: precision,+ });const position = {x: size.width / -2,@@ -57,7 +59,7 @@ export class Range extends LayoutShape {size.width -= 60;}- ctx.fillStyle = this.backgroundColor();+ ctx.fillStyle = style.backgroundLight;CanvasHelper.roundRect(ctx,position.x,@@ -68,8 +70,11 @@ export class Range extends LayoutShape {);ctx.fill();- const width = TimeTween.remap(range[0], range[1], 16, size.width, value);- ctx.fillStyle = this.foregroundColor();+ ctx.font = style.bodyFont;+ const textSize = ctx.measureText(minText);++ const width = TimeTween.remap(range[0], range[1], textSize.width + 40, size.width, value);+ ctx.fillStyle = style.foreground;CanvasHelper.roundRect(ctx,position.x,@@ -80,11 +85,12 @@ export class Range extends LayoutShape {);ctx.fill();- ctx.fillStyle = this.textColor();- ctx.font = 'bold 28px "JetBrains Mono"';+ ctx.font = style.bodyFont;+ ctx.fillStyle = getFontColor(style.foreground);ctx.fillText(text, position.x + 20, 10);if (label) {+ ctx.fillStyle = getFontColor(style.backgroundLight);ctx.fillText(label, position.x - 60, 10);}}diff --git a/src/components/Surface.ts b/src/components/Surface.tsindex 6805730e..bc634629 100644--- a/src/components/Surface.ts+++ b/src/components/Surface.ts@@ -1,24 +1,21 @@-import {Group} from 'konva/lib/Group';-import {Container, ContainerConfig} from 'konva/lib/Container';+import {Container} from 'konva/lib/Container';import {Rect} from 'konva/lib/shapes/Rect';-import {Shape} from 'konva/lib/Shape';import {parseColor} from 'mix-color';-import {LayoutGroup} from './LayoutGroup';+import {LayoutGroup, LayoutGroupConfig} from './LayoutGroup';import {Origin, Size} from '../types';import {getClientRect,getOriginDelta,getOriginOffset,- isLayoutNode,- LayoutAttrs,LayoutNode,} from './ILayoutNode';import {CanvasHelper} from '../helpers';import {Context} from 'konva/lib/Context';import {tween} from '../animations';-import {IRect} from 'konva/lib/types';+import {GetSet, IRect} from 'konva/lib/types';+import {getset, threadable} from '../decorators';+import {Node} from 'konva/lib/Node';-export type LayoutData = LayoutAttrs & Size;export interface SurfaceMask {width: number;height: number;@@ -32,38 +29,59 @@ export interface CircleMask {y: number;}-export interface SurfaceConfig extends ContainerConfig {+export interface SurfaceConfig extends LayoutGroupConfig {+ radius?: number;origin?: Origin;circleMask?: CircleMask;+ background?: string;+ child?: LayoutNode;}export class Surface extends LayoutGroup {- private box: Rect;- private ripple: Rect;- private child: LayoutNode;+ @getset(0, Surface.prototype.handleLayoutChange)+ public radius: GetSet<SurfaceConfig['radius'], this>;+ @getset('#FF00FF', Surface.prototype.handleLayoutChange)+ public background: GetSet<SurfaceConfig['background'], this>;+ @getset(null)+ public child: GetSet<SurfaceConfig['child'], this>;++ private readonly box: Rect;+ private readonly ripple: Rect;private surfaceMask: SurfaceMask | null = null;private circleMask: CircleMask | null = null;- private layoutData: LayoutData;+ private layoutData: Size;public constructor(config?: SurfaceConfig) {super(config);- this.add(- new Rect({- x: 0,- y: 0,- fill: '#242424',- name: 'ripple',- visible: false,- }),- );- this.add(- new Rect({- x: 0,- y: 0,- fill: '#242424',- name: 'box',- }),- );+ this.ripple = new Rect({+ x: 0,+ y: 0,+ fill: '#242424',+ name: 'ripple',+ visible: false,+ });+ this.box = new Rect({+ x: 0,+ y: 0,+ fill: '#242424',+ name: 'box',+ });++ this.add(this.ripple, this.box);+ this.handleLayoutChange();+ }++ public setChild(value: LayoutNode): this {+ this.attrs.child?.remove();+ this.attrs.child = value;+ this.add(value);+ this.handleLayoutChange();++ return this;+ }++ public getChild<T extends LayoutNode>(): T {+ return <T>this.attrs.child;}public setCircleMask(value: CircleMask | null): this {@@ -75,6 +93,21 @@ export class Surface extends LayoutGroup {return this.circleMask ?? null;}+ public clone(obj?: any): this {+ const clone: this = Node.prototype.clone.call(this, obj);+ const child = this.child();+ if (child) {+ clone.setChild(child.clone());+ }+ this.getChildren().forEach(node => {+ if (node !== child && node !== this.box && node !== this.ripple) {+ clone.add(node.clone());+ }+ });++ return clone;+ }+getLayoutSize(custom?: SurfaceConfig): Size {return {width: this.surfaceMask?.width ?? this.layoutData?.width ?? 0,@@ -82,49 +115,23 @@ export class Surface extends LayoutGroup {};}- add(...children: (Shape | Group)[]): this {- super.add(...children);- const child = children.find<LayoutNode>(isLayoutNode);- const ripple = children.find<Rect>((child): child is Rect =>- child.hasName('ripple'),- );- const box = children.find<Rect>((child): child is Rect =>- child.hasName('box'),- );-- if (box) {- this.box?.destroy();- this.box = box;- }- if (ripple) {- this.ripple?.destroy();- this.ripple = ripple;- }-- if (child) {- this.child = child;- this.handleLayoutChange();- }-- return this;- }-+ @threadable()public *doRipple() {if (this.surfaceMask) return;- const opaque = parseColor(this.layoutData.color);+ const opaque = parseColor(this.background());this.ripple.show();this.ripple.offsetX(this.layoutData.width / 2).offsetY(this.layoutData.height / 2).width(this.layoutData.width).height(this.layoutData.height)- .cornerRadius(this.layoutData.radius)+ .cornerRadius(this.radius()).fill(`rgba(${opaque.r}, ${opaque.g}, ${opaque.b}, ${0.5})`);yield* tween(1, value => {const width = this.layoutData.width + value.easeOutExpo(0, 100);const height = this.layoutData.height + value.easeOutExpo(0, 100);- const radius = this.layoutData.radius + value.easeOutExpo(0, 50);+ const radius = this.radius() + value.easeOutExpo(0, 50);this.ripple.offsetX(width / 2)@@ -143,10 +150,6 @@ export class Surface extends LayoutGroup {this.ripple.hide();}- public getChild<T extends LayoutNode>(): T {- return <T>this.child;- }-public setMask(data: SurfaceMask) {if (data === null) {this.surfaceMask = null;@@ -161,9 +164,10 @@ export class Surface extends LayoutGroup {.height(100000);}+ const child = this.child();const newOffset = getOriginOffset(data, this.getOrigin());- const contentSize = this.child.getLayoutSize();- const contentMargin = this.child.getMargin();+ const contentSize = child.getLayoutSize();+ const contentMargin = child.getMargin();const scale = Math.min(1,data.width / (contentSize.width + contentMargin.x),@@ -171,69 +175,63 @@ export class Surface extends LayoutGroup {this.surfaceMask = data;this.offset(newOffset);- this.child.scaleX(scale);- this.child.scaleY(scale);- this.child.position(- getOriginDelta(data, Origin.Middle, this.child.getOrigin()),- );+ child.scaleX(scale);+ child.scaleY(scale);+ child.position(getOriginDelta(data, Origin.Middle, child.getOrigin()));this.box.fill(data.color);this.fireLayoutChange();}public getMask(): SurfaceMask {- return this.layoutData;+ return {+ ...this.layoutData,+ radius: this.radius(),+ color: this.background(),+ };}protected handleLayoutChange() {if (this.surfaceMask) return;this.layoutData ??= {- origin: Origin.Middle,- color: '#F0F',- height: 0,width: 0,- padd: 0,- margin: 0,- radius: 0,+ height: 0,};- this.layoutData.origin = this.getOrigin();- if (this.child) {- const size = this.child.getLayoutSize();- const margin = this.child.getMargin();- const scale = this.child.getAbsoluteScale(this);+ const child = this.child();+ if (child) {+ const size = child.getLayoutSize();+ const margin = child.getMargin();+ const scale = child.getAbsoluteScale(this);const padding = this.getPadd();this.layoutData = {- ...this.layoutData,width: (size.width + margin.x + padding.x) * scale.x,height: (size.height + margin.y + padding.y) * scale.y,- radius: this.child.getRadius(),- color: this.child.getColor(),};- const offset = this.child.getOriginDelta(Origin.Middle);- this.child.position({+ const offset = child.getOriginDelta(Origin.Middle);+ child.position({x: -offset.x,y: -offset.y,});}- this.updateBackground(this.layoutData);+ this.updateBackground();this.setOrigin(this.getOrigin());this.fireLayoutChange();}- private updateBackground(data: LayoutData) {- if (this.box) {- this.box- .offsetX(data.width / 2)- .offsetY(data.height / 2)- .width(data.width)- .height(data.height)- .cornerRadius(data.radius)- .fill(data.color);- }+ private updateBackground() {+ if (!this.box) return;+ const size = this.getLayoutSize();+ this.box+ .offsetX(size.width / 2)+ .offsetY(size.height / 2)+ .width(size.width)+ .height(size.height)+ .cornerRadius(this.radius())+ .fill(this.background());}private drawMask(ctx: Context) {diff --git a/src/components/ThreeView.ts b/src/components/ThreeView.tsindex 7020da10..4ef56cca 100644--- a/src/components/ThreeView.ts+++ b/src/components/ThreeView.ts@@ -1,9 +1,11 @@import {LayoutShape, LayoutShapeConfig} from './LayoutShape';-import {Size} from '../types';+import {PossibleSpacing, Size} from '../types';import {Util} from 'konva/lib/Util';import {Context} from 'konva/lib/Context';import * as THREE from 'three';import {CanvasHelper} from '../helpers';+import {GetSet} from "konva/lib/types";+import {getset} from "../decorators";export interface ThreeViewConfig extends LayoutShapeConfig {canvasSize: Size;@@ -12,6 +14,8 @@ export interface ThreeViewConfig extends LayoutShapeConfig {skipFrames?: number;scene?: THREE.Scene;camera?: THREE.Camera;+ radius?: PossibleSpacing;+ background?: string;}interface Pool<T> {@@ -41,6 +45,21 @@ class RendererPool implements Pool<THREE.WebGLRenderer> {const rendererPool = new RendererPool();export class ThreeView extends LayoutShape {+ @getset(null)+ public scene: GetSet<ThreeViewConfig['scene'], this>+ @getset(null)+ public camera: GetSet<ThreeViewConfig['camera'], this>+ @getset({width: 0, height: 0}, ThreeView.prototype.handleCanvasSizeChange)+ public canvasSize: GetSet<ThreeViewConfig['canvasSize'], this>+ @getset(1, ThreeView.prototype.handleCanvasSizeChange)+ public cameraScale: GetSet<ThreeViewConfig['cameraScale'], this>+ @getset(1, ThreeView.prototype.handleCanvasSizeChange)+ public quality: GetSet<ThreeViewConfig['quality'], this>+ @getset(0)+ public skipFrames: GetSet<ThreeViewConfig['skipFrames'], this>+ @getset(0)+ public radius: GetSet<ThreeViewConfig['radius'], this>+private readonly renderer: THREE.WebGLRenderer;private readonly context: WebGLRenderingContext;@@ -54,67 +73,8 @@ export class ThreeView extends LayoutShape {this.handleCanvasSizeChange();}- public setScene(value: THREE.Scene): this {- this.attrs.scene = value;- return this;- }-- public getScene(): THREE.Scene {- return this.attrs.scene ?? null;- }-- public setCamera(value: THREE.Camera): this {- this.attrs.camera = value;- return this;- }-- public getCamera<TCamera extends THREE.Camera>(): TCamera {- return this.attrs.camera ?? null;- }-- public setCanvasSize(value: Size): this {- this.attrs.canvasSize = value;- this.handleCanvasSizeChange();- return this;- }-- public getCanvasSize(): Size {- return this.attrs.canvasSize- ? {...this.attrs.canvasSize}- : {width: 0, height: 0};- }-- public setCameraScale(value: number): this {- this.attrs.cameraScale = value;- this.handleCanvasSizeChange();- return this;- }-- public getCameraScale(): number {- return this.attrs.cameraScale ?? 1;- }-- public setQuality(value: number): this {- this.attrs.quality = value;- this.handleCanvasSizeChange();- return this;- }-- public getQuality(): number {- return this.attrs.quality ?? 1;- }-- public setSkipFrames(value: number): this {- this.attrs.skipFrames = value;- return this;- }-- public getSkipFrames(): number {- return this.attrs.skipFrames ?? 0;- }-- setColor(value: string): this {- const scene = this.getScene();+ setBackground(value: string): this {+ const scene = this.scene();if (scene) {scene.background = new THREE.Color(value);}@@ -122,8 +82,8 @@ export class ThreeView extends LayoutShape {return this;}- getColor(): string {- const background = this.getScene()?.background;+ getBackground(): string {+ const background = this.scene()?.background;return background instanceof THREE.Color? background.getHexString(): '#000000';@@ -138,11 +98,11 @@ export class ThreeView extends LayoutShape {private handleCanvasSizeChange() {if (!this.renderer) return;- const size = this.getCanvasSize();- const camera = this.getCamera();+ const size = this.canvasSize();+ const camera = this.camera();const ratio = size.width / size.height;- const scale = this.getCameraScale() / 2;+ const scale = this.cameraScale() / 2;if (camera instanceof THREE.OrthographicCamera) {camera.left = -ratio * scale;camera.right = ratio * scale;@@ -154,22 +114,22 @@ export class ThreeView extends LayoutShape {camera.updateProjectionMatrix();}- size.width *= this.getQuality();- size.height *= this.getQuality();+ size.width *= this.quality();+ size.height *= this.quality();this.renderer.setSize(size.width, size.height);}getLayoutSize(): Size {- return this.getCanvasSize();+ return this.canvasSize();}_sceneFunc(context: Context) {- const scale = this.getQuality();- const size = this.getCanvasSize();+ const scale = this.quality();+ const size = this.canvasSize();if (this.renderedFrames < 1) {- this.renderedFrames = this.getSkipFrames();- this.renderer.render(this.getScene(), this.getCamera());+ this.renderedFrames = this.skipFrames();+ this.renderer.render(this.scene(), this.camera());} else {this.renderedFrames--;}@@ -184,7 +144,7 @@ export class ThreeView extends LayoutShape {size.height / -2,size.width,size.height,- this.getRadius(),+ this.radius(),),);context._context.drawImage(diff --git a/src/helpers/CanvasHelper.ts b/src/helpers/CanvasHelper.tsindex a6be49b6..6d741218 100644--- a/src/helpers/CanvasHelper.ts+++ b/src/helpers/CanvasHelper.ts@@ -1,5 +1,5 @@import {Context} from 'konva/lib/Context';-import {PossibleSpacing, Spacing} from "MC/types";+import {PossibleSpacing, Spacing} from "../types";export namespace CanvasHelper {export function roundRect<T extends CanvasRenderingContext2D | Context>(diff --git a/src/jsx-runtime.ts b/src/jsx-runtime.tsindex 01b08ab9..2a6750d5 100644--- a/src/jsx-runtime.ts+++ b/src/jsx-runtime.ts@@ -1,23 +1,37 @@import {Node} from 'konva/lib/Node';import {Container} from 'konva/lib/Container';+import {Surface} from './components';+import {LayoutNode} from './components/ILayoutNode';export const Fragment = Symbol.for('mc.fragment');export function jsx<TNode extends Node, TConfig>(type: new (config?: TConfig) => TNode,- config: TConfig & {children?: Node | Node[], ref?: {value: TNode}},+ config: TConfig & {children?: Node | Node[]; ref?: {value: TNode} | [any, string]},maybeKey: string,): TNode {const {children, ref, ...rest} = config;const node = new type(<TConfig>rest);- if (children && node instanceof Container) {- if (Array.isArray(children)) {- node.add(...children);- } else {- node.add(children);+ if (children) {+ if (node instanceof Surface) {+ if (Array.isArray(children)) {+ node.setChild(<LayoutNode>children[0]);+ } else {+ node.setChild(<LayoutNode>children);+ }+ } else if (node instanceof Container) {+ if (Array.isArray(children)) {+ node.add(...children);+ } else {+ node.add(children);+ }}}if (ref) {- ref.value = node;+ if (Array.isArray(ref)) {+ ref[0][ref[1]] = node;+ } else {+ ref.value = node;+ }}return node;diff --git a/src/styles/Style.ts b/src/styles/Style.tsnew file mode 100644index 00000000..8884a46e--- /dev/null+++ b/src/styles/Style.ts@@ -0,0 +1,50 @@+import {Node} from 'konva/lib/Node';+import {parseColor} from 'mix-color';++export interface Style {+ labelFont: string;+ bodyFont: string;+ background: string;+ backgroundLight: string;+ foreground: string;+ foregroundLight: string;+}++export const MISSING_STYLE: Style = {+ labelFont: '20px "Times New Roman"',+ bodyFont: '20px Arial',+ background: '#FF00FF',+ backgroundLight: '#FF00FF',+ foreground: '#FF00FF',+ foregroundLight: '#FF00FF',+};++export function getStyle(node: Node): Style {+ let mergedStyle = {};++ do {+ //@ts-ignore+ const style = node.style?.() ?? null;+ if (style) {+ mergedStyle = {+ ...style,+ ...mergedStyle,+ };+ }++ node = node.getParent();+ } while (node);++ return {+ ...MISSING_STYLE,+ ...mergedStyle,+ };+}++export function getFontColor(background: string) {+ const color = parseColor(background);+ const brightness = Math.round(+ (color.r * 299 + color.g * 587 + color.b * 114) / 1000,+ );+ return brightness > 125 ? 'black' : 'white';+}diff --git a/src/styles/index.ts b/src/styles/index.tsnew file mode 100644index 00000000..766e846d--- /dev/null+++ b/src/styles/index.ts@@ -0,0 +1 @@+export * from './Style';\ No newline at end of filediff --git a/tsconfig.json b/tsconfig.jsonindex 8d3d9252..bff8cd13 100644--- a/tsconfig.json+++ b/tsconfig.json@@ -6,7 +6,6 @@"noImplicitAny": true,"module": "esnext","target": "es6",- "declaration": true,"moduleResolution": "node","resolveJsonModule": true,"allowSyntheticDefaultImports": true,@@ -16,7 +15,6 @@"paths": {"MC/*": ["../src/*",- "../dist/*"]}}