-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathavatarTools.ts
More file actions
163 lines (148 loc) · 5.27 KB
/
avatarTools.ts
File metadata and controls
163 lines (148 loc) · 5.27 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
import type { AvatarInteractionPayload } from './message-schema';
export type AvatarToolId = AvatarInteractionPayload['toolId'];
export type CursorVariant = 'primary' | 'secondary' | 'tertiary';
export type AvatarToolItem = {
id: AvatarToolId;
labelKey: string;
labelFallback: string;
iconImagePath: string;
iconImagePathAlt?: string;
iconImagePathAlt2?: string;
menuIconScale?: number;
menuIconOffsetX?: number;
menuIconOffsetY?: number;
menuIconOffsetXAlt?: number;
menuIconOffsetYAlt?: number;
menuIconOffsetXAlt2?: number;
menuIconOffsetYAlt2?: number;
cursorImagePath: string;
cursorImagePathAlt?: string;
cursorImagePathAlt2?: string;
cursorHotspotX?: number;
cursorHotspotY?: number;
};
export const ACTIVE_AVATAR_TOOLS_STORAGE_KEY = 'neko.reactChatWindow.activeAvatarTools';
export const MAX_ACTIVE_AVATAR_TOOLS = 3;
export const DEFAULT_ACTIVE_AVATAR_TOOL_IDS: AvatarToolId[] = ['lollipop', 'fist', 'hammer'];
export const AVAILABLE_AVATAR_TOOLS: AvatarToolItem[] = [
{
id: 'lollipop',
labelKey: 'chat.toolLollipop',
labelFallback: '棒棒糖',
iconImagePath: '/static/icons/chat_sugar1.png',
iconImagePathAlt: '/static/icons/chat_sugar2.png',
iconImagePathAlt2: '/static/icons/chat_sugar3.png',
cursorImagePath: '/static/icons/chat_sugar1_cursor.png',
cursorImagePathAlt: '/static/icons/chat_sugar2_cursor.png',
menuIconScale: 1.18,
cursorHotspotX: 27,
cursorHotspotY: 46,
},
{
id: 'fist',
labelKey: 'chat.toolFist',
labelFallback: '猫爪',
iconImagePath: '/static/icons/cat_claw1.png',
iconImagePathAlt: '/static/icons/cat_claw2.png',
cursorImagePath: '/static/icons/cat_claw1_cursor.png',
cursorImagePathAlt: '/static/icons/cat_claw2_cursor.png',
cursorHotspotX: 39,
cursorHotspotY: 46,
},
{
id: 'hammer',
labelKey: 'chat.toolHammer',
labelFallback: '锤子',
iconImagePath: '/static/icons/chat_hammer1.png',
iconImagePathAlt: '/static/icons/chat_hammer2.png',
cursorImagePath: '/static/icons/chat_hammer1_cursor.png',
cursorImagePathAlt: '/static/icons/chat_hammer2_cursor.png',
menuIconScale: 1.52,
menuIconOffsetX: -8,
menuIconOffsetY: 4,
menuIconOffsetXAlt: 1,
menuIconOffsetYAlt: -1,
cursorHotspotX: 50,
cursorHotspotY: 54,
},
];
const AVAILABLE_AVATAR_TOOL_IDS = new Set<AvatarToolId>(AVAILABLE_AVATAR_TOOLS.map(item => item.id));
export function isAvatarToolId(value: unknown): value is AvatarToolId {
return typeof value === 'string' && AVAILABLE_AVATAR_TOOL_IDS.has(value as AvatarToolId);
}
export function sanitizeAvatarToolIds(value: unknown): AvatarToolId[] {
if (!Array.isArray(value)) {
return [...DEFAULT_ACTIVE_AVATAR_TOOL_IDS];
}
const next: AvatarToolId[] = [];
value.forEach((candidate) => {
if (!isAvatarToolId(candidate)) return;
if (next.includes(candidate)) return;
if (next.length >= MAX_ACTIVE_AVATAR_TOOLS) return;
next.push(candidate);
});
return next;
}
export function readPersistedActiveAvatarToolIds(): AvatarToolId[] {
if (typeof window === 'undefined') {
return [...DEFAULT_ACTIVE_AVATAR_TOOL_IDS];
}
try {
const rawValue = window.localStorage?.getItem(ACTIVE_AVATAR_TOOLS_STORAGE_KEY);
if (rawValue === null || typeof rawValue === 'undefined') {
return [...DEFAULT_ACTIVE_AVATAR_TOOL_IDS];
}
return sanitizeAvatarToolIds(JSON.parse(rawValue));
} catch {
return [...DEFAULT_ACTIVE_AVATAR_TOOL_IDS];
}
}
export function persistActiveAvatarToolIds(ids: AvatarToolId[]) {
if (typeof window === 'undefined') return;
try {
window.localStorage?.setItem(
ACTIVE_AVATAR_TOOLS_STORAGE_KEY,
JSON.stringify(sanitizeAvatarToolIds(ids)),
);
} catch {
// Keep in-memory state when localStorage is unavailable.
}
}
export function resolveAvatarToolImagePaths(item: AvatarToolItem, variant: CursorVariant) {
return {
iconImagePath: variant === 'tertiary' && item.iconImagePathAlt2
? item.iconImagePathAlt2
: variant === 'secondary' && item.iconImagePathAlt
? item.iconImagePathAlt
: item.iconImagePath,
cursorImagePath: variant === 'tertiary' && item.cursorImagePathAlt2
? item.cursorImagePathAlt2
: variant === 'secondary' && item.cursorImagePathAlt
? item.cursorImagePathAlt
: variant === 'tertiary' && item.cursorImagePathAlt
? item.cursorImagePathAlt
: item.cursorImagePath,
};
}
export function resolveAvatarToolMenuIconVisual(item: AvatarToolItem, variant: CursorVariant) {
const imagePath = variant === 'tertiary' && item.iconImagePathAlt2
? item.iconImagePathAlt2
: variant === 'secondary' && item.iconImagePathAlt
? item.iconImagePathAlt
: item.iconImagePath;
const offsetX = variant === 'tertiary'
? (item.menuIconOffsetXAlt2 ?? item.menuIconOffsetXAlt ?? item.menuIconOffsetX ?? 0)
: variant === 'secondary'
? (item.menuIconOffsetXAlt ?? item.menuIconOffsetX ?? 0)
: (item.menuIconOffsetX ?? 0);
const offsetY = variant === 'tertiary'
? (item.menuIconOffsetYAlt2 ?? item.menuIconOffsetYAlt ?? item.menuIconOffsetY ?? 0)
: variant === 'secondary'
? (item.menuIconOffsetYAlt ?? item.menuIconOffsetY ?? 0)
: (item.menuIconOffsetY ?? 0);
return {
imagePath,
offsetX,
offsetY,
};
}