Repository navigation
Train Route algorithm robustness improvements + generator for stations + regenerate after 30.09 map update #158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cosminpolifronie
wants to merge
13
commits into
simrail:main
Choose a base branch
from
cosminpolifronie:bugfix/train-route-bugs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0321b03
Fix issue where random grey segments were shown between stations
cosminpolifronie a078309
Do not display grey segments if they are at the end of the route, as …
cosminpolifronie 4f97103
Re-introduce backtracking removal algorithm to prevent loops
cosminpolifronie 7db3b3f
Remove grey segments at the beginning of the route - previous commit …
cosminpolifronie b69bfc7
Add playable/unplayable/remote stations generator based on the SimRai…
cosminpolifronie b9a936f
Massively rework and simplify the route generation algorithm so now l…
cosminpolifronie ce890ca
Remove annoying map moving functionality whenever the hover popup doe…
cosminpolifronie b430938
Fix review findings
cosminpolifronie 136693d
Rename "Unplayable dispatch stations" layer to "Unplayable stations"
cosminpolifronie 88294e9
Revert "Rename "Unplayable dispatch stations" layer to "Unplayable st…
cosminpolifronie 27da7f7
Split passenger stops from unplayable dispatch stations and replace L…
cosminpolifronie 407061b
Make railData.json content stable - no difference should be visible i…
cosminpolifronie ad7a076
Keep availability colours on station joins
cosminpolifronie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| import L from "leaflet"; | ||
| import { useEffect, useRef } from "react"; | ||
| import { Popup, type PopupProps } from "react-leaflet"; | ||
|
|
||
| const POPUP_TIP_HEIGHT = 20; | ||
| const POPUP_EDGE_PADDING = 10; | ||
| // Keeps the tip from sliding past the rounded corners of the popup body. | ||
| const POPUP_TIP_INSET = 30; | ||
|
|
||
| type PopupInternals = { | ||
| _map?: L.Map; | ||
| _getAnchor: () => L.Point; | ||
| }; | ||
|
|
||
| // Places the popup above or below its anchor (whichever fits) and shifts it | ||
| // horizontally to stay inside the map, instead of letting Leaflet pan the map. | ||
| const placePopupInsideMap = (popup: L.Popup) => { | ||
| const internals = popup as unknown as PopupInternals; | ||
| const map = internals._map; | ||
| const container = popup.getElement(); | ||
| const latLng = popup.getLatLng(); | ||
| if (!map || !container || !latLng) return; | ||
|
|
||
| const wrapper = container.querySelector<HTMLElement>( | ||
| ".leaflet-popup-content-wrapper", | ||
| ); | ||
| const tipContainer = container.querySelector<HTMLElement>( | ||
| ".leaflet-popup-tip-container", | ||
| ); | ||
| if (!wrapper) return; | ||
|
|
||
| const width = wrapper.offsetWidth; | ||
| const height = wrapper.offsetHeight; | ||
| const mapSize = map.getSize(); | ||
| // The popup anchor (e.g. the icon's popupAnchor) is where the tip points | ||
| // when the popup sits above the marker; mirror it when placing below. | ||
| const anchor = internals._getAnchor(); | ||
| const point = map.latLngToContainerPoint(latLng).add([anchor.x, 0]); | ||
|
|
||
| const spaceAbove = point.y + anchor.y - POPUP_TIP_HEIGHT - height; | ||
| const spaceBelow = | ||
| mapSize.y - (point.y - anchor.y + POPUP_TIP_HEIGHT + height); | ||
| const placeBelow = spaceAbove < POPUP_EDGE_PADDING && spaceBelow > spaceAbove; | ||
|
|
||
| const minShift = POPUP_EDGE_PADDING - (point.x - width / 2); | ||
| const maxShift = mapSize.x - POPUP_EDGE_PADDING - (point.x + width / 2); | ||
| // Prefer the left edge when the popup is wider than the map. | ||
| const fitShift = | ||
| minShift > maxShift ? minShift : Math.min(Math.max(0, minShift), maxShift); | ||
| const shiftX = Math.round(fitShift); | ||
| // The tip stays over the anchor unless that would put it past the body's | ||
| // rounded corners (anchor right at the map edge); then it stops there. | ||
| const maxTipShift = Math.max(0, width / 2 - POPUP_TIP_INSET); | ||
| const tipShift = Math.min(Math.max(shiftX, -maxTipShift), maxTipShift); | ||
| const shiftY = placeBelow ? -2 * anchor.y + 2 * POPUP_TIP_HEIGHT + height : 0; | ||
|
|
||
| container.classList.toggle("popup-below", placeBelow); | ||
| if (tipContainer) tipContainer.style.marginLeft = `${-20 - tipShift}px`; | ||
|
|
||
| const current = L.point(popup.options.offset ?? [0, 0]); | ||
| if (current.x === shiftX && current.y === shiftY) return; | ||
| popup.options.offset = L.point(shiftX, shiftY); | ||
| popup.update(); | ||
| }; | ||
|
|
||
| // A popup that repositions itself to fit inside the map instead of panning it. | ||
| const HoverPopup = (props: PopupProps) => { | ||
| const popupRef = useRef<L.Popup | null>(null); | ||
|
|
||
| useEffect(() => { | ||
| const popup = popupRef.current; | ||
| if (!popup) return; | ||
|
|
||
| let observer: ResizeObserver | null = null; | ||
| let map: L.Map | null = null; | ||
| const place = () => placePopupInsideMap(popup); | ||
| const onAdd = () => { | ||
| const wrapper = popup | ||
| .getElement() | ||
| ?.querySelector(".leaflet-popup-content-wrapper"); | ||
| if (!wrapper) return; | ||
| // Also fires once the content renders or changes size (e.g. images). | ||
| observer = new ResizeObserver(place); | ||
| observer.observe(wrapper); | ||
| // The anchor moves within the map when it's panned, zoomed or resized. | ||
| map = (popup as unknown as PopupInternals)._map ?? null; | ||
| map?.on("resize moveend zoomend", place); | ||
| }; | ||
| const onRemove = () => { | ||
| observer?.disconnect(); | ||
| observer = null; | ||
| map?.off("resize moveend zoomend", place); | ||
| map = null; | ||
| }; | ||
|
|
||
| popup.on("add", onAdd); | ||
| popup.on("remove", onRemove); | ||
| return () => { | ||
| popup.off("add", onAdd); | ||
| popup.off("remove", onRemove); | ||
| onRemove(); | ||
| }; | ||
| }, []); | ||
|
|
||
| return <Popup ref={popupRef} autoPan={false} {...props} />; | ||
| }; | ||
|
|
||
| export default HoverPopup; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
packages/map/components/Markers/PassengerStationMarker.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import type { Station } from "@simrail/types"; | ||
| import L from "leaflet"; | ||
| import { Marker } from "react-leaflet"; | ||
|
|
||
| import HoverPopup from "./HoverPopup"; | ||
|
|
||
| import styles from "../../styles/MarkerPopup.module.css"; | ||
|
|
||
| type StationMarkerProps = { | ||
| station: Station; | ||
| }; | ||
|
|
||
| export const PassengerStationMarker = ({ station }: StationMarkerProps) => { | ||
| const icon = L.icon({ | ||
| iconUrl: "/markers/icon-station-passenger.png", | ||
| iconSize: [16, 16], | ||
| popupAnchor: [0, -16], | ||
| }); | ||
|
|
||
| return ( | ||
| <Marker | ||
| key={station.id} | ||
| icon={icon} | ||
| position={[station.Latititude, station.Longitude]} | ||
| zIndexOffset={30} | ||
| eventHandlers={{ | ||
| mouseover: (event) => event.target.openPopup(), | ||
| mouseout: (event) => event.target.closePopup(), | ||
| }} | ||
| > | ||
| <HoverPopup className="simple-map-popup"> | ||
| <div className={styles.simpleCard}> | ||
| <small>Passenger station</small> | ||
| <strong>{station.Name}</strong> | ||
| </div> | ||
| </HoverPopup> | ||
| </Marker> | ||
| ); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import React, { type FC } from "react"; | ||
|
|
||
| import { PassengerStationMarker } from "@/components/Markers/PassengerStationMarker"; | ||
| import stationsJson from "@/components/stationsPassenger.json"; | ||
|
|
||
| const PassengerStations: FC = () => ( | ||
| <> | ||
| {stationsJson.map((station) => ( | ||
| <PassengerStationMarker key={station.Name} station={station} /> | ||
| ))} | ||
| </> | ||
| ); | ||
|
|
||
| export default React.memo(PassengerStations); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.