Skip to content

Commit

Permalink
add color to engines
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscoBSalgueiro committed Nov 29, 2023
1 parent f7b630a commit 5392644
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 34 deletions.
5 changes: 5 additions & 0 deletions src/atoms/atoms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@ const pgnOptionsFamily = atomFamily((tab: string) =>
);
export const currentPgnOptionsAtom = tabValue(pgnOptionsFamily);

const arrowsFamily = atomFamily((tab: string) =>
atom(new Map<number, string[]>())
);
export const currentArrowsAtom = tabValue(arrowsFamily);

// Game

type GameState = "settingUp" | "playing" | "gameOver";
Expand Down
30 changes: 21 additions & 9 deletions src/components/boards/Board.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@ import EvalBar from "./EvalBar";
import PromotionModal from "./PromotionModal";
import "./board.css";
import { match } from "ts-pattern";
import { arrowColors } from "../panels/analysis/BestMoves";

interface ChessboardProps {
dirty: boolean;
currentNode: TreeNode;
arrows: string[];
arrows: Map<number, string[]>;
headers: GameHeaders;
root: TreeNode;
editingMode: boolean;
Expand Down Expand Up @@ -211,17 +212,28 @@ function Board({
}
}

let shapes: DrawShape[] =
showArrows && arrows.length > 0
? arrows.map((move, i) => {
let shapes: DrawShape[] = [];
if (showArrows && arrows.size > 0) {
const entries = Array.from(arrows.entries()).sort((a, b) => a[0] - b[0]);
for (const [i, moves] of entries) {
if (i < 4) {
for (const [j, move] of moves.entries()) {
const { from, to } = parseUci(move);
return {
if (shapes.find((s) => s.orig === from && s.dest === to)) continue;
shapes.push({
orig: from,
dest: to,
brush: i === 0 ? "paleBlue" : "paleGrey",
};
})
: [];
brush:
j === 0
? arrowColors[i]
: "pale" +
arrowColors[i].charAt(0).toUpperCase() +
arrowColors[i].slice(1),
});
}
}
}
}

if (currentNode.shapes.length > 0) {
shapes = shapes.concat(currentNode.shapes);
Expand Down
6 changes: 3 additions & 3 deletions src/components/boards/BoardAnalysis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import GameNotation from "./GameNotation";
import { useAtom, useAtomValue } from "jotai";
import {
autoSaveAtom,
currentArrowsAtom,
currentTabAtom,
currentTabSelectedAtom,
} from "@/atoms/atoms";
Expand All @@ -42,7 +43,7 @@ import { getMainLine } from "@/utils/chess";
function BoardAnalysis() {
const [editingMode, toggleEditingMode] = useToggle();
const [reportingMode, toggleReportingMode] = useToggle();
const [arrows, setArrows] = useState<string[]>([]);
const [arrows, setArrows] = useAtom(currentArrowsAtom);
const [currentTab, setCurrentTab] = useAtom(currentTabAtom);
const autoSave = useAtomValue(autoSaveAtom);
const dispatch = useContext(TreeDispatchContext);
Expand All @@ -55,7 +56,7 @@ function BoardAnalysis() {
const currentNode = getNodeAtPath(root, position);

useEffect(() => {
setArrows([]);
setArrows(new Map());
}, [position]);

const saveFile = useCallback(async () => {
Expand Down Expand Up @@ -166,7 +167,6 @@ function BoardAnalysis() {
<Tabs.Panel value="analysis" sx={{ flex: 1, overflowY: "hidden" }}>
<Suspense>
<AnalysisPanel
setArrows={setArrows}
toggleReportingMode={toggleReportingMode}
inProgress={inProgress}
setInProgress={setInProgress}
Expand Down
3 changes: 0 additions & 3 deletions src/components/panels/analysis/AnalysisPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,10 @@ import LogsPanel from "./LogsPanel";
import EvalChart from "@/components/common/EvalChart";

function AnalysisPanel({
setArrows,
toggleReportingMode,
inProgress,
setInProgress,
}: {
setArrows: (arrows: string[]) => void;
toggleReportingMode: () => void;
inProgress: boolean;
setInProgress: React.Dispatch<React.SetStateAction<boolean>>;
Expand Down Expand Up @@ -129,7 +127,6 @@ function AnalysisPanel({
<BestMoves
id={i}
engine={engine}
setArrows={setArrows}
fen={currentNode.fen}
halfMoves={currentNode.halfMoves}
/>
Expand Down
33 changes: 22 additions & 11 deletions src/components/panels/analysis/BestMoves.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
activeTabAtom,
currentArrowsAtom,
engineMovesFamily,
tabEngineSettingsFamily,
} from "@/atoms/atoms";
Expand Down Expand Up @@ -55,18 +56,18 @@ const useStyles = createStyles((theme) => ({
},
}));

export const arrowColors = ["blue", "green", "red", "yellow"];

interface BestMovesProps {
id: number;
engine: Engine;
setArrows: (arrows: string[]) => void;
fen: string;
halfMoves: number;
}

export default function BestMovesComponent({
id,
engine,
setArrows,
fen,
halfMoves,
}: BestMovesProps) {
Expand All @@ -78,6 +79,7 @@ export default function BestMovesComponent({
const [settings, setSettings] = useAtom(
tabEngineSettingsFamily({ engine: engine, tab: activeTab! })
);
const [, setArrows] = useAtom(currentArrowsAtom);

const engineVariations = useMemo(() => ev.get(fen) ?? [], [ev, fen]);

Expand Down Expand Up @@ -116,13 +118,14 @@ export default function BestMovesComponent({
type: "SET_SCORE",
payload: ev[0].score,
});
if (id === 0) {
setArrows(
ev.map((ev) => {
return ev.uciMoves[0];
})
setArrows((prev) => {
const newMap = new Map(prev);
newMap.set(
id,
ev.map((ev) => ev.uciMoves[0])
);
}
return newMap;
});
});
}
});
Expand Down Expand Up @@ -166,6 +169,14 @@ export default function BestMovesComponent({
newMap.set(fen, bestMoves);
return newMap;
});
setArrows((prev) => {
const newMap = new Map(prev);
newMap.set(
id,
bestMoves.map((ev) => ev.uciMoves[0])
);
return newMap;
});
}
});
}
Expand All @@ -185,7 +196,7 @@ export default function BestMovesComponent({
<ActionIcon
size="lg"
variant={settings.enabled ? "filled" : "transparent"}
color={theme.primaryColor}
color={id < 4 ? arrowColors[id] : theme.primaryColor}
onClick={() => {
setSettings((prev) => ({ ...prev, enabled: !prev.enabled }));
}}
Expand Down Expand Up @@ -280,6 +291,7 @@ export default function BestMovesComponent({
engine={engine}
settings={settings}
setSettings={setSettings}
color={id < 4 ? arrowColors[id] : theme.primaryColor}
/>
</Collapse>

Expand All @@ -288,8 +300,7 @@ export default function BestMovesComponent({
animate={progress < 100 && settings.enabled && !isGameOver}
size="xs"
striped={progress < 100 && !settings.enabled}
// color={threat ? "red" : "blue"}
color={threat ? "red" : theme.primaryColor}
color={id < 4 ? arrowColors[id] : theme.primaryColor}
/>
<Accordion.Panel>
<Table>
Expand Down
6 changes: 4 additions & 2 deletions src/components/panels/analysis/CoresSlider.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { SegmentedControl, useMantineTheme } from "@mantine/core";

export default function CoresSlide({
export default function CoresSlider({
value,
setValue,
color,
}: {
value: number;
setValue: (v: number) => void;
color?: string;
}) {
const theme = useMantineTheme();
const values = Array.from(
Expand All @@ -16,7 +18,7 @@ export default function CoresSlide({
return (
<SegmentedControl
size="xs"
color={theme.primaryColor}
color={color || theme.primaryColor}
value={value.toString()}
onChange={(v) => setValue(parseInt(v))}
data={values.map((v) => v.toString())}
Expand Down
3 changes: 3 additions & 0 deletions src/components/panels/analysis/DepthSlider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import { useStyles } from "./styles";
export default function DepthSlider({
value,
setValue,
color,
}: {
value: GoMode;
setValue: (v: GoMode) => void;
color?: string;
}) {
const [tempValue, setTempValue] = useState(value);
const MARKS = [
Expand Down Expand Up @@ -46,6 +48,7 @@ export default function DepthSlider({
min={10}
max={60}
value={v}
color={color}
label={(v) => (v === 60 ? "Infinite" : v)}
onChange={(v) => handleSliderChange(v, setTempValue)}
onChangeEnd={(v) => handleSliderChange(v, setValue)}
Expand Down
11 changes: 9 additions & 2 deletions src/components/panels/analysis/EngineSettingsForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Button,
Center,
Group,
MantineColor,
Modal,
NumberInput,
Select,
Expand All @@ -14,7 +15,6 @@ import {
Tooltip,
} from "@mantine/core";
import React, { memo, useState } from "react";
import CoresSlide from "./CoresSlider";
import DepthSlider from "./DepthSlider";
import LinesSlider from "./LinesSlider";
import {
Expand All @@ -30,18 +30,21 @@ import { appDataDir, resolve } from "@tauri-apps/api/path";
import { readTextFile, writeTextFile } from "@tauri-apps/api/fs";
import { Engine } from "@/utils/engines";
import { useAtom } from "jotai";
import CoresSlider from "./CoresSlider";

interface EngineSettingsProps {
engine: Engine;
settings: EngineSettings;
setSettings: (fn: (prev: EngineSettings) => EngineSettings) => void;
color?: MantineColor;
minimal?: boolean;
}

function EngineSettingsForm({
engine,
settings,
setSettings,
color,
minimal,
}: EngineSettingsProps) {
const [advancedOptions, setAdvancedOptions] = useState(false);
Expand Down Expand Up @@ -118,6 +121,7 @@ function EngineSettingsForm({
<DepthSlider
value={settings.go}
setValue={(v) => setSettings((prev) => ({ ...prev, go: v }))}
color={color}
/>
</Group>
) : (
Expand Down Expand Up @@ -156,6 +160,7 @@ function EngineSettingsForm({
options: { ...prev.options, multipv: v },
}))
}
color={color}
/>
</Group>
)}
Expand All @@ -164,14 +169,15 @@ function EngineSettingsForm({
<Text size="sm" fw="bold">
Number of cores
</Text>
<CoresSlide
<CoresSlider
value={settings.options.threads}
setValue={(v) =>
setSettings((prev) => ({
...prev,
options: { ...prev.options, threads: v },
}))
}
color={color}
/>
</Group>

Expand All @@ -187,6 +193,7 @@ function EngineSettingsForm({
options: { ...prev.options, hash: v },
}))
}
color={color}
/>
</Group>
</Stack>
Expand Down
3 changes: 3 additions & 0 deletions src/components/panels/analysis/HashSlider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import { useStyles } from "./styles";
export default function HashSlider({
value,
setValue,
color,
}: {
value: number;
setValue: (v: number) => void;
color?: string;
}) {
const [tempValue, setTempValue] = useState(Math.log2(value));
const { classes } = useStyles();
Expand All @@ -29,6 +31,7 @@ export default function HashSlider({
<Slider
min={0}
max={Math.log2(memorySize || 16)}
color={color}
value={tempValue}
onChange={setTempValue}
onChangeEnd={(v) => setValue(2 ** v)}
Expand Down
4 changes: 3 additions & 1 deletion src/components/panels/analysis/LinesSlider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ import { SegmentedControl, useMantineTheme } from "@mantine/core";
export default function DepthSlider({
value,
setValue,
color,
}: {
value: number;
setValue: (v: number) => void;
color?: string;
}) {
const theme = useMantineTheme();

return (
<SegmentedControl
size="xs"
color={theme.primaryColor}
color={color || theme.primaryColor}
data={["1", "2", "3", "4", "5"]}
value={value.toString()}
onChange={(v) => setValue(parseInt(v))}
Expand Down
6 changes: 3 additions & 3 deletions src/components/panels/analysis/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export const useStyles = createStyles((theme) => ({
backgroundColor: theme.white,
color: theme.colors.gray[5],
border:
rem(1) +
" solid " +
"light-dark(var(--mantine-color-gray-3), var(--mantine-color-dark-2))",
rem(1) + " solid " + theme.colorScheme === "dark"
? theme.colors.dark[2]
: theme.colors.gray[3],
},
}));

0 comments on commit 5392644

Please sign in to comment.