All files / src/components/music RollChannelEffectRow.tsx

0% Statements 0/42
0% Branches 0/40
0% Functions 0/11
0% Lines 0/39

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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191                                                                                                                                                                                                                                                                                                                                                                                             
import React, { useCallback } from "react";
import styled, { css } from "styled-components";
import { PatternCell } from "shared/lib/uge/song/PatternCell";
import trackerActions from "store/features/tracker/trackerActions";
import trackerDocumentActions from "store/features/trackerDocument/trackerDocumentActions";
import { useAppDispatch, useAppSelector } from "store/hooks";
 
interface RollChannelProps {
  patternId: number;
  channelId: number;
  renderPattern: PatternCell[][];
  cellSize: number;
}
 
interface WrapperProps {
  $rows: number;
  $cols: number;
  $size: number;
  $active?: boolean;
}
 
const Wrapper = styled.div<WrapperProps>`
  font-family: monospace;
  position: absolute;
  left: ${30 + 10 + 1}px;
 
  ${(props) => css`
    top: ${props.$size / 2}px;
    width: ${props.$cols * props.$size}px;
    height: ${props.$size - 1}px;
 
    border-top: 1px solid #808080;
    border-bottom: 1px solid #808080;
 
    background-image: linear-gradient(
      90deg,
      ${props.theme.colors.tracker.rollCell.border} 1px,
      transparent 1px
    ),
    linear-gradient(
      ${props.theme.colors.tracker.rollCell.border} 1px,
      transparent 1px
    ),
    linear-gradient(
      90deg,
      ${props.theme.colors.tracker.rollCell.border} 2px,
      transparent 1px
    );
  background-size: ${props.$size}px ${props.$size}px,
    ${props.$size}px ${props.$size}px, ${props.$size * 8}px ${
    props.$size * 12
  }px;
  }
 
  `}
`;
 
interface NoteProps {
  $size: number;
  $isSelected?: boolean;
}
 
const Note = styled.div<NoteProps>`
  position: absolute;
  height: ${(props) => `${props.$size - 1}px`};
  border: 1px solid black;
  text-align: center;
  line-height: 1.1em;
  pointer-events: none;
  box-shadow: ${(props) =>
    props.$isSelected ? `0 0 0px 2px ${props.theme.colors.highlight}` : ""};
  z-index: ${(props) => (props.$isSelected ? 1 : 0)};
  background-color: ${(props) => props.theme.colors.button.activeBackground};
`;
 
export const RollChannelEffectRowFwd = ({
  patternId,
  channelId,
  renderPattern,
  cellSize,
}: RollChannelProps) => {
  const dispatch = useAppDispatch();
  const tool = useAppSelector((state) => state.tracker.tool);
 
  const selectedEffectCell = useAppSelector(
    (state) => state.tracker.selectedEffectCell
  );
 
  // Mouse
  const handleMouseDown = useCallback(
    (e: MouseEvent) => {
      Iif (!renderPattern) return;
      const col = Math.floor(e.offsetX / cellSize);
      const cell = renderPattern[col][channelId];
      const lastCell = renderPattern[selectedEffectCell || 0][channelId];
 
      if (e.button === 0 && tool !== "eraser") {
        // If there's a note in position
        Iif (cell) {
          const changes = {
            effectcode:
              cell.effectcode !== null
                ? cell.effectcode
                : lastCell.effectcode || 0,
            effectparam:
              cell.effectparam !== null
                ? cell.effectparam
                : lastCell.effectparam || 0,
          };
          dispatch(
            trackerDocumentActions.editPatternCell({
              patternId: patternId,
              cell: [col, channelId],
              changes: changes,
            })
          );
          dispatch(trackerActions.setSelectedEffectCell(col));
          // Dispatch twice preserve effect param data when not arpeggio
          dispatch(
            trackerDocumentActions.editPatternCell({
              patternId: patternId,
              cell: [col, channelId],
              changes: changes,
            })
          );
        }
      } else Iif (e.button === 2 || (tool === "eraser" && e.button === 0)) {
        // If there's a note in position
        Iif (cell && (cell.effectcode !== null || cell.effectparam !== null)) {
          dispatch(
            trackerDocumentActions.editPatternCell({
              patternId: patternId,
              cell: [col, channelId],
              changes: {
                effectcode: null,
                effectparam: null,
              },
            })
          );
        }
      }
    },
    [
      renderPattern,
      cellSize,
      channelId,
      tool,
      dispatch,
      patternId,
      selectedEffectCell,
    ]
  );
 
  return (
    <Wrapper
      $rows={12 * 6}
      $cols={64}
      $size={cellSize}
      onMouseDown={(e) => {
        handleMouseDown(e.nativeEvent);
      }}
    >
      {renderPattern?.map((column: PatternCell[], columnIdx: number) => {
        const cell = column[channelId];
        const isSelected = selectedEffectCell === columnIdx;
        Iif (cell && (cell.effectcode !== null || cell.effectparam !== null)) {
          return (
            <Note
              data-type="note"
              data-column={columnIdx}
              key={`fx_${columnIdx}_${channelId}`}
              $size={cellSize}
              $isSelected={isSelected}
              style={{
                left: `${columnIdx * cellSize}px`,
                width: cellSize,
                bottom: `-1px`,
              }}
            >
              {cell.effectcode?.toString(16).toUpperCase()}
            </Note>
          );
        }
        return "";
      })}
    </Wrapper>
  );
};
 
export const RollChannelEffectRow = React.memo(RollChannelEffectRowFwd);