/* * MIT License * * Copyright (c) 2024 Robin E. R. Davies * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies * of the Software, and to permit persons to whom the Software is furnished to do * so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ import React from 'react'; import Typography from '@mui/material/Typography'; import IconButton from '@mui/material/IconButton'; import ButtonBase from '@mui/material/ButtonBase'; import ResizeResponsiveComponent from './ResizeResponsiveComponent'; import SaveIconOutline from '@mui/icons-material/Save'; import Button from "@mui/material/Button"; import EditIconOutline from '@mui/icons-material/Edit'; import { Snapshot } from './Pedalboard'; import { isDarkMode } from './DarkMode'; import { colorKeys, getBackgroundColor, getBorderColor } from './MaterialColors'; import { PiPedalModel, PiPedalModelFactory, SnapshotModifiedEvent } from './PiPedalModel'; export interface SnapshotButtonProps { snapshot: Snapshot | null; snapshotIndex: number; selected: boolean; largeText: boolean; collapseButtons: boolean; onEditSnapshot: (index: number) => void; onSaveSnapshot: (index: number) => void; onSelectSnapshot: (index: number) => void; }; export interface SnapshotButtonState { modified: boolean; uiScale: number; }; export default class SnapshotButton extends ResizeResponsiveComponent { private model: PiPedalModel; constructor(props: SnapshotButtonProps) { super(props); this.state = { modified: this.props.snapshot?.isModified ?? false, uiScale: 1.0 }; this.model = PiPedalModelFactory.getInstance(); this.onSnapshotModified = this.onSnapshotModified.bind(this); } is2x3Layout(width: number, height: number) { return width * 2 < height * 3; } onWindowSizeChanged(width: number, height: number): void { let uiScale: number; if (this.is2x3Layout(width,height)) { uiScale = width / 400; } else { uiScale = (width/3)/(400/2); } if (height < width) { // 1 at 400, 2 at 600 let hUiScale = (height-340)/(400-340); hUiScale += (height-400)/(460-400); uiScale = Math.min(hUiScale,uiScale); } if (uiScale < 1) uiScale = 1; if (uiScale > 4) uiScale = 4; this.setState({uiScale: uiScale}); } onSnapshotModified(event: SnapshotModifiedEvent) { if (event.snapshotIndex === this.props.snapshotIndex) { this.setState({ modified: event.modified }); } } componentDidMount(): void { super.componentDidMount?.(); this.model.onSnapshotModified.addEventHandler(this.onSnapshotModified); } componentWillUnmount(): void { this.model.onSnapshotModified.removeEventHandler(this.onSnapshotModified); super.componentWillUnmount(); } componentDidUpdate( prevProps: Readonly, prevState: Readonly): void { super.componentDidUpdate?.(prevProps, prevState); if (this.props.snapshot !== prevProps.snapshot) { this.setState({ modified: this.props.snapshot?.isModified ?? false }) } } render() { let { snapshot, snapshotIndex, selected } = this.props; let { modified, uiScale } = this.state; // (snapshot: Snapshot | null, index: number) { //let state = this.state; let color: string; let bordercolor: string; if (snapshot) { if (colorKeys.indexOf(snapshot.color) === -1) { bordercolor = color = snapshot.color; } else { color = getBackgroundColor(snapshot.color); bordercolor = getBorderColor(snapshot.color); } } else { bordercolor = color = getBackgroundColor("grey"); } let title = snapshot ? snapshot.name : ""; if (modified) { title = title + "*"; } let disabled = snapshot === null; let fontSize = (16*uiScale) + "px"; return (
{ if (!selected) ev.stopPropagation(); }} onClick={() => { if (snapshot) this.props.onSelectSnapshot(snapshotIndex); }} > {/* select background mask*/}
{ if (disabled) ev.stopPropagation(); }} >
{title}
{(snapshotIndex + 1).toString()}
{/* placeholder where we will float the buttons, which can't be witin another button (tons of DOM validation warning messages in chrome) */}
{!this.props.collapseButtons ? (
) : (
{ ev.stopPropagation(); /*don't prop to card*/ }} onClick={() => { this.props.onSaveSnapshot(snapshotIndex); }} > { ev.stopPropagation(); /*don't prop to card*/ }} onClick={() => { this.props.onEditSnapshot(snapshotIndex); }} >
)}
); } }