2025-08-05 23:30:00 +08:00
|
|
|
|
<script setup>
|
2025-09-09 20:46:03 +08:00
|
|
|
|
import { ref, onMounted, reactive, computed } from "vue";
|
2025-08-09 23:32:55 +08:00
|
|
|
|
import { Howl } from "howler";
|
2025-08-06 07:44:19 +08:00
|
|
|
|
|
2025-09-09 20:46:03 +08:00
|
|
|
|
// 默认图片(打包时位于 assets)
|
|
|
|
|
|
import defaultPet from "./assets/普通型道理.gif";
|
|
|
|
|
|
|
|
|
|
|
|
// 列出 renderer/src/assets 下的图片资源(Vite 特性)
|
|
|
|
|
|
// 只匹配常见的图片后缀
|
|
|
|
|
|
const modules = import.meta.glob('./assets/*.{png,jpg,jpeg,gif}', { as: 'url' });
|
|
|
|
|
|
const assetEntries = Object.entries(modules);
|
|
|
|
|
|
|
|
|
|
|
|
// 状态
|
|
|
|
|
|
const soundFiles = ref([]);
|
2025-08-06 14:15:15 +08:00
|
|
|
|
const showTooltip = ref(false);
|
|
|
|
|
|
const currentTooltip = ref("");
|
2025-09-09 20:46:03 +08:00
|
|
|
|
const isLoading = ref(true);
|
|
|
|
|
|
const isPlaying = ref(false);
|
|
|
|
|
|
|
|
|
|
|
|
// 处理宠物素材选择
|
|
|
|
|
|
const assetList = assetEntries.map(([path, resolver]) => ({ path, resolver }));
|
|
|
|
|
|
const assetPreviews = ref([]); // { path, url }
|
|
|
|
|
|
const selectedAsset = ref(null); // 将保存为 URL
|
|
|
|
|
|
const showSettings = ref(false);
|
|
|
|
|
|
|
|
|
|
|
|
const selectedAssetName = computed(() => {
|
|
|
|
|
|
if (!selectedAsset.value) return null;
|
|
|
|
|
|
// 从路径中取文件名
|
|
|
|
|
|
const parts = selectedAsset.value.split('/');
|
|
|
|
|
|
return parts[parts.length - 1];
|
|
|
|
|
|
});
|
2025-08-22 13:59:36 +08:00
|
|
|
|
|
2025-09-09 20:46:03 +08:00
|
|
|
|
async function loadSavedSelection() {
|
|
|
|
|
|
// 优先从主进程读取持久化选择
|
|
|
|
|
|
if (window.electronAPI && typeof window.electronAPI.getPetSelection === 'function') {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const assetPath = await window.electronAPI.getPetSelection();
|
|
|
|
|
|
if (assetPath) {
|
|
|
|
|
|
// assetPath 存储为相对于 assets 的文件名,例如 "pet2.gif"
|
|
|
|
|
|
const match = assetList.find(a => a.path.endsWith('/' + assetPath));
|
|
|
|
|
|
if (match) {
|
|
|
|
|
|
selectedAsset.value = await match.resolver();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
console.error('读取保存的宠物素材失败:', e);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// fallback: localStorage
|
|
|
|
|
|
const ls = localStorage.getItem('petAsset');
|
|
|
|
|
|
if (ls) {
|
|
|
|
|
|
const match = assetList.find(a => a.path.endsWith('/' + ls));
|
|
|
|
|
|
if (match) selectedAsset.value = await match.resolver();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function saveSelection(assetPath) {
|
|
|
|
|
|
const fileName = assetPath.replace(/^.*\//, '');
|
|
|
|
|
|
// 保存到主进程
|
|
|
|
|
|
if (window.electronAPI && typeof window.electronAPI.setPetSelection === 'function') {
|
|
|
|
|
|
try {
|
|
|
|
|
|
await window.electronAPI.setPetSelection(fileName);
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
console.error('保存宠物素材失败:', e);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// fallback: localStorage
|
|
|
|
|
|
try { localStorage.setItem('petAsset', fileName); } catch (_) {}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 初始化
|
2025-08-22 13:59:36 +08:00
|
|
|
|
onMounted(async () => {
|
2025-09-09 20:46:03 +08:00
|
|
|
|
// 加载声音文件列表
|
2025-08-22 13:59:36 +08:00
|
|
|
|
if (window.electronAPI && typeof window.electronAPI.getSoundFiles === 'function') {
|
|
|
|
|
|
try {
|
2025-08-26 18:58:07 +08:00
|
|
|
|
soundFiles.value = await window.electronAPI.getSoundFiles();
|
2025-09-09 20:46:03 +08:00
|
|
|
|
} catch (err) {
|
|
|
|
|
|
console.error("获取声音文件列表失败:", err);
|
2025-08-22 13:59:36 +08:00
|
|
|
|
} finally {
|
|
|
|
|
|
isLoading.value = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-09 20:46:03 +08:00
|
|
|
|
await loadSavedSelection();
|
|
|
|
|
|
// 预解析所有资源的 URL,用于设置面板的缩略图显示
|
|
|
|
|
|
try {
|
|
|
|
|
|
const previews = await Promise.all(assetList.map(async (a) => ({ path: a.path, url: await a.resolver() })));
|
|
|
|
|
|
assetPreviews.value = previews;
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
console.error('解析素材预览失败:', e);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 监听主进程通过右键菜单发来的选择变更
|
|
|
|
|
|
if (window.electronAPI && typeof window.electronAPI.onPetSelectionChanged === 'function') {
|
|
|
|
|
|
window.electronAPI.onPetSelectionChanged(async (fileName) => {
|
|
|
|
|
|
const match = assetPreviews.value.find(p => p.path.endsWith('/' + fileName));
|
|
|
|
|
|
if (match) selectedAsset.value = match.url;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2025-08-22 13:59:36 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
2025-09-09 20:46:03 +08:00
|
|
|
|
const petGifUrl = computed(() => selectedAsset.value || defaultPet);
|
|
|
|
|
|
|
2025-08-26 18:58:07 +08:00
|
|
|
|
const playRandomSound = async () => {
|
2025-09-03 20:42:04 +08:00
|
|
|
|
if (isPlaying.value) return;
|
2025-08-26 18:58:07 +08:00
|
|
|
|
if (isLoading.value || soundFiles.value.length === 0) return;
|
|
|
|
|
|
const randomSoundFile = soundFiles.value[Math.floor(Math.random() * soundFiles.value.length)];
|
2025-09-09 20:46:03 +08:00
|
|
|
|
isPlaying.value = true;
|
2025-08-07 01:15:18 +08:00
|
|
|
|
try {
|
2025-08-26 18:58:07 +08:00
|
|
|
|
const audioUrl = await window.electronAPI.getSoundPath(randomSoundFile);
|
2025-09-03 20:42:04 +08:00
|
|
|
|
if (audioUrl) {
|
|
|
|
|
|
new Howl({
|
|
|
|
|
|
src: [audioUrl],
|
|
|
|
|
|
format: ["mp3"],
|
|
|
|
|
|
onend: function() {
|
|
|
|
|
|
showTooltip.value = false;
|
|
|
|
|
|
isPlaying.value = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}).play();
|
|
|
|
|
|
} else {
|
2025-09-09 20:46:03 +08:00
|
|
|
|
isPlaying.value = false;
|
2025-09-03 20:42:04 +08:00
|
|
|
|
}
|
2025-08-07 01:15:18 +08:00
|
|
|
|
} catch (err) {
|
2025-09-03 20:42:04 +08:00
|
|
|
|
isPlaying.value = false;
|
2025-08-09 23:32:55 +08:00
|
|
|
|
console.error("播放失败:", err);
|
2025-08-07 01:15:18 +08:00
|
|
|
|
}
|
2025-09-09 20:46:03 +08:00
|
|
|
|
currentTooltip.value = randomSoundFile.replace(/\.mp3$/, '');
|
2025-08-06 14:15:15 +08:00
|
|
|
|
showTooltip.value = true;
|
|
|
|
|
|
};
|
2025-08-26 18:58:07 +08:00
|
|
|
|
|
|
|
|
|
|
const dragState = reactive({
|
2025-09-03 15:53:05 +08:00
|
|
|
|
isDragging: false,
|
|
|
|
|
|
hasMoved: false,
|
|
|
|
|
|
mouseStartX: 0,
|
|
|
|
|
|
mouseStartY: 0,
|
|
|
|
|
|
windowStartX: 0,
|
|
|
|
|
|
windowStartY: 0,
|
2025-08-26 18:58:07 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
2025-09-03 15:53:05 +08:00
|
|
|
|
async function handleMouseDown(event) {
|
2025-09-09 20:46:03 +08:00
|
|
|
|
if (event.button !== 0) return;
|
2025-09-03 15:53:05 +08:00
|
|
|
|
const { x, y } = await window.electronAPI.getWindowPosition();
|
|
|
|
|
|
dragState.windowStartX = x;
|
|
|
|
|
|
dragState.windowStartY = y;
|
|
|
|
|
|
dragState.mouseStartX = event.screenX;
|
|
|
|
|
|
dragState.mouseStartY = event.screenY;
|
2025-08-26 18:58:07 +08:00
|
|
|
|
dragState.isDragging = true;
|
2025-09-03 15:53:05 +08:00
|
|
|
|
dragState.hasMoved = false;
|
2025-08-26 18:58:07 +08:00
|
|
|
|
window.addEventListener('mousemove', handleMouseMove);
|
|
|
|
|
|
window.addEventListener('mouseup', handleMouseUp);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function handleMouseMove(event) {
|
|
|
|
|
|
if (!dragState.isDragging) return;
|
2025-09-03 15:53:05 +08:00
|
|
|
|
const deltaX = event.screenX - dragState.mouseStartX;
|
|
|
|
|
|
const deltaY = event.screenY - dragState.mouseStartY;
|
2025-09-09 20:46:03 +08:00
|
|
|
|
if (!dragState.hasMoved && (Math.abs(deltaX) > 5 || Math.abs(deltaY) > 5)) dragState.hasMoved = true;
|
2025-09-03 15:53:05 +08:00
|
|
|
|
const newWindowX = dragState.windowStartX + deltaX;
|
|
|
|
|
|
const newWindowY = dragState.windowStartY + deltaY;
|
|
|
|
|
|
window.electronAPI.moveWindow({ x: newWindowX, y: newWindowY });
|
2025-08-26 18:58:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function handleMouseUp() {
|
2025-09-09 20:46:03 +08:00
|
|
|
|
if (dragState.isDragging && !dragState.hasMoved) playRandomSound();
|
2025-08-26 18:58:07 +08:00
|
|
|
|
dragState.isDragging = false;
|
|
|
|
|
|
window.removeEventListener('mousemove', handleMouseMove);
|
|
|
|
|
|
window.removeEventListener('mouseup', handleMouseUp);
|
|
|
|
|
|
}
|
2025-09-03 20:42:04 +08:00
|
|
|
|
|
2025-09-09 20:46:03 +08:00
|
|
|
|
function handleRightClick() { window.electronAPI.showContextMenu(); }
|
|
|
|
|
|
|
|
|
|
|
|
async function chooseAsset(entry) {
|
|
|
|
|
|
// entry can be either {path, resolver} or a preview {path, url}
|
|
|
|
|
|
if (entry.url) {
|
|
|
|
|
|
selectedAsset.value = entry.url;
|
|
|
|
|
|
await saveSelection(entry.path);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
const url = await entry.resolver();
|
|
|
|
|
|
selectedAsset.value = url;
|
|
|
|
|
|
await saveSelection(entry.path);
|
|
|
|
|
|
}
|
|
|
|
|
|
showSettings.value = false;
|
2025-09-03 20:42:04 +08:00
|
|
|
|
}
|
2025-08-05 23:30:00 +08:00
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
<template>
|
2025-09-03 20:42:04 +08:00
|
|
|
|
<div
|
|
|
|
|
|
class="pet-container"
|
|
|
|
|
|
@mousedown="handleMouseDown"
|
|
|
|
|
|
@contextmenu.prevent="handleRightClick"
|
|
|
|
|
|
>
|
2025-08-06 07:44:19 +08:00
|
|
|
|
<transition name="fade">
|
|
|
|
|
|
<div v-if="showTooltip" class="tooltip">
|
|
|
|
|
|
{{ currentTooltip }}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</transition>
|
2025-09-09 20:46:03 +08:00
|
|
|
|
<img :src="petGifUrl" class="pet-gif" />
|
2025-08-05 23:30:00 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
|
2025-08-09 23:32:55 +08:00
|
|
|
|
<style>
|
2025-08-26 18:58:07 +08:00
|
|
|
|
/* 全局样式保持不变 */
|
2025-08-09 23:32:55 +08:00
|
|
|
|
html, body, #app {
|
|
|
|
|
|
background-color: transparent !important;
|
|
|
|
|
|
margin: 0;
|
|
|
|
|
|
padding: 0;
|
2025-08-26 18:58:07 +08:00
|
|
|
|
overflow: hidden;
|
2025-08-09 23:32:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
</style>
|
|
|
|
|
|
|
2025-08-05 23:30:00 +08:00
|
|
|
|
<style scoped>
|
2025-08-26 18:58:07 +08:00
|
|
|
|
/* 样式大大简化 */
|
2025-08-06 14:15:15 +08:00
|
|
|
|
.pet-container {
|
2025-08-26 18:58:07 +08:00
|
|
|
|
width: 200px;
|
|
|
|
|
|
height: 200px;
|
|
|
|
|
|
position: relative;
|
|
|
|
|
|
cursor: pointer;
|
|
|
|
|
|
user-select: none; /* 防止拖动时选中文本 */
|
2025-08-05 23:30:00 +08:00
|
|
|
|
}
|
2025-08-06 07:44:19 +08:00
|
|
|
|
|
2025-08-06 14:15:15 +08:00
|
|
|
|
.pet-gif {
|
2025-08-06 07:44:19 +08:00
|
|
|
|
width: 100%;
|
|
|
|
|
|
height: 100%;
|
2025-08-26 18:58:07 +08:00
|
|
|
|
display: block;
|
|
|
|
|
|
/* 图片现在不接收任何鼠标事件,所有事件都由父容器处理 */
|
|
|
|
|
|
pointer-events: none;
|
2025-08-06 07:44:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.tooltip {
|
|
|
|
|
|
position: absolute;
|
2025-08-26 18:58:07 +08:00
|
|
|
|
bottom: 10px;
|
2025-08-06 07:44:19 +08:00
|
|
|
|
left: 50%;
|
|
|
|
|
|
transform: translateX(-50%);
|
|
|
|
|
|
background: rgba(0, 0, 0, 0.7);
|
|
|
|
|
|
color: white;
|
|
|
|
|
|
padding: 8px 16px;
|
|
|
|
|
|
border-radius: 20px;
|
|
|
|
|
|
font-size: 14px;
|
2025-08-06 14:15:15 +08:00
|
|
|
|
white-space: nowrap;
|
2025-08-26 18:58:07 +08:00
|
|
|
|
pointer-events: none; /* 提示框也不响应鼠标 */
|
2025-08-06 07:44:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-26 18:58:07 +08:00
|
|
|
|
.fade-enter-active, .fade-leave-active {
|
2025-08-06 14:15:15 +08:00
|
|
|
|
transition: opacity 0.3s;
|
2025-08-05 23:30:00 +08:00
|
|
|
|
}
|
2025-08-26 18:58:07 +08:00
|
|
|
|
.fade-enter-from, .fade-leave-to {
|
2025-08-06 07:44:19 +08:00
|
|
|
|
opacity: 0;
|
2025-08-05 23:30:00 +08:00
|
|
|
|
}
|
2025-08-09 23:32:55 +08:00
|
|
|
|
</style>
|