创建新工程

This commit is contained in:
moyin
2025-12-05 19:00:14 +08:00
commit ff4fa5fffd
227 changed files with 32804 additions and 0 deletions

327
scripts/EmojiManager.gd Normal file
View File

@@ -0,0 +1,327 @@
extends Node
class_name EmojiManager
## 表情符号管理器
## 处理表情符号的显示、转换和管理
# 表情符号映射表
const EMOJI_MAP = {
# 基础表情
":)": "😊",
":-)": "😊",
":(": "😢",
":-(": "😢",
":D": "😃",
":-D": "😃",
";)": "😉",
";-)": "😉",
":P": "😛",
":-P": "😛",
":o": "😮",
":-o": "😮",
":O": "😲",
":-O": "😲",
":|": "😐",
":-|": "😐",
":/": "😕",
":-/": "😕",
"<3": "❤️",
"</3": "💔",
# 命名表情符号
":smile:": "😊",
":happy:": "😊",
":sad:": "😢",
":cry:": "😭",
":laugh:": "😂",
":love:": "😍",
":angry:": "😠",
":mad:": "😡",
":cool:": "😎",
":wink:": "😉",
":tongue:": "😛",
":surprised:": "😲",
":shocked:": "😱",
":confused:": "😕",
":thinking:": "🤔",
":sleepy:": "😴",
":sick:": "🤒",
":dizzy:": "😵",
":party:": "🥳",
":celebrate:": "🎉",
# 手势和动作
":thumbsup:": "👍",
":thumbsdown:": "👎",
":clap:": "👏",
":wave:": "👋",
":peace:": "✌️",
":ok:": "👌",
":point:": "👉",
":fist:": "",
":pray:": "🙏",
":muscle:": "💪",
# 物品和符号
":fire:": "🔥",
":star:": "",
":heart:": "❤️",
":broken_heart:": "💔",
":diamond:": "💎",
":crown:": "👑",
":gift:": "🎁",
":cake:": "🎂",
":coffee:": "",
":pizza:": "🍕",
":music:": "🎵",
":game:": "🎮",
":book:": "📚",
":phone:": "📱",
":computer:": "💻",
":car:": "🚗",
":house:": "🏠",
":sun:": "☀️",
":moon:": "🌙",
":cloud:": "☁️",
":rain:": "🌧️",
":snow:": "❄️",
":lightning:": "",
":rainbow:": "🌈"
}
# 表情符号类别
const EMOJI_CATEGORIES = {
"faces": ["😊", "😢", "😃", "😉", "😛", "😮", "😲", "😐", "😕", "😭", "😂", "😍", "😠", "😡", "😎", "😱", "🤔", "😴", "🤒", "😵", "🥳"],
"gestures": ["👍", "👎", "👏", "👋", "✌️", "👌", "👉", "", "🙏", "💪"],
"objects": ["🔥", "", "❤️", "💔", "💎", "👑", "🎁", "🎂", "", "🍕", "🎵", "🎮", "📚", "📱", "💻"],
"nature": ["☀️", "🌙", "☁️", "🌧️", "❄️", "", "🌈"],
"transport": ["🚗", "🏠"]
}
# 最近使用的表情符号
var recent_emojis: Array[String] = []
var max_recent_emojis: int = 20
func _ready():
"""初始化表情符号管理器"""
load_recent_emojis()
print("EmojiManager initialized with ", EMOJI_MAP.size(), " emojis")
## 转换文本中的表情符号
static func convert_text_to_emoji(text: String) -> String:
"""
将文本中的表情符号代码转换为实际的表情符号
@param text: 输入文本
@return: 转换后的文本
"""
var result = text
# 按长度排序,优先匹配长的表情符号代码
var sorted_keys = EMOJI_MAP.keys()
sorted_keys.sort_custom(func(a, b): return a.length() > b.length())
for emoji_code in sorted_keys:
var emoji = EMOJI_MAP[emoji_code]
result = result.replace(emoji_code, emoji)
return result
## 获取表情符号建议
static func get_emoji_suggestions(partial_code: String) -> Array[Dictionary]:
"""
根据部分输入获取表情符号建议
@param partial_code: 部分表情符号代码
@return: 建议数组,包含代码和对应的表情符号
"""
var suggestions = []
var partial_lower = partial_code.to_lower()
for emoji_code in EMOJI_MAP:
if emoji_code.to_lower().begins_with(partial_lower):
suggestions.append({
"code": emoji_code,
"emoji": EMOJI_MAP[emoji_code],
"description": _get_emoji_description(emoji_code)
})
# 限制建议数量
if suggestions.size() > 10:
suggestions = suggestions.slice(0, 10)
return suggestions
## 获取表情符号描述
static func _get_emoji_description(emoji_code: String) -> String:
"""
获取表情符号的描述
@param emoji_code: 表情符号代码
@return: 描述文本
"""
match emoji_code:
":smile:", ":)", ":-)":
return "微笑"
":sad:", ":(", ":-(":
return "伤心"
":laugh:", ":D", ":-D":
return "大笑"
":love:":
return "爱心眼"
":angry:":
return "生气"
":cool:":
return ""
":wink:", ";)", ";-)":
return "眨眼"
":thinking:":
return "思考"
":thumbsup:":
return "点赞"
":thumbsdown:":
return "点踩"
":clap:":
return "鼓掌"
":fire:":
return ""
":heart:", "<3":
return "爱心"
":star:":
return "星星"
_:
return "表情符号"
## 按类别获取表情符号
static func get_emojis_by_category(category: String) -> Array[String]:
"""
获取指定类别的表情符号
@param category: 类别名称
@return: 表情符号数组
"""
if EMOJI_CATEGORIES.has(category):
return EMOJI_CATEGORIES[category].duplicate()
return []
## 获取所有类别
static func get_all_categories() -> Array[String]:
"""
获取所有表情符号类别
@return: 类别名称数组
"""
return EMOJI_CATEGORIES.keys()
## 添加到最近使用
func add_to_recent(emoji: String) -> void:
"""
添加表情符号到最近使用列表
@param emoji: 表情符号
"""
# 如果已存在,先移除
if emoji in recent_emojis:
recent_emojis.erase(emoji)
# 添加到开头
recent_emojis.push_front(emoji)
# 限制数量
if recent_emojis.size() > max_recent_emojis:
recent_emojis.pop_back()
# 保存到本地
save_recent_emojis()
## 获取最近使用的表情符号
func get_recent_emojis() -> Array[String]:
"""
获取最近使用的表情符号
@return: 最近使用的表情符号数组
"""
return recent_emojis.duplicate()
## 检查是否包含表情符号
static func contains_emoji(text: String) -> bool:
"""
检查文本是否包含表情符号代码
@param text: 输入文本
@return: 是否包含表情符号
"""
for emoji_code in EMOJI_MAP:
if text.contains(emoji_code):
return true
return false
## 提取文本中的表情符号代码
static func extract_emoji_codes(text: String) -> Array[String]:
"""
提取文本中的所有表情符号代码
@param text: 输入文本
@return: 表情符号代码数组
"""
var codes = []
for emoji_code in EMOJI_MAP:
if text.contains(emoji_code):
codes.append(emoji_code)
return codes
## 获取随机表情符号
static func get_random_emoji(category: String = "") -> String:
"""
获取随机表情符号
@param category: 指定类别(空字符串表示从所有表情符号中选择)
@return: 随机表情符号
"""
var emoji_list = []
if category.is_empty():
emoji_list = EMOJI_MAP.values()
elif EMOJI_CATEGORIES.has(category):
emoji_list = EMOJI_CATEGORIES[category]
else:
return "😊" # 默认表情符号
if emoji_list.size() > 0:
return emoji_list[randi() % emoji_list.size()]
return "😊"
## 保存最近使用的表情符号
func save_recent_emojis() -> void:
"""保存最近使用的表情符号到本地文件"""
var file = FileAccess.open("user://recent_emojis.json", FileAccess.WRITE)
if file:
var json_string = JSON.stringify(recent_emojis)
file.store_string(json_string)
file.close()
## 加载最近使用的表情符号
func load_recent_emojis() -> void:
"""从本地文件加载最近使用的表情符号"""
if not FileAccess.file_exists("user://recent_emojis.json"):
return
var file = FileAccess.open("user://recent_emojis.json", FileAccess.READ)
if file:
var json_string = file.get_as_text()
file.close()
var json = JSON.new()
var parse_result = json.parse(json_string)
if parse_result == OK and json.data is Array:
recent_emojis = json.data
print("Loaded ", recent_emojis.size(), " recent emojis")
## 创建表情符号选择器数据
func create_emoji_picker_data() -> Dictionary:
"""
创建表情符号选择器所需的数据
@return: 包含分类表情符号的字典
"""
var picker_data = {
"recent": get_recent_emojis(),
"categories": {}
}
for category in EMOJI_CATEGORIES:
picker_data.categories[category] = EMOJI_CATEGORIES[category].duplicate()
return picker_data