-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First steps towards a region editor!
- Loading branch information
1 parent
50a082b
commit baf5402
Showing
5 changed files
with
246 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8"/> | ||
<title>Habitat Inspector</title> | ||
<link rel="stylesheet" href="style.css"> | ||
<script type="importmap"> | ||
{ | ||
"imports": { | ||
"htm": "./vendor/htm.mjs", | ||
"preact": "./vendor/preact.mjs", | ||
"preact/hooks": "./vendor/hooks.mjs", | ||
"@preact/signals-core": "./vendor/signals-core.mjs", | ||
"@preact/signals": "./vendor/signals.mjs" | ||
} | ||
} | ||
</script> | ||
<script type="module"> | ||
// import "https://esm.sh/*preact/devtools" | ||
import { render } from "preact" | ||
import { signal } from "@preact/signals" | ||
import { html, errors } from "./view.js" | ||
import { useHabitatJson, errorBucket, until } from "./data.js" | ||
import { regionView } from "./region.js" | ||
import { navigationView } from "./navigate.js" | ||
import { selectionInteraction, propEditor, createEditTracker } from "./edit.js" | ||
import { jsonDump } from "./show.js" | ||
|
||
const q = (k) => (new URLSearchParams(window.location.search)).get(k) | ||
const filename = q("f") ?? "db/new_Downtown/Downtown_4d.json" | ||
|
||
const tracker = createEditTracker() | ||
const objectList = signal( | ||
(await until(() => useHabitatJson(filename), o => o.length > 0)) | ||
.map(o => tracker.trackSignal(signal(o)))) | ||
|
||
const regionName = ({ objects }) => { | ||
const region = objects.find(obj => obj.type == "context") | ||
return html`<span>${(region && region.name) ?? filename}</span>` | ||
} | ||
|
||
const regionPage = (_) => html` | ||
<h1>Region Editor - <${regionName} objects=${objectList.value}/></h1> | ||
<div style="display: flex; flex-wrap: wrap"> | ||
<${regionView} objects=${objectList.value} interaction=${selectionInteraction}/> | ||
</div> | ||
<${propEditor} objects=${objectList.value}/> | ||
<${jsonDump} heading="JSON" value=${objectList.value}/> | ||
<${errors}/>` | ||
|
||
render(html`<${regionPage}/>`, document.getElementById("regionview")) | ||
</script> | ||
</head> | ||
<body> | ||
<div id="regionview"> | ||
</div> | ||
<a href="index.html">Home</a> | ||
</body> | ||
</html> |
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,154 @@ | ||
import { createContext } from "preact" | ||
import { useContext } from "preact/hooks" | ||
import { signal, effect } from "@preact/signals" | ||
import { html } from "./view.js" | ||
import { emptyBitmap } from "./codec.js" | ||
import { c64Colors, canvasFromBitmap, canvasImage } from "./render.js" | ||
import { colorsFromOrientation } from "./neohabitat.js" | ||
|
||
export const Selection = createContext(signal(null)) | ||
export const selectionInteraction = ({ object, children }) => { | ||
const selectionRef = useContext(Selection) | ||
return html` | ||
<div onclick=${() => selectionRef.value = object.ref}> | ||
${selectionRef.value === object.ref ? html` | ||
<div style="position: absolute; left: 0; right: 0; top: 0; bottom: 0; | ||
background-color: #ff000040; border: 1px solid red;"></div> | ||
` : null} | ||
${children} | ||
</a>` | ||
} | ||
|
||
export const createEditTracker = () => { | ||
const editHistory = [] | ||
const redoHistory = [] | ||
|
||
const update = (obj, key, value) => { | ||
const result = Array.isArray(obj) ? [...obj] : {...obj} | ||
result[key] = value | ||
return result | ||
} | ||
|
||
const updateIn = (obj, place, key, value) => { | ||
if (place.length == 0) { | ||
return update(obj, key, value) | ||
} else { | ||
return update(obj, place[0], updateIn(obj[place[0]], place.slice(1), key, value)) | ||
} | ||
} | ||
|
||
const valueAt = (sig, place) => { | ||
let value = sig.value | ||
for (const placeKey of place) { | ||
value = value[placeKey] | ||
} | ||
return value | ||
} | ||
|
||
const performEdit = (sig, place, key, value, history) => { | ||
const previous = valueAt(sig, place)[key] | ||
sig.value = updateIn(sig.value, place, key, value) | ||
history.push({ sig, place, key, value, previous }) | ||
} | ||
|
||
const change = (sig, place, key, value) => { | ||
performEdit(sig, place, key, value, editHistory) | ||
redoHistory.length = 0 | ||
} | ||
|
||
const undo = (fromHistory = editHistory, toHistory = redoHistory) => { | ||
const edit = fromHistory.pop() | ||
if (edit) { | ||
performEdit(edit.sig, edit.place, edit.key, edit.previous, toHistory) | ||
} | ||
} | ||
|
||
const redo = () => undo(redoHistory, editHistory) | ||
const dynamicProxy = (targetGetter, handler) => { | ||
return new Proxy(targetGetter(), { | ||
ownKeys(target) { return Reflect.ownKeys(targetGetter(target)) }, | ||
getOwnPropertyDescriptor(target, prop) { return Reflect.getOwnPropertyDescriptor(targetGetter(target), prop) }, | ||
...handler | ||
}) | ||
} | ||
const wrapInnerValue = (sig, value, place, refreshParent = () => {}) => { | ||
if (typeof(value) === "object") { | ||
let innerTarget = value | ||
const refresh = () => { innerTarget = valueAt(sig, place) } | ||
return dynamicProxy(() => innerTarget, { | ||
get(target, property, receiver) { | ||
return wrapInnerValue(sig, innerTarget[property], [...place, property], refresh) | ||
}, | ||
set(target, property, newValue, receiver) { | ||
change(sig, place, property, newValue) | ||
refresh() | ||
} | ||
}) | ||
} else { | ||
return value | ||
} | ||
} | ||
|
||
return { | ||
undo, | ||
redo, | ||
editHistory, | ||
redoHistory, | ||
trackSignal(sig) { | ||
return dynamicProxy(() => sig.value, { | ||
get(target, property, receiver) { | ||
return wrapInnerValue(sig, sig.value[property], [property]) | ||
}, | ||
set(target, property, newValue, receiver) { | ||
change(sig, [], property, newValue) | ||
} | ||
}) | ||
} | ||
} | ||
} | ||
|
||
export const orientationEditor = ({ obj }) => { | ||
const mod = obj.mods[0] | ||
// color, pattern, flip | ||
const colors = colorsFromOrientation(mod.orientation) | ||
const flipped = (mod.orientation & 0x01) != 0 | ||
return html` | ||
<fieldset> | ||
<legend>Orientation</legend> | ||
<div style="display: flex"> | ||
${c64Colors.map((color, icolor) => html` | ||
<div key=${`color${icolor}`} | ||
style="width: 48px; height: 48px; margin: 2px; | ||
border: 4px dotted ${colors.wildcard === icolor ? " black" : "transparent"};" | ||
onclick=${() => { mod.orientation = (mod.orientation & 0x07) | 0x80 | (icolor << 3) }}> | ||
<div style="background-color: #${color.toString(16).padStart(6, "0")}; width: 100%; height: 100%;"/> | ||
</div>`)} | ||
</div> | ||
<div style="display: flex"> | ||
${[...Array(15).keys()].map((ipattern) => html` | ||
<div key=${`pattern${ipattern}`} | ||
style="width: 48px; height: 48px; margin: 2px; | ||
border: 4px dotted ${colors.pattern === ipattern ? " black" : "transparent"};" | ||
onclick=${() => { mod.orientation = (mod.orientation & 0x07) | (ipattern << 3) }}> | ||
<${canvasImage} canvas=${canvasFromBitmap(emptyBitmap(2, 16, 1), { pattern: ipattern })}/> | ||
</div>`)} | ||
</div> | ||
<div> | ||
<label> | ||
<input type="checkbox" checked=${flipped} | ||
onclick=${() => { mod.orientation = (mod.orientation & 0xfe) | (flipped ? 0 : 1) }}/> | ||
Flip horizontally | ||
</label> | ||
</div> | ||
</fieldset>` | ||
} | ||
|
||
export const propEditor = ({ objects }) => { | ||
const selectionRef = useContext(Selection) | ||
if (selectionRef.value != null) { | ||
const obj = objects.find(o => o.ref === selectionRef.value) | ||
if (obj) { | ||
return html`<${orientationEditor} obj=${obj}/>` | ||
} | ||
} | ||
} |
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