All files / src/components/editors/prefab ActorPrefabEditorProperties.tsx

0% Statements 0/26
0% Branches 0/1
0% Functions 0/6
0% Lines 0/26

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                                                                                                                                                                                                                                                                   
import React, { FC, useCallback } from "react";
import { FormContainer, FormField, FormRow } from "ui/form/layout/FormLayout";
import entitiesActions from "store/features/entities/entitiesActions";
import {
  ActorPrefabNormalized,
  CollisionGroup,
} from "shared/lib/entities/entitiesTypes";
import { SidebarColumn } from "ui/sidebars/Sidebar";
import { SpriteSheetSelectButton } from "components/forms/SpriteSheetSelectButton";
import { AnimationSpeedSelect } from "components/forms/AnimationSpeedSelect";
import { MovementSpeedSelect } from "components/forms/MovementSpeedSelect";
import CollisionMaskPicker from "components/forms/CollisionMaskPicker";
import l10n from "shared/lib/lang/l10n";
import { useAppDispatch } from "store/hooks";
 
interface ActorPrefabEditorPropertiesProps {
  prefab: ActorPrefabNormalized;
}
 
export const ActorPrefabEditorProperties: FC<ActorPrefabEditorPropertiesProps> =
  ({ prefab }) => {
    const dispatch = useAppDispatch();
 
    const onChangeActorPrefabProp = useCallback(
      <K extends keyof ActorPrefabNormalized>(
        key: K,
        value: ActorPrefabNormalized[K]
      ) => {
        dispatch(
          entitiesActions.editActorPrefab({
            actorPrefabId: prefab.id,
            changes: {
              [key]: value,
            },
          })
        );
      },
      [dispatch, prefab.id]
    );
 
    const onChangeSpriteSheetId = useCallback(
      (e: string) => onChangeActorPrefabProp("spriteSheetId", e),
      [onChangeActorPrefabProp]
    );
 
    const onChangeMoveSpeed = useCallback(
      (e: number) => onChangeActorPrefabProp("moveSpeed", e),
      [onChangeActorPrefabProp]
    );
 
    const onChangeAnimSpeed = useCallback(
      (e: number) => onChangeActorPrefabProp("animSpeed", e),
      [onChangeActorPrefabProp]
    );
 
    const onChangeCollisionGroup = useCallback(
      (e: CollisionGroup) => onChangeActorPrefabProp("collisionGroup", e),
      [onChangeActorPrefabProp]
    );
 
    Iif (!prefab) {
      return <div />;
    }
 
    return (
      <>
        <SidebarColumn>
          <FormContainer>
            <FormRow>
              <FormField name="actorSprite" label={l10n("FIELD_SPRITE_SHEET")}>
                <SpriteSheetSelectButton
                  name="actorSprite"
                  value={prefab.spriteSheetId}
                  direction={"down"}
                  frame={0}
                  onChange={onChangeSpriteSheetId}
                  includeInfo
                />
              </FormField>
            </FormRow>
          </FormContainer>
        </SidebarColumn>
        <SidebarColumn>
          <FormContainer>
            <FormRow>
              <FormField
                name="actorMoveSpeed"
                label={l10n("FIELD_MOVEMENT_SPEED")}
              >
                <MovementSpeedSelect
                  name="actorMoveSpeed"
                  value={prefab.moveSpeed}
                  onChange={onChangeMoveSpeed}
                />
              </FormField>
              <FormField
                name="actorAnimSpeed"
                label={l10n("FIELD_ANIMATION_SPEED")}
              >
                <AnimationSpeedSelect
                  name="actorAnimSpeed"
                  value={prefab.animSpeed}
                  onChange={onChangeAnimSpeed}
                />
              </FormField>
            </FormRow>
          </FormContainer>
        </SidebarColumn>
 
        <SidebarColumn>
          <FormContainer>
            <FormRow>
              <FormField
                name="actorCollisionGroup"
                label={l10n("FIELD_COLLISION_GROUP")}
              >
                <CollisionMaskPicker
                  id="actorCollisionGroup"
                  value={prefab.collisionGroup}
                  onChange={onChangeCollisionGroup}
                  includeNone
                />
              </FormField>
            </FormRow>
          </FormContainer>
        </SidebarColumn>
      </>
    );
  };