diff --git a/CHANGELOG.md b/CHANGELOG.md index 438650c1..ebb214ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -82,6 +82,7 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ### Fixed - Fixed artifacts present in some TrueType fonts. +- Allow to override the default style in styled text chunks with `override: true` flag - Fixed `.use()` and `.unuse()` with area components. ## [3001.0.0] "Spooky Beans!" - 2024-10-31 diff --git a/src/gfx/draw/drawText.ts b/src/gfx/draw/drawText.ts index bafb7b42..3e919bc7 100644 --- a/src/gfx/draw/drawText.ts +++ b/src/gfx/draw/drawText.ts @@ -114,6 +114,13 @@ export interface CharTransform { * For example, an opacity of 0.4 with 2 set in the transformation, the resulting opacity will be 0.8 (0.4 × 2). */ opacity?: number; + + /** + * If true, the styles applied by this specific {@link DrawTextOpt.styles} entry transform + * will override, rather than compose with, the default styles given in {@link DrawTextOpt.transform} and by other + * components' styles. + */ + override?: boolean } /** diff --git a/src/gfx/formatText.ts b/src/gfx/formatText.ts index 1340cb54..0f2fae43 100644 --- a/src/gfx/formatText.ts +++ b/src/gfx/formatText.ts @@ -30,6 +30,10 @@ type FontAtlas = { const fontAtlases: Record = {}; function applyCharTransform(fchar: FormattedChar, tr: CharTransform) { + if (tr.override) { + Object.assign(fchar, tr); + return; + } if (tr.pos) fchar.pos = fchar.pos.add(tr.pos); if (tr.scale) fchar.scale = fchar.scale.scale(vec2(tr.scale)); if (tr.angle) fchar.angle += tr.angle; diff --git a/tests/playtests/styleOverride.js b/tests/playtests/styleOverride.js new file mode 100644 index 00000000..b560588d --- /dev/null +++ b/tests/playtests/styleOverride.js @@ -0,0 +1,43 @@ +kaplay({ background: "#000000" }); + +add([ + pos(100, 100), + text("No override: Hello [foo]styled[/foo] text", { + transform: { + color: WHITE.darken(200), + }, + styles: { + foo: { + color: RED + } + } + }) +]); + +add([ + pos(100, 150), + text("With override: Hello [foo]styled[/foo] text", { + transform: { + color: WHITE.darken(200), + }, + styles: { + foo: { + color: RED, + override: true + } + } + }) +]); + +add([ + pos(100, 200), + text("With override and color(): Hello [foo]styled[/foo] text", { + styles: { + foo: { + color: RED, + override: true + } + } + }), + color(WHITE.darken(200)), +]);