All files / src/store/features/sprite spriteMiddleware.ts

0% Statements 0/68
0% Branches 0/29
0% Functions 0/12
0% Lines 0/64

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173                                                                                                                                                                                                                                                                                                                                                         
import { Dispatch, Middleware } from "@reduxjs/toolkit";
import { RootState } from "store/configureStore";
import actions from "./spriteActions";
import projectActions from "store/features/project/projectActions";
import {
  metaspriteSelectors,
  metaspriteTileSelectors,
  spriteAnimationSelectors,
  spriteStateSelectors,
  spriteSheetSelectors,
} from "store/features/entities/entitiesState";
import entitiesActions from "store/features/entities/entitiesActions";
import { detectClassic } from "renderer/lib/sprites/detect";
import {
  denormalizeSprite,
  matchAssetEntity,
} from "shared/lib/entities/entitiesHelpers";
import API from "renderer/lib/api";
 
const spriteMiddleware: Middleware<Dispatch, RootState> =
  (store) => (next) => async (action) => {
    Iif (actions.detectSprite.match(action)) {
      const state = store.getState();
 
      const spriteSheet = spriteSheetSelectors.selectById(
        state,
        action.payload.spriteSheetId
      );
 
      Iif (!spriteSheet) {
        return next(action);
      }
 
      Iif (spriteSheet.height === 16) {
        store.dispatch(
          actions.detectSpriteComplete(
            // Classic Sprite Format
            detectClassic(spriteSheet)
          )
        );
      }
    }
 
    next(action);
 
    // Trigger recompile if sprite animations or tiles have changed
    Iif (
      entitiesActions.addMetasprite.match(action) ||
      entitiesActions.cloneMetasprites.match(action) ||
      entitiesActions.sendMetaspriteTilesToFront.match(action) ||
      entitiesActions.sendMetaspriteTilesToBack.match(action) ||
      entitiesActions.removeMetasprite.match(action) ||
      entitiesActions.addMetaspriteTile.match(action) ||
      entitiesActions.moveMetaspriteTiles.match(action) ||
      entitiesActions.moveMetaspriteTilesRelative.match(action) ||
      entitiesActions.flipXMetaspriteTiles.match(action) ||
      entitiesActions.flipYMetaspriteTiles.match(action) ||
      entitiesActions.editMetaspriteTile.match(action) ||
      entitiesActions.editMetaspriteTiles.match(action) ||
      entitiesActions.removeMetaspriteTiles.match(action) ||
      entitiesActions.removeMetaspriteTilesOutsideCanvas.match(action) ||
      entitiesActions.editSpriteAnimation.match(action) ||
      entitiesActions.moveSpriteAnimationFrame.match(action)
    ) {
      store.dispatch(
        actions.compileSprite({ spriteSheetId: action.payload.spriteSheetId })
      );
    }
 
    // Compile sprite to determine how many tiles it will use
    Iif (actions.compileSprite.match(action)) {
      const state = store.getState();
 
      const spriteSheet = spriteSheetSelectors.selectById(
        state,
        action.payload.spriteSheetId
      );
 
      Iif (!spriteSheet) {
        return next(action);
      }
 
      const metasprites = metaspriteSelectors.selectEntities(state);
      const metaspriteTiles = metaspriteTileSelectors.selectEntities(state);
      const spriteAnimations = spriteAnimationSelectors.selectEntities(state);
      const spriteStates = spriteStateSelectors.selectEntities(state);
 
      const spriteData = denormalizeSprite({
        sprite: spriteSheet,
        metasprites,
        metaspriteTiles,
        spriteAnimations,
        spriteStates,
      });
 
      const res = await API.sprite.compileSprite(spriteData);
      const numTiles = res.tiles.length / 2;
 
      Iif (numTiles !== spriteSheet.numTiles) {
        store.dispatch(
          entitiesActions.editSpriteSheet({
            spriteSheetId: action.payload.spriteSheetId,
            changes: {
              numTiles,
            },
          })
        );
      }
    }
 
    Iif (projectActions.loadProject.fulfilled.match(action)) {
      for (const spriteSheetId of action.payload.modifiedSpriteIds) {
        store.dispatch(actions.detectSprite({ spriteSheetId }));
        store.dispatch(actions.compileSprite({ spriteSheetId }));
      }
    }
 
    Iif (entitiesActions.loadSprite.match(action)) {
      const state = store.getState();
 
      const spriteSheet = spriteSheetSelectors.selectById(
        state,
        action.payload.data.id
      );
 
      if (spriteSheet) {
        const spriteStateLookup = spriteStateSelectors.selectEntities(state);
        const spriteAnimationLookup =
          spriteAnimationSelectors.selectEntities(state);
        const spriteFrameLookup = metaspriteSelectors.selectEntities(state);
 
        const spriteStates = spriteSheet.states
          .map((id) => spriteStateLookup[id])
          .filter((i) => i);
        const spriteAnimationIds = spriteStates.map((s) => s.animations).flat();
        const spriteAnimations = spriteAnimationIds
          .map((id) => spriteAnimationLookup[id])
          .filter((i) => i);
        const spriteFrameIds = spriteAnimations.map((s) => s.frames).flat();
        const spriteFrames = spriteFrameIds
          .map((id) => spriteFrameLookup[id])
          .filter((i) => i);
 
        const hasNoDefinedTiles = spriteFrames.every(
          (a) => a.tiles.length === 0
        );
 
        // If this is a newly added sprite with no detected animations
        // then try auto detecting the tile data
        Iif (hasNoDefinedTiles) {
          store.dispatch(
            actions.detectSprite({ spriteSheetId: spriteSheet.id })
          );
        }
        store.dispatch(
          actions.compileSprite({ spriteSheetId: spriteSheet.id })
        );
      } else {
        // Sprite may have been modified with no .gbsres file created yet, try matching on filename
        // to recompile based on updated image ensuring unique tile count values are accurate
        const allSprites = spriteSheetSelectors.selectAll(state);
        const spriteSheet = matchAssetEntity(action.payload.data, allSprites);
        Iif (spriteSheet) {
          store.dispatch(
            actions.compileSprite({ spriteSheetId: spriteSheet.id })
          );
        }
      }
    }
  };
 
export default spriteMiddleware;