mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
feat(globe): render the tilt and north-up buttons from the globe actions store
The camera orientation controls publish whether the map is tilted and the camera's whole-degree heading instead of writing the buttons' attributes, and answer tilt and north-up through the globe actions store. The new GlobeCompass renders the heading, so a rotating camera re-renders only the compass. The store drops an unchanged camera state and keeps each clear owner's busy state, so the buttons show the shell that answers them: a shell replaced by a newer one, or disposed mid-clear, leaves the live shell's state alone, and an older shell that answers again shows its own. createOwners gains owns() and ownerOf().
This commit is contained in:
1 parent
7edf8a577e
commit
eccaeea0c4
15 files changed
+495
-180
No files matched your search
@@ -1,22 +1,82 @@
|
||||
import { defineStore } from 'pinia';
|
||||
import { ref } from 'vue';
|
||||
import { computed, ref, shallowReactive } from 'vue';
|
||||
import { createOwners } from './owners.js';
|
||||
|
||||
/**
|
||||
* The top-center globe actions as their view renders them. The shell
|
||||
* publishes whether a clear of the selected data layers is under way, and
|
||||
* answers the clear and share buttons through act().
|
||||
* publishes whether a clear of the selected data layers is under way and the
|
||||
* camera's tilt and heading, and answers the buttons through act().
|
||||
*/
|
||||
export const useGlobeActionsStore = defineStore('gev-globe-actions', () => {
|
||||
/** Whether a clear is under way; null until the shell first says. */
|
||||
const clearBusy = ref(null);
|
||||
const owners = createOwners();
|
||||
// Who answers which action, as the computed state below reads it.
|
||||
const ownership = ref(0);
|
||||
/** Each clear owner's clear under way, by its token. */
|
||||
const busyBy = shallowReactive(new Map());
|
||||
|
||||
/** @param {boolean | null} busy null when no shell answers the clear. */
|
||||
function publishClearBusy(busy) {
|
||||
clearBusy.value = busy == null ? null : Boolean(busy);
|
||||
/**
|
||||
* Whether the clear owner's clear is under way; null until it says. Each
|
||||
* owner keeps its own, so the button shows the one that answers it.
|
||||
*/
|
||||
const clearBusy = computed(() => {
|
||||
void ownership.value;
|
||||
return busyBy.get(owners.ownerOf('clear')) ?? null;
|
||||
});
|
||||
|
||||
/**
|
||||
* @param {object} token The clear owner's, from connect().
|
||||
* @param {boolean} busy
|
||||
*/
|
||||
function publishClearBusy(token, busy) {
|
||||
busyBy.set(token, Boolean(busy));
|
||||
}
|
||||
|
||||
const { connect, detach, act } = createOwners();
|
||||
/**
|
||||
* Whether the map reads as tilted and the camera's whole-degree heading;
|
||||
* null until the camera controls first say.
|
||||
* @type {import('vue').Ref<{ tilted: boolean, heading: number } | null>}
|
||||
*/
|
||||
const camera = ref(null);
|
||||
|
||||
return { clearBusy, publishClearBusy, connect, detach, act };
|
||||
/**
|
||||
* Take the camera's state; the same state again changes nothing, so its
|
||||
* owner can publish it on every frame the camera draws.
|
||||
* @param {{ tilted: boolean, heading: number } | null} state
|
||||
*/
|
||||
function publishCamera(state) {
|
||||
const current = camera.value;
|
||||
if (!state) {
|
||||
camera.value = null;
|
||||
return;
|
||||
}
|
||||
const tilted = Boolean(state.tilted);
|
||||
if (current?.tilted === tilted && current.heading === state.heading) return;
|
||||
camera.value = { tilted, heading: state.heading };
|
||||
}
|
||||
|
||||
/** @param {Record<string, Function>} actions */
|
||||
function connect(actions) {
|
||||
const token = owners.connect(actions);
|
||||
ownership.value += 1;
|
||||
return token;
|
||||
}
|
||||
|
||||
function detach(token) {
|
||||
owners.detach(token);
|
||||
busyBy.delete(token);
|
||||
ownership.value += 1;
|
||||
}
|
||||
|
||||
const { act, owns } = owners;
|
||||
|
||||
return {
|
||||
clearBusy,
|
||||
publishClearBusy,
|
||||
camera,
|
||||
publishCamera,
|
||||
connect,
|
||||
detach,
|
||||
act,
|
||||
owns,
|
||||
};
|
||||
});
|
||||
+22
-2
@@ -3,7 +3,7 @@
|
||||
* the newest connected owner with an action of that name answers it, so an
|
||||
* owner that connects again replaces itself, and owners of different actions
|
||||
* sit side by side.
|
||||
* @returns {{connect: Function, detach: Function, act: Function}}
|
||||
* @returns {{connect: Function, detach: Function, act: Function, owns: Function, ownerOf: Function}}
|
||||
*/
|
||||
export function createOwners() {
|
||||
/** @type {Array<{actions: object}>} Oldest first. */
|
||||
@@ -40,5 +40,25 @@ export function createOwners() {
|
||||
}
|
||||
}
|
||||
|
||||
return { connect, detach, act };
|
||||
/**
|
||||
* The token of the owner that answers `name`, if any.
|
||||
* @param {string} name
|
||||
* @returns {object | undefined}
|
||||
*/
|
||||
function ownerOf(name) {
|
||||
for (let index = owners.length - 1; index >= 0; index -= 1)
|
||||
if (owners[index].actions[name]) return owners[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the owner holding `token` is the one that answers `name`, so an
|
||||
* owner that also publishes state can leave it to a newer one.
|
||||
* @param {object} token
|
||||
* @param {string} name
|
||||
*/
|
||||
function owns(token, name) {
|
||||
return Boolean(token) && ownerOf(name) === token;
|
||||
}
|
||||
|
||||
return { connect, detach, act, owns, ownerOf };
|
||||
}
|
||||
@@ -33,3 +33,25 @@ test('act returns what the answering action returned', () => {
|
||||
assert.equal(act('pick', 'jazz'), 'kept jazz');
|
||||
assert.equal(act('missing'), undefined);
|
||||
});
|
||||
|
||||
test('owns names the owner that answers an action', () => {
|
||||
const { connect, detach, owns } = createOwners();
|
||||
const older = connect({ tilt() {}, share() {} });
|
||||
const newer = connect({ tilt() {} });
|
||||
assert.equal(owns(newer, 'tilt'), true);
|
||||
assert.equal(owns(older, 'tilt'), false);
|
||||
assert.equal(owns(older, 'share'), true);
|
||||
assert.equal(owns(newer, 'missing'), false);
|
||||
assert.equal(owns(null, 'tilt'), false);
|
||||
detach(newer);
|
||||
assert.equal(owns(older, 'tilt'), true);
|
||||
});
|
||||
|
||||
test('ownerOf names the token that answers an action', () => {
|
||||
const { connect, ownerOf } = createOwners();
|
||||
const older = connect({ clear() {}, share() {} });
|
||||
const newer = connect({ clear() {} });
|
||||
assert.equal(ownerOf('clear'), newer);
|
||||
assert.equal(ownerOf('share'), older);
|
||||
assert.equal(ownerOf('missing'), undefined);
|
||||
});
|
||||
Reference in new issue
Block a user