|
| 1 | +import { Vector2 } from "../types/vector2"; |
| 2 | +import { Border, BorderStyling, InstantiateRenderElementSidesStyling, IRenderElement, RenderElementSides, RenderElementSidesStyling } from "./interfaces"; |
| 3 | + |
| 4 | +export enum Display { |
| 5 | + Flex = "flex", |
| 6 | + None = "none" |
| 7 | +} |
| 8 | + |
| 9 | +export interface RenderElementBaseStyling { |
| 10 | + Margin?: RenderElementSidesStyling | number; |
| 11 | + Padding?: RenderElementSidesStyling | number; |
| 12 | + Border?: BorderStyling; |
| 13 | + BackgroundColor?: string; |
| 14 | + MinWidth?: number; |
| 15 | + MaxWidth?: number; |
| 16 | + MaxHeight?: number; |
| 17 | + MinHeight?: number; |
| 18 | + Grow?: number; |
| 19 | + Display?: Display; |
| 20 | +} |
| 21 | + |
| 22 | +export abstract class RenderElementBase implements IRenderElement { |
| 23 | + |
| 24 | + #margin: RenderElementSides; |
| 25 | + |
| 26 | + #padding: RenderElementSides; |
| 27 | + |
| 28 | + #border: Border; |
| 29 | + |
| 30 | + #backgroundColor?: string; |
| 31 | + |
| 32 | + #minWidth?: number; |
| 33 | + |
| 34 | + #maxWidth?: number; |
| 35 | + |
| 36 | + #minHeight?: number; |
| 37 | + |
| 38 | + #maxHeight?: number; |
| 39 | + |
| 40 | + #grow?: number; |
| 41 | + |
| 42 | + #display: Display; |
| 43 | + |
| 44 | + constructor(config?: RenderElementBaseStyling) { |
| 45 | + this.#margin = InstantiateRenderElementSidesStyling(config?.Margin); |
| 46 | + this.#padding = InstantiateRenderElementSidesStyling(config?.Padding); |
| 47 | + this.#backgroundColor = config?.BackgroundColor; |
| 48 | + this.#border = { |
| 49 | + Color: config?.Border?.Color ?? "black", |
| 50 | + Thickness: config?.Border?.Thickness ?? 0, |
| 51 | + Radius: config?.Border?.Radius ?? 0, |
| 52 | + } |
| 53 | + this.#minWidth = config?.MinWidth; |
| 54 | + this.#maxWidth = config?.MaxWidth; |
| 55 | + this.#minHeight = config?.MinHeight; |
| 56 | + this.#maxHeight = config?.MaxHeight; |
| 57 | + this.#grow = config?.Grow; |
| 58 | + this.#display = config?.Display ?? Display.Flex; |
| 59 | + } |
| 60 | + |
| 61 | + setBackgroundColor(newColor: string): void { |
| 62 | + this.#backgroundColor = newColor; |
| 63 | + } |
| 64 | + |
| 65 | + abstract doRender(ctx: CanvasRenderingContext2D, position: Vector2, graphScale: number, scaledFillableSpace: Vector2): void; |
| 66 | + |
| 67 | + render(ctx: CanvasRenderingContext2D, position: Vector2, graphScale: number, scaledFillableSpace: Vector2): void { |
| 68 | + if (this.#display === Display.None) { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + const scaledSize = { x: 0, y: 0 }; |
| 73 | + this.size(ctx, scaledSize); |
| 74 | + |
| 75 | + scaledSize.x = scaledSize.x * graphScale; |
| 76 | + scaledSize.y = scaledSize.y * graphScale; |
| 77 | + scaledSize.x = Math.max(scaledSize.x, scaledFillableSpace.x); |
| 78 | + scaledSize.y = Math.max(scaledSize.y, scaledFillableSpace.y); |
| 79 | + |
| 80 | + if (this.#backgroundColor) { |
| 81 | + ctx.fillStyle = this.#backgroundColor; |
| 82 | + ctx.beginPath(); |
| 83 | + ctx.roundRect( |
| 84 | + position.x + (this.#margin.Left * graphScale), |
| 85 | + position.y + (this.#margin.Right * graphScale), |
| 86 | + scaledSize.x - ((this.#margin.Left + this.#margin.Right) * graphScale), |
| 87 | + scaledSize.y - ((this.#margin.Top + this.#margin.Bottom) * graphScale), |
| 88 | + this.#border.Radius * graphScale |
| 89 | + ); |
| 90 | + ctx.fill(); |
| 91 | + } |
| 92 | + |
| 93 | + if (this.#border.Thickness > 0) { |
| 94 | + ctx.lineWidth = this.#border.Thickness * graphScale; |
| 95 | + ctx.strokeStyle = this.#border.Color; |
| 96 | + ctx.stroke(); |
| 97 | + } |
| 98 | + |
| 99 | + const offsetPosition = { |
| 100 | + x: position.x + (this.#totalLeftOffset() * graphScale), |
| 101 | + y: position.y + (this.#totalTopOffset() * graphScale), |
| 102 | + }; |
| 103 | + const elementSize = { |
| 104 | + x: scaledSize.x - (this.#horizontalOffset() * graphScale), |
| 105 | + y: scaledSize.y - (this.#verticalOffset() * graphScale) |
| 106 | + } |
| 107 | + this.doRender(ctx, offsetPosition, graphScale, elementSize); |
| 108 | + } |
| 109 | + |
| 110 | + abstract calcSize(ctx: CanvasRenderingContext2D, out: Vector2, limitations: Vector2): void; |
| 111 | + |
| 112 | + protected maxLimitations(out: Vector2): void { |
| 113 | + out.x = -1; |
| 114 | + out.y = -1; |
| 115 | + |
| 116 | + if (this.#maxWidth) { |
| 117 | + out.x = this.#maxWidth; |
| 118 | + } |
| 119 | + |
| 120 | + if (this.#maxHeight) { |
| 121 | + out.y = this.#maxHeight; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + setDisplay(display: Display): void { |
| 126 | + this.#display = display; |
| 127 | + } |
| 128 | + |
| 129 | + getDisplay(): Display { |
| 130 | + return this.#display; |
| 131 | + } |
| 132 | + |
| 133 | + size(ctx: CanvasRenderingContext2D, out: Vector2): void { |
| 134 | + out.x = 0; |
| 135 | + out.y = 0; |
| 136 | + if (this.#display === Display.None) { |
| 137 | + return; |
| 138 | + } |
| 139 | + |
| 140 | + const maxLimits = { x: -1, y: -1 } |
| 141 | + this.maxLimitations(maxLimits); |
| 142 | + |
| 143 | + this.calcSize(ctx, out, maxLimits); |
| 144 | + |
| 145 | + out.x += this.#horizontalOffset(); |
| 146 | + out.y += this.#verticalOffset(); |
| 147 | + |
| 148 | + if (this.#minWidth) { |
| 149 | + out.x = Math.max(out.x, this.#minWidth) |
| 150 | + } |
| 151 | + |
| 152 | + if (this.#maxWidth) { |
| 153 | + out.x = Math.min(this.#maxWidth, out.x); |
| 154 | + } |
| 155 | + |
| 156 | + if (this.#maxHeight) { |
| 157 | + out.y = Math.min(this.#maxHeight, out.y); |
| 158 | + } |
| 159 | + |
| 160 | + if (this.#minHeight) { |
| 161 | + out.y = Math.max(this.#minHeight, out.y); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + #horizontalOffset(): number { |
| 166 | + return (this.#border.Thickness) + |
| 167 | + this.#margin.Left + |
| 168 | + this.#margin.Right + |
| 169 | + this.#padding.Left + |
| 170 | + this.#padding.Right; |
| 171 | + } |
| 172 | + |
| 173 | + #verticalOffset(): number { |
| 174 | + return (this.#border.Thickness) + |
| 175 | + this.#margin.Top + |
| 176 | + this.#margin.Bottom + |
| 177 | + this.#padding.Top + |
| 178 | + this.#padding.Bottom; |
| 179 | + } |
| 180 | + |
| 181 | + #totalTopOffset(): number { |
| 182 | + return (this.#border.Thickness / 2) + |
| 183 | + this.#margin.Top + |
| 184 | + this.#padding.Top; |
| 185 | + } |
| 186 | + |
| 187 | + #totalLeftOffset(): number { |
| 188 | + return (this.#border.Thickness / 2) + |
| 189 | + this.#margin.Left + |
| 190 | + this.#padding.Left; |
| 191 | + } |
| 192 | +} |
0 commit comments