cc-pets 1.4.0 → 1.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +54 -0
- package/README.md +100 -0
- package/Sources/CCPets/CCPetsAppDelegate.m +123 -138
- package/Sources/CCPets/CCPetsPhrases.h +77 -2
- package/Sources/CCPets/CCPetsPhrases.m +446 -153
- package/Sources/CCPets/CCPetsPhrasesEditor.h +17 -0
- package/Sources/CCPets/CCPetsPhrasesEditor.m +369 -0
- package/Sources/CCPets/PetView.m +28 -1
- package/package.json +1 -1
- package/scripts/build.sh +5 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
#import <Cocoa/Cocoa.h>
|
|
2
|
+
|
|
3
|
+
// app 内置的台词编辑器。
|
|
4
|
+
//
|
|
5
|
+
// 为什么不用系统默认编辑器(原来是 openURL: 交给"文本编辑"):那条路没有任何反馈。
|
|
6
|
+
// 用户改完保存、桌宠照旧,他无从知道是小节名拼错了、还是句子超了 30 字、还是槽位
|
|
7
|
+
// 写错了名字——这些全都是静默失效。编辑器存在的唯一理由就是把校验结果当场摆出来。
|
|
8
|
+
@interface PetPhrasesEditorController : NSWindowController
|
|
9
|
+
|
|
10
|
+
// 打开(已开着就前置)。窗口是单例,避免两个窗口各自编辑同一个文件互相覆盖。
|
|
11
|
+
+ (void)present;
|
|
12
|
+
|
|
13
|
+
// "试说一句"回调:把选中情境的一条台词交给桌宠当场说出来。
|
|
14
|
+
// 编辑器不认识气泡,也不该认识——它只负责给出文本。
|
|
15
|
+
+ (void)setSpeakHandler:(void (^)(NSString *text))handler;
|
|
16
|
+
|
|
17
|
+
@end
|
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
#import "CCPetsPhrasesEditor.h"
|
|
2
|
+
#import "CCPetsPhrases.h"
|
|
3
|
+
|
|
4
|
+
static void (^PetPhrasesSpeakHandler)(NSString *);
|
|
5
|
+
|
|
6
|
+
@interface PetPhrasesEditorController () <NSTextViewDelegate>
|
|
7
|
+
@property NSTextView *textView;
|
|
8
|
+
@property NSPopUpButton *tagPicker;
|
|
9
|
+
@property NSTextField *statusLabel;
|
|
10
|
+
@property NSButton *saveButton;
|
|
11
|
+
@property NSButton *saveAndCloseButton;
|
|
12
|
+
// 载入时文件的 mtime。保存前比一次,防止把外部编辑器的改动直接盖掉。
|
|
13
|
+
@property NSDate *loadedStamp;
|
|
14
|
+
@end
|
|
15
|
+
|
|
16
|
+
@implementation PetPhrasesEditorController
|
|
17
|
+
|
|
18
|
+
+ (instancetype)shared {
|
|
19
|
+
static PetPhrasesEditorController *shared;
|
|
20
|
+
static dispatch_once_t once;
|
|
21
|
+
dispatch_once(&once, ^{ shared = [PetPhrasesEditorController new]; });
|
|
22
|
+
return shared;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
+ (void)setSpeakHandler:(void (^)(NSString *))handler {
|
|
26
|
+
PetPhrasesSpeakHandler = [handler copy];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
+ (void)present {
|
|
30
|
+
PetPhrasesEditorController *controller = [self shared];
|
|
31
|
+
[controller buildWindowIfNeeded];
|
|
32
|
+
[controller reloadFromDisk];
|
|
33
|
+
[NSApp activateIgnoringOtherApps:YES];
|
|
34
|
+
[controller.window makeKeyAndOrderFront:nil];
|
|
35
|
+
[controller.window makeFirstResponder:controller.textView];
|
|
36
|
+
[controller offerToRemoveLegacyFile];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// 老词库在启动时已经合并进新文件了(见 PetPhrasesEnsureFileExists),但老文件还留着。
|
|
40
|
+
// 在这里问一次要不要收起来:合并是我们替用户做的,动他的文件必须他自己点头。
|
|
41
|
+
//
|
|
42
|
+
// 他选"先留着"的话下次打开还会问。留着原名不动有两个坑:改它不会有任何效果;
|
|
43
|
+
// 而且哪天 speech.txt 丢了,启动时会再从它合并一遍,把迁移之后写的台词全冲掉。
|
|
44
|
+
- (void)offerToRemoveLegacyFile {
|
|
45
|
+
if (!PetPhrasesLegacyFileExists()) return;
|
|
46
|
+
|
|
47
|
+
NSString *legacy = PetPhrasesLegacyFilePath();
|
|
48
|
+
NSAlert *alert = [NSAlert new];
|
|
49
|
+
alert.messageText = @"旧的台词文件已更新";
|
|
50
|
+
alert.informativeText = [NSString stringWithFormat:
|
|
51
|
+
@"正在使用的台词全部保留并合并到 %@。\n\n"
|
|
52
|
+
"旧文件 %@ 已经失效,后续修改不会有任何效果。"
|
|
53
|
+
"可以把它改名成 %@.bak 收起来,内容还留在原地。",
|
|
54
|
+
PetPhrasesFilePath(), legacy, legacy.lastPathComponent];
|
|
55
|
+
[alert addButtonWithTitle:@"改名收起来"];
|
|
56
|
+
[alert addButtonWithTitle:@"先留着"];
|
|
57
|
+
if ([alert runModal] != NSAlertFirstButtonReturn) return;
|
|
58
|
+
if (PetPhrasesArchiveLegacyFile()) {
|
|
59
|
+
[self showStatus:[NSString stringWithFormat:@"旧文件已改名成 %@.bak。",
|
|
60
|
+
legacy.lastPathComponent] warning:NO];
|
|
61
|
+
} else {
|
|
62
|
+
[self showStatus:@"旧文件改名失败,可以手动改。" warning:YES];
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
#pragma mark - 界面
|
|
67
|
+
|
|
68
|
+
- (void)buildWindowIfNeeded {
|
|
69
|
+
if (self.window) return;
|
|
70
|
+
|
|
71
|
+
NSWindow *window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 620, 640)
|
|
72
|
+
styleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable |
|
|
73
|
+
NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable
|
|
74
|
+
backing:NSBackingStoreBuffered defer:NO];
|
|
75
|
+
window.title = @"桌宠台词";
|
|
76
|
+
window.releasedWhenClosed = NO;
|
|
77
|
+
window.minSize = NSMakeSize(460, 400);
|
|
78
|
+
[window center];
|
|
79
|
+
|
|
80
|
+
NSView *root = window.contentView;
|
|
81
|
+
|
|
82
|
+
NSScrollView *scroll = [[NSScrollView alloc] initWithFrame:NSZeroRect];
|
|
83
|
+
scroll.hasVerticalScroller = YES;
|
|
84
|
+
scroll.autohidesScrollers = YES;
|
|
85
|
+
scroll.borderType = NSNoBorder;
|
|
86
|
+
scroll.translatesAutoresizingMaskIntoConstraints = NO;
|
|
87
|
+
|
|
88
|
+
NSTextView *textView = [[NSTextView alloc] initWithFrame:NSZeroRect];
|
|
89
|
+
textView.minSize = NSMakeSize(0, 0);
|
|
90
|
+
textView.maxSize = NSMakeSize(CGFLOAT_MAX, CGFLOAT_MAX);
|
|
91
|
+
textView.verticallyResizable = YES;
|
|
92
|
+
textView.horizontallyResizable = NO;
|
|
93
|
+
textView.autoresizingMask = NSViewWidthSizable;
|
|
94
|
+
textView.textContainer.widthTracksTextView = YES;
|
|
95
|
+
textView.font = [NSFont monospacedSystemFontOfSize:13 weight:NSFontWeightRegular];
|
|
96
|
+
// 台词是纯文本:富文本会把粘贴进来的字体样式一起带进文件。
|
|
97
|
+
textView.richText = NO;
|
|
98
|
+
textView.allowsUndo = YES;
|
|
99
|
+
// 智能引号会把英文引号换成弯引号、破折号会被合并——台词里无所谓,
|
|
100
|
+
// 但 {slot} 这类记号一旦被"智能"处理过就再也对不上白名单了。
|
|
101
|
+
textView.automaticQuoteSubstitutionEnabled = NO;
|
|
102
|
+
textView.automaticDashSubstitutionEnabled = NO;
|
|
103
|
+
textView.automaticTextReplacementEnabled = NO;
|
|
104
|
+
textView.automaticSpellingCorrectionEnabled = NO;
|
|
105
|
+
textView.textContainerInset = NSMakeSize(8, 10);
|
|
106
|
+
textView.delegate = self;
|
|
107
|
+
scroll.documentView = textView;
|
|
108
|
+
self.textView = textView;
|
|
109
|
+
|
|
110
|
+
NSTextField *status = [NSTextField labelWithString:@""];
|
|
111
|
+
status.font = [NSFont systemFontOfSize:11];
|
|
112
|
+
status.textColor = NSColor.secondaryLabelColor;
|
|
113
|
+
status.lineBreakMode = NSLineBreakByTruncatingTail;
|
|
114
|
+
status.translatesAutoresizingMaskIntoConstraints = NO;
|
|
115
|
+
self.statusLabel = status;
|
|
116
|
+
|
|
117
|
+
NSPopUpButton *picker = [[NSPopUpButton alloc] initWithFrame:NSZeroRect pullsDown:NO];
|
|
118
|
+
picker.translatesAutoresizingMaskIntoConstraints = NO;
|
|
119
|
+
for (NSString *tag in PetPhraseAllTags()) {
|
|
120
|
+
[picker addItemWithTitle:[NSString stringWithFormat:@"%@(%@)",
|
|
121
|
+
PetPhraseTagDescription(tag), tag]];
|
|
122
|
+
picker.lastItem.representedObject = tag;
|
|
123
|
+
}
|
|
124
|
+
self.tagPicker = picker;
|
|
125
|
+
|
|
126
|
+
NSButton *speak = [NSButton buttonWithTitle:@"试说一句" target:self
|
|
127
|
+
action:@selector(speakSample:)];
|
|
128
|
+
speak.translatesAutoresizingMaskIntoConstraints = NO;
|
|
129
|
+
|
|
130
|
+
NSButton *save = [NSButton buttonWithTitle:@"保存" target:self action:@selector(save:)];
|
|
131
|
+
// ⌘S 而不是回车:这是个多行文本编辑器,回车要用来换行。
|
|
132
|
+
save.keyEquivalent = @"s";
|
|
133
|
+
save.keyEquivalentModifierMask = NSEventModifierFlagCommand;
|
|
134
|
+
save.translatesAutoresizingMaskIntoConstraints = NO;
|
|
135
|
+
self.saveButton = save;
|
|
136
|
+
|
|
137
|
+
NSButton *saveAndClose = [NSButton buttonWithTitle:@"保存并关闭" target:self
|
|
138
|
+
action:@selector(saveAndClose:)];
|
|
139
|
+
saveAndClose.keyEquivalent = @"\r";
|
|
140
|
+
saveAndClose.translatesAutoresizingMaskIntoConstraints = NO;
|
|
141
|
+
self.saveAndCloseButton = saveAndClose;
|
|
142
|
+
|
|
143
|
+
NSButton *revert = [NSButton buttonWithTitle:@"恢复出厂台词…" target:self
|
|
144
|
+
action:@selector(revertToDefault:)];
|
|
145
|
+
revert.translatesAutoresizingMaskIntoConstraints = NO;
|
|
146
|
+
|
|
147
|
+
for (NSView *view in @[scroll, status, picker, speak, save, saveAndClose, revert]) {
|
|
148
|
+
[root addSubview:view];
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
[NSLayoutConstraint activateConstraints:@[
|
|
152
|
+
[scroll.topAnchor constraintEqualToAnchor:root.topAnchor],
|
|
153
|
+
[scroll.leadingAnchor constraintEqualToAnchor:root.leadingAnchor],
|
|
154
|
+
[scroll.trailingAnchor constraintEqualToAnchor:root.trailingAnchor],
|
|
155
|
+
[scroll.bottomAnchor constraintEqualToAnchor:status.topAnchor constant:-8],
|
|
156
|
+
|
|
157
|
+
[status.leadingAnchor constraintEqualToAnchor:root.leadingAnchor constant:16],
|
|
158
|
+
[status.trailingAnchor constraintEqualToAnchor:root.trailingAnchor constant:-16],
|
|
159
|
+
[status.bottomAnchor constraintEqualToAnchor:picker.topAnchor constant:-8],
|
|
160
|
+
|
|
161
|
+
[picker.leadingAnchor constraintEqualToAnchor:root.leadingAnchor constant:16],
|
|
162
|
+
[picker.bottomAnchor constraintEqualToAnchor:root.bottomAnchor constant:-14],
|
|
163
|
+
[picker.widthAnchor constraintLessThanOrEqualToConstant:240],
|
|
164
|
+
[speak.leadingAnchor constraintEqualToAnchor:picker.trailingAnchor constant:8],
|
|
165
|
+
[speak.centerYAnchor constraintEqualToAnchor:picker.centerYAnchor],
|
|
166
|
+
[revert.leadingAnchor constraintGreaterThanOrEqualToAnchor:speak.trailingAnchor constant:8],
|
|
167
|
+
[revert.trailingAnchor constraintEqualToAnchor:save.leadingAnchor constant:-8],
|
|
168
|
+
[revert.centerYAnchor constraintEqualToAnchor:picker.centerYAnchor],
|
|
169
|
+
[save.trailingAnchor constraintEqualToAnchor:saveAndClose.leadingAnchor constant:-8],
|
|
170
|
+
[save.centerYAnchor constraintEqualToAnchor:picker.centerYAnchor],
|
|
171
|
+
[saveAndClose.trailingAnchor constraintEqualToAnchor:root.trailingAnchor constant:-16],
|
|
172
|
+
[saveAndClose.centerYAnchor constraintEqualToAnchor:picker.centerYAnchor],
|
|
173
|
+
]];
|
|
174
|
+
|
|
175
|
+
self.window = window;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
#pragma mark - 读写
|
|
179
|
+
|
|
180
|
+
- (void)reloadFromDisk {
|
|
181
|
+
PetPhrasesEnsureFileExists();
|
|
182
|
+
NSString *path = PetPhrasesFilePath();
|
|
183
|
+
NSString *text = [NSString stringWithContentsOfFile:path
|
|
184
|
+
encoding:NSUTF8StringEncoding error:nil] ?: @"";
|
|
185
|
+
self.textView.string = text;
|
|
186
|
+
self.loadedStamp = [NSFileManager.defaultManager attributesOfItemAtPath:path
|
|
187
|
+
error:nil][NSFileModificationDate];
|
|
188
|
+
[self showStatus:[NSString stringWithFormat:@"%@", path] warning:NO];
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
- (void)showStatus:(NSString *)message warning:(BOOL)warning {
|
|
192
|
+
self.statusLabel.stringValue = message ?: @"";
|
|
193
|
+
self.statusLabel.textColor = warning ? NSColor.systemOrangeColor :
|
|
194
|
+
NSColor.secondaryLabelColor;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
- (void)save:(id)sender {
|
|
198
|
+
[self performSave];
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// 保存成功才关窗。失败或被用户取消时留在原地,否则用户的改动会连同窗口一起消失。
|
|
202
|
+
- (void)saveAndClose:(id)sender {
|
|
203
|
+
if (![self performSave]) return;
|
|
204
|
+
[self.window close];
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// 保存时校验,不做实时校验——边打边标红只会干扰输入,而且写到一半的行必然是"错"的。
|
|
208
|
+
// 返回是否真的写进了文件(被校验拦下、被 mtime 冲突取消都算没写)。
|
|
209
|
+
- (BOOL)performSave {
|
|
210
|
+
// 标签被删掉时先补回原位再校验。
|
|
211
|
+
//
|
|
212
|
+
// 只报错是把人卡死:编辑器里已经看不到那一行了,用户既不知道少的是哪个,也不知道
|
|
213
|
+
// 该写成什么样,只能去翻文档或者点"恢复出厂"把自己的改动全丢掉。
|
|
214
|
+
NSArray<NSString *> *restored = nil;
|
|
215
|
+
NSString *text = self.textView.string ?: @"";
|
|
216
|
+
NSString *repaired = PetPhraseTextWithRestoredTags(text, &restored);
|
|
217
|
+
if (restored.count > 0) {
|
|
218
|
+
// 走 shouldChangeTextInRange: 让这一步能被 ⌘Z 撤销,用户不满意可以退回去。
|
|
219
|
+
NSRange all = NSMakeRange(0, text.length);
|
|
220
|
+
if ([self.textView shouldChangeTextInRange:all replacementString:repaired]) {
|
|
221
|
+
[self.textView.textStorage replaceCharactersInRange:all withString:repaired];
|
|
222
|
+
[self.textView didChangeText];
|
|
223
|
+
text = repaired;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
NSArray<PetPhraseIssue *> *issues = PetPhraseValidateText(text);
|
|
228
|
+
if (PetPhraseIssuesHaveError(issues)) {
|
|
229
|
+
[self presentIssues:issues blocking:YES restored:restored];
|
|
230
|
+
return NO;
|
|
231
|
+
}
|
|
232
|
+
if (![self confirmNoExternalChange]) return NO;
|
|
233
|
+
|
|
234
|
+
NSString *path = PetPhrasesFilePath();
|
|
235
|
+
NSError *error = nil;
|
|
236
|
+
// 结尾补一个换行:没有的话下次追加小节会直接粘在最后一句后面。
|
|
237
|
+
NSString *normalized = [text hasSuffix:@"\n"] ? text : [text stringByAppendingString:@"\n"];
|
|
238
|
+
if (![normalized writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error]) {
|
|
239
|
+
[self showStatus:[NSString stringWithFormat:@"保存失败:%@",
|
|
240
|
+
error.localizedDescription ?: @"未知原因"] warning:YES];
|
|
241
|
+
return NO;
|
|
242
|
+
}
|
|
243
|
+
self.loadedStamp = [NSFileManager.defaultManager attributesOfItemAtPath:path
|
|
244
|
+
error:nil][NSFileModificationDate];
|
|
245
|
+
|
|
246
|
+
if (issues.count > 0 || restored.count > 0) {
|
|
247
|
+
[self presentIssues:issues blocking:NO restored:restored];
|
|
248
|
+
[self showStatus:restored.count > 0 ?
|
|
249
|
+
[NSString stringWithFormat:@"已保存,补回了 %lu 个标签", (unsigned long)restored.count] :
|
|
250
|
+
[NSString stringWithFormat:@"已保存,有 %lu 处提示", (unsigned long)issues.count]
|
|
251
|
+
warning:YES];
|
|
252
|
+
return YES;
|
|
253
|
+
}
|
|
254
|
+
[self showStatus:@"已保存,立刻生效。" warning:NO];
|
|
255
|
+
return YES;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// 外部编辑器可能同时改了这个文件。直接覆盖会静默吃掉别人的改动,所以问一次。
|
|
259
|
+
- (BOOL)confirmNoExternalChange {
|
|
260
|
+
NSDate *current = [NSFileManager.defaultManager
|
|
261
|
+
attributesOfItemAtPath:PetPhrasesFilePath() error:nil][NSFileModificationDate];
|
|
262
|
+
if (!current || !self.loadedStamp || [current isEqualToDate:self.loadedStamp]) return YES;
|
|
263
|
+
|
|
264
|
+
NSAlert *alert = [NSAlert new];
|
|
265
|
+
alert.messageText = @"文件在外部被改过";
|
|
266
|
+
alert.informativeText = @"打开编辑器之后,台词文件被别的程序修改了。"
|
|
267
|
+
"继续保存会覆盖掉那些改动。";
|
|
268
|
+
[alert addButtonWithTitle:@"覆盖"];
|
|
269
|
+
[alert addButtonWithTitle:@"取消"];
|
|
270
|
+
[alert addButtonWithTitle:@"重新载入"];
|
|
271
|
+
NSModalResponse response = [alert runModal];
|
|
272
|
+
if (response == NSAlertSecondButtonReturn) return NO;
|
|
273
|
+
if (response == NSAlertThirdButtonReturn) {
|
|
274
|
+
[self reloadFromDisk];
|
|
275
|
+
return NO;
|
|
276
|
+
}
|
|
277
|
+
return YES;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
- (void)presentIssues:(NSArray<PetPhraseIssue *> *)issues blocking:(BOOL)blocking
|
|
281
|
+
restored:(NSArray<NSString *> *)restored {
|
|
282
|
+
NSMutableArray<NSString *> *lines = [NSMutableArray array];
|
|
283
|
+
// 补回标签这件事必须说在最前面:用户看到的文本被我们改过了,不说清楚就是偷偷动手。
|
|
284
|
+
if (restored.count > 0) {
|
|
285
|
+
NSMutableArray<NSString *> *names = [NSMutableArray array];
|
|
286
|
+
for (NSString *tag in restored) {
|
|
287
|
+
[names addObject:[NSString stringWithFormat:@"[%@](%@)", tag,
|
|
288
|
+
PetPhraseTagDescription(tag)]];
|
|
289
|
+
}
|
|
290
|
+
[lines addObject:[NSString stringWithFormat:
|
|
291
|
+
@"标签不能删,已经帮你把 %@ 加回原位了(⌘Z 可撤销)。",
|
|
292
|
+
[names componentsJoinedByString:@"、"]]];
|
|
293
|
+
[lines addObject:@""];
|
|
294
|
+
}
|
|
295
|
+
for (PetPhraseIssue *issue in issues) {
|
|
296
|
+
NSString *prefix = issue.level == PetPhraseIssueLevelError ? @"必须改:" : @"提示:";
|
|
297
|
+
NSString *where = issue.line > 0 ?
|
|
298
|
+
[NSString stringWithFormat:@"第 %ld 行 ", (long)issue.line] : @"";
|
|
299
|
+
[lines addObject:[NSString stringWithFormat:@"%@%@%@", prefix, where, issue.message]];
|
|
300
|
+
// 一屏放不下就没人看了,剩下的等改完这批再报。
|
|
301
|
+
if (lines.count >= 12) {
|
|
302
|
+
[lines addObject:[NSString stringWithFormat:@"…另有 %lu 条",
|
|
303
|
+
(unsigned long)(issues.count - lines.count + 1)]];
|
|
304
|
+
break;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
NSAlert *alert = [NSAlert new];
|
|
308
|
+
if (blocking) {
|
|
309
|
+
alert.messageText = @"还不能保存";
|
|
310
|
+
} else {
|
|
311
|
+
alert.messageText = issues.count > 0 ? @"已保存,但有几处不会生效" : @"已保存";
|
|
312
|
+
}
|
|
313
|
+
alert.informativeText = [lines componentsJoinedByString:@"\n"];
|
|
314
|
+
alert.alertStyle = blocking ? NSAlertStyleWarning : NSAlertStyleInformational;
|
|
315
|
+
[alert addButtonWithTitle:@"好"];
|
|
316
|
+
[alert runModal];
|
|
317
|
+
if (blocking) {
|
|
318
|
+
[self showStatus:@"有必须改的地方,还没保存。" warning:YES];
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
#pragma mark - 试说一句
|
|
323
|
+
|
|
324
|
+
// 对着编辑器里【还没保存】的文本取,这样用户改完不用先保存就能听到效果。
|
|
325
|
+
- (void)speakSample:(id)sender {
|
|
326
|
+
NSString *tag = self.tagPicker.selectedItem.representedObject;
|
|
327
|
+
NSString *text = PetPhraseForTagInText(tag, self.textView.string ?: @"", [self sampleSlots]);
|
|
328
|
+
if (text.length == 0) {
|
|
329
|
+
[self showStatus:[NSString stringWithFormat:@"[%@] 里没有能说的句子。", tag] warning:YES];
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
if (PetPhrasesSpeakHandler) PetPhrasesSpeakHandler(text);
|
|
333
|
+
[self showStatus:[NSString stringWithFormat:@"试说:%@", text] warning:NO];
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// 预览用的假数据。用真实数据的话,没在跑 agent 时 {toolName} 之类全是空的,
|
|
337
|
+
// 带槽位的句子会被整条跳过,用户会以为自己写错了。
|
|
338
|
+
- (NSDictionary<NSString *, NSString *> *)sampleSlots {
|
|
339
|
+
return @{
|
|
340
|
+
@"quota5h": @"12%", @"resetTime": @"18:30", @"toolName": @"Bash",
|
|
341
|
+
@"sessionMin": @"95", @"failCount": @"3", @"hour": @"2",
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
#pragma mark - 恢复出厂
|
|
346
|
+
|
|
347
|
+
- (void)revertToDefault:(id)sender {
|
|
348
|
+
NSAlert *alert = [NSAlert new];
|
|
349
|
+
alert.messageText = @"恢复出厂台词?";
|
|
350
|
+
alert.informativeText = @"编辑器里的内容会被出厂台词替换。还没保存,可以按 ⌘Z 撤销。";
|
|
351
|
+
[alert addButtonWithTitle:@"恢复"];
|
|
352
|
+
[alert addButtonWithTitle:@"取消"];
|
|
353
|
+
if ([alert runModal] != NSAlertFirstButtonReturn) return;
|
|
354
|
+
|
|
355
|
+
NSString *text = [NSString stringWithContentsOfFile:PetPhrasesDefaultFilePath()
|
|
356
|
+
encoding:NSUTF8StringEncoding error:nil];
|
|
357
|
+
if (text.length == 0) {
|
|
358
|
+
[self showStatus:@"找不到出厂台词文件。" warning:YES];
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
// 走 shouldChangeTextInRange: 而不是直接设 string,这样这一步能被 ⌘Z 撤销。
|
|
362
|
+
NSRange all = NSMakeRange(0, self.textView.string.length);
|
|
363
|
+
if (![self.textView shouldChangeTextInRange:all replacementString:text]) return;
|
|
364
|
+
[self.textView.textStorage replaceCharactersInRange:all withString:text];
|
|
365
|
+
[self.textView didChangeText];
|
|
366
|
+
[self showStatus:@"已填入出厂台词,按保存才会写进文件。" warning:NO];
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
@end
|
package/Sources/CCPets/PetView.m
CHANGED
|
@@ -4,6 +4,10 @@
|
|
|
4
4
|
#import "CCPetsPaths.h"
|
|
5
5
|
#import "CCPetsImageLoader.h"
|
|
6
6
|
|
|
7
|
+
// 碎碎念频率档位的 defaults 键。定义在 CCPetsAppDelegate.m,这里只读不写;
|
|
8
|
+
// 单独 extern 而不 import 那个头文件,是因为它反过来 import 了 PetView.h。
|
|
9
|
+
extern NSString *const PetSpeechFrequencyKey;
|
|
10
|
+
|
|
7
11
|
// 按下后位移超过这个距离才判定为拖动,之内视为点击。
|
|
8
12
|
static const CGFloat PetDragThreshold = 3.0;
|
|
9
13
|
// 菜单里自绘行的宽度:主菜单要撑满 menu.minimumWidth,子菜单按自身内容收窄,
|
|
@@ -1441,8 +1445,31 @@ typedef NS_ENUM(NSInteger, PetMicroBehaviorKind) {
|
|
|
1441
1445
|
action:@selector(toggleSpeech:)
|
|
1442
1446
|
width:PetSubmenuRowWidth
|
|
1443
1447
|
tag:1];
|
|
1448
|
+
// 频率是四道闸的组合,单调任何一道都不会真的变频繁,所以只给一个档位。
|
|
1449
|
+
// 用系统的打勾单选行,不用自绘开关:这几项互斥,开关会看起来像四个独立选项。
|
|
1450
|
+
[speechMenu addItem:NSMenuItem.separatorItem];
|
|
1451
|
+
NSMenuItem *frequencyHeader = [speechMenu addItemWithTitle:@"频率" action:nil keyEquivalent:@""];
|
|
1452
|
+
frequencyHeader.enabled = NO;
|
|
1453
|
+
NSString *frequency = [NSUserDefaults.standardUserDefaults
|
|
1454
|
+
stringForKey:PetSpeechFrequencyKey] ?: @"normal";
|
|
1455
|
+
NSArray<NSDictionary *> *frequencyOptions = @[
|
|
1456
|
+
@{@"title": @"很少", @"value": @"low"},
|
|
1457
|
+
@{@"title": @"正常", @"value": @"normal"},
|
|
1458
|
+
@{@"title": @"较多", @"value": @"high"},
|
|
1459
|
+
@{@"title": @"话痨", @"value": @"chatty"},
|
|
1460
|
+
];
|
|
1461
|
+
for (NSDictionary *option in frequencyOptions) {
|
|
1462
|
+
NSMenuItem *item = [speechMenu addItemWithTitle:option[@"title"]
|
|
1463
|
+
action:@selector(setSpeechFrequency:) keyEquivalent:@""];
|
|
1464
|
+
item.target = NSApp.delegate;
|
|
1465
|
+
item.representedObject = option[@"value"];
|
|
1466
|
+
item.state = [frequency isEqualToString:option[@"value"]] ?
|
|
1467
|
+
NSControlStateValueOn : NSControlStateValueOff;
|
|
1468
|
+
item.indentationLevel = 1;
|
|
1469
|
+
}
|
|
1470
|
+
[speechMenu addItem:NSMenuItem.separatorItem];
|
|
1444
1471
|
// 没有这个入口,九成用户不会知道台词可以自己改。
|
|
1445
|
-
NSMenuItem *editPhrases = [speechMenu addItemWithTitle:@"
|
|
1472
|
+
NSMenuItem *editPhrases = [speechMenu addItemWithTitle:@"编辑台词…"
|
|
1446
1473
|
action:@selector(editPhrasesFile:) keyEquivalent:@""];
|
|
1447
1474
|
editPhrases.target = NSApp.delegate;
|
|
1448
1475
|
speechItem.submenu = speechMenu;
|
package/package.json
CHANGED
package/scripts/build.sh
CHANGED
|
@@ -51,6 +51,11 @@ if (( ${#SPRITES[@]} > 0 )); then
|
|
|
51
51
|
cp "${SPRITES[@]}" "${BUILD_DIR}/"
|
|
52
52
|
cp "${SPRITES[@]}" "${APP_RESOURCES_DIR}/"
|
|
53
53
|
fi
|
|
54
|
+
# 出厂台词。代码里不再留内置词库,这个文件就是默认台词的唯一来源,
|
|
55
|
+
# 首次启动时被拷到 ~/.cc-pets/phrases.txt。
|
|
56
|
+
# 裸二进制旁边也放一份:直接跑 .build/release/cc-pets 时没有 app bundle。
|
|
57
|
+
cp "${PROJECT_DIR}/Resources/phrases.default.txt" "${APP_RESOURCES_DIR}/phrases.default.txt"
|
|
58
|
+
cp "${PROJECT_DIR}/Resources/phrases.default.txt" "${BUILD_DIR}/phrases.default.txt"
|
|
54
59
|
cp "${BUILD_DIR}/cc-pets" "${APP_MACOS_DIR}/cc-pets"
|
|
55
60
|
cp "${PROJECT_DIR}/Resources/Info.plist" "${APP_DIR}/Contents/Info.plist"
|
|
56
61
|
cp "${PROJECT_DIR}/Resources/AppIcon.icns" "${APP_RESOURCES_DIR}/AppIcon.icns"
|