Recurring cleanup round over the code since round 6 (056a622), 105
commits. Behaviour is unchanged. Duplicated code in src went from 29
clones with 229 lines (0.32 %) to 14 clones with 109 lines (0.15 %),
measured with jscpd and the same settings as round 6.
- BehaviorSystem.ts shrinks from 941 to 589 lines. Collecting the
pulses (keys, timers, collisions, animations) moved to
behaviorPulses.ts. The NodeContext the nodes receive moved to
behaviorContext.ts. Imports from outside stay the same, TimeScaled
is still exported from BehaviorSystem.ts.
- PointKindButton and drawPointGrip replace the "Point:" button with
its menu and the point and handle drawing that the curve and the
vector art tab each had on their own.
- lerpVec, cubicAt and splitCubic in src/core/util/vec.ts replace four
copies of the cubic curve and two de Casteljau splits.
- mapNested in Properties.ts is the one walk over lists and structs
that liveObjects and objectRefs did separately. withFieldRenamed
and withoutField replace three copies of renaming a struct field
and two of removing one.
- jsonBody and staleRevision build the JSON body and turn a 409 into
StaleRevisionError for all stores. The auth store turns a 401 into
its error in one place.
- Smaller helpers: structAssetFrom for the two "Make struct type"
commands, shownObject in the inspector, selectedItem for the size
gizmo and the polygon corners, solidTiles in the tilemap collision,
requireVariablesAsset and requireBehaviorVariable.
- Left as is on purpose: the plain forwarding from the collision and
physics systems to Solids, the pointer wiring of the canvases and
the identical last lines of the two make commands.
- Removed unused code: tilemapTileIds and tilemapsUsing were never
called since #128 and #131. EditorState.runtimeProject became unused
with GameRun in #266. The assetId getter of the make commands was
read only by a test.
- 60 values used only in their own file lost their export. The shader
and particle node catalogues stay exported as in round 6.
- Five comments that told history now describe the current state. The
doc comment of BehaviorSystem sits on the class again.
- README lists the struct, clip area and curve assets and mentions the
game link from #266.
Findings:
- Deleting a tile that is painted on a tilemap leaves its cells in
place, and they draw nothing. The delete dialog does not mention the
tilemaps. That was probably the job of the unused tilemapsUsing.
Left as is, because fixing it changes behaviour.
- The solid filter for tile surfaces was untested. A bent filter
stayed green. A new test in tests/core/surface.test.ts now fails
against it.
Tests:
- ./check.sh: 195 files, 2815 tests, exit 0 (one test removed with
tilemapsUsing, one added for tile surfaces).
- Full e2e run: ALL PASSED, 1580 checks.
- Smokes jumprun, tetris, actor: 24, 19 and 21 checks, all passed.
The example project files were restored byte for byte afterwards.
- Counter-checks, 12, all fail as expected: 10 in unit tests (among
them mapNested, splitCubic, cubicAt, staleRevision, the stay pulse,
the late timer and the time scale), 2 in the browser (Point button
always enabled fails 47, selectedItem without a hit fails 29).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
109 lines
3.5 KiB
TypeScript
109 lines
3.5 KiB
TypeScript
import {
|
||
GroupObjectsCommand,
|
||
RenameGroupCommand,
|
||
SetObjectGroupCommand,
|
||
UngroupCommand,
|
||
} from '../core/commands/groupCommands';
|
||
import { nextFreeName } from '../core/commands/naming';
|
||
import { canGroup, findGroup, findObject, groupMemberIds } from '../core/model/hierarchy';
|
||
import type { ObjectHolder } from '../core/model/Project';
|
||
import type { MenuEntry, MenuItem } from '../ui';
|
||
import { GroupNameDialog } from './panels/GroupNameDialog';
|
||
import { type EditorState, objectSelection } from './state';
|
||
|
||
/** "Group", "Group 2", … – free among the groups that are listed. */
|
||
function nextGroupName(holder: ObjectHolder): string {
|
||
const listed = (holder.groups ?? []).filter((g) => groupMemberIds(holder, g.id).length > 0);
|
||
return nextFreeName(
|
||
listed.map((g) => g.name),
|
||
'Group',
|
||
);
|
||
}
|
||
|
||
/**
|
||
* The object menu's entries for groups (#241). `source` is the stored holder: content of a placed
|
||
* container is a child, so it can never be grouped.
|
||
*/
|
||
export function groupMenuItems(
|
||
state: EditorState,
|
||
source: ObjectHolder,
|
||
selected: readonly string[],
|
||
): MenuItem[] {
|
||
const many = selected.length > 1;
|
||
const grouped = selected.filter((id) => findGroup(source, findObject(source, id)?.groupId ?? ''));
|
||
return [
|
||
{
|
||
label: many ? `Group ${selected.length} objects…` : 'Group…',
|
||
disabled: !canGroup(source, selected),
|
||
onSelect: () => groupObjects(state, source, selected),
|
||
},
|
||
{
|
||
label: 'Remove from group',
|
||
disabled: grouped.length === 0,
|
||
onSelect: () => state.execute(new SetObjectGroupCommand(source.id, grouped, '')),
|
||
},
|
||
];
|
||
}
|
||
|
||
/**
|
||
* Asks for the name of a new group – or, from a dropdown, for an existing group to put the objects
|
||
* into. A group all of them are in already is not offered.
|
||
*/
|
||
function groupObjects(state: EditorState, source: ObjectHolder, ids: readonly string[]): void {
|
||
const title = ids.length > 1 ? `Group ${ids.length} objects` : 'Group the object';
|
||
const groups = (source.groups ?? []).filter((g) => {
|
||
const members = groupMemberIds(source, g.id);
|
||
return members.length > 0 && !ids.every((id) => members.includes(id));
|
||
});
|
||
GroupNameDialog.open(
|
||
new GroupNameDialog(
|
||
title,
|
||
nextGroupName(source),
|
||
'Group',
|
||
(name) => state.execute(new GroupObjectsCommand(source.id, ids, name)),
|
||
{
|
||
groups,
|
||
confirm: 'Add',
|
||
onJoin: (groupId) => state.execute(new SetObjectGroupCommand(source.id, ids, groupId)),
|
||
},
|
||
),
|
||
);
|
||
}
|
||
|
||
/** The menu of a group's row in the hierarchy; `extras` go below, where the panel adds folding. */
|
||
export function groupRowItems(
|
||
state: EditorState,
|
||
source: ObjectHolder,
|
||
groupId: string,
|
||
extras: readonly MenuItem[],
|
||
): MenuEntry[] {
|
||
const group = findGroup(source, groupId);
|
||
if (!group) return [...extras];
|
||
return [
|
||
{
|
||
label: 'Select members',
|
||
onSelect: () => selectGroup(state, source, groupId),
|
||
},
|
||
{
|
||
label: 'Rename…',
|
||
onSelect: () =>
|
||
GroupNameDialog.open(
|
||
new GroupNameDialog(`Rename group "${group.name}"`, group.name, 'Rename', (name) => {
|
||
if (name !== group.name) state.execute(new RenameGroupCommand(source.id, groupId, name));
|
||
}),
|
||
),
|
||
},
|
||
{
|
||
label: 'Ungroup',
|
||
onSelect: () => state.execute(new UngroupCommand(source.id, groupId)),
|
||
},
|
||
{ separator: true },
|
||
...extras,
|
||
];
|
||
}
|
||
|
||
export function selectGroup(state: EditorState, source: ObjectHolder, groupId: string): void {
|
||
const members = groupMemberIds(source, groupId);
|
||
state.select(objectSelection(source.id, members, members[0]));
|
||
}
|