Skip to content

Commit

Permalink
feat: Add a "Zoom to Object" button on the starmap editor.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderson1993 committed Jan 7, 2023
1 parent 2c2ee59 commit 5940621
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
1 change: 0 additions & 1 deletion client/src/components/Starmap/Nebula/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ const nebulaGeometry = new BoxBufferGeometry(1, 1, 1);
const sides = ["back", "bottom", "front", "left", "right", "top"];
function generateMaterial(primary: boolean, index: number) {
const canvas = document.createElement("canvas");
document.body.appendChild(canvas);
canvas.width = canvas.height = CANVAS_WIDTH;

if ("transferControlToOffscreen" in canvas) {
Expand Down
7 changes: 2 additions & 5 deletions client/src/components/Starmap/Planet/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,7 @@ export function Planet({
const position = getOrbitPosition(planet.satellite);

// TODO - April 7, 2022 - Figure out how to make the scales nice for solar system editing
const size =
viewingMode !== "editor"
? radius
: (((isSatellite ? 1 : 5) + 100 * radius * 10) as Kilometer);
const size = radius;

// TODO - April 7, 2022 - Add moons
const satellites: PlanetPlugin[] = [];
Expand All @@ -133,7 +130,7 @@ export function Planet({
if (
size &&
distance / size > 100 &&
(viewingMode === "core" || showSprite)
(viewingMode === "core" || viewingMode === "editor" || showSprite)
) {
planetSpriteRef.current.visible = true;
planetMeshRef.current.visible = false;
Expand Down
48 changes: 48 additions & 0 deletions client/src/components/Starmap/SolarSystemMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import CameraControlsClass from "camera-controls";
import {
astronomicalUnitToKilometer,
Kilometer,
solarRadiusToKilometers,
} from "server/src/utils/unitTypes";
import {Box3, Camera, Vector3} from "three";
import Button from "../ui/Button";
Expand All @@ -30,6 +31,9 @@ import Input from "@thorium/ui/Input";
import Checkbox from "@thorium/ui/Checkbox";
import {useParams} from "react-router-dom";
import {q} from "@client/context/AppContext";
import PlanetPlugin from "@server/classes/Plugins/Universe/Planet";
import SolarSystemPlugin from "@server/classes/Plugins/Universe/SolarSystem";
import {getOrbitPosition} from "@server/utils/getOrbitPosition";
const ACTION = CameraControlsClass.ACTION;

// 10% further than Neptune's orbit
Expand Down Expand Up @@ -428,6 +432,7 @@ export function SolarSystemPalette() {
className="w-full h-full overflow-y-auto overflow-x-hidden text-white"
key={results.object.name}
>
<ZoomToObject object={results.object} />
<BasicDisclosure object={results.object} type={results.type} />
{results.type === "planet" && (
<>
Expand All @@ -441,6 +446,49 @@ export function SolarSystemPalette() {
);
}

function ZoomToObject({
object,
}: {
object: StarPlugin | PlanetPlugin | SolarSystemPlugin;
}) {
const useStarmapStore = useGetStarmapStore();

if (!("satellite" in object)) {
return null;
}

return (
<Button
className="btn-block btn-xs"
onClick={() => {
const position = getOrbitPosition(object.satellite);
let radius = 0;
if ("isPlanet" in object) {
radius = object.isPlanet.radius;
} else {
radius = solarRadiusToKilometers(object.radius);
}

const box = new Box3(
new Vector3(
position.x - radius,
position.y - radius,
position.z - radius
),
new Vector3(
position.x + radius,
position.y + radius,
position.z + radius
)
);
useStarmapStore.getState().cameraControls?.current?.fitToBox(box, true);
}}
>
Zoom to Object
</Button>
);
}

function StarDisclosure({object}: {object: StarPlugin}) {
const [pluginId, solarSystemId] = useSystemIds();

Expand Down

0 comments on commit 5940621

Please sign in to comment.