Skip to content

Commit

Permalink
refactor(3d-barcodecontext): 🎉 update 3d functions
Browse files Browse the repository at this point in the history
  • Loading branch information
gokhangunduz committed Jan 10, 2024
1 parent 836b078 commit a53ebdb
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 77 deletions.
Binary file added public/models/robot.glb
Binary file not shown.
27 changes: 27 additions & 0 deletions public/svg/general/application/application-light.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 45 additions & 0 deletions public/svg/general/robot/robot-light.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
import { ReactElement } from "react";
import { Dispatch, ReactElement, SetStateAction } from "react";
import Card from "../Card/Card";

interface ICodeEditorSwitcher {
activeTabCodeEditor: number;
setActiveTabCodeEditor: any;
interface ILayoutTabSwitcher {
tabs: string[];
activeTab: string;
setActiveTab: Dispatch<SetStateAction<any>>;
}

export default function CodeEditorSwitcher({
activeTabCodeEditor,
setActiveTabCodeEditor,
}: ICodeEditorSwitcher): ReactElement {
export default function LayoutTabSwitcher({
tabs,
activeTab,
setActiveTab,
}: ILayoutTabSwitcher): ReactElement {
return (
<Card className="!h-fit flex-none p-3 !pb-0">
<ul className=" flex w-full justify-center gap-6 rounded ">
{["Cloud IDE", "Physical IDE"].map((tab: any, index: number) => {
{tabs?.map((tab: string, index: number) => {
return (
<li
className="flex w-full cursor-pointer flex-col items-center gap-3"
onClick={() => setActiveTabCodeEditor(index + 1)}
onClick={() => setActiveTab(tab)}
key={index}
>
<div
className={`flex min-w-max items-center gap-1 px-2 text-xs font-medium transition-all duration-500 hover:scale-90
${
index + 1 === activeTabCodeEditor
tab === activeTab
? "text-primary-500"
: "text-light-500"
} `}
Expand All @@ -32,11 +34,7 @@ export default function CodeEditorSwitcher({
</div>
<div
className={`h-[2px] w-full transition-all duration-500
${
index + 1 === activeTabCodeEditor
? "bg-primary-500"
: "bg-light-100"
} `}
${tab === activeTab ? "bg-primary-500" : "bg-light-100"} `}
/>
</li>
);
Expand Down
2 changes: 1 addition & 1 deletion src/components/Machine3D/Machine3D.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Html, useCursor } from "@react-three/drei";
import { useFrame, useThree } from "@react-three/fiber";
import React, { useMemo, useRef, useState } from "react";
import { useMemo, useRef, useState } from "react";
import * as THREE from "three";
import MachineBarcode from "../MachineBarcode/MachineBarcode";

Expand Down
26 changes: 12 additions & 14 deletions src/components/ModeSwitcher/ModeSwitcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,17 @@ export default function ModeSwitcher(): ReactElement {
const dispatch = useAppDispatch();

return (
<>
<Toggle
defaultChecked={applicationMode}
icons={false}
className={`toggle-color`}
disabled={!envSwitchableMode}
onChange={(e) => {
setTimeout(() => {
dispatch(setApplicationMode(e.target.checked));
window.location.reload();
}, 1000);
}}
/>
</>
<Toggle
defaultChecked={applicationMode}
icons={false}
className={`toggle-color`}
disabled={!envSwitchableMode}
onChange={(e) => {
setTimeout(() => {
dispatch(setApplicationMode(e.target.checked));
window.location.reload();
}, 1000);
}}
/>
);
}
2 changes: 1 addition & 1 deletion src/components/Robot3D/Robot3D.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default function Robot3D() {
useMemo(() => {
const loader = new GLTFLoader();
loader.load(
"https://github.com/robolaunch/ui-models/raw/main/robot.glb",
"/models/robot.glb",
(gltf: any) => {
console.log("Robot model loaded:", gltf);
setRobotModel(gltf.scene);
Expand Down
45 changes: 40 additions & 5 deletions src/contexts/BarcodeContext.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { createContext, useEffect, useState } from "react";
import ROSLIB from "roslib";
import { createContext, useEffect, useState } from "react";
import useRobot from "../hooks/useRobot";
import ROSLIB from "roslib";

export const BarcodeContext: any = createContext<any>(null);

Expand All @@ -26,8 +26,10 @@ export default ({ children }: any) => {
});

ros &&
barcodes.subscribe(function (barcode: any) {
console.log("barcode", barcode);
barcodes.subscribe(function (message: any) {
const messageWithScannerId = JSON.parse(message?.data);

handleBarcodeSetters(messageWithScannerId);
});
}, [ros]);

Expand All @@ -40,7 +42,6 @@ export default ({ children }: any) => {

ros &&
poseTopic.subscribe(function (pose: any) {
console.log(pose);
setRobotLocation({
...pose?.pose?.position,
z: quaternionToEuler(pose?.pose?.orientation),
Expand All @@ -53,6 +54,40 @@ export default ({ children }: any) => {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ros]);

function handleBarcodeSetters(message: any) {
setBarcodeItems((prevBarcodeItems: any) => {
const updatedBarcodeItems = [...prevBarcodeItems];

const barcodeIndex = prevBarcodeItems.findIndex(
(barcodeItem: any) =>
barcodeItem.coordinates &&
Math.sqrt(
Math.pow(barcodeItem.coordinates.x - message?.coordinates.x, 2) +
Math.pow(barcodeItem.coordinates.y - message?.coordinates.y, 2),
) < 0.02,
);

if (barcodeIndex !== -1) {
updatedBarcodeItems[barcodeIndex] = {
...prevBarcodeItems[barcodeIndex],
barcodes: prevBarcodeItems[barcodeIndex].barcodes.map(
(barcode: any, index: number) =>
index === message?.scannerId ? message?.barcode : barcode,
),
};
} else {
updatedBarcodeItems.push({
barcodes: Array.apply(null, Array(3)).map((_, index: number) =>
index === message?.scannerId ? message?.barcode : "",
),
coordinates: message?.coordinates,
});
}

return updatedBarcodeItems;
});
}

function quaternionToEuler(q: {
x: number;
y: number;
Expand Down
38 changes: 6 additions & 32 deletions src/layouts/TaskManagementLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import MissionManagement from "../pages/EnvironmentPage/TaskManagement/MissionManagement";
import BarcodeManagement from "../pages/EnvironmentPage/TaskManagement/BarcodeManagement";
import LayoutTabSwitcher from "../components/LayoutTabSwitcher/LayoutTabSwitcher";
import { Fragment, ReactElement, useState } from "react";
import Card from "../components/Card/Card";
import useRobot from "../hooks/useRobot";

export default function TaskManagementLayout(): ReactElement {
Expand All @@ -13,37 +13,11 @@ export default function TaskManagementLayout(): ReactElement {

return (
<Fragment>
<Card className="!h-fit flex-none p-3 !pb-0">
<ul className="flex overflow-x-auto text-center">
{[{ name: "Mission Management" }, { name: "Barcode Management" }].map(
(tab: any, index: number) => {
return (
<div
className="flex w-full cursor-pointer flex-col gap-3"
onClick={() => setActiveTab(tab?.name)}
key={index}
>
<li
className={`min-w-max px-2 text-xs font-medium transition-all duration-500 hover:scale-90 ${
tab.name === activeTab
? "text-primary-500"
: "text-light-500"
} `}
>
{tab.name}
</li>
<div
className={`h-[2px] w-full transition-all duration-500
${
tab.name === activeTab ? "bg-primary-500" : "bg-light-100"
} `}
/>
</div>
);
},
)}
</ul>
</Card>
<LayoutTabSwitcher
tabs={["Mission Management", "Barcode Management"]}
activeTab={activeTab}
setActiveTab={setActiveTab}
/>
{(() => {
switch (activeTab) {
case "Mission Management":
Expand Down
23 changes: 16 additions & 7 deletions src/pages/EnvironmentPage/CodeEditor/CodeEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import CodeEditorSwitcher from "../../../components/CodeEditorSwitcher/CodeEditorSwitcher";
import LayoutTabSwitcher from "../../../components/LayoutTabSwitcher/LayoutTabSwitcher";
import { FullScreen, useFullScreenHandle } from "react-full-screen";
import ControlBar from "../../../components/ControlBar/ControlBar";
import useCreateRobot from "../../../hooks/useCreateRobot";
Expand All @@ -8,7 +8,9 @@ import Card from "../../../components/Card/Card";
import useRobot from "../../../hooks/useRobot";

export default function CodeEditor(): ReactElement {
const [activeTabCodeEditor, setActiveTabCodeEditor] = useState<1 | 2>(1);
const [activeTabCodeEditor, setActiveTabCodeEditor] = useState<
"Virtual IDE" | "Physical IDE"
>("Virtual IDE");

const handleFullScreen = useFullScreenHandle();

Expand All @@ -27,9 +29,10 @@ export default function CodeEditor(): ReactElement {
}
>
{robotData.step1.services.physicalIde?.isEnabled && (
<CodeEditorSwitcher
activeTabCodeEditor={activeTabCodeEditor}
setActiveTabCodeEditor={setActiveTabCodeEditor}
<LayoutTabSwitcher
tabs={["Virtual IDE", "Physical IDE"]}
activeTab={activeTabCodeEditor}
setActiveTab={setActiveTabCodeEditor}
/>
)}
<Card loading className="relative">
Expand All @@ -43,15 +46,21 @@ export default function CodeEditor(): ReactElement {
title="Virtual IDE"
allow="clipboard-read"
className={`animate-fadeIn h-full w-full
${activeTabCodeEditor === 2 && "absolute -top-[9999px]"}
${
activeTabCodeEditor === "Physical IDE" &&
"absolute -top-[9999px]"
}
${handleFullScreen?.active && "!h-screen"}`}
src={urls?.ide || robotData.step1.services.ide?.httpsEndpoint}
/>
<iframe
title="Physical IDE"
allow="clipboard-read"
className={`animate-fadeIn h-full w-full
${activeTabCodeEditor === 1 && "absolute -top-[9999px]"}
${
activeTabCodeEditor === "Virtual IDE" &&
"absolute -top-[9999px]"
}
${handleFullScreen?.active && "!h-screen"}`}
src={robotData.step1.services.physicalIde?.httpsEndpoint}
/>
Expand Down
2 changes: 1 addition & 1 deletion src/toolkit/RobotSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ export const RobotSlice = createSlice({
urls: {
vdi: isProduction ? "" : "ws://localhost:8080/",
ide: isProduction ? "" : "https://robolaunch.io",
ros: isProduction ? "" : "",
ros: isProduction ? "" : "ws://localhost:9090",
},
},
reducers: {},
Expand Down

0 comments on commit a53ebdb

Please sign in to comment.