apple2js/js/components/Mouse.tsx

38 lines
1008 B
TypeScript
Raw Normal View History

2022-05-12 00:21:21 +00:00
import { RefObject } from 'preact';
2022-05-12 14:59:12 +00:00
import Apple2IO, { slot } from '../apple2io';
2022-05-12 00:21:21 +00:00
import { MouseUI } from '../ui/mouse';
import MouseCard from '../cards/mouse';
import CPU6502 from '../cpu6502';
import { useEffect } from 'preact/hooks';
2022-05-12 14:59:12 +00:00
/**
* Mouse component properties.
*/
2022-05-12 00:21:21 +00:00
export interface MouseProps {
cpu: CPU6502 | undefined;
io: Apple2IO | undefined;
screen: RefObject<HTMLCanvasElement>;
2022-05-12 14:59:12 +00:00
slot: slot;
2022-05-12 00:21:21 +00:00
}
2022-05-12 14:59:12 +00:00
/**
* Mouse card component that adds a simple mouse driver.
*
* @param cpu CPU6502 object
* @param screen Screen element reference
* @param io Apple2IO object
* @param slot Slot to register card in
* @returns Mouse component
*/
export const Mouse = ({ cpu, screen, io, slot }: MouseProps) => {
2022-05-12 00:21:21 +00:00
useEffect(() => {
if (cpu && io && screen.current) {
const mouseUI = new MouseUI(screen.current);
const mouse = new MouseCard(cpu, mouseUI);
2022-05-12 14:59:12 +00:00
io.setSlot(slot, mouse);
2022-05-12 00:21:21 +00:00
}
}, [cpu, io, screen, slot]);
2022-05-12 00:21:21 +00:00
return null;
};