Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion apps/maker-gjs/src/services/layer-draft.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { describe, expect, it } from '@gjsify/unit'

import type { LayerData } from '@pixelrpg/engine'
import { nextLayerDraft } from './layer-draft.ts'
import { nextLayerDraft, planeTarget } from './layer-draft.ts'

const layer = (id: string, name: string): LayerData => ({ id, name, visible: true })

/** The layer id a tap lands on, or null when it would create one. */
const targetId = (t: ReturnType<typeof planeTarget>): string | null => (t.kind === 'existing' ? t.id : null)

export default async () => {
await describe('nextLayerDraft', async () => {
await it('numbers the new layer after the existing count', async () => {
Expand All @@ -27,4 +30,28 @@ export default async () => {
expect(existing.some((l) => l.id === draft.id)).toBe(false)
})
})

await describe('planeTarget', async () => {
const starter = [layer('layer_0', 'Background')]

await it('creates a layer for a plane a new game does not have yet', async () => {
expect(planeTarget(starter, 'hero').kind).toBe('create')
expect(planeTarget(starter, 'overlay').kind).toBe('create')
})

await it('lands on the ground layer, which has no plane key', async () => {
expect(targetId(planeTarget(starter, 'ground'))).toBe('layer_0')
})

await it('prefers the remembered layer, then the topmost of the plane', async () => {
const layers = [
...starter,
{ ...layer('a', 'A'), plane: 'hero' as const },
{ ...layer('b', 'B'), plane: 'hero' as const },
]
expect(targetId(planeTarget(layers, 'hero'))).toBe('b')
expect(targetId(planeTarget(layers, 'hero', 'a'))).toBe('a')
expect(targetId(planeTarget(layers, 'hero', 'gone'))).toBe('b')
})
})
}
15 changes: 15 additions & 0 deletions apps/maker-gjs/src/services/layer-draft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,18 @@ export function nextLayerDraft(layers: readonly LayerData[], plane?: LayerPlane)
const name = `Layer ${layers.length + 1}`
return { id: uniqueIdFrom(name, taken, 'layer'), name, visible: true, ...(plane ? { plane } : {}) }
}

/**
* Where a tap on a plane chip lands: the remembered layer if it still
* exists, else the plane's topmost layer, else `create` - a new game
* starts with the ground only, and every height must stay reachable.
*/
export function planeTarget(
layers: ReadonlyArray<{ id: string; plane?: LayerPlane }>,
plane: LayerPlane,
remembered?: string,
): { kind: 'existing'; id: string } | { kind: 'create' } {
if (remembered && layers.some((l) => l.id === remembered)) return { kind: 'existing', id: remembered }
const top = [...layers].reverse().find((l) => (l.plane ?? 'ground') === plane)
return top ? { kind: 'existing', id: top.id } : { kind: 'create' }
}
17 changes: 7 additions & 10 deletions apps/maker-gjs/src/widgets/scene-editor-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import {
import { terrainBrushKey, terrainIdOfBrushKey, tileBrushKey, tileIdOfBrushKey } from '../services/brush-keys.ts'
import type { DoorInfo } from '../services/door-target-flow.ts'
import { toLayerDescriptors } from '../services/layer-descriptors.ts'
import { planeTarget } from '../services/layer-draft.ts'
import { loadStarterPacks } from '../services/starter-pack-catalog.ts'
import { shelfModel } from '../services/starter-packs.ts'
import type { LoadedProject } from '../services/project-loader.ts'
Expand Down Expand Up @@ -1463,18 +1464,14 @@ export class SceneEditorView extends ResponsiveEditorView {

/**
* A plane chip was tapped: activate that plane's last-used layer, or
* its topmost one when this session has not been there yet. The chip
* is insensitive for a plane with no layers, so there is always one to
* land on.
* its topmost one when this session has not been there yet. A plane
* with no layer gets one (a single undo step) — a new game starts
* with the ground only, and the author must still reach every height.
*/
private _selectPlane(plane: LayerPlane): void {
const remembered = this._lastLayerByPlane.get(plane)
if (remembered && this._layers.some((l) => l.id === remembered)) {
this._setActiveLayer(remembered)
return
}
const fallback = [...this._layers].reverse().find((l) => planeOf(l) === plane)
if (fallback) this._setActiveLayer(fallback.id)
const target = planeTarget(this._layers, plane, this._lastLayerByPlane.get(plane))
if (target.kind === 'existing') this._setActiveLayer(target.id)
else this.activate_action('win.new-layer', GLib.Variant.new_string(plane))
}

/**
Expand Down
9 changes: 4 additions & 5 deletions packages/gjs/src/widgets/editor/brush-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,18 +444,17 @@ export class BrushPage extends Adw.Bin {
}

/**
* Check the chip for the active layer's plane, and grey out the planes
* the map has no layer on — tapping one would have nowhere to go, and
* a tooltip says so rather than the tap silently doing nothing.
* Check the chip for the active layer's plane. Every chip stays
* tappable: a plane the map has no layer on gets one when tapped, and
* its tooltip says so.
*/
setActivePlane(plane: LayerPlane, populated: readonly LayerPlane[]): void {
this._planes = new Set(populated)
for (const candidate of CHIP_PLANES) {
const toggle = this._plane_chips.get_toggle_by_name(candidate)
if (!toggle) continue
const has = this._planes.has(candidate)
toggle.set_enabled(has)
toggle.set_tooltip(has ? PLANE_CAPTIONS[candidate].long() : _('No layer here yet'))
toggle.set_tooltip(has ? PLANE_CAPTIONS[candidate].long() : _('Adds a layer here'))
}
this._echo = true
try {
Expand Down
6 changes: 3 additions & 3 deletions packages/translations/de.po
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,9 @@ msgstr "„%s“ zu dieser Karte hinzufügen"
msgid "Added flag “%s”"
msgstr "Flagge „%s“ hinzugefügt"

msgid "Adds a layer here"
msgstr "Legt hier eine Ebene an"

#, javascript-format
msgid "Adds: %s"
msgstr "Fügt hinzu: %s"
Expand Down Expand Up @@ -1293,9 +1296,6 @@ msgstr "Noch keine Grafiken"
msgid "No hero yet"
msgstr "Noch keine Heldenfigur"

msgid "No layer here yet"
msgstr "Hier noch keine Ebene"

msgid "No map yet"
msgstr "Noch keine Karte"

Expand Down
6 changes: 3 additions & 3 deletions packages/translations/org.pixelrpg.maker.pot
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,9 @@ msgstr ""
msgid "Added flag “%s”"
msgstr ""

msgid "Adds a layer here"
msgstr ""

#, javascript-format
msgid "Adds: %s"
msgstr ""
Expand Down Expand Up @@ -1294,9 +1297,6 @@ msgstr ""
msgid "No hero yet"
msgstr ""

msgid "No layer here yet"
msgstr ""

msgid "No map yet"
msgstr ""

Expand Down
Loading