Skip to content

Commit

Permalink
feat(settings): add type effect sounds
Browse files Browse the repository at this point in the history
  • Loading branch information
Muhammed-Rahif committed Jan 2, 2025
1 parent bcd1747 commit 17f0d21
Show file tree
Hide file tree
Showing 19 changed files with 65 additions and 44 deletions.
Binary file added src/assets/sounds/beep.wav
Binary file not shown.
Binary file modified src/assets/sounds/click.wav
Binary file not shown.
Binary file added src/assets/sounds/damage.wav
Binary file not shown.
Binary file added src/assets/sounds/fist-fight.wav
Binary file not shown.
Binary file added src/assets/sounds/hitmarker.wav
Binary file not shown.
Binary file added src/assets/sounds/missed-punch.wav
Binary file not shown.
Binary file added src/assets/sounds/nk-creams.wav
Binary file not shown.
Binary file added src/assets/sounds/osu.wav
Binary file not shown.
Binary file added src/assets/sounds/pop.wav
Binary file not shown.
Binary file added src/assets/sounds/rubber-keys.wav
Binary file not shown.
Binary file added src/assets/sounds/square.wav
Binary file not shown.
Binary file added src/assets/sounds/triangle.wav
Binary file not shown.
Binary file added src/assets/sounds/typewriter.wav
Binary file not shown.
8 changes: 4 additions & 4 deletions src/lib/components/editor/Editor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
const updateCaretPosAndEditorData = async () => {
updateEditorData();
await tick();
fakeCaret.updateCaretPosition();
fakeCaret?.updateCaretPosition();
};
onMount(async () => {
Expand All @@ -110,15 +110,15 @@
quill.on('editor-change', updateCaretPosAndEditorData);
// Marks editor as not saved when text content changes.
quill.on('text-change', (delta) => {
let pressedKey: undefined | string;
let insertedText: undefined | string;
delta.ops.forEach((op) => {
if (op.insert && typeof op.insert === 'string' && op.insert.length == 1) {
pressedKey = op.insert;
insertedText = op.insert;
}
});
Notpad.typeEffectPlayer.play(pressedKey);
Notpad.typeEffectPlayer.play(insertedText);
Notpad.editors.setIsSaved(editor.id, false);
});
});
Expand Down
15 changes: 7 additions & 8 deletions src/lib/components/editor/FakeCaret.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import { settings } from '@/store/store';
import type Quill from 'quill';
import { cn } from '@/utils';
import { CaretAnimation, CaretStyle } from '@/types/SettingsType';
import { CaretAnimation } from '@/types/SettingsType';
interface Props {
quill: Quill;
Expand All @@ -15,6 +15,7 @@
let { quill, editorContainer }: Props = $props();
let caret = $state({ top: 10, left: 8, height: 24 });
let fakeCaretElement: HTMLSpanElement = $state(null!);
const caretStyle = $derived($settings.caret.style);
const resumeFakeCaretBlink = debounce(function () {
if (fakeCaretElement) fakeCaretElement.classList.add('animate-caret-blink');
Expand Down Expand Up @@ -54,15 +55,13 @@
'fake-caret absolute z-0 w-0.5 animate-caret-blink rounded-[.06em]',
'[transition:left_var(--caret-animation-duration),top_var(--caret-animation-duration)]',
{
'bg-primary': $settings.caret.style != CaretStyle.HollowBlock,
'border border-primary': $settings.caret.style == CaretStyle.HollowBlock
'bg-primary': caretStyle != 'HollowBlock',
'border border-primary': caretStyle == 'HollowBlock'
}
)}
style:width="calc({$settings.caret.style == CaretStyle.VerticalBar ? '2px' : '1ch'} * var(--editor-zoom))"
style:height={$settings.caret.style == CaretStyle.Underline ? '2px' : `${caret.height}px`}
style:margin-top={$settings.caret.style == CaretStyle.Underline
? `${caret.height + 2}px`
: undefined}
style:width="calc({caretStyle == 'VerticalBar' ? '2px' : '1ch'} * var(--editor-zoom))"
style:height={caretStyle == 'Underline' ? '2px' : `${caret.height}px`}
style:margin-top={caretStyle == 'Underline' ? `${caret.height + 2}px` : undefined}
style:top="{caret.top}px"
style:left="{caret.left}px"
style:--caret-animation-duration={CaretAnimation[$settings.caret.animation]}
Expand Down
43 changes: 22 additions & 21 deletions src/lib/components/menubar/SettingsMenu.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<Menubar.Sub>
<Menubar.SubTrigger class="gap-2">Sound</Menubar.SubTrigger>
<Menubar.SubContent>
{#each Object.values(TypeEffectSound) as key}
{#each Object.keys(TypeEffectSound) as key}
<Menubar.CheckboxItem
checked={$settings.typeEffect.sound == key}
onclick={() => {
Expand All @@ -66,7 +66,6 @@
<Menubar.SubTrigger class="gap-2">Volume</Menubar.SubTrigger>
<Menubar.SubContent>
{#each Object.keys(TypeEffectVolume) as key}
{console.log($settings.typeEffect.volume, key)}
<Menubar.CheckboxItem
checked={$settings.typeEffect.volume == key}
onclick={() => {
Expand All @@ -81,23 +80,25 @@
</Menubar.SubContent>
</Menubar.Sub>

<Menubar.Sub>
<Menubar.SubTrigger class="gap-2">Vibration</Menubar.SubTrigger>
<Menubar.SubContent>
{#each Object.keys(TypeEffectVibration) as key}
<Menubar.CheckboxItem
checked={$settings.typeEffect.vibration == key}
onclick={() => {
Notpad.settings.updateTypeEffect({
vibration: key as keyof typeof TypeEffectVibration
});
}}
>
{key}
</Menubar.CheckboxItem>
{/each}
</Menubar.SubContent>
</Menubar.Sub>
{#if 'vibrate' in navigator}
<Menubar.Sub>
<Menubar.SubTrigger class="gap-2">Vibration</Menubar.SubTrigger>
<Menubar.SubContent>
{#each Object.keys(TypeEffectVibration) as key}
<Menubar.CheckboxItem
checked={$settings.typeEffect.vibration == key}
onclick={() => {
Notpad.settings.updateTypeEffect({
vibration: key as keyof typeof TypeEffectVibration
});
}}
>
{key}
</Menubar.CheckboxItem>
{/each}
</Menubar.SubContent>
</Menubar.Sub>
{/if}
</Menubar.SubContent>
</Menubar.Sub>

Expand All @@ -108,7 +109,7 @@
<Menubar.Sub>
<Menubar.SubTrigger class="gap-2">Style</Menubar.SubTrigger>
<Menubar.SubContent>
{#each Object.values(CaretStyle) as key}
{#each Object.keys(CaretStyle) as key}
<Menubar.CheckboxItem
checked={$settings.caret.style == key}
onclick={() => {
Expand All @@ -124,7 +125,7 @@
<Menubar.Sub>
<Menubar.SubTrigger class="gap-2">Animation</Menubar.SubTrigger>
<Menubar.SubContent>
{#each Object.values(CaretAnimation) as key}
{#each Object.keys(CaretAnimation) as key}
<Menubar.CheckboxItem
checked={$settings.caret.animation == key}
onclick={() => {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/helpers/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ export class Settings {
enable: true
},
typeEffect: {
sound: 'Click',
sound: 'Nk Creams',
vibration: 'Medium',
volume: 'ThreeQuarter'
volume: 'Full'
}
};

Expand Down
14 changes: 6 additions & 8 deletions src/lib/helpers/type-effect-player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,22 @@ export class TypeEffectPlayer {
key = key.toLowerCase(); // Normalize key to lowercase for comparison

if (farLeftKeys.includes(key)) {
return -1; // Far-left
return -0.5; // Far-left
} else if (leftKeys.includes(key)) {
return -0.5; // Left
return -0.3; // Left
} else if (centerKeys.includes(key)) {
return 0; // Center
} else if (rightKeys.includes(key)) {
return 0.5; // Right
return 0.3; // Right
} else if (farRightKeys.includes(key)) {
return 1; // Far-right
return 0.5; // Far-right
} else {
return 0; // Default to center if key is not found in any section
}
}

private triggerVibration(strength: number): void {
if (navigator.vibrate) {
if ('vibrate' in navigator) {
const duration = Math.max(1, Math.min(100, Math.round(strength * 100)));
navigator.vibrate(duration);
} else {
Expand Down Expand Up @@ -103,9 +103,7 @@ export class TypeEffectPlayer {
source.start(0);

const vibration = TypeEffectVibration[this.settings.vibration];
if (vibration > 0) {
this.triggerVibration(vibration);
}
if (vibration > 0) this.triggerVibration(vibration);
}

public resumeAudioContext(): void {
Expand Down
25 changes: 24 additions & 1 deletion src/lib/types/SettingsType.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
import clickSound from '@/src/assets/sounds/click.wav';
import beepSound from '@/src/assets/sounds/beep.wav';
import damageSound from '@/src/assets/sounds/damage.wav';
import fistFightSound from '@/src/assets/sounds/fist-fight.wav';
import hitmarkerSound from '@/src/assets/sounds/hitmarker.wav';
import missedPunchSound from '@/src/assets/sounds/missed-punch.wav';
import nkCreamsSound from '@/src/assets/sounds/nk-creams.wav';
import osuSound from '@/src/assets/sounds/osu.wav';
import popSound from '@/src/assets/sounds/pop.wav';
import rubberKeysSound from '@/src/assets/sounds/rubber-keys.wav';
import squareSound from '@/src/assets/sounds/square.wav';
import triangleSound from '@/src/assets/sounds/triangle.wav';
import typewriterSound from '@/src/assets/sounds/typewriter.wav';

/**
* Available font families for the editor.
Expand Down Expand Up @@ -50,7 +62,18 @@ export const CaretStyle = {

export const TypeEffectSound = {
Click: clickSound,
Pop: clickSound,
Beep: beepSound,
Damage: damageSound,
'Fist Fight': fistFightSound,
Hitmarker: hitmarkerSound,
'Missed Punch': missedPunchSound,
'Nk Creams': nkCreamsSound,
Osu: osuSound,
Pop: popSound,
'Rubber Keys': rubberKeysSound,
Square: squareSound,
Triangle: triangleSound,
Typewriter: typewriterSound,
None: null
};

Expand Down

0 comments on commit 17f0d21

Please sign in to comment.