cc-pets 1.3.2 → 1.4.0

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.
@@ -0,0 +1,52 @@
1
+ #import <Foundation/Foundation.h>
2
+
3
+ // 桌宠说的话。内置词库 + 用户词库合并,按情境标签取一条,填好槽位后返回。
4
+ //
5
+ // 这一层刻意和"怎么显示""什么时候说"完全分开:取句子是一个纯函数式的接口,
6
+ // 以后换成大模型生成只是换掉这里的实现,气泡和触发逻辑一个字都不用动。
7
+
8
+ // 情境标签。**这是发布给用户的稳定契约**,用户词库按它分组,不要随版本改名,
9
+ // 否则别人写好的词库会静默失效。
10
+ extern NSString *const PetPhraseTagDone; // 任务完成
11
+ extern NSString *const PetPhraseTagFail; // 任务失败
12
+ extern NSString *const PetPhraseTagQuotaLow; // 额度告急
13
+ extern NSString *const PetPhraseTagLateNight; // 深夜
14
+ extern NSString *const PetPhraseTagLongSession; // 久坐
15
+ extern NSString *const PetPhraseTagWake; // 久别重逢
16
+ extern NSString *const PetPhraseTagIdle; // 闲着
17
+
18
+ // Agent 状态的宠物口吻文案。这些不是"额外说话",而是状态卡副行的正文来源——
19
+ // 每个 hook 状态都由宠物来讲,所以不受说话预算限制。
20
+ // 前缀 state_ 是为了和上面那组情绪标签分开:情绪句会在关键时刻顶掉常规状态文案。
21
+ extern NSString *const PetPhraseTagStateStarting;
22
+ extern NSString *const PetPhraseTagStateIdle;
23
+ extern NSString *const PetPhraseTagStateThinking;
24
+ extern NSString *const PetPhraseTagStateAutoReview;
25
+ extern NSString *const PetPhraseTagStateApproval;
26
+ extern NSString *const PetPhraseTagStateSubagent;
27
+ extern NSString *const PetPhraseTagStateTool; // 说不清是哪类工具时的兜底
28
+ extern NSString *const PetPhraseTagStateToolBash; // 执行命令
29
+ extern NSString *const PetPhraseTagStateToolEdit; // 编辑文件
30
+ extern NSString *const PetPhraseTagStateToolRead; // 查找资料
31
+ extern NSString *const PetPhraseTagStateToolDone;
32
+ extern NSString *const PetPhraseTagStateToolFailed;
33
+ extern NSString *const PetPhraseTagStateCompleted;
34
+ extern NSString *const PetPhraseTagStateFailed;
35
+ extern NSString *const PetPhraseTagStateNotification;
36
+
37
+ // 用户词库路径:~/.cc-pets/phrases.txt。可用 CC_PETS_PHRASES_FILE 覆盖(测试用)。
38
+ //
39
+ // 纯文本而不是 JSON:普通用户写不了 JSON,而且少个逗号整个文件失效、我们静默降级,
40
+ // 用户只会觉得"改了没用"。纯文本一行一句,写错只丢那一行。
41
+ NSString *PetPhrasesFilePath(void);
42
+
43
+ // 取一条填好槽位的话。取不到返回 nil(调用方应当直接不说话,而不是显示占位符)。
44
+ // slots 的键是白名单里的槽位名(不含花括号),值统一按字符串处理。
45
+ NSString *PetPhraseForTag(NSString *tag, NSDictionary<NSString *, NSString *> *slots);
46
+
47
+ // 允许出现在模板里的槽位。白名单制:没列在这里的一律原样留着,不做替换。
48
+ NSArray<NSString *> *PetPhraseSlotNames(void);
49
+
50
+ // 单条模板的长度上限(字符)。超了直接丢弃——气泡布局撑不下,
51
+ // 与其截断出半句话不如不说。
52
+ extern const NSUInteger PetPhraseMaxLength;
@@ -0,0 +1,307 @@
1
+ #import "CCPetsPhrases.h"
2
+
3
+ NSString *const PetPhraseTagDone = @"done";
4
+ NSString *const PetPhraseTagFail = @"fail";
5
+ NSString *const PetPhraseTagQuotaLow = @"quota_low";
6
+ NSString *const PetPhraseTagLateNight = @"late_night";
7
+ NSString *const PetPhraseTagLongSession = @"long_session";
8
+ NSString *const PetPhraseTagWake = @"wake";
9
+ NSString *const PetPhraseTagIdle = @"idle";
10
+
11
+ NSString *const PetPhraseTagStateStarting = @"state_starting";
12
+ NSString *const PetPhraseTagStateIdle = @"state_idle";
13
+ NSString *const PetPhraseTagStateThinking = @"state_thinking";
14
+ NSString *const PetPhraseTagStateAutoReview = @"state_auto_review";
15
+ NSString *const PetPhraseTagStateApproval = @"state_approval";
16
+ NSString *const PetPhraseTagStateSubagent = @"state_subagent";
17
+ NSString *const PetPhraseTagStateTool = @"state_tool";
18
+ NSString *const PetPhraseTagStateToolBash = @"state_tool_bash";
19
+ NSString *const PetPhraseTagStateToolEdit = @"state_tool_edit";
20
+ NSString *const PetPhraseTagStateToolRead = @"state_tool_read";
21
+ NSString *const PetPhraseTagStateToolDone = @"state_tool_done";
22
+ NSString *const PetPhraseTagStateToolFailed = @"state_tool_failed";
23
+ NSString *const PetPhraseTagStateCompleted = @"state_completed";
24
+ NSString *const PetPhraseTagStateFailed = @"state_failed";
25
+ NSString *const PetPhraseTagStateNotification = @"state_notification";
26
+
27
+ const NSUInteger PetPhraseMaxLength = 30;
28
+ // 最近说过的这么多条不再重复。模板库不大,靠这个把主观重复感压下去。
29
+ static const NSUInteger PetPhraseRecentMemory = 12;
30
+
31
+ NSArray<NSString *> *PetPhraseSlotNames(void) {
32
+ static NSArray<NSString *> *names;
33
+ static dispatch_once_t once;
34
+ dispatch_once(&once, ^{
35
+ names = @[@"quota5h", @"resetTime", @"toolName", @"sessionMin", @"failCount", @"hour"];
36
+ });
37
+ return names;
38
+ }
39
+
40
+ NSString *PetPhrasesFilePath(void) {
41
+ NSString *override = NSProcessInfo.processInfo.environment[@"CC_PETS_PHRASES_FILE"];
42
+ if (override.length > 0) return override.stringByStandardizingPath;
43
+ return [NSHomeDirectory() stringByAppendingPathComponent:@".cc-pets/phrases.txt"];
44
+ }
45
+
46
+ // 内置词库。每个情境 8 条以上,尽量带槽位——模板能填进精确的真实数据,
47
+ // 这一点恰恰是生成式给不了的:"你已经连着干了 97 分钟了"比"干挺久了"有力得多。
48
+ static NSDictionary<NSString *, NSArray<NSString *> *> *BuiltinPhrases(void) {
49
+ static NSDictionary *phrases;
50
+ static dispatch_once_t once;
51
+ dispatch_once(&once, ^{
52
+ phrases = @{
53
+ PetPhraseTagDone: @[
54
+ @"搞定了,歇会儿吧。", @"这次挺顺利的。", @"又完成一件,不错。",
55
+ @"收工,{toolName} 没闹脾气。", @"好了,接下来干嘛?", @"稳。",
56
+ @"完事了,喝口水。", @"这波很干净。", @"成了,我看着呢。"
57
+ ],
58
+ PetPhraseTagFail: @[
59
+ @"失败 {failCount} 次了,喘口气。", @"没事,再试一次。",
60
+ @"{toolName} 这货不靠谱。", @"卡住了?我陪你看看。",
61
+ @"别急,慢慢来。", @"又红了…深呼吸。", @"这个坑我也没见过。",
62
+ @"失败也是信息。", @"要不换个思路?"
63
+ ],
64
+ PetPhraseTagQuotaLow: @[
65
+ @"额度只剩 {quota5h} 了。", @"省着点用啊。",
66
+ @"快见底了,{resetTime} 才回血。", @"要不歇会儿等回血?",
67
+ @"额度告急,悠着点。", @"{quota5h}…有点悬。",
68
+ @"再跑就没了。", @"离 {resetTime} 还早呢。"
69
+ ],
70
+ PetPhraseTagLateNight: @[
71
+ @"都 {hour} 点了。", @"该睡了吧?", @"这个点还在写…",
72
+ @"明天再改也不迟。", @"夜深了,注意身体。",
73
+ @"{hour} 点的代码不太可信。", @"熬夜写的明天要重写的。",
74
+ @"我先困了,你随意。"
75
+ ],
76
+ PetPhraseTagLongSession: @[
77
+ @"连着干 {sessionMin} 分钟了。", @"起来走两步吧。",
78
+ @"{sessionMin} 分钟了,眼睛歇歇。", @"坐太久了。",
79
+ @"要不伸个懒腰?", @"该补水了。", @"歇五分钟不亏。",
80
+ @"腰还好吗?"
81
+ ],
82
+ PetPhraseTagWake: @[
83
+ @"你回来啦。", @"好久不见。", @"等你半天了。",
84
+ @"来活了?", @"我还以为你不来了。", @"醒了醒了。",
85
+ @"开工吧。", @"哟。"
86
+ ],
87
+ // Agent 状态的宠物口吻文案。标题仍然给事实("Claude · 正在编辑文件"),
88
+ // 这里给的是旁边那句话,所以不必重复状态本身,只说宠物的观察和态度。
89
+ PetPhraseTagStateStarting: @[
90
+ @"正在把它叫醒…", @"它还在穿鞋,稍等。", @"马上就好。", @"它爬起来了。"
91
+ ],
92
+ PetPhraseTagStateIdle: @[
93
+ @"它准备好了,你说吧。", @"安静着呢,随时开工。",
94
+ @"就位了,等你发话。", @"我们都听着。"
95
+ ],
96
+ PetPhraseTagStateThinking: @[
97
+ @"它在想,我在旁边看着。", @"让它想想,别催。",
98
+ @"脑子转起来了。", @"这题它得琢磨会儿。", @"安静,它在思考。"
99
+ ],
100
+ PetPhraseTagStateAutoReview: @[
101
+ @"有人替你把关了。", @"自动审着呢,不用管。", @"这轮它自己会查。"
102
+ ],
103
+ PetPhraseTagStateApproval: @[
104
+ @"它等你点头。", @"卡住了,回终端看一眼。",
105
+ @"要你拿主意。", @"不敢乱动,等你发话。"
106
+ ],
107
+ PetPhraseTagStateSubagent: @[
108
+ @"它喊帮手了。", @"派了个小弟出去。", @"这活儿分出去了。"
109
+ ],
110
+ PetPhraseTagStateTool: @[
111
+ @"它动手了。", @"在翻工具箱。", @"忙起来了。"
112
+ ],
113
+ PetPhraseTagStateToolBash: @[
114
+ @"在敲命令了。", @"终端里忙着呢。", @"这条命令我看着。"
115
+ ],
116
+ PetPhraseTagStateToolEdit: @[
117
+ @"在改文件,别手抖。", @"动笔了。", @"文件正在被折腾。"
118
+ ],
119
+ PetPhraseTagStateToolRead: @[
120
+ @"到处翻资料呢。", @"在找线索。", @"翻书中…"
121
+ ],
122
+ PetPhraseTagStateToolDone: @[
123
+ @"这一步成了。", @"搞定一个。", @"稳住了。"
124
+ ],
125
+ PetPhraseTagStateToolFailed: @[
126
+ @"这步崴了。", @"工具闹脾气了。", @"它好像有点坏了。。。"
127
+ ],
128
+ PetPhraseTagStateCompleted: @[
129
+ @"干完了,歇会儿。", @"收工!", @"这轮结束了。", @"齐活。"
130
+ ],
131
+ PetPhraseTagStateFailed: @[
132
+ @"翻车了,回终端看看。", @"没成,去瞧瞧。", @"这次栽了。"
133
+ ],
134
+ PetPhraseTagStateNotification: @[
135
+ @"有点事找你聊聊。", @"抬头看一眼。", @"这儿有情况。"
136
+ ],
137
+ PetPhraseTagIdle: @[
138
+ @"发会儿呆。", @"我来啦!!!", @"随时叫我。",
139
+ @"今天挺安静。", @"我就看看。", @"要不要蹬会儿?",
140
+ @"闲着也是闲着。", @"嗯…"
141
+ ]
142
+ };
143
+ });
144
+ return phrases;
145
+ }
146
+
147
+ // 去掉行尾注释。
148
+ //
149
+ // 必须支持行尾注释,否则模板里 "[state_subagent] # 子 Agent 工作中" 这样的小节名
150
+ // 会因为结尾不是 ] 而不被识别,它下面的台词全部无处安放被丢掉——整个文件静默失效,
151
+ // 用户取消注释后什么都不会发生,连报错都没有。
152
+ //
153
+ // 只在 # 前面是空白(或位于行首)时才截断:这样台词里写 "进度#1" 之类不会被误伤。
154
+ static NSString *PetLineWithoutTrailingComment(NSString *line) {
155
+ if ([line hasPrefix:@"#"]) return @"";
156
+ NSRange search = NSMakeRange(0, line.length);
157
+ while (search.length > 0) {
158
+ NSRange hash = [line rangeOfString:@"#" options:0 range:search];
159
+ if (hash.location == NSNotFound) break;
160
+ unichar previous = [line characterAtIndex:hash.location - 1];
161
+ if (previous == ' ' || previous == '\t') {
162
+ return [[line substringToIndex:hash.location] stringByTrimmingCharactersInSet:
163
+ NSCharacterSet.whitespaceCharacterSet];
164
+ }
165
+ NSUInteger next = hash.location + 1;
166
+ search = NSMakeRange(next, line.length - next);
167
+ }
168
+ return line;
169
+ }
170
+
171
+ // 用户词库:~/.cc-pets/phrases.txt
172
+ //
173
+ // # 井号开头是注释
174
+ // [fail]
175
+ // 又挂了?我陪你看看。
176
+ // [done]
177
+ // 收工!
178
+ //
179
+ // 逐行解析,容错到底:不认识的小节名跳过、空行跳过、超长的丢掉,
180
+ // 单行有问题绝不影响其他行。这是选纯文本而不是 JSON 的全部理由。
181
+ //
182
+ // 首行写 replace 可以完全顶掉内置词条;缺省是和内置合并。
183
+ static NSDictionary *LoadUserPhrases(void) {
184
+ static NSDictionary *cached;
185
+ static NSDate *cachedStamp;
186
+ static NSString *cachedPath;
187
+
188
+ NSString *path = PetPhrasesFilePath();
189
+ NSDictionary *attributes = [NSFileManager.defaultManager attributesOfItemAtPath:path error:nil];
190
+ NSDate *stamp = attributes[NSFileModificationDate];
191
+ // 比对 mtime 做热加载:取句子本来就低频,这点开销可以忽略,
192
+ // 换来的是用户改完存盘立刻生效,不用重启桌宠。
193
+ if (stamp && cachedStamp && [stamp isEqualToDate:cachedStamp] &&
194
+ [path isEqualToString:cachedPath]) {
195
+ return cached;
196
+ }
197
+ cachedPath = path;
198
+ cachedStamp = stamp;
199
+ cached = nil;
200
+ if (!stamp) return nil;
201
+
202
+ NSString *text = [NSString stringWithContentsOfFile:path
203
+ encoding:NSUTF8StringEncoding error:nil];
204
+ if (text.length == 0) return nil;
205
+
206
+ NSMutableDictionary<NSString *, NSMutableArray<NSString *> *> *groups =
207
+ [NSMutableDictionary dictionary];
208
+ BOOL replace = NO;
209
+ NSString *current = nil;
210
+ for (NSString *rawLine in [text componentsSeparatedByCharactersInSet:
211
+ NSCharacterSet.newlineCharacterSet]) {
212
+ NSString *line = [rawLine stringByTrimmingCharactersInSet:
213
+ NSCharacterSet.whitespaceCharacterSet];
214
+ line = PetLineWithoutTrailingComment(line);
215
+ if (line.length == 0) continue;
216
+ if ([line isEqualToString:@"replace"]) { replace = YES; continue; }
217
+ if ([line hasPrefix:@"["] && [line hasSuffix:@"]"]) {
218
+ current = [line substringWithRange:NSMakeRange(1, line.length - 2)];
219
+ current = [current stringByTrimmingCharactersInSet:
220
+ NSCharacterSet.whitespaceCharacterSet];
221
+ continue;
222
+ }
223
+ // 还没进任何小节的散句无处安放,直接跳过,不猜用户想放哪。
224
+ if (current.length == 0) continue;
225
+ if (line.length > PetPhraseMaxLength) continue;
226
+ NSMutableArray *bucket = groups[current];
227
+ if (!bucket) { bucket = [NSMutableArray array]; groups[current] = bucket; }
228
+ [bucket addObject:line];
229
+ }
230
+ if (groups.count == 0 && !replace) return nil;
231
+ cached = @{@"mode": replace ? @"replace" : @"merge", @"phrases": groups};
232
+ return cached;
233
+ }
234
+
235
+ // 模板本身是数据,不是格式串。**绝不能把它交给 stringWithFormat:**——
236
+ // 用户词库里一个 %@ 就会让桌宠当场崩溃。这里只做白名单内的字面量替换。
237
+ static NSString *FillSlots(NSString *template, NSDictionary<NSString *, NSString *> *slots) {
238
+ NSMutableString *result = [template mutableCopy];
239
+ for (NSString *name in PetPhraseSlotNames()) {
240
+ NSString *token = [NSString stringWithFormat:@"{%@}", name];
241
+ if ([result rangeOfString:token].location == NSNotFound) continue;
242
+ NSString *value = slots[name];
243
+ // 该填的槽位没给值:整条作废。宁可不说,也不能吐出 "额度只剩 {quota5h} 了"。
244
+ if (value.length == 0) return nil;
245
+ [result replaceOccurrencesOfString:token withString:value
246
+ options:NSLiteralSearch range:NSMakeRange(0, result.length)];
247
+ }
248
+ return result;
249
+ }
250
+
251
+ // 合并候选:内置 + 用户。replace 模式下只要用户的。
252
+ static NSArray<NSString *> *CandidatesForTag(NSString *tag) {
253
+ NSDictionary *user = LoadUserPhrases();
254
+ BOOL replace = [user[@"mode"] isKindOfClass:NSString.class] &&
255
+ [user[@"mode"] isEqualToString:@"replace"];
256
+ NSMutableArray<NSString *> *result = [NSMutableArray array];
257
+ if (!replace) {
258
+ NSArray *builtin = BuiltinPhrases()[tag];
259
+ if (builtin) [result addObjectsFromArray:builtin];
260
+ }
261
+ id userPhrases = user[@"phrases"];
262
+ if ([userPhrases isKindOfClass:NSDictionary.class]) {
263
+ id entries = ((NSDictionary *)userPhrases)[tag];
264
+ if ([entries isKindOfClass:NSArray.class]) {
265
+ for (id entry in (NSArray *)entries) {
266
+ // 解析时已逐行校验过,这里再挡一道,防止将来换了来源出岔子。
267
+ if (![entry isKindOfClass:NSString.class]) continue;
268
+ NSString *text = entry;
269
+ if (text.length == 0 || text.length > PetPhraseMaxLength) continue;
270
+ [result addObject:text];
271
+ }
272
+ }
273
+ }
274
+ return result;
275
+ }
276
+
277
+ NSString *PetPhraseForTag(NSString *tag, NSDictionary<NSString *, NSString *> *slots) {
278
+ if (tag.length == 0) return nil;
279
+ NSArray<NSString *> *candidates = CandidatesForTag(tag);
280
+ if (candidates.count == 0) return nil;
281
+
282
+ // 最近说过的先排除。全被排除掉时(词条太少)就放开限制,总比不说话强。
283
+ static NSMutableArray<NSString *> *recent;
284
+ static dispatch_once_t once;
285
+ dispatch_once(&once, ^{ recent = [NSMutableArray array]; });
286
+
287
+ NSMutableArray<NSString *> *pool = [NSMutableArray array];
288
+ for (NSString *text in candidates) {
289
+ if (![recent containsObject:text]) [pool addObject:text];
290
+ }
291
+ if (pool.count == 0) [pool addObjectsFromArray:candidates];
292
+
293
+ // 槽位填不上的条目要跳过,所以随机打乱后依次试,而不是一把梭。
294
+ NSMutableArray<NSString *> *shuffled = [pool mutableCopy];
295
+ for (NSUInteger index = shuffled.count; index > 1; index--) {
296
+ NSUInteger swap = arc4random_uniform((uint32_t)index);
297
+ [shuffled exchangeObjectAtIndex:index - 1 withObjectAtIndex:swap];
298
+ }
299
+ for (NSString *template in shuffled) {
300
+ NSString *text = FillSlots(template, slots);
301
+ if (text.length == 0) continue;
302
+ [recent addObject:template];
303
+ while (recent.count > PetPhraseRecentMemory) [recent removeObjectAtIndex:0];
304
+ return text;
305
+ }
306
+ return nil;
307
+ }
@@ -4,3 +4,6 @@
4
4
  int RenderQuotaDashboard(NSString *path);
