-
-
Notifications
You must be signed in to change notification settings - Fork 653
Expand file tree
/
Copy pathBase.vue
More file actions
702 lines (634 loc) · 20.6 KB
/
Copy pathBase.vue
File metadata and controls
702 lines (634 loc) · 20.6 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
<script setup lang="ts">
import { useEventListener, useLocalStorage } from "@vueuse/core";
import type { Emitter } from "mitt";
import { storeToRefs } from "pinia";
import { computed, inject, onBeforeUnmount, onMounted, ref, watch } from "vue";
import { useI18n } from "vue-i18n";
import { useRoute } from "vue-router";
import { useDisplay } from "vuetify";
import type { FirmwareSchema, SaveSchema, StateSchema } from "@/__generated__";
import AssetCard from "@/components/common/Game/AssetCard.vue";
import GameCard from "@/components/common/Game/Card/Base.vue";
import { ROUTES } from "@/plugins/router";
import firmwareApi from "@/services/api/firmware";
import romApi from "@/services/api/rom";
import storeAuth from "@/stores/auth";
import storeConfig from "@/stores/config";
import storePlaying from "@/stores/playing";
import { type DetailedRom } from "@/stores/roms";
import type { Events } from "@/types/emitter";
import { getSupportedEJSCores } from "@/utils";
import CacheDialog from "@/views/Player/EmulatorJS/CacheDialog.vue";
import Player from "@/views/Player/EmulatorJS/Player.vue";
const { t } = useI18n();
const { xs, mdAndUp, smAndDown } = useDisplay();
const emitter = inject<Emitter<Events>>("emitter");
const route = useRoute();
const auth = storeAuth();
const playingStore = storePlaying();
const configStore = storeConfig();
const { playing, fullScreen } = storeToRefs(playingStore);
const rom = ref<DetailedRom | null>(null);
const firmwareOptions = ref<FirmwareSchema[]>([]);
const selectedSave = ref<SaveSchema | null>(null);
const isSavesTabSelected = ref(true);
const selectedState = ref<StateSchema | null>(null);
const selectedDisc = ref<number | null>(null);
const selectedCore = ref<string | null>(null);
const selectedFirmware = ref<FirmwareSchema | null>(null);
const supportedCores = ref<string[]>([]);
const gameRunning = ref(false);
const fullScreenOnPlay = useLocalStorage("emulation.fullScreenOnPlay", true);
declare global {
interface Navigator {
keyboard: {
lock: (keys: string[]) => Promise<void>;
unlock: () => void;
};
}
}
const compatibleStates = computed(
() =>
rom.value?.user_states.filter(
(s) => !s.emulator || s.emulator === selectedCore.value,
) ?? [],
);
async function onPlay() {
if (rom.value && auth.scopes.includes("roms.user.write")) {
romApi.updateUserRomProps({
romId: rom.value.id,
data: rom.value.rom_user,
updateLastPlayed: true,
});
}
gameRunning.value = true;
window.EJS_fullscreenOnLoaded = fullScreenOnPlay.value;
fullScreen.value = fullScreenOnPlay.value;
playing.value = true;
const { EJS_NETPLAY_ENABLED } = configStore.config;
const EMULATORJS_VERSION = EJS_NETPLAY_ENABLED ? "nightly" : "4.2.3";
const LOCAL_PATH = "/assets/emulatorjs/data";
const CDN_PATH = `https://cdn.emulatorjs.org/${EMULATORJS_VERSION}/data`;
function loadScript(src: string): Promise<void> {
return new Promise((resolve, reject) => {
const s = document.createElement("script");
s.src = src;
s.async = true;
s.onload = () => resolve();
s.onerror = () => reject(new Error("Failed loading " + src));
document.body.appendChild(s);
});
}
async function attemptLoad(path: string) {
window.EJS_pathtodata = path;
await loadScript(`${path}/loader.js`);
}
try {
try {
await attemptLoad(EJS_NETPLAY_ENABLED ? CDN_PATH : LOCAL_PATH);
} catch (e) {
console.warn("[Play] Local loader failed, trying CDN", e);
await attemptLoad(EJS_NETPLAY_ENABLED ? LOCAL_PATH : CDN_PATH);
}
playing.value = true;
fullScreen.value = fullScreenOnPlay.value;
} catch (err) {
console.error("[Play] Emulator load failure:", err);
}
}
function onFullScreenChange() {
fullScreenOnPlay.value = !fullScreenOnPlay.value;
}
function selectSave(save: SaveSchema) {
selectedSave.value = save;
// Deselect state when selecting a save (mutually exclusive)
if (selectedState.value) {
selectedState.value = null;
localStorage.removeItem(`player:${rom.value?.platform_slug}:state_id`);
}
localStorage.setItem(
`player:${rom.value?.platform_slug}:save_id`,
save.id.toString(),
);
// Switch to saves tab
isSavesTabSelected.value = true;
}
function unselectSave() {
selectedSave.value = null;
localStorage.removeItem(`player:${rom.value?.platform_slug}:save_id`);
}
function selectState(state: StateSchema) {
selectedState.value = state;
// Deselect save when selecting a state (mutually exclusive)
if (selectedSave.value) {
selectedSave.value = null;
localStorage.removeItem(`player:${rom.value?.platform_slug}:save_id`);
}
localStorage.setItem(
`player:${rom.value?.platform_slug}:state_id`,
state.id.toString(),
);
// Switch to states tab
isSavesTabSelected.value = false;
}
function unselectState() {
selectedState.value = null;
localStorage.removeItem(`player:${rom.value?.platform_slug}:state_id`);
}
watch(selectedCore, (newSelectedCore) => {
if (
selectedState.value &&
selectedState.value.emulator &&
selectedState.value.emulator !== newSelectedCore
) {
selectedState.value = null;
localStorage.removeItem(`player:${rom.value?.platform_slug}:state_id`);
}
});
onMounted(async () => {
const romResponse = await romApi.getRom({
romId: parseInt(route.params.rom as string),
});
rom.value = romResponse.data;
if (rom.value) {
document.title = `${rom.value.name} | Play`;
}
const firmwareResponse = await firmwareApi.getFirmware({
platformId: romResponse.data.platform_id,
});
firmwareOptions.value = firmwareResponse.data;
supportedCores.value = [...getSupportedEJSCores(rom.value.platform_slug)];
// Listen for save/state selection from dialogs
emitter?.on("saveSelected", selectSave);
emitter?.on("stateSelected", selectState);
if ("keyboard" in navigator) {
useEventListener(document, "fullscreenchange", () => {
if (document.fullscreenElement) {
navigator.keyboard
.lock(["Escape", "Tab", "AltLeft", "ControlLeft", "MetaLeft"])
.catch(() => {});
} else {
navigator.keyboard.unlock();
}
});
}
// Determine default tab and selection (mutually exclusive)
const compatibleStates = rom.value.user_states.filter(
(s) => !s.emulator || s.emulator === supportedCores.value[0],
);
if (compatibleStates.length > 0) {
// If there are states, default to states tab with first state
isSavesTabSelected.value = false;
selectedState.value = compatibleStates[0];
selectedSave.value = null;
} else if (rom.value.user_saves.length > 0) {
// If no states but there are saves, default to saves tab with first save
isSavesTabSelected.value = true;
selectedSave.value = rom.value.user_saves[0];
selectedState.value = null;
} else {
// No saves or states, default to saves tab
isSavesTabSelected.value = true;
selectedSave.value = null;
selectedState.value = null;
}
const storedDisc = localStorage.getItem(`player:${rom.value.id}:disc`);
const storedDiscId = storedDisc ? parseInt(storedDisc) : null;
if (storedDiscId && rom.value.files.some((f) => f.id === storedDiscId)) {
selectedDisc.value = storedDiscId;
} else {
if (storedDisc) localStorage.removeItem(`player:${rom.value.id}:disc`);
selectedDisc.value = rom.value.files[0]?.id ?? null;
}
const storedCore = localStorage.getItem(
`player:${rom.value.platform_slug}:core`,
);
if (storedCore) {
selectedCore.value = storedCore;
} else {
// Otherwise auto select first supported core
selectedCore.value = supportedCores.value[0];
}
const coreOptions = configStore.getEJSCoreOptions(selectedCore.value);
const storedBiosID = localStorage.getItem(
`player:${rom.value.platform_slug}:bios_id`,
);
const biosFromStorage = storedBiosID
? firmwareOptions.value.find((f) => f.id === parseInt(storedBiosID))
: undefined;
// Use default bios file if set in config.yml
const biosFromConfig = coreOptions["bios_file"]
? firmwareOptions.value.find(
(f) => f.file_name === coreOptions["bios_file"],
)
: undefined;
// Auto-select firmware if only one option is available
const biosFromSingleOption =
firmwareOptions.value.length === 1 ? firmwareOptions.value[0] : undefined;
selectedFirmware.value =
biosFromStorage ?? biosFromConfig ?? biosFromSingleOption ?? null;
});
onBeforeUnmount(async () => {
window.EJS_emulator?.callEvent("exit");
emitter?.off("saveSelected", selectSave);
emitter?.off("stateSelected", selectState);
});
function openStateDialog() {
if (rom.value) {
emitter?.emit("selectStateDialog", rom.value);
}
}
function openSaveDialog() {
if (rom.value) {
emitter?.emit("selectSaveDialog", rom.value);
}
}
function openCacheDialog() {
emitter?.emit("openEmulatorJSCacheDialog", null);
}
</script>
<template>
<v-row
v-if="rom"
class="align-center justify-center scroll h-100 px-4"
no-gutters
>
<v-col v-if="!gameRunning" cols="12" lg="8">
<v-row class="mt-4" no-gutters>
<!-- Game Info -->
<v-col class="game-info-col">
<v-container :width="220" class="pa-0 text-wrap text-center mb-6">
<GameCard
:key="rom.updated_at"
:rom="rom"
:show-platform-icon="false"
:show-action-bar="false"
/>
</v-container>
</v-col>
<!-- Saves & States -->
<v-col
class="flex-col"
:class="{ 'pr-2': !xs, 'mb-4': smAndDown, 'pl-4': mdAndUp }"
>
<v-card variant="flat" rounded="lg">
<v-tabs
v-model="isSavesTabSelected"
bg-color="transparent"
color="primary"
grow
>
<v-tab :value="true">
<v-icon start>mdi-content-save</v-icon>
{{ t("common.saves") }}
<v-badge
v-if="rom.user_saves.length > 0"
:content="rom.user_saves.length"
color="primary"
inline
class="ml-2"
/>
</v-tab>
<v-tab :value="false">
<v-icon start>mdi-file</v-icon>
{{ t("common.states") }}
<v-badge
v-if="compatibleStates.length > 0"
:content="compatibleStates.length"
color="primary"
inline
class="ml-2"
/>
</v-tab>
</v-tabs>
<v-divider />
<v-card-text class="pa-4" style="min-height: 200px">
<!-- Saves Tab Content -->
<div v-show="isSavesTabSelected">
<!-- Selected Save Preview -->
<div v-if="selectedSave" class="mb-3">
<AssetCard
:asset="selectedSave"
type="save"
:show-hover-actions="false"
:show-close-button="true"
:transform-scale="false"
@close="unselectSave"
/>
</div>
<!-- No Save Selected Message -->
<div v-else class="text-center py-8">
<v-icon size="48" color="medium-emphasis"
>mdi-content-save-outline</v-icon
>
<p class="text-body-2 text-medium-emphasis mt-2">
{{ t("play.no-save-selected") }}
</p>
</div>
<!-- Select Save Button -->
<v-btn
block
variant="tonal"
color="primary"
:prepend-icon="
selectedSave ? 'mdi-swap-horizontal' : 'mdi-plus'
"
:disabled="rom.user_saves.length == 0"
@click="openSaveDialog"
>
{{
rom.user_saves.length == 0
? t("play.no-saves-available")
: selectedSave
? t("play.change-save")
: t("play.select-save")
}}
</v-btn>
</div>
<!-- States Tab Content -->
<div v-show="!isSavesTabSelected">
<!-- Selected State Preview -->
<div v-if="selectedState" class="mb-3">
<AssetCard
:asset="selectedState"
type="state"
:show-hover-actions="false"
:show-close-button="true"
:transform-scale="false"
@close="unselectState"
/>
</div>
<!-- No State Selected Message -->
<div v-else class="text-center py-8">
<v-icon size="48" color="medium-emphasis"
>mdi-file-outline</v-icon
>
<p class="text-body-2 text-medium-emphasis mt-2">
{{ t("play.no-state-selected") }}
</p>
</div>
<!-- Select State Button -->
<v-btn
block
variant="tonal"
color="primary"
:prepend-icon="
selectedState ? 'mdi-swap-horizontal' : 'mdi-plus'
"
:disabled="
!rom.user_states.some(
(s) => !s.emulator || s.emulator === selectedCore,
)
"
@click="openStateDialog"
>
{{
!rom.user_states.some(
(s) => !s.emulator || s.emulator === selectedCore,
)
? t("play.no-states-available")
: selectedState
? t("play.change-state")
: t("play.select-state")
}}
</v-btn>
</div>
</v-card-text>
</v-card>
</v-col>
<!-- Settings & Actions -->
<v-col class="flex-col" :class="{ 'pl-2': !smAndDown }">
<v-card variant="flat" rounded="lg" class="mb-6">
<v-card-text class="pa-3">
<!-- Configuration Section -->
<!-- Disc Selector -->
<v-select
v-if="rom.files.length > 1"
v-model="selectedDisc"
class="mb-3"
hide-details
variant="outlined"
density="comfortable"
prepend-inner-icon="mdi-disc"
clearable
:label="t('rom.file')"
:items="
rom.files.map((f) => ({
title: f.file_name,
value: f.id,
}))
"
/>
<!-- Core Selector -->
<v-select
v-if="supportedCores.length > 1"
v-model="selectedCore"
class="mb-3"
hide-details
variant="outlined"
prepend-inner-icon="mdi-chip"
density="comfortable"
clearable
:label="t('common.core')"
:items="
supportedCores.map((c) => ({
title: c,
value: c,
}))
"
/>
<!-- BIOS Selector -->
<v-select
v-if="firmwareOptions.length > 0"
v-model="selectedFirmware"
hide-details
variant="outlined"
density="comfortable"
prepend-inner-icon="mdi-memory"
clearable
:label="t('common.firmware')"
:items="
firmwareOptions.map((f) => ({
title: f.file_name,
value: f,
})) ?? []
"
/>
<v-divider
v-if="
rom.files.length > 1 ||
supportedCores.length > 1 ||
firmwareOptions.length > 0
"
class="my-4"
/>
<!-- Action Buttons -->
<!-- Fullscreen Toggle -->
<v-btn
block
variant="tonal"
class="mb-2"
:color="fullScreenOnPlay ? 'primary' : ''"
@click="onFullScreenChange"
>
<v-icon class="mr-2">
{{
fullScreenOnPlay
? "mdi-checkbox-outline"
: "mdi-checkbox-blank-outline"
}}
</v-icon>
{{ t("play.full-screen") }}
<v-icon class="ml-auto">
{{
fullScreenOnPlay ? "mdi-fullscreen" : "mdi-fullscreen-exit"
}}
</v-icon>
</v-btn>
<!-- Play Button -->
<v-btn
block
size="large"
variant="flat"
color="primary"
class="mb-3 play-button"
prepend-icon="mdi-play-circle"
@click="onPlay"
>
<span class="text-h6">{{ t("play.play") }}</span>
</v-btn>
<!-- Navigation Buttons -->
<v-btn
block
variant="text"
size="small"
class="mb-2"
prepend-icon="mdi-arrow-left"
@click="
$router.push({
name: ROUTES.ROM,
params: { rom: rom?.id },
})
"
>
{{ t("play.back-to-game-details") }}
</v-btn>
<v-btn
block
variant="text"
size="small"
prepend-icon="mdi-arrow-left"
@click="
$router.push({
name: ROUTES.PLATFORM,
params: { platform: rom?.platform_id },
})
"
>
{{ t("play.back-to-gallery") }}
</v-btn>
<v-btn
block
class="text-romm-red mt-6"
variant="flat"
prepend-icon="mdi-database-remove"
@click="openCacheDialog"
>
{{ t("play.clear-cache") }}
</v-btn>
</v-card-text>
</v-card>
</v-col>
</v-row>
<v-row class="mb-8" no-gutters>
<v-col class="text-right align-center">
<span class="text-medium-emphasis text-caption font-italic mr-2"
>Powered by emulatorjs</span
>
<v-avatar size="50" rounded="0">
<v-img src="/assets/emulatorjs/emulatorjs-logotype.svg" />
</v-avatar>
</v-col>
</v-row>
<CacheDialog />
</v-col>
<template v-else>
<v-col id="game-wrapper" cols="12" class="bg-background pr-2" rounded>
<Player
:rom="rom"
:state="selectedState"
:save="selectedSave"
:bios="selectedFirmware"
:core="selectedCore"
:disc="selectedDisc"
/>
</v-col>
</template>
</v-row>
</template>
<style scoped>
#game-wrapper {
height: 100vh;
}
@media (max-width: 960px) {
#game-wrapper {
height: calc(100vh - 55px);
}
}
.play-button {
position: relative;
overflow: hidden;
font-weight: 600;
letter-spacing: 0.5px;
}
.game-info-col {
flex: 0 0 220px;
max-width: 220px;
}
.flex-col {
flex: 1 1 0;
min-width: 0;
}
.play-button::before {
content: "";
position: absolute;
top: 50%;
left: 50%;
width: 0;
height: 0;
border-radius: 50%;
background: rgba(255, 255, 255, 0.15);
transform: translate(-50%, -50%);
transition:
width 0.5s,
height 0.5s;
}
.play-button:hover::before {
width: 600px;
height: 300px;
}
.play-button:hover {
box-shadow: 0 6px 20px rgba(var(--v-theme-primary), 0.3);
}
/* Tab styling improvements */
.v-tabs :deep(.v-tab) {
text-transform: none;
letter-spacing: 0.25px;
font-weight: 500;
}
.v-tabs :deep(.v-tab--selected) {
color: rgb(var(--v-theme-primary));
}
/* Smooth fade transitions */
.v-expand-transition-enter-active,
.v-expand-transition-leave-active {
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
@media (max-width: 960px) {
.game-info-col,
.flex-col {
flex: 1 1 100%;
max-width: 100%;
}
}
</style>