-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractive_cli.py
More file actions
1227 lines (1002 loc) · 41.7 KB
/
Copy pathinteractive_cli.py
File metadata and controls
1227 lines (1002 loc) · 41.7 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
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
交互系统 CLI 入口 - 带GUI视频播放功能
运行角色一天的交互事件流程,支持图形界面观看视频剧情
使用方法:
# 交互式运行(带GUI)
python interactive_cli.py luna_002 2026-01-17 --gui
# 使用预设选择运行(带GUI)
python interactive_cli.py luna_002 2026-01-17 --gui --preset '{"09:00-11:00": ["A"], "17:00-19:00": ["B"], "19:00-21:00": ["A", "A", "A"]}'
# 纯CLI模式(不带GUI)
python interactive_cli.py luna_002 2026-01-17
# 指定数据目录
python interactive_cli.py luna_002 2026-01-17 --data-dir ./my_data
"""
import argparse
import json
import sys
import os
from pathlib import Path
from typing import Optional, Dict, List, Tuple
import threading
import queue
# 添加项目路径
sys.path.insert(0, str(Path(__file__).parent))
from src.core.interactive_session import (
InteractiveSession,
Event,
Choice,
Resolution,
Phase
)
# GUI相关导入
try:
import tkinter as tk
from tkinter import ttk, messagebox
from PIL import Image, ImageTk
import cv2
GUI_AVAILABLE = True
except ImportError:
GUI_AVAILABLE = False
print("警告: GUI功能需要安装以下依赖:")
print(" pip install opencv-python pillow")
print(" 将使用纯CLI模式运行")
# ==================== 视频文件映射器 ====================
class VideoMapper:
"""
视频文件映射器 - 根据时间槽和事件类型查找对应的视频文件
新的命名格式 (严格参照name.txt):
- N事件: 时间槽_N_事件序号_事件名
例如: 01-00-03-00_N_07_DreamingoftheStage
- R/SR事件: 时间槽_事件类型_事件序号_场景序号_场景类型_中文标题_事件名
例如: 09-00-11-00_R_01_001_前置剧情_便利店的意外_AClumsyEncounter
"""
def __init__(self, performance_dir: str):
"""
初始化视频映射器
Args:
performance_dir: 性能数据目录,如 data/performance/luna_002_2026-01-17
"""
self.performance_dir = Path(performance_dir)
self.video_map: Dict[str, List[Path]] = {}
self._scan_videos()
def _scan_videos(self):
"""扫描性能目录中的所有视频文件"""
if not self.performance_dir.exists():
print(f"[Backend] 警告: 性能目录不存在: {self.performance_dir}")
return
# 扫描所有.mp4文件
for video_file in self.performance_dir.glob("*.mp4"):
# 新格式: 时间槽_事件类型_事件序号_场景序号_场景类型_中文标题_事件名
# 例如: 01-00-03-00_N_07_DreamingoftheStage
# 09-00-11-00_R_01_001_前置剧情_便利店的意外_AClumsyEncounter
parts = video_file.stem.split('_')
if len(parts) >= 3:
time_slot_part = parts[0] # 01-00-03-00
event_type = parts[1] # N, R, SR
# 转换时间槽格式: 01-00-03-00 -> 01:00-03:00
try:
time_parts = time_slot_part.split('-')
if len(time_parts) == 4:
time_slot = f"{time_parts[0]}:{time_parts[1]}-{time_parts[2]}:{time_parts[3]}"
key = f"{time_slot}_{event_type}"
if key not in self.video_map:
self.video_map[key] = []
self.video_map[key].append(video_file)
except (ValueError, IndexError):
continue
# 对每个key的视频进行排序(按场景序号)
def get_sort_key(video_path: Path) -> int:
"""从文件名中提取排序用的数字"""
parts = video_path.stem.split('_')
event_type = parts[1] if len(parts) > 1 else ""
# R/SR事件: 场景序号在parts[3]
if event_type in ['R', 'SR']:
if len(parts) > 3 and parts[3].isdigit():
return int(parts[3])
# N事件: 事件序号在parts[2]
elif event_type == 'N':
if len(parts) > 2 and parts[2].isdigit():
return int(parts[2])
return 0
for key in self.video_map:
self.video_map[key].sort(key=get_sort_key)
print(f"[Backend] 扫描到 {sum(len(v) for v in self.video_map.values())} 个视频文件")
def get_videos(self, time_slot: str, event_type: str) -> List[Path]:
"""
获取指定时间槽和事件类型的视频列表
Args:
time_slot: 时间槽,如 "01:00-03:00"
event_type: 事件类型,如 "N", "R", "SR"
Returns:
视频文件路径列表
"""
key = f"{time_slot}_{event_type}"
return self.video_map.get(key, [])
def get_videos_for_path(self, time_slot: str, event_type: str,
choice_path: List[str] = None) -> List[Path]:
"""
根据选择路径获取对应的视频列表
新格式: 时间槽_事件类型_事件序号_场景序号_场景类型_中文标题_事件名
例如: 09-00-11-00_R_01_001_前置剧情_便利店的意外_AClumsyEncounter
Args:
time_slot: 时间槽,如 "09:00-11:00"
event_type: 事件类型,如 "R", "SR"
choice_path: 选择路径,如 ["A"] for R event or ["A", "A", "A"] for SR event
Returns:
应该播放的视频路径列表
"""
all_videos = self.get_videos(time_slot, event_type)
if event_type == "N":
# N事件直接返回所有视频(通常只有一个)
return all_videos
if not choice_path:
# 没有选择路径时,返回前置剧情和叙事段落的视频
return [v for v in all_videos if any(
keyword in v.stem for keyword in ["前置剧情", "叙事段落", "Prologue", "Narrative"]
)]
# 对于R/SR事件,根据选择路径筛选视频
result = []
# 添加前置剧情和叙事段落
for video in all_videos:
stem = video.stem
if any(keyword in stem for keyword in ["前置剧情", "叙事段落", "Prologue", "Narrative"]):
result.append(video)
# 根据选择路径添加分支视频
if event_type == "R" and choice_path:
# R事件:只有一个选择
choice = choice_path[0]
for video in all_videos:
stem = video.stem
# 新格式使用下划线: 分支1_A, 分支1_A_Part1
# 兼容旧格式: 分支1-A, Branch-A
if f"分支1_{choice}" in stem or f"分支1-{choice}" in stem or f"branch_{choice}" in stem.lower() or f"branch-{choice}" in stem.lower():
result.append(video)
# 查找结局视频
if "结局" in stem or "ending" in stem.lower():
# 根据选择判断是good还是bad ending
if choice == "A" and ("good" in stem.lower() or "好" in stem):
result.append(video)
elif choice == "B" and ("bad" in stem.lower() or "坏" in stem):
result.append(video)
elif event_type == "SR" and len(choice_path) >= 1:
# SR事件:多个阶段的选择
# 第一阶段选择
choice1 = choice_path[0]
for video in all_videos:
stem = video.stem
# 新格式: 分支1_A, 分支1_A_Part1
if f"分支1_{choice1}" in stem or f"分支1-{choice1}" in stem or f"branch1_{choice1}" in stem.lower() or f"branch1-{choice1}" in stem.lower():
result.append(video)
# 第二阶段选择(如果有)
if len(choice_path) >= 2:
choice2 = choice_path[1]
for video in all_videos:
stem = video.stem
if f"分支2_{choice2}" in stem or f"分支2-{choice2}" in stem or f"branch2_{choice2}" in stem.lower() or f"branch2-{choice2}" in stem.lower():
result.append(video)
# 第三阶段选择(如果有)
if len(choice_path) >= 3:
choice3 = choice_path[2]
for video in all_videos:
stem = video.stem
if f"分支3_{choice3}" in stem or f"分支3-{choice3}" in stem or f"branch3_{choice3}" in stem.lower() or f"branch3-{choice3}" in stem.lower():
result.append(video)
# 结局视频
path_str = "-".join(choice_path)
for video in all_videos:
stem = video.stem
if "结局" in stem or "ending" in stem.lower():
# 根据路径判断是哪个结局
if "ending_a" in stem.lower() and path_str.endswith("A"):
result.append(video)
elif "ending_b" in stem.lower() and path_str.endswith("B"):
result.append(video)
elif "ending_c" in stem.lower() and path_str.endswith("C"):
result.append(video)
return result
def get_video_count(self, time_slot: str, event_type: str) -> int:
"""获取指定时间槽和事件类型的视频数量"""
return len(self.get_videos(time_slot, event_type))
# ==================== GUI视频播放器 ====================
class VideoPlayerGUI:
"""GUI视频播放器 - 支持视频播放、暂停、重播、跳过"""
def __init__(self, master, video_mapper: VideoMapper, log_queue: queue.Queue):
"""
初始化GUI视频播放器
Args:
master: Tkinter根窗口
video_mapper: 视频映射器
log_queue: 日志队列(用于将GUI操作传递到后台)
"""
self.master = master
self.video_mapper = video_mapper
self.log_queue = log_queue
self.current_videos: List[Path] = []
self.current_video_index = 0
self.is_playing = False
self.is_paused = False
self.video_capture = None
self.playback_thread = None
self.stop_playback = threading.Event()
# 当前事件信息
self.current_event_time_slot = ""
self.current_event_type = ""
self.current_event_name = ""
# R/SR事件选项
self.current_choices: List[Choice] = []
self.on_choice_callback = None
self._setup_ui()
def _setup_ui(self):
"""设置GUI界面"""
# 设置窗口标题
self.master.title("Interactive Film Character Daily Agent - 视频剧情播放器")
self.master.geometry("1000x700")
# 创建主框架
main_frame = ttk.Frame(self.master)
main_frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
# 标题区域
title_frame = ttk.Frame(main_frame)
title_frame.pack(fill=tk.X, pady=(0, 10))
self.title_label = ttk.Label(
title_frame,
text="欢迎来到 Interactive Film Character Daily Agent",
font=('Arial', 16, 'bold')
)
self.title_label.pack()
self.event_info_label = ttk.Label(
title_frame,
text="等待开始...",
font=('Arial', 12)
)
self.event_info_label.pack()
# 视频播放区域
video_frame = ttk.Frame(main_frame)
video_frame.pack(fill=tk.BOTH, expand=True)
self.video_canvas = tk.Canvas(
video_frame,
bg='black',
width=800,
height=450
)
self.video_canvas.pack(fill=tk.BOTH, expand=True)
# 显示视频结束时的占位信息
self.video_canvas.create_text(
400, 225,
text="等待播放视频...",
fill='white',
font=('Arial', 14)
)
# 视频控制按钮区域
control_frame = ttk.Frame(main_frame)
control_frame.pack(fill=tk.X, pady=(10, 0))
self.play_pause_btn = ttk.Button(
control_frame,
text="播放",
command=self.toggle_play_pause,
state=tk.DISABLED
)
self.play_pause_btn.pack(side=tk.LEFT, padx=5)
self.replay_btn = ttk.Button(
control_frame,
text="重播",
command=self.replay_video,
state=tk.DISABLED
)
self.replay_btn.pack(side=tk.LEFT, padx=5)
self.skip_btn = ttk.Button(
control_frame,
text="跳过",
command=self.skip_video,
state=tk.DISABLED
)
self.skip_btn.pack(side=tk.LEFT, padx=5)
# 进度条
self.progress_var = tk.DoubleVar()
self.progress_bar = ttk.Progressbar(
control_frame,
variable=self.progress_var,
maximum=100,
length=300
)
self.progress_bar.pack(side=tk.LEFT, padx=20)
# R/SR事件选项区域
self.options_frame = ttk.LabelFrame(main_frame, text="剧情选项")
self.options_frame.pack(fill=tk.X, pady=(10, 0))
self.options_frame.pack_forget() # 初始隐藏
# 继续按钮区域(用于N事件等自动继续的情况)
self.continue_frame = ttk.Frame(main_frame)
# 状态栏
status_frame = ttk.Frame(main_frame)
status_frame.pack(fill=tk.X, pady=(5, 0))
self.status_label = ttk.Label(
status_frame,
text="就绪",
relief=tk.SUNKEN
)
self.status_label.pack(fill=tk.X)
# 继续按钮的回调
self.on_continue_callback = None
def play_event(self, time_slot: str, event_type: str, event_name: str,
choices: List[Choice] = None,
on_choice_callback = None,
choice_path: List[str] = None,
on_continue_callback = None):
"""
播放指定事件的视频
Args:
time_slot: 时间槽
event_type: 事件类型
event_name: 事件名称
choices: R/SR事件的选项列表
on_choice_callback: 选择回调函数
choice_path: 选择路径(用于筛选视频)
on_continue_callback: 继续下一个事件的回调函数
"""
# 停止当前播放
self._stop_current_playback()
# 清除继续按钮
for widget in self.continue_frame.winfo_children():
widget.destroy()
self.continue_frame.pack_forget()
self.options_frame.pack_forget()
# 保存事件信息
self.current_event_time_slot = time_slot
self.current_event_type = event_type
self.current_event_name = event_name
self.current_choices = choices or []
self.on_choice_callback = on_choice_callback
self.on_continue_callback = on_continue_callback
self.current_choice_path = choice_path
# 获取视频列表(根据选择路径筛选)
if choice_path:
self.current_videos = self.video_mapper.get_videos_for_path(
time_slot, event_type, choice_path
)
else:
self.current_videos = self.video_mapper.get_videos(time_slot, event_type)
self.current_video_index = 0
# 更新UI
event_type_text = {"N": "普通事件", "R": "R事件(剧情分支)", "SR": "SR事件(重要剧情)"}.get(event_type, event_type)
self.title_label.config(text=f"{time_slot} - {event_name}")
self.event_info_label.config(text=f"{event_type_text}")
# 显示日志
print(f"\n{'─'*60}")
print(f"⏰ {time_slot} | {event_name}")
print(f"{'─'*60}")
print(f"[GUI] 开始播放事件: {event_name} ({event_type})")
if choice_path:
print(f"[GUI] 选择路径: {'-'.join(choice_path)}")
print(f"[GUI] 找到 {len(self.current_videos)} 个视频片段")
if not self.current_videos:
print(f"[GUI] 警告: 没有找到视频文件")
self.video_canvas.create_text(
400, 225,
text=f"没有找到视频\n({event_name})",
fill='white',
font=('Arial', 14)
)
# 没有视频时,如果有选项,直接显示选项
if self.current_choices:
self._show_choices()
elif on_continue_callback:
# 没有视频也没有选项,直接继续
self._show_continue_button()
return
# 开始播放第一个视频
self._play_current_video()
def _play_current_video(self):
"""播放当前视频"""
if self.current_video_index >= len(self.current_videos):
# 所有视频播放完毕
self._on_all_videos_finished()
return
video_path = self.current_videos[self.current_video_index]
print(f"[GUI] 播放视频 {self.current_video_index + 1}/{len(self.current_videos)}: {video_path.name}")
# 启用控制按钮
self.play_pause_btn.config(state=tk.NORMAL, text="暂停")
self.replay_btn.config(state=tk.NORMAL)
self.skip_btn.config(state=tk.NORMAL)
# 使用OpenCV播放视频
self.is_playing = True
self.is_paused = False
self.stop_playback.clear()
# 在新线程中播放视频,避免阻塞GUI
self.playback_thread = threading.Thread(
target=self._play_video_cv2,
args=(str(video_path),),
daemon=True
)
self.playback_thread.start()
def _play_video_cv2(self, video_path: str):
"""使用OpenCV播放视频(在独立线程中运行)"""
cap = cv2.VideoCapture(video_path)
self.video_capture = cap
if not cap.isOpened():
print(f"[GUI] 错误: 无法打开视频 {video_path}")
self.log_queue.put(("video_error", video_path))
return
# 获取视频信息
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
fps = cap.get(cv2.CAP_PROP_FPS) or 30
print(f"[GUI] 视频信息: 总帧数={total_frames}, FPS={fps}")
frame_count = 0
while not self.stop_playback.is_set():
if self.is_paused:
self.master.update()
continue
ret, frame = cap.read()
if not ret:
break
frame_count += 1
# 更新进度
if total_frames > 0:
progress = (frame_count / total_frames) * 100
self.log_queue.put(("progress", progress))
# 转换颜色空间并显示
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame_resized = cv2.resize(frame_rgb, (800, 450))
# 在主线程中更新显示
self.master.after(0, self._update_frame, frame_resized)
# 控制播放速度
cv2.waitKey(int(1000 / fps))
cap.release()
self.video_capture = None
if not self.stop_playback.is_set():
# 视频正常结束
self.log_queue.put(("video_finished", None))
def _update_frame(self, frame):
"""更新视频帧显示(在主线程中调用)"""
# 将OpenCV图像转换为Tkinter可显示的格式
image = Image.fromarray(frame)
photo = ImageTk.PhotoImage(image)
# 清除画布并显示新帧
self.video_canvas.delete("all")
self.video_canvas.create_image(400, 225, image=photo)
# 保持引用,防止被垃圾回收
self.video_canvas.image = photo
def toggle_play_pause(self):
"""切换播放/暂停"""
if self.is_playing:
self.is_paused = not self.is_paused
self.play_pause_btn.config(text="播放" if self.is_paused else "暂停")
print(f"[GUI] {'暂停' if self.is_paused else '继续'}播放")
def replay_video(self):
"""重播当前视频"""
print("[GUI] 重播当前视频")
self._stop_current_playback()
self._play_current_video()
def skip_video(self):
"""跳过当前视频"""
print("[GUI] 跳过当前视频")
self._stop_current_playback()
self._play_next_or_finish()
def _play_next_or_finish(self):
"""播放下一个视频或完成"""
self.current_video_index += 1
if self.current_video_index < len(self.current_videos):
self._play_current_video()
else:
self._on_all_videos_finished()
def _on_all_videos_finished(self):
"""所有视频播放完毕"""
print("[GUI] 所有视频播放完毕")
# 禁用控制按钮
self.play_pause_btn.config(state=tk.DISABLED)
self.replay_btn.config(state=tk.DISABLED)
self.skip_btn.config(state=tk.DISABLED)
# 清除画布
self.video_canvas.create_text(
400, 225,
text="视频播放完毕\n请选择剧情选项(如有)",
fill='white',
font=('Arial', 14)
)
# 如果有选项,显示选项
if self.current_choices:
self._show_choices()
elif self.on_continue_callback:
# 有继续回调(R/SR事件的分支视频播放完毕)
# 对于R事件,我们需要应用结果
if self.current_event_type == "R" and hasattr(self, 'current_choice_path') and self.current_choice_path:
# 调用一个特殊回调来处理R事件结果
# 这个回调应该由GUISessionRunner设置
if hasattr(self, 'on_branch_videos_finished') and self.on_branch_videos_finished:
self.on_branch_videos_finished()
else:
self._show_continue_button()
else:
self._show_continue_button()
else:
# 没有选项和回调,显示继续按钮(会被忽略)
self._show_continue_button()
def _show_choices(self):
"""显示R/SR事件选项"""
# 清除之前的选项
for widget in self.options_frame.winfo_children():
widget.destroy()
# 显示选项
for i, choice in enumerate(self.current_choices):
btn = ttk.Button(
self.options_frame,
text=f"{choice.option_id}. {choice.strategy_tag}",
command=lambda c=choice: self._on_choice_selected(c)
)
btn.pack(fill=tk.X, padx=10, pady=5)
self.options_frame.pack(fill=tk.X, pady=(10, 0))
print(f"[GUI] 显示 {len(self.current_choices)} 个剧情选项")
def _on_choice_selected(self, choice: Choice):
"""用户选择了一个选项"""
print(f"[GUI] 用户选择了: {choice.option_id}. {choice.strategy_tag}")
print(f"[GUI] 行动: {choice.action}")
# 隐藏选项
self.options_frame.pack_forget()
# 清除画布
self.video_canvas.create_text(
400, 225,
text=f"你选择了: {choice.option_id}\n{choice.strategy_tag}\n\n请继续剧情...",
fill='white',
font=('Arial', 14)
)
# 调用回调
if self.on_choice_callback:
self.on_choice_callback(choice)
def _show_continue_button(self):
"""显示继续按钮"""
# 清除之前的按钮
for widget in self.continue_frame.winfo_children():
widget.destroy()
# 显示继续按钮
btn = ttk.Button(
self.continue_frame,
text="继续下一个事件",
command=self._on_continue_clicked
)
btn.pack(pady=10)
self.continue_frame.pack(fill=tk.X, pady=(10, 0))
print(f"[GUI] 显示继续按钮")
def _on_continue_clicked(self):
"""继续按钮被点击"""
print("[GUI] 继续按钮被点击")
# 隐藏继续按钮
self.continue_frame.pack_forget()
# 调用回调
if self.on_continue_callback:
self.on_continue_callback()
def _stop_current_playback(self):
"""停止当前播放"""
self.stop_playback.set()
self.is_playing = False
self.is_paused = False
if self.video_capture:
self.video_capture.release()
self.video_capture = None
if self.playback_thread and self.playback_thread.is_alive():
self.playback_thread.join(timeout=1.0)
def process_queue(self):
"""处理日志队列中的消息"""
try:
while True:
msg_type, msg_data = self.log_queue.get_nowait()
if msg_type == "progress":
self.progress_var.set(msg_data)
elif msg_type == "video_finished":
self._play_next_or_finish()
elif msg_type == "video_error":
self.status_label.config(text=f"视频加载错误: {msg_data}")
elif msg_type == "continue":
# 通知主循环继续
pass
except queue.Empty:
pass
# 定期检查
self.master.after(100, self.process_queue)
def close(self):
"""关闭播放器"""
self._stop_current_playback()
# ==================== GUI交互会话包装器 ====================
class GUISessionRunner:
"""GUI交互会话运行器 - 将交互会话与GUI连接"""
def __init__(self, session: InteractiveSession, gui: VideoPlayerGUI,
user_choices: Dict[str, List[str]] = None):
"""
初始化GUI会话运行器
Args:
session: 交互会话对象
gui: GUI播放器
user_choices: 预设的用户选择
"""
self.session = session
self.gui = gui
self.user_choices = user_choices or {}
# 当前处理的事件索引
self.current_event_index = 0
# 事件队列
self.event_queue: queue.Queue = queue.Queue()
def start(self):
"""开始运行会话"""
print(f"\n{'='*60}")
print(f"📅 {self.session.schedule.date} - {self.session.context.character_dna.name} 的一天")
print(f"{'='*60}")
print(f"⚡ 初始能量: {self.session.context.actor_state.energy}")
print(f"😊 初始心情: {self.session.context.actor_state.mood}")
print(f"📍 初始位置: {self.session.context.actor_state.location}")
print(f"❤️ 初始亲密度: {self.session.context.user_profile.intimacy_points}")
print(f"{'='*60}\n")
# 启动第一个事件
self._process_next_event()
def _process_next_event(self):
"""处理下一个事件"""
if self.current_event_index >= len(self.session.schedule.events):
# 所有事件处理完毕
self._on_session_complete()
return
event = self.session.schedule.events[self.current_event_index]
# 根据事件类型处理
if event.event_type == "N":
self._process_n_event_gui(event)
elif event.event_type == "R":
self._process_r_event_gui(event)
elif event.event_type == "SR":
self._process_sr_event_gui(event)
def _process_n_event_gui(self, event: Event):
"""处理N事件(自动应用)"""
print(f"📖 {event.event_name}")
# 应用属性变化
if event.attribute_change:
self.session._apply_attribute_change(
event.attribute_change,
event.event_name,
record_memory=False
)
print(f" ✅ 能量变化: {event.attribute_change.get('energy_change', 0):+d}")
print(f" 💭 心情变化: {event.attribute_change.get('mood_change', '无变化')}")
else:
print(" (无属性变化)")
# 播放视频(如果有)
self.gui.play_event(
event.time_slot,
event.event_type,
event.event_name,
on_continue_callback=self._continue_to_next_event
)
def _process_r_event_gui(self, event: Event):
"""处理R事件(单次选择)"""
print(f"\n🎭 【R事件】{event.meta_info.get('script_name', event.event_name) if event.meta_info else event.event_name}")
print(f" 类型: {event.meta_info.get('event_type', '') if event.meta_info else ''}")
print(f" 核心冲突: {event.meta_info.get('core_conflict', '') if event.meta_info else ''}")
print(f"\n📜 序幕 (Prologue):")
print(f" {event.prologue}")
# 获取选项
choices = event.interaction.choices if event.interaction else []
# 播放前置剧情视频(choice_path=None表示只播放前置视频)
self.gui.play_event(
event.time_slot,
event.event_type,
event.event_name,
choices=choices,
on_choice_callback=lambda choice: self._on_r_choice_selected(event, choice),
choice_path=None # 先播放前置视频
)
def _on_r_choice_selected(self, event: Event, choice: Choice):
"""R事件选择回调"""
choice_id = choice.option_id
print(f"[GUI] 用户选择了: {choice_id}")
# 播放选择后的分支视频和结局
choice_path = [choice_id]
# 获取该路径对应的视频
videos = self.gui.video_mapper.get_videos_for_path(
event.time_slot,
event.event_type,
choice_path
)
# 过滤掉已经播放过的前置视频,只播放分支和结局视频
branch_videos = [
v for v in videos
if not any(keyword in v.stem for keyword in ["前置剧情", "叙事段落", "Prologue", "Narrative"])
]
print(f"[GUI] 播放分支视频 {len(branch_videos)} 个")
if branch_videos:
# 设置分支视频完成后的回调
self.gui.on_branch_videos_finished = lambda: self._apply_r_event_result(event, choice_id)
# 更新当前视频列表并播放
self.gui.current_videos = branch_videos
self.gui.current_video_index = 0
self.gui.current_choice_path = choice_path
self.gui.current_choices = [] # 清除选项
self.gui._play_current_video()
else:
# 没有分支视频,直接应用结果并继续
self._apply_r_event_result(event, choice_id)
def _apply_r_event_result(self, event: Event, choice_id: str):
"""应用R事件的结果"""
# 记录选择
self.session.choice_history[event.time_slot] = [choice_id]
# 匹配结局
resolution = self.session._match_resolution(event.resolutions, [choice_id])
if resolution:
print(f"\n🎬 结局: {resolution.ending_title}")
print(f" 类型: {resolution.ending_type}")
print(f" 你的选择: {choice_id}")
print(f"\n📖 剧情收尾:")
print(f" {resolution.plot_closing}")
print(f"\n💭 角色反应:")
print(f" {resolution.character_reaction}")
# 应用属性变化
self.session._apply_attribute_change(
resolution.attribute_change,
event.event_name,
resolution=resolution
)
# 记录结果
self.session.event_results.append({
"time_slot": event.time_slot,
"event_name": event.event_name,
"event_type": "R",
"choices": [choice_id],
"ending_id": resolution.ending_id,
"ending_title": resolution.ending_title
})
# 显示结果在GUI上
self.gui.video_canvas.create_text(
400, 225,
text=f"结局: {resolution.ending_title}\n\n{resolution.plot_closing[:100] if len(resolution.plot_closing) > 100 else resolution.plot_closing}...",
fill='white',
font=('Arial', 12)
)
# 显示继续按钮
self.gui._show_continue_button()
else:
print(f"\n⚠️ 未找到匹配的结局 (选择: {choice_id})")
self._continue_to_next_event()
def _process_sr_event_gui(self, event: Event):
"""处理SR事件(多阶段选择)"""
print(f"\n🎭 【SR事件】{event.meta_info.get('script_name', event.event_name) if event.meta_info else event.event_name}")
print(f" 类型: {event.meta_info.get('event_type', '') if event.meta_info else ''}")
print(f" 核心冲突: {event.meta_info.get('core_conflict', '') if event.meta_info else ''}")
print(f"\n📜 序幕 (Prologue):")
print(f" {event.prologue}")
# 检查是否有预设选择
if event.time_slot in self.user_choices:
# 使用预设选择,不显示GUI
self._process_sr_event_auto(event)
return
# 播放视频(第一个阶段的视频)
self.gui.play_event(
event.time_slot,
event.event_type,
event.event_name
)
# 简化处理:SR事件暂时使用CLI方式选择
# TODO: 完整实现SR事件的多阶段GUI选择
self._process_sr_event_auto(event)
def _process_sr_event_auto(self, event: Event):
"""自动处理SR事件(使用CLI或预设选择)"""
choice_path = []
# 处理每个阶段
for phase in event.phases:
print(f"\n{'─'*40}")
print(f"阶段 {phase.phase_number}: {phase.phase_title}")
print(f"{'─'*40}")
print(f"{phase.phase_description}")
choice_id = self.session._get_user_choice(
event.time_slot,
phase_num=phase.phase_number,
choices=phase.choices,
user_choices=self.user_choices
)
choice_path.append(choice_id)
# 显示选择结果
selected_choice = next((c for c in phase.choices if c.option_id == choice_id), None)
if selected_choice:
print(f"\n ➤ 你的选择: {choice_id}. {selected_choice.strategy_tag}")
print(f" 行动: {selected_choice.action}")
print(f" 结果: {selected_choice.result}")
# 记录选择路径
self.session.choice_history[event.time_slot] = choice_path
# 匹配结局
path_str = "-".join(choice_path)
resolution = self.session._match_resolution(event.resolutions, choice_path)
if resolution:
print(f"\n{'='*40}")
print(f"🎬 结局: {resolution.ending_title}")
print(f" 类型: {resolution.ending_type}")
print(f" 你的路径: {path_str}")
print(f"\n📖 剧情收尾:")
print(f" {resolution.plot_closing}")
print(f"\n💭 角色反应:")
print(f" {resolution.character_reaction}")
# 应用属性变化
self.session._apply_attribute_change(
resolution.attribute_change,
event.event_name,
resolution=resolution
)
# 记录结果
self.session.event_results.append({
"time_slot": event.time_slot,
"event_name": event.event_name,
"event_type": "SR",
"choices": choice_path,
"ending_id": resolution.ending_id,
"ending_title": resolution.ending_title
})
else:
print(f"\n⚠️ 未找到匹配的结局 (路径: {path_str})")