From 1d4befb04b916e7db5aa454648d8c7dc71de7364 Mon Sep 17 00:00:00 2001 From: Julien Mourer Date: Tue, 23 Feb 2021 22:39:08 +0100 Subject: [PATCH] Add shortcut support for adding to a selection This series of commit closes #46! --- src/epics/select.ts | 44 +++++++++++++++++++++----------------------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/src/epics/select.ts b/src/epics/select.ts index e3ca3c5..3a02491 100644 --- a/src/epics/select.ts +++ b/src/epics/select.ts @@ -1,5 +1,5 @@ import { fromEvent, empty, of, merge } from 'rxjs'; -import { map, tap, switchMap, takeUntil, ignoreElements, filter, scan, mergeMap, pluck, switchMapTo } from 'rxjs/operators'; +import { map, tap, switchMap, takeUntil, ignoreElements, filter, scan, mergeMap, pluck, switchMapTo, mapTo } from 'rxjs/operators'; import { ofType, Epic } from 'epix'; import { resolveIdentifier } from 'mobx-state-tree'; import { testPolygonCircle, testPolygonPolygon, Polygon, Vector, pointInCircle, pointInPolygon } from 'sat'; @@ -166,23 +166,12 @@ export const selectVertex: Epic = (action$, { store }) => { ); }; -export const unselect: Epic = (action$, { store }) => { +export const selectionBox: Epic = (action$, { store }) => { return action$.pipe( ofType('backgroundPointerDown'), // middle click is panning only filter(({ ev }) => !(ev.data.pointerType === 'mouse' && ev.data.button === 1)), filter(() => store.editor.mode === EditorMode.select), - tap(() => { - store.editor.clearSelection(); - }), - ignoreElements(), - ); -}; - -export const selectionBox: Epic = (action$, { store }) => { - return action$.pipe( - ofType('backgroundPointerDown'), - filter(() => store.editor.mode === EditorMode.select), // it's important to use global and not original event // because TouchEvents don't have clientX pluck('ev', 'data', 'global'), @@ -200,32 +189,41 @@ export const selectionBox: Epic = (action$, { store }) => { store.editor.updateSelectionBox(posInWorld); }), takeUntil(merge( - fromEvent(document, 'pointerup'), + fromEvent(document, 'pointerup').pipe( + map((ev) => isShortcut(ev)), + ), fromMobx(() => store.editor.mode).pipe( - filter((mode) => mode !== EditorMode.select) + filter((mode) => mode !== EditorMode.select), + mapTo(false), ), ).pipe( - tap(() => { - store.level.entities.forEach((entity: IEntity) => { + tap((shortcut) => { + const entitiesToAdd = store.level.entities.filter((entity: IEntity) => { if ('params' in entity && 'asSatCircle' in entity.params) { const tester = store.editor.selectionBoxAsSat instanceof Vector ? pointInCircle : testPolygonCircle; - if (!tester( + return tester( store.editor.selectionBoxAsSat, entity.params.asSatCircle - )) return; + ); store.editor.addToSelection(entity); } if ('params' in entity && 'asSatPolygons' in entity.params) { const tester = store.editor.selectionBoxAsSat instanceof Vector ? pointInPolygon : testPolygonPolygon; - if (entity.params.asSatPolygons.every((polygon: Polygon) => !tester(store.editor.selectionBoxAsSat, polygon))) { - return; - } + return entity.params.asSatPolygons + .some((polygon: Polygon) => tester(store.editor.selectionBoxAsSat, polygon)); - store.editor.addToSelection(entity); } + + return false; }); + if (shortcut) { + entitiesToAdd.forEach((entity: IEntity) => store.editor.addToSelection(entity)); + } else { + store.editor.setSelection(entitiesToAdd); + } + store.editor.endSelectionBox(); store.undoManager.stopGroup(); }),