Skip to content

Commit

Permalink
Export special objects to ppen; improve SpecialObject typings
Browse files Browse the repository at this point in the history
  • Loading branch information
perliedman committed Dec 30, 2024
1 parent a72a735 commit bfcab9a
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 12 deletions.
12 changes: 7 additions & 5 deletions src/CourseOptions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,32 +60,34 @@ export default function CourseOptions({
<Checkbox
checked={printArea.auto}
label="Auto"
onChange={(auto) => setPrintArea({ auto })}
onChange={(auto) =>
setPrintArea({ auto, ...(auto ? { restrictToPage: false } : {}) })
}
/>
<div className="grid grid-cols-3 gap-2 items-center">
<div />
<ExtentInput
extent={extent}
index={3}
onChange={(extent) => setPrintArea({ extent })}
onChange={(extent) => setPrintArea({ extent, restrictToPage: false })}
/>
<div />
<ExtentInput
extent={extent}
index={0}
onChange={(extent) => setPrintArea({ extent })}
onChange={(extent) => setPrintArea({ extent, restrictToPage: false })}
/>
<div className="justify-self-center w-8 h-8 border border-dashed border-gray-400" />
<ExtentInput
extent={extent}
index={2}
onChange={(extent) => setPrintArea({ extent })}
onChange={(extent) => setPrintArea({ extent, restrictToPage: false })}
/>
<div />
<ExtentInput
extent={extent}
index={1}
onChange={(extent) => setPrintArea({ extent })}
onChange={(extent) => setPrintArea({ extent, restrictToPage: false })}
/>
<div />
</div>
Expand Down
4 changes: 3 additions & 1 deletion src/course-feature-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ export function courseFeatureStyle(
} else if (kind === "line") {
const stroke = lineStyle.getStroke();
stroke.setWidth(
dimension(feature.get("lineWidth") || overprintLineWidth) * lineWidthRatio
((feature.get("lineWidth") || overprintLineWidth) / resolution) *
5 /* TODO: most likely incorrect, but without this lines are much too thin - should be conversion of paper mm to geographic coordinates */ *
lineWidthRatio
);
stroke.setColor(color);
style = lineStyle;
Expand Down
12 changes: 8 additions & 4 deletions src/models/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import * as CourseAppearance from "./course-appearance";
import { CourseAppearance as CourseAppearanceType } from "./course-appearance";
import * as PrintArea from "./print-area";
import { PrintArea as PrintAreaType } from "./print-area";
import { SpecialObject } from "./special-object";
import { SpecialObject as SpecialObjectType } from "./special-object";
import * as SpecialObject from "./special-object";

export const ALL_CONTROLS_ID = -1;

Expand All @@ -25,7 +26,7 @@ export interface Event {
mapScale: number;
mapFilename: string;
controls: Record<string, ControlType>;
specialObjects: SpecialObject[];
specialObjects: SpecialObjectType[];
courseAppearance: CourseAppearanceType;
printArea: PrintAreaType;
// allControls: CourseType;
Expand Down Expand Up @@ -181,9 +182,9 @@ export function getAllControls(event: Event): CourseType {

export function addSpecialObject(
event: Event,
object: Omit<SpecialObject, "id">,
object: Omit<SpecialObjectType, "id">,
courseId: number | undefined
): SpecialObject {
): SpecialObjectType {
const specialObject = { id: event.idGenerator.next(), ...object };

event.specialObjects.push(specialObject);
Expand Down Expand Up @@ -276,6 +277,9 @@ export function toPpen(event: Event): Xml {
},
...Object.values(event.controls).map((c) => Control.toPpen(c)),
...Course.toPpen(event.courses),
...event.specialObjects.map((o) =>
SpecialObject.toPpen(o, event.courses)
),
],
};
}
70 changes: 69 additions & 1 deletion src/models/special-object.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,76 @@
import type { Coordinate } from "ol/coordinate";
import { Course } from "./course";

export interface SpecialObject {
interface SpecialObjectBase {
id: number;
kind: string;
isAllCourses: boolean;
locations: Coordinate[];
}

interface LineObject extends SpecialObjectBase {
kind: "line";
color?: string;
lineWidth?: string;
}

interface WhiteOutObject extends SpecialObjectBase {
kind: "white-out";
}

interface DescriptionsObject extends SpecialObjectBase {
kind: "descriptions";
}

export type SpecialObject = LineObject | WhiteOutObject | DescriptionsObject;

export function toPpen(object: SpecialObject, courses: Course[]) {
return {
type: "special-object",
attrs: {
id: object.id,
kind: object.kind,
},
children: [
...object.locations.map((l) => ({
type: "location",
attrs: {
x: l[0],
y: l[1],
},
})),
...appearance(object),
{
type: "courses",
attrs: {
all: object.isAllCourses,
},
children: !object.isAllCourses
? courses
.filter((c) => c.specialObjects.some((o) => o.id === object.id))
.map((c) => ({
type: "course",
attrs: { course: c.id },
}))
: [],
},
],
};
}

function appearance(object: SpecialObject) {
if (object.kind === "line") {
return [
{
type: "appearance",
attrs: {
"line-kind": "single",
color: object.color,
"line-width": object.lineWidth,
},
},
];
} else {
return [];
}
}
6 changes: 5 additions & 1 deletion src/tools/Objects.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import ToolButton, { ModeButton } from "../ui/ToolButton";
import { Coordinate } from "ol/coordinate";
import Draw from "ol/interaction/Draw";
import { Polygon } from "ol/geom";
import { overprintLineWidth } from "../services/use-number-positions";

type ObjectMode = "edit" | "white-out" | "line" | "descriptions";

Expand Down Expand Up @@ -70,7 +71,7 @@ export default function Objects(): JSX.Element {
const objects = useEvent.getState().specialObjects;
setSelectedObjectId(objects[objects.length - 1].id);
setMode("edit");
}
}
});
map.addInteraction(interaction);

Expand All @@ -97,6 +98,9 @@ export default function Objects(): JSX.Element {
transform(c, map.getView().getProjection(), ppenProjection)
),
isAllCourses: false,
...(mode === "line"
? { lineWidth: overprintLineWidth, color: "purple" }
: null),
},
selectedCourse.id
);
Expand Down

0 comments on commit bfcab9a

Please sign in to comment.