Skip to content

Commit

Permalink
[feat] Adding setAnimationConfig action (#2887)
Browse files Browse the repository at this point in the history
- Added setAnimationConfig action
- Passing filter to PlaybackControls

Signed-off-by: Ihor Dykhta <dikhta.igor@gmail.com>
  • Loading branch information
igorDykhta authored Jan 3, 2025
1 parent 67b0019 commit 0ad5372
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/actions/src/action-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export const ActionTypes = {
TOGGLE_FILTER_ANIMATION: `${ACTION_PREFIX}TOGGLE_FILTER_ANIMATION`,
UPDATE_FILTER_ANIMATION_SPEED: `${ACTION_PREFIX}UPDATE_FILTER_ANIMATION_SPEED`,
PLAY_ANIMATION: `${ACTION_PREFIX}PLAY_ANIMATION`,
SET_ANIMATION_CONFIG: `${ACTION_PREFIX}SET_ANIMATION_CONFIG`,
SET_LAYER_ANIMATION_TIME: `${ACTION_PREFIX}SET_LAYER_ANIMATION_TIME`,
SET_LAYER_ANIMATION_TIME_CONFIG: `${ACTION_PREFIX}SET_LAYER_ANIMATION_TIME_CONFIG`,
UPDATE_ANIMATION_SPEED: `${ACTION_PREFIX}UPDATE_ANIMATION_SPEED`,
Expand Down
21 changes: 20 additions & 1 deletion src/actions/src/vis-state-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import {
ParsedConfig,
ParsedLayer,
EffectPropsPartial,
SyncTimelineMode
SyncTimelineMode,
AnimationConfig,
FilterAnimationConfig
} from '@kepler.gl/types';
import {createAction} from '@reduxjs/toolkit';

Expand Down Expand Up @@ -979,6 +981,23 @@ export function updateFilterAnimationSpeed(
};
}

export type SetAnimationConfigUpdaterAction = {
config: AnimationConfig | FilterAnimationConfig;
};
/**
* Set animation config: works with both layer animation and filter animation
* @param config
* @returns action
*/
export function setAnimationConfig(
config: AnimationConfig | FilterAnimationConfig
): Merge<SetAnimationConfigUpdaterAction, {type: typeof ActionTypes.SET_ANIMATION_CONFIG}> {
return {
type: ActionTypes.SET_ANIMATION_CONFIG,
config
};
}

export type SetLayerAnimationTimeUpdaterAction = {
value: number | null;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import styled from 'styled-components';
import classnames from 'classnames';

import {media} from '@kepler.gl/styles';
import {Timeline} from '@kepler.gl/types';
import {Timeline, Filter} from '@kepler.gl/types';

import TimelineSliderFactory from '../timeline-slider';
import PlaybackControlsFactory from './playback-controls';
Expand Down Expand Up @@ -68,6 +68,7 @@ const AnimationWidgetInner = styled.div`
const TIMELINE_PLAYBACK_STYLE = {flex: 1};

export type AnimationControlProps = {
filter?: Filter;
timeline?: Timeline;
isAnimatable?: boolean;
isAnimating?: boolean;
Expand Down Expand Up @@ -95,6 +96,7 @@ function AnimationControlFactory(
TimelineSlider: ReturnType<typeof TimelineSliderFactory>
) {
const AnimationControl: React.FC<AnimationControlProps> = ({
filter,
className,
style,
isAnimatable,
Expand Down Expand Up @@ -135,6 +137,7 @@ function AnimationControlFactory(
{showControls ? (
<PlaybackControls
className="animation-control-playpause"
filter={filter}
isAnimatable={isAnimatable}
startAnimation={toggleAnimation}
isAnimating={isAnimating}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import styled from 'styled-components';
import classnames from 'classnames';
import {Reset, Play, Pause, Save, Rocket, AnchorWindow, FreeWindow} from '../icons';
import {ANIMATION_WINDOW} from '@kepler.gl/constants';
import {Filter} from '@kepler.gl/types';
import AnimationSpeedSliderFactory from './animation-speed-slider';
import WindowActionControlFactory from './window-action-control';
import AnimationWindowControlFactory, {AnimationItem} from './animation-window-control';
Expand Down Expand Up @@ -58,6 +59,7 @@ const DEFAULT_ANIMATE_ITEMS = {
}
};
export interface PlaybackControlsProps {
filter?: Filter;
isAnimatable?: boolean;
isAnimating?: boolean;
width?: number;
Expand Down Expand Up @@ -102,6 +104,7 @@ function PlaybackControlsFactory(

// eslint-disable-next-line complexity
const PlaybackControls: React.FC<PlaybackControlsProps> = ({
filter,
isAnimatable,
isAnimating = true,
width,
Expand Down Expand Up @@ -154,6 +157,7 @@ function PlaybackControlsFactory(
animationItems={animationItems}
animationWindow={animationWindow}
buttonHeight={buttonHeight}
filter={filter}
setFilterAnimationWindow={setFilterAnimationWindow}
updateAnimationSpeed={updateAnimationSpeed}
isAnimating={isAnimating}
Expand Down
36 changes: 34 additions & 2 deletions src/reducers/src/vis-state-updaters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ import {
setFilter,
processFileContent,
fitBounds as fitMapBounds,
toggleLayerForMap
toggleLayerForMap,
applyFilterConfig
} from '@kepler.gl/actions';

// Utils
Expand Down Expand Up @@ -69,7 +70,8 @@ import {
set,
updateFilterPlot,
removeFilterPlot,
isLayerAnimatable
isLayerAnimatable,
isSideFilter
} from '@kepler.gl/utils';
import {generateHashId, toArray} from '@kepler.gl/common-utils';
// Mergers
Expand Down Expand Up @@ -117,6 +119,7 @@ import {
Filter,
InteractionConfig,
AnimationConfig,
FilterAnimationConfig,
Editor,
Field,
TimeRangeFilter
Expand Down Expand Up @@ -1529,6 +1532,35 @@ export const toggleFilterAnimationUpdater = (
filters: state.filters.map((f, i) => (i === action.idx ? {...f, isAnimating: !f.isAnimating} : f))
});

export function isFilterAnimationConfig(config: AnimationConfig | FilterAnimationConfig): boolean {
return 'dataId' in config && 'animationWindow' in config;
}

export function setAnimationConfigUpdater(
state: VisState,
action: VisStateActions.SetAnimationConfigUpdaterAction
): VisState {
const {config} = action;
if (isFilterAnimationConfig(config)) {
// Find filter used for animation
// Assuming there's only one filter used for animation, see setFilterViewUpdater
const filter = state.filters.find(f => !isSideFilter(f));
if (!filter) {
return state;
}
const newFilter = {...filter, ...config};
return applyFilterConfigUpdater(state, applyFilterConfig(filter.id, newFilter));
} else {
return {
...state,
animationConfig: {
...state.animationConfig,
...config
}
};
}
}

/**
* @memberof visStateUpdaters
* @public
Expand Down
2 changes: 2 additions & 0 deletions src/reducers/src/vis-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ const actionHandler = {

[ActionTypes.UPDATE_FILTER_ANIMATION_SPEED]: visStateUpdaters.updateFilterAnimationSpeedUpdater,

[ActionTypes.SET_ANIMATION_CONFIG]: visStateUpdaters.setAnimationConfigUpdater,

[ActionTypes.SET_LAYER_ANIMATION_TIME]: visStateUpdaters.setLayerAnimationTimeUpdater,

[ActionTypes.UPDATE_LAYER_ANIMATION_SPEED]: visStateUpdaters.updateLayerAnimationSpeedUpdater,
Expand Down
11 changes: 11 additions & 0 deletions src/types/reducers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,17 @@ export type AnimationConfig = {
hideControl?: boolean;
};

export type FilterAnimationConfig = Pick<
TimeRangeFilter,
| 'dataId'
| 'value'
| 'animationWindow'
| 'speed'
| 'syncedWithLayerTimeline'
| 'syncTimelineMode'
| 'timezone'
>;

export type Timeline = {
domain: [number, number] | null;
value: number | [number, number];
Expand Down

0 comments on commit 0ad5372

Please sign in to comment.