Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions src/assets/textures/rect.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Graphics } from 'pixi.js';
import { EachRadius } from '../../display/data-schema/primitive-schema';
import { getColor } from '../../utils/get';
import { cacheKey, generateTexture } from './utils';

Expand All @@ -15,10 +16,16 @@ export const createRectTexture = (renderer, theme, rectOpts) => {
texture.id = cacheKey(renderer, rectOpts);
texture.metadata = {
slice: {
topHeight: borderWidth + 4,
leftWidth: borderWidth + 4,
rightWidth: borderWidth + 4,
bottomHeight: borderWidth + 4,
topHeight:
getSliceDimension(radius, 'topLeft', 'topRight') + (borderWidth ?? 0),
leftWidth:
getSliceDimension(radius, 'topLeft', 'bottomLeft') + (borderWidth ?? 0),
rightWidth:
getSliceDimension(radius, 'topRight', 'bottomRight') +
(borderWidth ?? 0),
bottomHeight:
getSliceDimension(radius, 'bottomLeft', 'bottomRight') +
(borderWidth ?? 0),
},
borderWidth: borderWidth,
config: { ...rectOpts },
Expand All @@ -31,8 +38,20 @@ const createRect = (theme, { fill, borderWidth, borderColor, radius }) => {
const size = 20 + borderWidth;

const xywh = [0, 0, size, size];
if (radius > 0) {
const parsedRadius = EachRadius.safeParse(radius);
if (typeof radius === 'number' && radius > 0) {
graphics.roundRect(...xywh, radius);
} else if (parsedRadius.success) {
const r = parsedRadius.data;
graphics.roundShape(
[
{ x: 0, y: 0, radius: r.topLeft },
{ x: size, y: 0, radius: r.topRight },
{ x: size, y: size, radius: r.bottomRight },
{ x: 0, y: size, radius: r.bottomLeft },
],
0,
);
} else {
graphics.rect(...xywh);
}
Expand All @@ -46,3 +65,9 @@ const createRect = (theme, { fill, borderWidth, borderColor, radius }) => {
}
return graphics;
};

const getSliceDimension = (radius, key1, key2) => {
return typeof radius === 'number'
? radius
: Math.max(radius?.[key1] ?? 0, radius?.[key2] ?? 0);
};
2 changes: 1 addition & 1 deletion src/assets/textures/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ export const generateTexture = (target, renderer, opts = {}) => {
export const cacheKey = (renderer, config) => {
return [
renderer.uid,
...TextureStyle.keyof().options.map((key) => config[key]),
...TextureStyle.keyof().options.map((key) => JSON.stringify(config[key])),
].join('-');
};
9 changes: 8 additions & 1 deletion src/display/data-schema/primitive-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,20 @@ export const Margin = z.preprocess(
.default({}),
);

export const EachRadius = z.object({
topLeft: z.number().nonnegative().default(0),
topRight: z.number().nonnegative().default(0),
bottomRight: z.number().nonnegative().default(0),
bottomLeft: z.number().nonnegative().default(0),
});

export const TextureStyle = z
.object({
type: z.enum(['rect']),
fill: z.string().default('transparent'),
borderWidth: z.number().default(0),
borderColor: z.string().default('black'),
radius: z.number().default(0),
radius: z.union([z.number().nonnegative(), EachRadius]).default(0),
})
.partial();

Expand Down
2 changes: 1 addition & 1 deletion src/display/mixins/Sourceable.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const Sourceable = (superClass) => {
const { source } = relevantChanges;
const { viewport, theme } = this.context;
const texture = getTexture(viewport.app.renderer, theme, source);
this.texture = texture;
Object.assign(this, { texture, ...(texture?.metadata?.slice ?? {}) });
}
};
MixedClass.registerHandler(
Expand Down