import { fileStorage, Placeable, Placement, useLayout } from "@apricotjs/react-native";
import type {
ChangeOptions, Collide, Extent, PlaceChange, PlaceOptions, FreePlaceOptions, KeptLayout, Layout, LayoutItem, LayoutOptions, LayoutStorage, MarkerOptions, Place, PlaceCheck, PlaceOn,
PlacementArea, PlacementBounds, PlacementHandle, PlacementProps, PlacementSnap, PlacementZone, PlaceableProps, SnapPoint, WallLine,
} from "@apricotjs/react-native";Placement#
Lets people pick things up, move them and turn them by hand. Tap a <Placeable> to pick it up, drag to move it, twist two fingers to turn it, and tap empty space to put it down. A drag that starts on the marker moves the picked-up thing. Other drags still reach <CameraRig orbit>. Put <Placement> inside <Scene>.
| Prop | Type | Default | Description |
|---|---|---|---|
bounds |
[minX, minZ, maxX, maxZ] |
anywhere | The floor area things stay inside, in metres. Also the four walls of a rectangular room |
walls |
(Outline | WallLine)[] |
the four walls round bounds |
Walls of any shape: room outlines and free-standing walls. See walls |
wallHeight |
metres | 2.4 |
Height of the walls (round bounds, or of walls), for things hung on them |
areas |
PlacementArea[] |
anywhere (or bounds) |
Ground and water of any shape, each at its level |
zones |
PlacementZone[] |
none | Places nothing may stand |
snaps |
SnapPoint[] |
none | Spots that pull things into place and turn them |
snap |
PlacementSnap |
free | { grid, turn, wall } steps while moving |
collide |
Collide |
"block" |
What a moved thing does when it meets another |
canPlace |
(id, place: Place, check: PlaceCheck) => boolean |
Your own rule, called while a thing moves or turns | |
selected |
string | null |
uncontrolled | The picked-up thing's id, to control it yourself |
onSelect |
(id: string | null) => void |
Picked up (its id) or put down (null) |
|
onChange |
(id: string, place: Place, change: PlaceChange) => void |
A thing's place changed: its new place and why. Things standing on it report theirs too | |
onArrive |
(id: string, place: Place | null) => void |
A <Placeable findRoom> has arrived: where it is now, or null when there was no room (it stays where it was put) |
|
onOutside |
(ids: string[]) => void |
The ids wholly or partly outside the space, each time that set changes | |
onFitChange |
(id: string, fits: boolean) => void |
Whether the picked-up thing fits where it is | |
lift |
metres | 0.03 |
How high a picked-up thing floats while moved |
marker |
colour, MarkerOptions or false |
"#4f8cff" |
The patch under (or behind) the picked-up thing |
ref |
Ref<PlacementHandle> |
See PlacementHandle |
|
children |
ReactNode |
<Placeable>s, possibly inside your own components |
Places come from your state (position, turn on each <Placeable>) and go back through onChange. Store them, for example with useLayout.
bounds, walls, areas, zones and canPlace can change at any time. The picked-up thing is checked again at once, and onOutside reports what is left outside. Nothing is moved for you: call moveInside.
PlaceChange#
{ by: "hand" | "code" | "arrival" }.
by |
The thing was |
|---|---|
"hand" |
Moved or turned by hand, or carried by what it stands on |
"code" |
Moved by the ref (turnBy, moveInside) |
"arrival" |
Moved to find room as it arrived (findRoom) |
walls#
Each item is an Outline or a WallLine.
| Item | Description |
|---|---|
Outline |
A room's corners [x, z] in order round it, either way round. Each side is a wall facing into the room. Without areas, the outline is also the floor things stay on |
WallLine |
A free-standing wall (a partition, a screen) with a face on each side. Floor things keep off it |
WallLine field |
Type | Default | Description |
|---|---|---|---|
from, to |
[x, z] |
required | Its ends, metres |
height |
metres | wallHeight |
How high it is |
id |
string |
wall <n> (its place in walls) |
What canPlace is told, in check.zones, when a floor thing touches it |
Things hang, snap flush and are picked up on walls at any angle. walls replaces the four walls of bounds. If bounds is also given, it still keeps things inside its rectangle. walls only describes where the walls are: draw them yourself.
Collide#
| Value | A moved thing that meets another |
|---|---|
"block" |
Stops against it and slides along it. Where canPlace returns false, it stays where it last fitted |
"warn" |
Passes through it, with the marker in its blocked colour. If let go there, it goes back to where it last fitted. It also passes over the wrong kind of ground, water and zones, marked |
"off" |
Ignores it |
PlacementSnap#
| Field | Type | Default | Description |
|---|---|---|---|
grid |
metres | 0 (free) |
Grid step for positions |
turn |
degrees | 0 (free) |
Step for turns |
wall |
metres | 0 (off) |
Moves things flush against a wall within this distance |
Place and PlaceCheck#
Place is { position: Vec3, turn: number } (turn in degrees).
PlaceCheck, given to canPlace:
| Field | Type | Description |
|---|---|---|
on |
PlaceOn |
Where it goes |
standingOn |
string | null |
The id of what it would stand on (a desk), or null (the floor, a wall) |
overlapping |
string[] |
Solid things it would overlap (always empty with "block" or "off") |
area |
string | null |
The id of the area it would stand or float in |
zones |
string[] |
The ids of the no-go zones it would touch, free-standing walls included |
MarkerOptions#
| Field | Type | Default | Description |
|---|---|---|---|
color |
colour | "#4f8cff" |
Where it fits |
blocked |
colour | "#ff5a4f" |
Where it doesn't (overlap, zone, wrong ground, or canPlace false) |
opacity |
0–1 | 0.8 |
Opacity of the marker |
margin |
metres | 0.12 |
How far it reaches past the thing on each side |
radius |
metres | 0.06 |
Corner rounding. Large values make a pill or a disc |
outline |
metres | 0 (filled) |
Draws only a ring this thick |
Areas, zones and snap points#
PlacementArea:
| Field | Type | Default | Description |
|---|---|---|---|
id |
string |
Given to canPlace as check.area |
|
outline |
Outline |
required | [x, z] corners in order, metres |
level |
metres | 0 |
Height of its top, where things stand |
water |
Waves | true |
Water: only on="water" things go in it, bobbing on these waves |
Things stay wholly inside one area, sliding along its edge. Later areas lie on earlier ones (the sand on the sea, a deck on the sand).
PlacementZone: { id?, outline }. A thing touching one doesn't fit.
SnapPoint:
| Field | Type | Default | Description |
|---|---|---|---|
at |
[x, z] |
required | Where, metres |
turn |
degrees | as it was | The turn it gives what snaps to it |
radius |
metres | 0.3 |
How close it pulls from |
accepts |
(id: string) => boolean |
all | Which things it takes |
PlacementHandle#
| Method | Description |
|---|---|
turnBy(id, degrees) |
Turns a thing (snapped to snap.turn), carrying what stands on it. Returns false if it isn't there or hangs on a wall. With "block", also false if it has no room or canPlace says no. Otherwise it turns even where it doesn't fit, marked |
standingOn(id) |
The ids of the things standing on it |
findFreePlace(options) |
The nearest Place where something fits, with the same checks as moving by hand, or null when there is no room. For something not placed yet, give size or extent. For a placed thing, give its id. See FreePlaceOptions |
outside() |
The ids of the things outside the space now |
moveInside(ids?) |
Moves those things (by default every one outside) to the nearest place they fit, near where they are, with what stands on them. Reported to onChange with by: "code". Returns the ids it found no room for (they stay) |
FreePlaceOptions#
| Option | Type | Default | Description |
|---|---|---|---|
size |
[width, height, depth] |
Metres, as <Box size>: standing on its bottom's middle, or hung by its back's middle |
|
extent |
Extent |
Or its exact extent round its origin (measureModel gives it for a model) |
|
on |
PlaceOn |
"floor", or its own |
"surface": tops of surfaces first, then the floor. "wall": the walls |
id |
string |
"" |
The id it will have, for canPlace. Or a placed thing's id: without size or extent it is measured as it is, and neither it nor what stands on it is in its way. findFreePlace returns null while its model loads |
turn |
degrees | 0, or its own |
Its turn (on a wall, the wall's) |
near |
[x, z] |
the middle of the space | Look near here first |
step |
metres | snap.grid, or 0.1 |
Distance between the places tried |
height |
metres | 1.5 |
On a wall: the height to hang it at, if there is room |
solid |
boolean |
true, or its own |
Whether others can't overlap it |
Extent is { minX, maxX, minY, maxY, minZ, maxZ } in metres, round the thing's origin. findFreePlace throws without size, extent or a placed thing's id.
Placeable#
Something to place by hand. Must be inside a <Placement> (it throws otherwise).
| Prop | Type | Default | Description |
|---|---|---|---|
id |
string |
required | Unique within its <Placement>: what onSelect and onChange report |
position |
Vec3 |
[0, 0, 0] |
Metres. On the floor, y is the level it moves along; on a surface, the surface's top; on a wall, a point on the wall's face, with y its height |
turn |
degrees | 0 |
Round the vertical axis. On a wall, set by the wall: the turn facing out from it. Round bounds: 0 back (min z), 90 min x, 180 front (max z), 270 max x |
on |
PlaceOn |
"floor" |
"floor", "surface" (on things marked surface, or the floor), "wall" (hung on the walls, facing out from them), "water" (floats in water areas) |
surface |
boolean | number |
false |
Things with on="surface" can stand on it and move with it: true for its top, or its top's height in its own space |
solid |
boolean |
true |
Others can't overlap it, nor it them. false for a rug |
locked |
boolean |
false |
Can't be picked up, but still blocks others |
footprint |
[width, depth] | { minX, maxX, minZ, maxZ } |
measured from its parts | The floor it takes up, in metres round its origin, instead of what its parts cover. To leave out only some parts, put them in <Group footprint={false}> |
findRoom |
boolean |
false |
When it first appears (or its model arrives), if it doesn't fit there it moves to the nearest place it does, turned a quarter if only that fits, before it is drawn. Reported to onChange (by: "arrival") and onArrive |
children |
ReactNode |
What it is: shapes, <Merged>, <Model>, a <Lamp> |
Build children standing on their origin and facing +z. Build hung things with their back at z = 0.
The footprint is the floor the children cover, unless given with footprint. It leaves out a <Lamp>'s glow, anything in <Group footprint={false}>, and objects marked with leaveOutOfFootprint(object). A <Model> still loading has no size: it fits anywhere until it arrives, then it is checked.
A thing casts no shadow while it moves (dragged, easing back, or bobbing on water).
useLayout#
const layout = useLayout<T extends LayoutItem>(initial: T[], options?: LayoutOptions): Layout<T>;LayoutItem is { id: string; position: Vec3; turn: number }; add your own fields (kind, a colour).
LayoutOptions#
| Option | Type | Default | Description |
|---|---|---|---|
save |
string |
not kept | Keep it between runs under this name, and start from what was kept |
version |
number |
1 |
The version of your items' shape. A layout kept by another version goes through migrate; without one (or when it returns null) the layout starts from initial |
history |
number |
100 |
How many steps undo goes back |
migrate |
(kept: KeptLayout) => T[] | null |
Turns a layout kept by another version into this version's items, or null when it can't |
|
storage |
LayoutStorage |
fileStorage() |
Where it is kept |
Layout<T>#
| Member | Description |
|---|---|
items |
The layout now |
place(id, place, options?) |
Moves an item. Pass it to <Placement onChange>. Calls in the same moment (one gesture) are one undo step. With { by: "arrival" }, as onChange sends for findRoom, it is not a step of its own. PlaceOptions is ChangeOptions plus by |
add(item, options?) |
Adds an item |
update(id, change, options?) |
Changes some of an item's fields |
remove(ids, options?) |
Removes one id or several, as one step |
set(items) |
Replaces every item (a reset), as one step |
undo(), redo() |
Step back or forward |
clearHistory() |
Forgets the steps to undo and redo; the layout stays |
ensure(items) |
Adds those of items not in the layout yet (by id), in one change that undo doesn't take back. For things kept elsewhere (a server's list of purchases). Items already in stay as they are |
canUndo, canRedo |
Whether there is a step to undo or redo |
newId(prefix) |
An unused id: prefix-1, prefix-2… |
loaded |
With save: true once the kept layout was read. True at once without save |
ChangeOptions (for add, update, remove) is { history?: boolean }. With history: false the change is not an undo step, and undo and redo keep it. A thing bought stays, and one sold stays gone.
A kept layout is written about 0.3 s after the last change, and at once when the component unmounts. It is JSON, { "version": 1, "items": [...] } (KeptLayout), each item { "id", "position", "turn", ...your own fields }. A layout that can't be read (another version with no migration, or a damaged file) is never written over. It is kept beside the new one as <save>.v<its version> or <save>.unreadable.
LayoutStorage and fileStorage#
LayoutStorage is { read(name): Promise<string | null>; write(name, text): Promise<void> | void }.
fileStorage(), the default:
- on iOS and Android, a
<name>.jsonfile in anapricotfolder in the app's documents (expo-file-system). Withoutexpo-file-systeminstalled, nothing is kept and a warning says so once; - on the web,
localStorageunderapricot:<name>.
Your own, for example with AsyncStorage or a server:
import AsyncStorage from "@react-native-async-storage/async-storage";
import type { LayoutStorage } from "@apricotjs/react-native";
export const asyncStorage: LayoutStorage = {
read: (name) => AsyncStorage.getItem(`layout:${name}`),
write: (name, text) => AsyncStorage.setItem(`layout:${name}`, text),
};@apricotjs/core/layout reads and writes kept layouts without React: see Core utilities.