5
5
  int RenderAgentStatusCard(NSString *path);
6
6
  int RenderMenuSwitches(NSString *path);
7
+ // 把 PetView 的每一格(rowCount 行 × 8 帧)逐格渲染拼成一张大图,用于渲染改造的
8
+ // 逐字节等价校验。上面三个命令一格宠物都不画,改坏 PetView 它们照样一致。
9
+ int RenderPetSheet(NSString *path, NSString *sheetPath, NSInteger rowCount);
@@ -2,6 +2,8 @@
2
2
  #import "QuotaDashboardView.h"
3
3
  #import "MenuToggleSwitch.h"
4
4
  #import "CCPetsUsage.h"
5
+ #import "PetView.h"
6
+ #import "CCPetsImageLoader.h"
5
7
  #import <Cocoa/Cocoa.h>
6
8
 
7
9
  int RenderQuotaDashboard(NSString *path) {
@@ -199,3 +201,61 @@ int RenderMenuSwitches(NSString *path) {
199
201
  NSData *png = [bitmap representationUsingType:NSBitmapImageFileTypePNG properties:@{}];
200
202
  return [png writeToFile:path atomically:YES] ? EXIT_SUCCESS : EXIT_FAILURE;
201
203
  }
204
+
205
+ // 逐格渲染 PetView,用于 M1 渲染层改造的等价校验。
206
+ // 尺寸取运行时真实值:视图 144×150(AppDelegate 建 PetView 用的就是这个),
207
+ // 素材按 NSMakeSize(140, 150) 装载——两处任何一个写错,改造后的图都会对不上。
208
+ // 走 cacheDisplayInRect: 而不是 displayRectIgnoringOpacity:,因为改成 layer-backed
209
+ // 之后内容在 layer 里,后者抓不到。宿主窗口是必需的:无窗口的视图 cacheDisplay 行为
210
+ // 不保证。
211
+ int RenderPetSheet(NSString *path, NSString *sheetPath, NSInteger rowCount) {
212
+ [NSApplication sharedApplication];
213
+ if (rowCount <= 0) rowCount = 9;
214
+ NSImage *sheet = LoadPetSpriteImage(sheetPath, NSMakeSize(140, 150), rowCount);
215
+ if (!sheet) {
216
+ fprintf(stderr, "无法装载素材: %s\n", sheetPath.UTF8String);
217
+ return EXIT_FAILURE;
218
+ }
219
+
220
+ const NSInteger columns = 8;
221
+ NSRect cellFrame = NSMakeRect(0, 0, 144, 150);
222
+ PetView *view = [[PetView alloc] initWithFrame:cellFrame sheet:sheet rowCount:rowCount];
223
+ view.animationSuspended = YES;
224
+
225
+ NSWindow *host = [[NSWindow alloc] initWithContentRect:cellFrame
226
+ styleMask:NSWindowStyleMaskBorderless backing:NSBackingStoreBuffered defer:NO];
227
+ host.contentView = view;
228
+
229
+ NSSize size = NSMakeSize(NSWidth(cellFrame) * columns, NSHeight(cellFrame) * rowCount);
230
+ NSBitmapImageRep *canvas = [[NSBitmapImageRep alloc]
231
+ initWithBitmapDataPlanes:NULL pixelsWide:(NSInteger)size.width pixelsHigh:(NSInteger)size.height
232
+ bitsPerSample:8 samplesPerPixel:4 hasAlpha:YES isPlanar:NO
233
+ colorSpaceName:NSDeviceRGBColorSpace bytesPerRow:0 bitsPerPixel:0];
234
+ canvas.size = size;
235
+ NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithBitmapImageRep:canvas];
236
+ [NSGraphicsContext saveGraphicsState];
237
+ NSGraphicsContext.currentContext = context;
238
+
239
+ for (NSInteger row = 0; row < rowCount; row++) {
240
+ for (NSInteger frame = 0; frame < columns; frame++) {
241
+ view.rowIndex = row;
242
+ view.frameIndex = frame;
243
+ view.needsDisplay = YES;
244
+ [view displayIfNeeded];
245
+
246
+ NSBitmapImageRep *cell = [view bitmapImageRepForCachingDisplayInRect:view.bounds];
247
+ if (!cell) continue;
248
+ [view cacheDisplayInRect:view.bounds toBitmapImageRep:cell];
249
+ // 行 0 画在最上面,和素材表本身的排列一致,肉眼比对时不用换算。
250
+ NSPoint origin = NSMakePoint(frame * NSWidth(cellFrame),
251
+ size.height - (row + 1) * NSHeight(cellFrame));
252
+ [cell drawInRect:NSMakeRect(origin.x, origin.y, NSWidth(cellFrame), NSHeight(cellFrame))
253
+ fromRect:NSZeroRect operation:NSCompositingOperationSourceOver fraction:1.0
254
+ respectFlipped:NO hints:@{NSImageHintInterpolation: @(NSImageInterpolationNone)}];
255
+ }
256
+ }
257
+
258
+ [NSGraphicsContext restoreGraphicsState];
259
+ NSData *png = [canvas representationUsingType:NSBitmapImageFileTypePNG properties:@{}];
260
+ return [png writeToFile:path atomically:YES] ? EXIT_SUCCESS : EXIT_FAILURE;
261
+ }
@@ -1,12 +1,17 @@
1
1
  #import <Cocoa/Cocoa.h>
2
2
 
3
3
  @interface PetView : NSView <NSMenuDelegate>
4
- @property NSImage *sheet;
5
- @property NSInteger spriteRowCount;
4
+ @property(nonatomic) NSImage *sheet;
5
+ @property(nonatomic) NSInteger spriteRowCount;
6
6
  @property(nonatomic) NSInteger lookDirection;
7
- @property NSInteger frameIndex;
8
- @property NSInteger rowIndex;
7
+ @property(nonatomic) NSInteger frameIndex;
8
+ @property(nonatomic) NSInteger rowIndex;
9
9
  @property NSArray<NSNumber *> *frames;
10
+ // 与 frames 等长,每项是该帧要停留几个动画节拍。全 1 就是原来的匀速播放。
11
+ @property NSArray<NSNumber *> *frameHolds;
12
+ // 与 frames 等长的行号。为 nil 时整段都用 rowIndex;转头要横跨 row 9/10 两行,
13
+ // 只有逐帧带行号才播得出来。
14
+ @property NSArray<NSNumber *> *frameRows;
10
15
  @property NSUInteger sequenceIndex;
11
16
  @property BOOL oneShot;
12
17
  @property BOOL draggingPet;