Skip to content

Commit

Permalink
feat: add ability to override text styles (#559)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Báez <[email protected]>
  • Loading branch information
dragoncoder047 and lajbel authored Dec 27, 2024
1 parent 62b31d6 commit 95fac6e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,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
Expand Down
7 changes: 7 additions & 0 deletions src/gfx/draw/drawText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/gfx/formatText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ type FontAtlas = {
const fontAtlases: Record<string, FontAtlas> = {};

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;
Expand Down
43 changes: 43 additions & 0 deletions tests/playtests/styleOverride.js
Original file line number Diff line number Diff line change
@@ -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)),
]);

0 comments on commit 95fac6e

Please sign in to comment.