Skip to content

Commit

Permalink
Merge branch 'main' into zlw-ref
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeLonewolf committed May 18, 2024
2 parents 9b2faed + 2c23198 commit 18f738e
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 41 deletions.
97 changes: 79 additions & 18 deletions shieldlib/src/shield.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,20 @@ import * as ShieldDraw from "./shield_canvas_draw";
import * as Gfx from "./screen_gfx";
import { drawBanners, drawBannerHalos, getBannerCount } from "./shield_banner";
import { ShieldRenderingContext } from "./shield_renderer";
import { Dimension, RouteDefinition, ShieldDefinition, ShieldDefinitions } from "./types";
import {
Dimension,
RouteDefinition,
ShieldDefinition,
ShieldDefinitions,
} from "./types";
import { TextPlacement } from "./shield_text";
import { StyleImage } from "maplibre-gl";

function compoundShieldSize(r: ShieldRenderingContext, dimension: Dimension, bannerCount: number): Dimension {
function compoundShieldSize(
r: ShieldRenderingContext,
dimension: Dimension,
bannerCount: number
): Dimension {
return {
width: dimension.width,
height:
Expand All @@ -31,7 +40,11 @@ export function isValidRef(ref: string): boolean {
* @param {*} routeDef - route tagging from OSM
* @returns shield blank or null if no shield exists
*/
function getRasterShieldBlank(r: ShieldRenderingContext, shieldDef: ShieldDefinition, routeDef: RouteDefinition): StyleImage {
function getRasterShieldBlank(
r: ShieldRenderingContext,
shieldDef: ShieldDefinition,
routeDef: RouteDefinition
): StyleImage {
let shieldArtwork = null;
let textPlacement: TextPlacement;
let bannerCount: number = 0;
Expand Down Expand Up @@ -66,9 +79,19 @@ function textColor(shieldDef: ShieldDefinition): string {
return "black";
}

function getDrawFunc(shieldDef: ShieldDefinition): (r: ShieldRenderingContext, ctx: CanvasRenderingContext2D, ref: string) => void {
function getDrawFunc(
shieldDef: ShieldDefinition
): (
r: ShieldRenderingContext,
ctx: CanvasRenderingContext2D,
ref: string
) => void {
if (typeof shieldDef.shapeBlank != "undefined") {
return (r: ShieldRenderingContext, ctx: CanvasRenderingContext2D, ref: string) =>
return (
r: ShieldRenderingContext,
ctx: CanvasRenderingContext2D,
ref: string
) =>
ShieldDraw.draw(
r,
shieldDef.shapeBlank.drawFunc,
Expand All @@ -78,17 +101,30 @@ function getDrawFunc(shieldDef: ShieldDefinition): (r: ShieldRenderingContext, c
);
}
console.warn(`Draw function not defined in:\n${shieldDef}`);
return (r: ShieldRenderingContext, ctx: CanvasRenderingContext2D, ref: string) => { };
return (
r: ShieldRenderingContext,
ctx: CanvasRenderingContext2D,
ref: string
) => {};
}

function getDrawHeight(r: ShieldRenderingContext, shieldDef: ShieldDefinition): number {
function getDrawHeight(
r: ShieldRenderingContext,
shieldDef: ShieldDefinition
): number {
if (typeof shieldDef.shapeBlank != "undefined") {
return ShieldDraw.shapeHeight(r, shieldDef.shapeBlank.drawFunc);
}
return r.shieldSize();
}

function drawShieldText(r: ShieldRenderingContext, ctx: CanvasRenderingContext2D, shieldDef: ShieldDefinition, routeDef: RouteDefinition, shieldBounds: Dimension): CanvasRenderingContext2D {
function drawShieldText(
r: ShieldRenderingContext,
ctx: CanvasRenderingContext2D,
shieldDef: ShieldDefinition,
routeDef: RouteDefinition,
shieldBounds: Dimension
): CanvasRenderingContext2D {
if (shieldDef.notext) {
//If the shield definition says not to draw a ref, ignore ref
return ctx;
Expand Down Expand Up @@ -120,16 +156,21 @@ function drawShieldText(r: ShieldRenderingContext, ctx: CanvasRenderingContext2D
r.px(shieldDef.padding.left - 0.5),
r.px(shieldDef.padding.top - 0.5),
shieldBounds.width -
r.px(shieldDef.padding.left + shieldDef.padding.right - 1),
r.px(shieldDef.padding.left + shieldDef.padding.right - 1),
shieldBounds.height -
r.px(shieldDef.padding.top + shieldDef.padding.bottom - 1)
r.px(shieldDef.padding.top + shieldDef.padding.bottom - 1)
);
}

return ctx;
}

export function missingIconLoader(r: ShieldRenderingContext, routeDef: RouteDefinition, spriteID: string, update: boolean): void {
export function missingIconLoader(
r: ShieldRenderingContext,
routeDef: RouteDefinition,
spriteID: string,
update: boolean
): void {
let ctx = generateShieldCtx(r, routeDef);
if (ctx == null) {
// Want to return null here, but that gives a corrupted display. See #243
Expand All @@ -139,7 +180,12 @@ export function missingIconLoader(r: ShieldRenderingContext, routeDef: RouteDefi
storeSprite(r, spriteID, ctx, update);
}

function storeSprite(r: ShieldRenderingContext, id: string, ctx: CanvasRenderingContext2D, update: boolean): void {
function storeSprite(
r: ShieldRenderingContext,
id: string,
ctx: CanvasRenderingContext2D,
update: boolean
): void {
const imgData = ctx.getImageData(0, 0, ctx.canvas.width, ctx.canvas.height);
r.spriteRepo.putSprite(
id,
Expand Down Expand Up @@ -172,7 +218,10 @@ function refForDefs(routeDef: RouteDefinition, shieldDef: ShieldDefinition) {
return routeDef.ref;
}

function getShieldDef(shields: ShieldDefinitions, routeDef: RouteDefinition): ShieldDefinition {
function getShieldDef(
shields: ShieldDefinitions,
routeDef: RouteDefinition
): ShieldDefinition {
if (!shields) {
//This occurs if the ShieldJSON is loaded from the network and hasn't loaded yet.
return null;
Expand Down Expand Up @@ -256,7 +305,11 @@ export function romanizeRef(ref: string): string {
return roman + ref.slice(number.toString().length);
}

function getDrawnShieldBounds(r: ShieldRenderingContext, shieldDef: ShieldDefinition, ref: string): Dimension {
function getDrawnShieldBounds(
r: ShieldRenderingContext,
shieldDef: ShieldDefinition,
ref: string
): Dimension {
let width = Math.max(
r.shieldSize(),
ShieldDraw.computeWidth(
Expand All @@ -271,16 +324,24 @@ function getDrawnShieldBounds(r: ShieldRenderingContext, shieldDef: ShieldDefini
return { width, height };
}

function bannerAreaHeight(r: ShieldRenderingContext, bannerCount: number): number {
function bannerAreaHeight(
r: ShieldRenderingContext,
bannerCount: number
): number {
if (bannerCount === 0) {
return 0;
}
return bannerCount * r.px(r.options.bannerHeight) +
return (
bannerCount * r.px(r.options.bannerHeight) +
//No padding after last banner
(bannerCount - 1) * r.px(r.options.bannerPadding);
(bannerCount - 1) * r.px(r.options.bannerPadding)
);
}

export function generateShieldCtx(r: ShieldRenderingContext, routeDef: RouteDefinition): CanvasRenderingContext2D {
export function generateShieldCtx(
r: ShieldRenderingContext,
routeDef: RouteDefinition
): CanvasRenderingContext2D {
let shieldDef: ShieldDefinition = getShieldDef(r.shieldDef, routeDef);

if (shieldDef == null) {
Expand Down
6 changes: 5 additions & 1 deletion shieldlib/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { StyleImage, StyleImageInterface, StyleImageMetadata } from "maplibre-gl";
import {
StyleImage,
StyleImageInterface,
StyleImageMetadata,
} from "maplibre-gl";

/** Defines the set of routes that a shield applies to */
export interface RouteDefinition {
Expand Down
23 changes: 14 additions & 9 deletions src/americana.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,20 @@ upgradeLegacyHash();

loadRTLPlugin();

export const map = createMap(window, (shields) => shieldDefLoad(shields), {
container: "map", // container id
hash: "map",
antialias: true,
style: buildStyle(),
center: [-94, 40.5],
zoom: 4,
attributionControl: false,
}, debugOptions);
export const map = createMap(
window,
(shields) => shieldDefLoad(shields),
{
container: "map", // container id
hash: "map",
antialias: true,
style: buildStyle(),
center: [-94, 40.5],
zoom: 4,
attributionControl: false,
},
debugOptions
);

// Add our sample data.
let sampleControl = new SampleControl({ permalinks: true });
Expand Down
25 changes: 15 additions & 10 deletions src/bare_americana.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,21 @@ import { debugOptions } from "./debug_config.js";

loadRTLPlugin();

export const map = createMap(window, (shields) => shieldDefLoad(), {
container: "map", // container id
hash: "map",
antialias: true,
style: buildStyle(),
center: [-94, 40.5],
zoom: 4,
fadeDuration: 0,
attributionControl: false,
}, debugOptions);
export const map = createMap(
window,
(shields) => shieldDefLoad(),
{
container: "map", // container id
hash: "map",
antialias: true,
style: buildStyle(),
center: [-94, 40.5],
zoom: 4,
fadeDuration: 0,
attributionControl: false,
},
debugOptions
);

function shieldDefLoad() {
if (window.top === window.self) {
Expand Down
6 changes: 3 additions & 3 deletions src/debug_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import config from "./config.js";
export const debugOptions: DebugOptions = {};

if (config.SHIELD_TEXT_HALO_COLOR_OVERRIDE) {
debugOptions.shieldTextHaloColor = config.SHIELD_TEXT_HALO_COLOR_OVERRIDE;
debugOptions.shieldTextHaloColor = config.SHIELD_TEXT_HALO_COLOR_OVERRIDE;
}

if (config.SHIELD_TEXT_BBOX_COLOR) {
debugOptions.shieldTextBboxColor = config.SHIELD_TEXT_BBOX_COLOR;
debugOptions.shieldTextBboxColor = config.SHIELD_TEXT_BBOX_COLOR;
}

export { debugOptions as default }
export { debugOptions as default };

0 comments on commit 18f738e

Please sign in to comment.