extends Control class_name ErrorNotification ## 错误通知系统 ## 显示网络错误、操作失败等提示信息 # 通知类型 enum NotificationType { INFO, WARNING, ERROR, SUCCESS } # UI 元素 @onready var notification_panel: Panel = $NotificationPanel @onready var message_label: Label = $NotificationPanel/MessageLabel @onready var timer: Timer = $Timer # 信号 signal notification_closed() # 当前通知配置 var current_notification_type: NotificationType = NotificationType.INFO var auto_hide: bool = true var auto_hide_duration: float = 3.0 func _ready(): """初始化错误通知系统""" # 默认隐藏 hide() # 连接信号 if timer: timer.timeout.connect(_on_timer_timeout) # 设置统一的字体样式 _setup_font_style() print("ErrorNotification initialized") ## 显示错误通知 func show_error(message: String, auto_hide_after: float = 5.0) -> void: """ 显示错误通知 @param message: 错误消息 @param auto_hide_after: 自动隐藏时间(默认5秒,0表示不自动隐藏) """ _show_notification(message, NotificationType.ERROR, auto_hide_after) ## 显示警告通知 func show_warning(message: String, auto_hide_after: float = 3.0) -> void: """ 显示警告通知 @param message: 警告消息 @param auto_hide_after: 自动隐藏时间 """ _show_notification(message, NotificationType.WARNING, auto_hide_after) ## 显示信息通知 func show_info(message: String, auto_hide_after: float = 2.0) -> void: """ 显示信息通知 @param message: 信息消息 @param auto_hide_after: 自动隐藏时间 """ _show_notification(message, NotificationType.INFO, auto_hide_after) ## 显示成功通知 func show_success(message: String, auto_hide_after: float = 2.0) -> void: """ 显示成功通知 @param message: 成功消息 @param auto_hide_after: 自动隐藏时间 """ _show_notification(message, NotificationType.SUCCESS, auto_hide_after) ## 显示网络错误 func show_network_error(error_message: String = "网络连接失败") -> void: """ 显示网络错误通知 @param error_message: 错误消息 """ # 将技术错误转换为用户友好的消息 var user_friendly_message = _make_error_user_friendly(error_message) show_error(user_friendly_message, 8.0) # 8秒后自动隐藏 ## 将错误消息转换为用户友好的格式 func _make_error_user_friendly(error_message: String) -> String: """ 将技术错误消息转换为用户友好的格式 @param error_message: 原始错误消息 @return: 用户友好的错误消息 """ var message = error_message.to_lower() # 网络相关错误 if "connection" in message or "连接" in message: if "timeout" in message or "超时" in message: return "连接超时,请检查网络连接后重试" elif "refused" in message or "拒绝" in message: return "无法连接到服务器,服务器可能暂时不可用" else: return "网络连接出现问题,请检查网络设置" # 认证相关错误 elif "auth" in message or "认证" in message or "登录" in message: return "登录失败,请检查用户名或稍后重试" # 角色创建相关错误 elif "character" in message or "角色" in message: if "exists" in message or "存在" in message: return "角色名称已被使用,请选择其他名称" elif "invalid" in message or "无效" in message: return "角色名称格式不正确,请使用2-20个字符" else: return "角色创建失败,请稍后重试" # 服务器相关错误 elif "server" in message or "服务器" in message: return "服务器暂时不可用,请稍后重试" # 如果无法识别,返回通用友好消息 else: return "操作失败,请稍后重试。如果问题持续存在,请联系客服" ## 显示重连提示 func show_reconnecting(attempt: int, max_attempts: int) -> void: """ 显示重连提示 @param attempt: 当前尝试次数 @param max_attempts: 最大尝试次数 """ var message = "正在重新连接... (%d/%d)" % [attempt, max_attempts] show_info(message, 0.0) # 不自动隐藏 ## 显示操作失败提示 func show_operation_failed(operation: String, reason: String = "") -> void: """ 显示操作失败提示 @param operation: 操作名称 @param reason: 失败原因 """ var message = "操作失败: " + operation if not reason.is_empty(): message += "\n原因: " + reason show_error(message, 5.0) ## 隐藏通知 func hide_notification() -> void: """隐藏通知(带淡出效果)""" if timer: timer.stop() # 使用UIAnimationManager的淡出动画 var tween = UIAnimationManager.fade_out(self, 0.3) if tween: tween.tween_callback(func(): notification_closed.emit() ) ## 内部方法:显示通知 func _show_notification(message: String, type: NotificationType, auto_hide_after: float) -> void: """ 内部方法:显示通知 @param message: 消息内容 @param type: 通知类型 @param auto_hide_after: 自动隐藏时间(0 表示不自动隐藏) """ current_notification_type = type # 设置消息文本 if message_label: message_label.text = message # 设置颜色主题 _apply_notification_style(type) # 使用UIAnimationManager的动画效果 if type == NotificationType.ERROR: # 错误通知使用摇摆+淡入效果 UIAnimationManager.fade_slide_in(self, "top", 0.4) # 添加轻微摇摆效果强调错误 await get_tree().create_timer(0.4).timeout UIAnimationManager.shake_error(self, 5.0, 0.3) else: # 其他通知使用平滑滑入效果 UIAnimationManager.fade_slide_in(self, "top", 0.3) # 设置自动隐藏 if auto_hide_after > 0.0 and timer: timer.wait_time = auto_hide_after timer.start() ## 应用通知样式 func _apply_notification_style(type: NotificationType) -> void: """ 根据通知类型应用样式 @param type: 通知类型 """ if not notification_panel: return # 根据类型设置颜色 var style_box = StyleBoxFlat.new() match type: NotificationType.ERROR: style_box.bg_color = Color(0.8, 0.2, 0.2, 0.9) # 红色 NotificationType.WARNING: style_box.bg_color = Color(0.9, 0.7, 0.2, 0.9) # 黄色 NotificationType.INFO: style_box.bg_color = Color(0.2, 0.5, 0.8, 0.9) # 蓝色 NotificationType.SUCCESS: style_box.bg_color = Color(0.2, 0.8, 0.3, 0.9) # 绿色 style_box.corner_radius_top_left = 8 style_box.corner_radius_top_right = 8 style_box.corner_radius_bottom_left = 8 style_box.corner_radius_bottom_right = 8 notification_panel.add_theme_stylebox_override("panel", style_box) ## 设置字体样式 func _setup_font_style() -> void: """设置统一的字体样式""" if message_label: # 设置字体大小 message_label.add_theme_font_size_override("font_size", 18) # 设置文字颜色 message_label.add_theme_color_override("font_color", Color.WHITE) # 添加阴影效果使文字更清晰 message_label.add_theme_color_override("font_shadow_color", Color(0, 0, 0, 0.8)) message_label.add_theme_constant_override("shadow_offset_x", 1) message_label.add_theme_constant_override("shadow_offset_y", 1) ## 定时器超时 func _on_timer_timeout() -> void: """定时器超时,自动隐藏通知""" hide_notification()