godot_experiments/station_info_panel/style1/site_panel_content.gd

161 lines
5.3 KiB
GDScript3
Raw Normal View History

2024-04-30 18:09:56 +08:00
@tool
extends Node2D
## 从左到右/从右到左
@export var leftToRight: bool = true:
get:
return leftToRight
set(v):
leftToRight = v
update()
## 车站列表
@export var stations: Array[String] = [
"航天新城|HANGTIANXINCHENG","航天东路|HANGTIANDONGLU","神舟大道|SHENZHOUDADAO",
"东长安街|DONGCHANGANJIE","飞天路|FEITIANLU",
"航天大道|HANGTIANDADAO","金滹沱|JINHUTUO","曲江池西|QUJIANGCHIXI",
"大唐芙蓉园|DATANGFURONGYUAN","大雁塔|DAYANTA","西安科技大学|XIANKEJIDAXUE",
"建筑科技大学·李家村|JIANZHUKEJIDAXUE-LIJIACUN","和平门|HEPINGMEN",
"大差市|DACHAISHI","五路口|WULUKOU","西安站|XIANZHAN","含元殿|HANYUANDIAN",
"大明宫|DAMINGGONG","大明宫北|DAMINGGONGBEI","余家寨|YUJIAZHAI",
"百花村|BAIHUACUN","常青路|CHANGQINGLU",
"市中医医院|SHIZHONGYIYIYUAN","行政中心|XINGZHENGZHONGXIN","文景路|WENJINGLU",
"凤城九路|FENGCHENGJIULU","凤城十二路|FENGCHENGSHIERLU","元朔路|YUANSHUOLU",
"北客站(北广场)|BEIKEZHAN(BEIGUANGCHANG)"]:
get:
return stations
set(v):
stations = v
assert(stations.size() > 0, "车站列表不能为空")
for s in stations:
assert(s.split("|").size() == 2, "车站名格式:'车站名|车站英文名'")
update()
## 当前车站索引
@export var currentStationIndex: int = 0:
get:
return currentStationIndex
set(v):
currentStationIndex = v
assert(v >= 0 && v < stations.size(), "当前站超出车站索引范围")
update()
## 背景色
@export var bgColor: Color = Color.DARK_GRAY:
get:
return bgColor
set(v):
bgColor = v
update()
## 已经过站点图标颜色
@export var finishedColor: Color = Color.DARK_GRAY:
get:
return finishedColor
set(v):
finishedColor = v
update()
## 未经过站点图标颜色
@export var unfinishedColor: Color = Color.DARK_GREEN:
get:
return unfinishedColor
set(v):
unfinishedColor = v
update()
@onready var siteInfoScene: PackedScene = preload("res://station_info_panel/style1/site_info.tscn")
@onready var siteCircleStyle: StyleBoxFlat = preload("res://station_info_panel/style1/site_style_circle.tres")
@onready var siteStripStyle: StyleBoxFlat = preload("res://station_info_panel/style1/site_style_strip.tres")
## 站点样式缓存
var siteStyleMap: Dictionary = {}
const BorderWidth = 2
const PassedColor = Color.DARK_GRAY
var isReady = false
# 重绘完成和未完成的线路条
func updateBgStrips():
var totalWidth = $Bg.size.x - (2 * BorderWidth)
if leftToRight:
var currentSite = $Sites.get_child(currentStationIndex)
$Bg/Finished["theme_override_styles/panel"].bg_color = finishedColor
$Bg/Finished.position.x = BorderWidth
$Bg/Finished.size.x = currentSite.position.x - BorderWidth
$Bg/Unfinished["theme_override_styles/panel"].bg_color = unfinishedColor
$Bg/Unfinished.position.x = currentSite.position.x
$Bg/Unfinished.size.x = totalWidth - currentSite.position.x
else:
var currentSite = $Sites.get_child(stations.size()-1-currentStationIndex)
$Bg/Finished["theme_override_styles/panel"].bg_color = finishedColor
$Bg/Finished.position.x = currentSite.position.x
$Bg/Finished.size.x = totalWidth - currentSite.position.x
$Bg/Unfinished["theme_override_styles/panel"].bg_color = unfinishedColor
$Bg/Unfinished.position.x = BorderWidth
$Bg/Unfinished.size.x = currentSite.position.x - BorderWidth
## 更新站点颜色样式
func updateSite(site, siteStyleColor: Color, nameColor):
var key = siteStyleColor.to_html()
var siteStyle
if siteStyleMap.has(key):
siteStyle = siteStyleMap[key]
else:
var circleStyle = siteCircleStyle.duplicate()
var stripStyle = siteStripStyle.duplicate()
circleStyle.border_color = siteStyleColor
stripStyle.bg_color = siteStyleColor
siteStyle = {"circle": circleStyle, "strip": stripStyle}
siteStyleMap[key] = siteStyle
site.siteCircleStyle = siteStyle["circle"]
site.siteStripStyle = siteStyle["strip"]
site.nameColor = nameColor
## 重绘
func update():
if not isReady:
return
# 更新前先删除之前的
for c in $Sites.get_children():
c.free()
# 更新背景色
$Bg["theme_override_styles/panel"].bg_color = bgColor
# 根据方向添加站点信息组件
var size = stations.size()
var range: Array
if leftToRight:
range = range(size)
else:
range = range(size-1, -1, -1)
assert(currentStationIndex >= 0 && currentStationIndex < size, "当前站超出车站索引范围")
for i in range:
var si = stations[i]
var sns = si.split("|")
var sis = siteInfoScene.instantiate()
sis.stationName = sns[0]
sis.stationEnName = sns[1]
if i > currentStationIndex:
updateSite(sis, unfinishedColor, unfinishedColor)
else:
#sis.siteCircleStyle = siteFinishedCircleStyle
#sis.siteStripStyle = siteFinishedStripStyle
#sis.nameColor = finishedColor
updateSite(sis, finishedColor, finishedColor)
$Sites.add_child(sis)
# 计算边界最小尺寸(最右边文字不超出边界)
var last = $Sites.get_child(size-1)
var edgeWidth = last.calculateWidth()
# 计算间隔
var totalWidth = $Bg.size.x - (2 * BorderWidth)
var interval = totalWidth / (size + 1)
print(edgeWidth, ", ", interval)
if interval < edgeWidth:
var remain = totalWidth - edgeWidth
interval = remain / size
var i = 1
for sc in $Sites.get_children():
sc.position.x += i * interval
i += 1
# 更新线路条
updateBgStrips()
func _ready():
isReady = true
update()