-
Notifications
You must be signed in to change notification settings - Fork 587
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add monkey patch to restore background to minecraft workspace (#10220)
* Add monkey patch to restore background to minecraft workspace * also scale image
- Loading branch information
Showing
5 changed files
with
77 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import * as Blockly from "blockly"; | ||
|
||
interface ExtendedGridOptions extends Blockly.Options.GridOptions { | ||
image?: { | ||
path: string; | ||
width: string; | ||
height: string; | ||
opacity: number; | ||
} | ||
} | ||
|
||
export function monkeyPatchGrid() { | ||
const options = pxt.appTarget.appTheme.blocklyOptions?.grid as ExtendedGridOptions; | ||
|
||
if (!options?.image?.path) return; | ||
|
||
const gridPatternIds: string[] = []; | ||
|
||
Blockly.Grid.createDom = function (rnd: string, gridOptions: Blockly.Options.GridOptions, defs: SVGElement) { | ||
const id = "blocklyGridPattern" + rnd; | ||
|
||
const gridPattern = Blockly.utils.dom.createSvgElement( | ||
Blockly.utils.Svg.PATTERN, | ||
{ | ||
id, | ||
patternUnits: "userSpaceOnUse", | ||
width: options.image.width, | ||
height: options.image.height | ||
}, | ||
defs, | ||
); | ||
|
||
gridPatternIds.push(id) | ||
|
||
const image = Blockly.utils.dom.createSvgElement( | ||
Blockly.utils.Svg.IMAGE, | ||
{ | ||
width: options.image.width, | ||
height: options.image.height, | ||
opacity: options.image.opacity | ||
}, | ||
gridPattern | ||
); | ||
|
||
image.setAttributeNS("http://www.w3.org/1999/xlink", "xlink:href", options.image.path) | ||
|
||
return gridPattern; | ||
} | ||
|
||
const oldGridUpdate = Blockly.Grid.prototype.update; | ||
|
||
Blockly.Grid.prototype.update = function (this: Blockly.Grid, scale: number) { | ||
oldGridUpdate.call(this, scale); | ||
|
||
const patternsToRemove: string[] = []; | ||
for (const patternId of gridPatternIds) { | ||
const imagePattern = document.getElementById(patternId) as unknown as SVGPatternElement; | ||
|
||
if (!imagePattern) { | ||
patternsToRemove.push(patternId); | ||
continue; | ||
} | ||
|
||
imagePattern.setAttribute("width", options.image.width); | ||
imagePattern.setAttribute("height", options.image.height); | ||
imagePattern.setAttribute('patternTransform', 'scale(' + scale + ')'); | ||
} | ||
|
||
for (const patternId of patternsToRemove) { | ||
gridPatternIds.splice(gridPatternIds.indexOf(patternId), 1); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import { monkeyPatchBlockSvg } from "./blockSvg"; | ||
import { monkeyPatchGrid } from "./grid"; | ||
|
||
export function applyMonkeyPatches() { | ||
monkeyPatchBlockSvg(); | ||
monkeyPatchGrid(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters