diff --git a/blocks/blocks.js b/blocks/blocks.js index e756e389..73897511 100644 --- a/blocks/blocks.js +++ b/blocks/blocks.js @@ -11,6 +11,7 @@ import { getMeshFromBlock, clearSkyMesh, setClearSkyToBlack, + handleSkyBlockDeletion, } from "../ui/blockmesh.js"; import { registerFieldColour } from "@blockly/field-colour"; import { createThemeConfig } from "../main/themes.js"; @@ -102,8 +103,7 @@ export function handleBlockDelete(event) { clearSkyMesh(); setClearSkyToBlack(); } else if (blockJson.type === "set_sky_color") { - clearSkyMesh(); - setClearSkyToBlack(); + handleSkyBlockDeletion(blockJson.id); } // Check inputs for child blocks @@ -173,8 +173,7 @@ export function handleMeshLifecycleChange(block, changeEvent) { clearSkyMesh(); setClearSkyToBlack(); } else if (block.type === "set_sky_color") { - clearSkyMesh(); - setClearSkyToBlack(); + handleSkyBlockDeletion(block.id); } } return true; diff --git a/ui/blockmesh.js b/ui/blockmesh.js index 47d5ecd8..3ab5404d 100644 --- a/ui/blockmesh.js +++ b/ui/blockmesh.js @@ -389,6 +389,33 @@ export function clearSkyMesh({ preserveClearColor = true } = {}) { delete meshMap["sky"]; } +/** + * Called when a set_sky_color block is deleted or disabled. + * Only clears the sky if the given block was the active one; if another + * enabled sky block remains in the workspace it is reapplied automatically. + */ +export function handleSkyBlockDeletion(blockId) { + // If the removed block wasn't the one controlling the sky, leave it alone. + if (meshBlockIdMap["sky"] !== blockId) { + return; + } + + clearSkyMesh(); + + const workspace = Blockly.getMainWorkspace(); + const remainingSky = workspace + ?.getAllBlocks(false) + .find((b) => b.type === "set_sky_color" && b.isEnabled()); + + if (remainingSky) { + meshMap["sky"] = remainingSky; + meshBlockIdMap["sky"] = remainingSky.id; + updateSkyFromBlock(null, remainingSky, null); + } else { + setClearSkyToBlack(); + } +} + export function setClearSkyToBlack() { console.log("*** Setting clear sky to black"); const fallbackColor =