问题详情
代码纯小白,在chatgpt帮助下写了一个脚本,运行后通过弹窗显示轨道名称,并通过操作实现相应功能。其他一切运行正常,但只有一个问题不能解决:显示的轨道名称中,中文为空白,请大佬解答~~
代码如下:
local r = reaper
function msg(value)
--r.ShowConsoleMsg(tostring(value)..'\n') -- 如果需要输出到控制台,请取消注释此行
end
gfx.clear = 1210758 -- 背景颜色
wt = "我的按钮 "
-- 获取轨道数量
local trackCount = r.CountTracks(0)
-- 计算窗口大小
local buttonHeight = 30
local windowHeight = buttonHeight * trackCount + 20 + (trackCount - 1) * 5
gfx.init(wt, 200, windowHeight, 0, 200, 300) -- 窗口初始化
local state_last = 0 -- 初始化上一个状态
local colorTable = {} -- 随机颜色存储表
function generateRandomColor()
-- 生成随机的RGB颜色
local rColor = math.random(0, 255) / 255
local gColor = math.random(0, 255) / 255
local bColor = math.random(0, 255) / 255
return rColor, gColor, bColor
end
function drawButtons()
local y = 10
local colorIndex = 1 -- 颜色索引
for i = 0, trackCount - 1 do
local track = r.GetTrack(0, i)
local _, trackName = r.GetSetMediaTrackInfo_String(track, "P_NAME", "", false)
local ww = 180
local buttonX = (200 - ww) / 2 -- 计算按钮的横坐标使其居中
local buttonY = y
local rColor, gColor, bColor
if colorTable[i] then
rColor, gColor, bColor = table.unpack(colorTable[i])
else
rColor, gColor, bColor = generateRandomColor()
colorTable[i] = {rColor, gColor, bColor}
end
if colorIndex % 2 == 0 then
gfx.set(rColor, gColor, bColor, 1) -- 偶数索引的按钮颜色为随机颜色
else
gfx.set(1-rColor, 1-gColor, 1-bColor, 1) -- 奇数索引的按钮颜色为随机颜色的补色
end
gfx.rect(buttonX, buttonY, ww, buttonHeight) -- 画出按钮背景
gfx.set(0, 0, 0, 1) -- 按钮文字颜色
local textWidth, _ = gfx.measurestr(trackName) -- 获取轨道名字的宽和高
if textWidth > ww then
trackName = string.sub(trackName, 1, math.floor(ww / gfx.measurestr("x"))) -- 如果文字超出按钮宽度,则裁剪文字
end
gfx.x, gfx.y = buttonX + (ww - gfx.measurestr(trackName)) / 2, buttonY + (buttonHeight - gfx.texth) / 2 -- 文字位置偏移以使其居中
gfx.drawstr(trackName) -- 画出按钮标题
if gfx.mouse_x > buttonX and gfx.mouse_x < (buttonX + ww) and gfx.mouse_y > buttonY and gfx.mouse_y < (buttonY + buttonHeight) then
if gfx.mouse_cap ~= state_last then -- 当前状态跟上一个状态不同
if state_last == 1 then
--msg(trackName) -- 取消控制台输出弹窗
-- 选择轨道
r.SetMediaTrackInfo_Value(track, "I_SELECTED", 1)
-- 执行命令
PS:关于代码,白的不能再白,其中如果有其他逻辑错误,请忽略。。。