效果图:

PS. 我不擅长写代码,代码写得很烂,大佬们可以帮我改代码(bushi
PS2. 这个面板只是虚有其表,没有深层次功能
首先,我们需要创建一个信息面板的场景,类似这样:

首先把需要赋值的节点存到变量里:
onready var 织物_ = $"Panel/Vbox/Mar/Vbox2/资源/Hbox/Vbox4/Hbox2/织物"
onready var 金币_ = $"Panel/Vbox/Mar/Vbox2/资源/Hbox/Vbox4/Hbox3/金币"
................
然后写一个更新内容的函数:
# 因为我的事件和NPC名数据都有一个ID,所以事件数组、NPC数组里全都是ID
# 类似: 事件数组 = [100001, 100002, 100003]
func 更新(信息数组, 事件数组, NPC数组):
# 我的信息数组长这样:["地名", "445", ......]
地名_.text = str(信息数组[0]) + " • " + "类型:" + str(信息数组[8])
木材_.text = str(信息数组[1])
食物_.text = str(信息数组[2])
药材_.text = str(信息数组[3])
金石_.text = str(信息数组[4])
织物_.text = str(信息数组[5])
金币_.text = str(信息数组[6])
# 添加事件 这里判断有没有事件,也可以用size(),但是我懒得改
# 为什么要删除子节点呢,因为添加事件的时候是直接instance()然后add_child()
if str(事件数组[0]) == "没有事件":
删除子节点($"Panel/Vbox/Mar/Vbox2/事件/Vbox3")
event_title.hide()
else:
删除子节点($"Panel/Vbox/Mar/Vbox2/事件/Vbox3")
event_title.show()
# 循环添加文本节点
for i in 事件数组.size():
Label是一个label场景,就是显示事件名的模板
var b_label = label.instance()
$"Panel/Vbox/Mar/Vbox2/事件/Vbox3".add_child(b_label)
b_label.text = str(数据.Event_data.get(int(事件数组[i])).事件名)
b_label.set("custom_colors/font_color", "bf8e4a")
# 添加NPC 原理同上添加事件
if str(NPC数组[0]) == "没有NPC":
删除子节点($"Panel/Vbox/Mar/Vbox2/NPC/Vbox3")
npc_title.hide()
else:
删除子节点($"Panel/Vbox/Mar/Vbox2/NPC/Vbox3")
npc_title.show()
for i in NPC数组.size():
var b_label = label.instance()
$"Panel/Vbox/Mar/Vbox2/NPC/Vbox3".add_child(b_label)
b_label.text = str(数据.Npc_name_data.get(int(NPC数组[i])).人名)
# 自适应大小,根据内容长度改变面板长度,可以自己调试一下
$Panel/Vbox.rect_size.y = 27 + 地名_.rect_size.y + $Panel/Vbox/Mar/Vbox2.rect_size.y
$Panel.rect_size.y = $Panel/Vbox.rect_size.y + 50
删除子节点函数(上面添加事件传的参数是事件的父节点):
func 删除子节点(node):
for n in node.get_children():
node.remove_child(n)
n.queue_free()
再写一个reset函数:
# 因为我们后面更新信息框不是Instance(),而是show() & hide()
# 所以要清空事件和NPC的文本,防止每次更新都接着上一个状态添加了
func reset():
删除子节点($"Panel/Vbox/Mar/Vbox2/NPC/Vbox3")
删除子节点($"Panel/Vbox/Mar/Vbox2/事件/Vbox3")
然后找到我们需要悬浮信息的图块模板:
func _on_格子模板_mouse_entered():
# 获取鼠标坐标
var mouse_pos = get_viewport().get_mouse_position()
var 图块索引 = self.get_index()
# 添加 Tooltip
# Uhd是一个单例,根节点是Canvaslayer,专门用来显示UI,其中实例化了悬浮信息框
Uhd.add_tooltip(mouse_pos, 图块索引)
func 更新信息框():
# 这里我用了call_group(),图个方便,不过不好维护,大佬们应该有更好的方法
# 于是又绕到开始的更新函数了
# 至于地块里生成数据,可以在站里搜太吾绘卷,梁咸蛋的帖子,我就是抄的那个
get_tree().call_group("信息框", "更新", 信息数组, 事件数组, NPC数组)
Uhd:
onready var tween = $Tween
onready var toottip = $悬浮信息框
func add_tooltip(鼠标坐标, 图块索引):
toottip.reset()
# 加一个Tween,让鼠标移动到不同地块时更顺滑
tween.interpolate_property(toottip, "rect_position", toottip.rect_position,
鼠标坐标, 0.2, Tween.TRANS_SINE, Tween.EASE_OUT)
# 获取图块node,调用更新信息框函数
# 因为我的数据在地块ready的时候就储存在了地块
get_parent().get_node("Test/TileMap").get_child(图块索引).更新信息框()
toottip.popup()
tween.start()