Skip to content

Commit

Permalink
Merge pull request #39 from fechan/pipe-mode-switcher
Browse files Browse the repository at this point in the history
Add pipe mode selector
  • Loading branch information
fechan authored Oct 27, 2024
2 parents 879a7b2 + 515c049 commit ee08a5f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
28 changes: 26 additions & 2 deletions client/src/components/EdgeOptions.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Pipe } from "@server/types/core-types";
import { Pipe, PipeMode } from "@server/types/core-types";
import { useState } from "react";
import { SendMessage } from "react-use-websocket";
import { Edge, useOnSelectionChange, useStoreApi } from "reactflow";
Expand All @@ -14,15 +14,20 @@ export function EdgeOptions({ sendMessage }: EdgeOptionsProps) {
const [ selectedEdges, setSelectedEdges ] = useState([] as Edge[]);

const pipes = useFactoryStore(state => state.factory.pipes);
const groups = useFactoryStore(state => state.factory.groups);

const [ nickname, setNickname ] = useState("");
const [ filter, setFilter ] = useState("");
const [ isFluid, setIsFluid ] = useState(false);
const [ mode, setMode ] = useState(undefined as string | undefined);

useOnSelectionChange({
onChange: ({ edges }) => {
setSelectedEdges(edges);
setFilter(edges.length === 1 ? (pipes[edges[0].id].filter || "") : "...");
setNickname(edges.length === 1 ? (pipes[edges[0].id].nickname || "") : "...");
setMode(edges.length === 1 ? pipes[edges[0].id].mode : "...");
setIsFluid(edges.length > 0 && groups[pipes[edges[0].id].from].fluid === true);
}
});

Expand All @@ -40,6 +45,11 @@ export function EdgeOptions({ sendMessage }: EdgeOptionsProps) {
changes = true;
}

if (mode && mode !== "...") {
edits.mode = mode as PipeMode;
changes = true;
}

if (changes) {
for (let edge of selectedEdges) {
GraphUpdateCallbacks.onPipeUpdate(edge.id, edits, sendMessage)
Expand Down Expand Up @@ -78,7 +88,7 @@ export function EdgeOptions({ sendMessage }: EdgeOptionsProps) {
/>
</div>

<div className="flex flex-col mb-5">
<div className="flex flex-col mb-3">
<label htmlFor="pipeFilter" className="mb-1">Item filter</label>
<input
type="text"
Expand All @@ -91,6 +101,20 @@ export function EdgeOptions({ sendMessage }: EdgeOptionsProps) {
<FilterSyntax />
</div>

{ !isFluid &&
<div className="mb-5">
<label htmlFor="mode" className="block mb-1">Mode</label>
<select
value={ mode }
onChange={ e => setMode(e.target.value) }
className="mcui-button p-2 w-full h-10"
>
<option value="natural">Natural (default)</option>
</select>
</div>
}


<div className="text-right box-border">
<button
className="mcui-button bg-red-700 w-32 h-10 me-3"
Expand Down
5 changes: 3 additions & 2 deletions client/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
.mcui-button {
@apply bg-neutral-500;
@apply text-white;
@apply outline-white;
@apply border-2;
@apply border-t-mcgui-slot-border-light;
@apply border-s-mcgui-slot-border-light;
Expand All @@ -42,10 +43,10 @@
@apply disabled:bg-stone-800;

@apply hover:border-white;
@apply hover:border-4;
@apply hover:outline;

@apply active:border-white;
@apply active:border-4;
@apply active:outline-2;
@apply active:bg-stone-700;

text-shadow: 1.5px 1.5px 0px #373737;
Expand Down
6 changes: 5 additions & 1 deletion computercraft/sigils/pipe.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ local function processPipe (pipe, groupMap, missingPeriphs)

local ok, transferOrders = pcall(
function ()
return PipeModeNatural.getTransferOrders(groupMap[pipe.from], groupMap[pipe.to], missingPeriphs, filter)
if pipe.mode == "natural" then -- you can add more item pipe modes with more if/else statements
return PipeModeNatural.getTransferOrders(groupMap[pipe.from], groupMap[pipe.to], missingPeriphs, filter)
else -- natural is the default
return PipeModeNatural.getTransferOrders(groupMap[pipe.from], groupMap[pipe.to], missingPeriphs, filter)
end
end
)

Expand Down
3 changes: 3 additions & 0 deletions server/src/types/core-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,16 @@ export interface Factory {
export type PipeId = string;
export type PipeMap = { [key: PipeId]: Pipe };

export type PipeMode = 'natural' | 'spread';

/**
* Data structure representing a pipe for transferring items between two Groups
*/
export interface Pipe {
id: PipeId,
from: GroupId,
to: GroupId
mode?: PipeMode,
nickname?: string,
filter?: string,
};
Expand Down

0 comments on commit ee08a5f

Please sign in to comment.