Refactor time settings update logic in App.tsx
This commit is contained in:
208
src/app/App.tsx
208
src/app/App.tsx
@@ -3439,60 +3439,206 @@ export function App({ store, initialStatusMessage }: AppProps) {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const applyProjectTimePhaseColor = (
|
const updateWorldTimeOfDaySettings = (
|
||||||
phase: ProjectTimePhaseKey,
|
label: string,
|
||||||
field: ProjectTimePhaseColorField,
|
successMessage: string,
|
||||||
|
mutate: (world: WorldSettings) => void
|
||||||
|
) => {
|
||||||
|
const nextWorld = cloneWorldSettings(editorState.document.world);
|
||||||
|
mutate(nextWorld);
|
||||||
|
applyWorldSettings(nextWorld, label, successMessage);
|
||||||
|
};
|
||||||
|
|
||||||
|
const applyWorldTimePhaseColor = (
|
||||||
|
phase: WorldTimePhaseKey,
|
||||||
|
field: WorldTimePhaseColorField,
|
||||||
colorHex: string,
|
colorHex: string,
|
||||||
label: string,
|
label: string,
|
||||||
successMessage: string
|
successMessage: string
|
||||||
) => {
|
) => {
|
||||||
updateProjectTimeSettings(label, successMessage, (time) => {
|
updateWorldTimeOfDaySettings(label, successMessage, (world) => {
|
||||||
time[phase][field] = colorHex;
|
world.timeOfDay[phase][field] = colorHex;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const applyProjectTimePhaseNumericField = (
|
const applyWorldTimePhaseNumericField = (
|
||||||
phase: ProjectTimePhaseKey,
|
phase: WorldTimePhaseKey,
|
||||||
field: ProjectTimePhaseNumericField,
|
field: WorldTimePhaseNumericField,
|
||||||
draftValue: string,
|
draftValue: string,
|
||||||
label: string,
|
label: string,
|
||||||
draftLabel: string,
|
draftLabel: string,
|
||||||
successMessage: string
|
successMessage: string
|
||||||
) => {
|
) => {
|
||||||
updateProjectTimeSettings(label, successMessage, (time) => {
|
try {
|
||||||
time[phase][field] = readNonNegativeNumberDraft(draftValue, draftLabel);
|
updateWorldTimeOfDaySettings(label, successMessage, (world) => {
|
||||||
|
world.timeOfDay[phase][field] = readNonNegativeNumberDraft(
|
||||||
|
draftValue,
|
||||||
|
draftLabel
|
||||||
|
);
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
setStatusMessage(getErrorMessage(error));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const applyWorldNightEnvironmentColor = (
|
||||||
|
field: WorldNightEnvironmentColorField,
|
||||||
|
colorHex: string,
|
||||||
|
label: string,
|
||||||
|
successMessage: string
|
||||||
|
) => {
|
||||||
|
updateWorldTimeOfDaySettings(label, successMessage, (world) => {
|
||||||
|
world.timeOfDay.night[field] = colorHex;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const applyProjectTimeNightBackgroundAssetId = (assetId: string | null) => {
|
const applyWorldNightEnvironmentNumericField = (
|
||||||
updateProjectTimeSettings(
|
field: WorldNightEnvironmentNumericField,
|
||||||
assetId === null
|
draftValue: string,
|
||||||
? "Clear night background image"
|
label: string,
|
||||||
: "Set night background image",
|
draftLabel: string,
|
||||||
assetId === null
|
successMessage: string
|
||||||
? "Night background image cleared."
|
) => {
|
||||||
: `Night background image set to ${
|
try {
|
||||||
editorState.document.assets[assetId]?.sourceName ?? assetId
|
updateWorldTimeOfDaySettings(label, successMessage, (world) => {
|
||||||
}.`,
|
world.timeOfDay.night[field] = readNonNegativeNumberDraft(
|
||||||
(time) => {
|
draftValue,
|
||||||
time.nightBackground.assetId = assetId;
|
draftLabel
|
||||||
|
);
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
setStatusMessage(getErrorMessage(error));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const applyNightBackgroundMode = (
|
||||||
|
mode: WorldBackgroundMode,
|
||||||
|
imageAssetId?: string
|
||||||
|
) => {
|
||||||
|
const currentBackground = editorState.document.world.timeOfDay.night.background;
|
||||||
|
|
||||||
|
if (mode === "image") {
|
||||||
|
const currentBackgroundAssetId =
|
||||||
|
currentBackground.mode === "image" ? currentBackground.assetId : null;
|
||||||
|
const nextImageAssetId =
|
||||||
|
imageAssetId ??
|
||||||
|
(currentBackgroundAssetId !== null &&
|
||||||
|
editorState.document.assets[currentBackgroundAssetId]?.kind === "image"
|
||||||
|
? currentBackgroundAssetId
|
||||||
|
: imageAssetList[0]?.id);
|
||||||
|
|
||||||
|
if (nextImageAssetId === undefined) {
|
||||||
|
setStatusMessage(
|
||||||
|
"Import an image asset before using a night image background."
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
updateWorldTimeOfDaySettings(
|
||||||
|
"Set night background image",
|
||||||
|
`Night background set to ${
|
||||||
|
editorState.document.assets[nextImageAssetId]?.sourceName ??
|
||||||
|
nextImageAssetId
|
||||||
|
}.`,
|
||||||
|
(world) => {
|
||||||
|
world.timeOfDay.night.background = changeWorldBackgroundMode(
|
||||||
|
world.timeOfDay.night.background,
|
||||||
|
"image",
|
||||||
|
nextImageAssetId
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
updateWorldTimeOfDaySettings(
|
||||||
|
"Set night background mode",
|
||||||
|
mode === "solid"
|
||||||
|
? "Night background set to a solid color."
|
||||||
|
: "Night background set to a vertical gradient.",
|
||||||
|
(world) => {
|
||||||
|
world.timeOfDay.night.background = changeWorldBackgroundMode(
|
||||||
|
world.timeOfDay.night.background,
|
||||||
|
mode
|
||||||
|
);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const applyProjectTimeNightBackgroundEnvironmentIntensity = () => {
|
const applyNightBackgroundColor = (colorHex: string) => {
|
||||||
updateProjectTimeSettings(
|
if (editorState.document.world.timeOfDay.night.background.mode !== "solid") {
|
||||||
"Set night background environment intensity",
|
return;
|
||||||
"Updated the night background environment intensity.",
|
}
|
||||||
(time) => {
|
|
||||||
time.nightBackground.environmentIntensity = readNonNegativeNumberDraft(
|
updateWorldTimeOfDaySettings(
|
||||||
projectTimeNightBackgroundEnvironmentIntensityDraft,
|
"Set night background color",
|
||||||
"Night background environment intensity"
|
"Updated the night background color.",
|
||||||
);
|
(world) => {
|
||||||
|
world.timeOfDay.night.background = {
|
||||||
|
mode: "solid",
|
||||||
|
colorHex
|
||||||
|
};
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const applyNightGradientColor = (
|
||||||
|
edge: "top" | "bottom",
|
||||||
|
colorHex: string
|
||||||
|
) => {
|
||||||
|
if (
|
||||||
|
editorState.document.world.timeOfDay.night.background.mode !==
|
||||||
|
"verticalGradient"
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
updateWorldTimeOfDaySettings(
|
||||||
|
edge === "top"
|
||||||
|
? "Set night gradient top color"
|
||||||
|
: "Set night gradient bottom color",
|
||||||
|
edge === "top"
|
||||||
|
? "Updated the night gradient top color."
|
||||||
|
: "Updated the night gradient bottom color.",
|
||||||
|
(world) => {
|
||||||
|
world.timeOfDay.night.background =
|
||||||
|
edge === "top"
|
||||||
|
? {
|
||||||
|
...world.timeOfDay.night.background,
|
||||||
|
topColorHex: colorHex
|
||||||
|
}
|
||||||
|
: {
|
||||||
|
...world.timeOfDay.night.background,
|
||||||
|
bottomColorHex: colorHex
|
||||||
|
};
|
||||||
|
}
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const applyNightBackgroundEnvironmentIntensity = () => {
|
||||||
|
if (editorState.document.world.timeOfDay.night.background.mode !== "image") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
updateWorldTimeOfDaySettings(
|
||||||
|
"Set night background environment intensity",
|
||||||
|
"Updated the night background environment intensity.",
|
||||||
|
(world) => {
|
||||||
|
world.timeOfDay.night.background = {
|
||||||
|
...world.timeOfDay.night.background,
|
||||||
|
environmentIntensity: readNonNegativeNumberDraft(
|
||||||
|
worldNightBackgroundEnvironmentIntensityDraft,
|
||||||
|
"Night background environment intensity"
|
||||||
|
)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
setStatusMessage(getErrorMessage(error));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const applySceneName = () => {
|
const applySceneName = () => {
|
||||||
const normalizedName = sceneNameDraft.trim() || "Untitled Scene";
|
const normalizedName = sceneNameDraft.trim() || "Untitled Scene";
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user