本文档旨在介绍和解释一系列 Lua 接口函数/方法的使用方法和功能。这些接口为 Lua 开发者提供了一组强大的工具,以便在 Lua 环境中进行各种操作和计算。
以下是本文档涵盖的主要 API 接口列表:
属性
ssr.set_property ssr.set_property_table ssr.get_property ssr.get_property_table ssr.register_property_change
计时器
ssr.timer_create ssr.timer_start ssr.timer_stop ssr.timer_destory
动画
ssr.animation_create ssr.animation_add_step ssr.animation_destory ssr.animation_trigger
多语言
ssr.language_set_type ssr.language_get_type
加载多工程
ssr.container_set_file
移动节点层级
ssr.move_page_child ssr.move_child
事件
ssr.event_binding ssr.event_relation_define ssr.event_trigger
获取路径
ssr.get_root_path
退出程序
ssr.exit
状态机
ssr.gotoState
设置节点的属性值
key:字符串类型,要更改的属性变量名
value:字符串类型,要更改的属性变量设值
NONE
function airConditioner_Open_fun()
--更换风量调节图标
ssr.set_property("Layer0/Touch0/Source","open.png")
end
设置多个节点的属性值
table:table是一个表,其中包含要更改的属性变量名作为键,更改的属性变量值作为键值
NONE
function airConditioner_Open_fun()
local data_table = {}
data_table["Layer0/Touch0/Source"] = "open.png" --更换风量调节图标
data_table["Layer0/Touch0/Rotation"] = "360" --设置风量图标旋转
data_table["Layer0/Temperature.FontColor"] = "#3DAEFFFF" --设置温度文字颜色
ssr.set_property_table(data_table)
end
获取节点的属性值
key:字符串类型,要获取的属性变量名
字符串类型,返回一个要获取的属性变量值
function lua_fun()
--Item节点的X属性:"Layer/Item/X"
local strVal = ssr.get_property("Layer/Item/X")
end
获取多个节点的属性值
table:table是一个表,其中包含要获取的属性变量名
table类型,返回一个表包含要获取的变量值
function lua_fun()
--Item节点的X属性:"Layer/Item/X"
--Item节点的Y属性:"Layer/Item/Y"
local table = {"Layer/Item/X", "Layer/Item/Y"}
local retTable = ssr.get_property_table(table)
local retval = retTable["Layer/Item/X"]
end
获取注册的属性值变化
table:返回一个表,其中包含要获取的属性变量名
cb_function:注册一个回调函数,函数参数包含变化的属性值
table类型,返回一个表,包含变化的属性变量值
function onPropertyChange(table)
for k, v in pairs(table) do
ssr.print(k.." "..v)
end
end
function lua_fun()
--Item节点的X属性:"Layer/Item/X"
--Item节点的Y属性:"Layer/Item/Y"
local table = {"Layer/Item/X", "Layer/Item/Y"}
ssr.register_property_change(table,"onPropertyChange")
end
创建一个计时器,每隔"interval"时间触发一次,执行function
interval: 计时器触发的时间间隔,单位为毫秒
count: 计时器触发的次数;如果设置为 1,计时器只触发一次,如果设置为 -1,计时器会循环一直触发
function:回调函数的名称(字符串形式),当计时器触发时执行此函数
int类型,返回计时器的ID(可用于启动、停止和销毁该计时器)
-- 定义回调函数
function cb_function()
ssr.print("cb_function")
end
--创建一个计时器,1s执行1次一共执行3次
function lua_fun()
local timer_id = ssr.timer_create(1000, 3, "cb_function")
ssr.timer_start(timer_id)
end
启动一个已创建的计时器,计时器会按照指定的间隔触发回调函数
timer_id:计时器的ID(ssr.timer_create函数的返回值)
NONE
function cb_function()
ssr.print("cb_function")
end
--创建一个计时器,1s执行1次一共执行5次
function lua_fun()
local timer_id = ssr.timer_create(1000, 5, "cb_function")
--用创建计时器时返回的timer_id作为参数开始计时器
ssr.timer_start(timer_id)
end
停止一个已经运行的计时器 (停止后,计时器不再触发回调,但不会销毁计时器)
timer_id:计时器ID(ssr.timer_create函数的返回值)
NONE
local timer_id
-- 定义回调函数
function cb_function()
ssr.print("cb_function")
end
--创建一个计时器,1s执行1次一共执行5次
function lua_fun()
timer_id = ssr.timer_create(1000, 5, "cb_function")
--用创建计时器时返回的timer_id作为参数开始计时器
ssr.timer_start(timer_id)
end
--暂停一个计时器
function cb_stop_timer()
ssr.timer_stop(timer_id)
end
销毁一个已经存在的计时器,销毁的计时器timer_id不可继续使用
timer_id:创建计时器时返回的标志位
NONE
local timer_id
function cb_function()
ssr.print("cb_function")
end
--创建一个计时器,1s执行1次一共执行5次
function lua_fun()
timer_id = ssr.timer_create(1000, 5, cb_function)
--用创建计时器时返回的timer_id作为参数开始计时器
ssr.timer_start(timer_id)
end
--销毁一个计时器
function cb_destory_timer(timer_id)
ssr.timer_destory(timer_id)
end
创建一个动画,它将返回一个动画ID
NONE
int类型,在以后调用的动画中所使用的ID
function animation_create()
lcoal animation_id = ssr.animation_create()
--定义动画所需的步骤
local animation_step_table = {}
animation_step_table["node"] = "Layer0/Item0"
animation_step_table["propertyType"] = "X"
animation_step_table["from"] = "0"
animation_step_table["to"] = "1000"
animation_step_table["duration"] = "1000"
animation_step_table["delay"] = "0"
animation_step_table["easingType"] = "None"
animation_step_table["cbkFunName"] = ""
--添加动画所需的步骤
ssr.animation_add_step(animation_id, animation_step_table)
--触发动画开始
ssr.animation_trigger(animation_id)
end
为已创建的动画添加步骤,animation_id是ssr.animation_create()返回的动画id,table定义动画的参数
animation_id:动画id
table:一个动画的参数,其中包括:
node:节点的路径
propertyType:节点的属性名
from:动画的起始值
to:动画的结束值
delay:延时开始动画的时间(msec)
duration:动画执行的时间(msec)
easingType:动画执行的缓动曲线
cbkFunName:动画结束后执行的函数
NONE
function animation_create()
lcoal animation_id = ssr.animation_create()
--定义动画所需的步骤
local animation_step_table = {}
animation_step_table["node"] = "Layer0/Item0"
animation_step_table["propertyType"] = "X"
animation_step_table["from"] = "0"
animation_step_table["to"] = "1000"
animation_step_table["duration"] = "1000"
animation_step_table["delay"] = "0"
animation_step_table["easingType"] = "None"
animation_step_table["cbkFunName"] = ""
--添加动画所需的步骤
ssr.animation_add_step(animation_id, animation_step_table)
--触发动画开始
ssr.animation_trigger(animation_id)
end
销毁一个已经创建的动画
animation_id:销毁的动画id
NONE
function animation_create()
lcoal animation_id = ssr.animation_create()
--定义动画所需的步骤
local animation_step_table = {}
local animation_step_table = {}
animation_step_table["node"] = "Layer0/Item0"
animation_step_table["propertyType"] = "X"
animation_step_table["from"] = "0"
animation_step_table["to"] = "1000"
animation_step_table["duration"] = "1000"
animation_step_table["delay"] = "0"
animation_step_table["easingType"] = "None"
animation_step_table["cbkFunName"] = ""
--添加动画所需的步骤
ssr.animation_add_step(animation_id, animation_step_table)
--销毁动画
ssr.animation_destory(animation_id)
end
触发一个已经创建的动画
animation_id:动画id
NONE
function animation_create()
lcoal animation_id = ssr.animation_create()
--定义动画所需的步骤
local animation_step_table =
{
{node = "Layer0/Item0", propertyType = "X", from = 0, to = 1000, delay = 0, duration = 16, easingType = "NONE", cbkFunName = ""},
{node = "Layer0/Item0", propertyType = "X", from = 1000, to = 0, delay = 0, duration = 16, easingType = "NONE", cbkFunName = ""},
}
--添加动画所需的步骤
for k, v in pairs(animation_step_table) do
ssr.animation_add_step(animation_id, v)
end
--触发动画开始
ssr.animation_trigger(animation_id)
end
设置语言类型
type:int类型,设置当前的语言类型(类型通过多语言工具获取)
NONE
function set_language_fun()
--设置第一个语言类型
ssr.set_language_type(0)
end
设置语言类型
NONE
type:int类型,获取当前的语言类型(类型通过多语言工具获取)
function set_language_fun()
--获取当前的语言类型
local type = ssr.get_language_type()
end
给某个节点绑定一个ssr系统自带的事件,当触发该事件时会调用回调函数
path: 节点的路径
event: 节点要绑定的事件类型
cb_function: 事件触发时调用的函数
NONE
function onReleaseEvent()
ssr.print("onReleaseEvent")
end
function event_binding_fun()
--添加一个"Touch0"节点,绑定"Release"事件,注册一个回调函数"onReleaseEvent"
ssr.event_binding("Layer0/Item0/Touch0","Released","onReleaseEvent")
end
自定义一个事件,配合ssr.send()使用
eventName: 字符串类型, 定义一个事件名称
cb_function: 定义一个回调函数,当有eventName消息发送时会调用cb_function函数
NONE
function cb_function(table)
ssr.print(table["eventParam"])
--table["eventParam"]:接收的事件参数,返回的是"param"
end
function event_define_fun()
--eventName:事件的名称
--cb_function:回调的函数
ssr.event_relation_define("eventName", "cb_function")
end
function event_trigger_fun()
--发送一个名叫"eventName"的事件,参数是"param"
ssr.event_trigger("eventName","param")
end
发送一个事件消息配合ssr.event_relation_define()
eventName: 字符串类型, 发送一个消息的名称
param: 字符串类型,发送的消息内容
NONE
function cb_function(table)
ssr.print(table["eventParam"])
--table["eventParam"]:接收的事件参数,返回的是"param"
end
function event_define_fun()
--eventName:事件的名称
--cb_function:回调的函数
ssr.event_relation_define("eventName", "cb_function")
end
function event_trigger_fun()
--发送一个名叫"eventName"的事件,参数是"param"
ssr.event_trigger("eventName","param")
end
设置一个container工程
注:Container工程文件加载路径位于主工程的根目录
path:container工程的路径
containerProjName:container的工程名字
int类型,返回0代表设置成功,返回-1代表设置失败
function lua_fun()
--Container0:表示container的路径
--ContainerProName:表示container的工程名字
ssr.container_set_file("Container0","ContainerProName")
end
移动Layer节点和Page节点的显示层级
nodePath:字符串类型,Layer节点或者Page节点的路径(用于多个container的显示层级处理)
index:int类型,节点的显示层级,0表示显示层级在最底层,-1表示显示层级在最顶层
int类型,返回0代表设置成功,返回-1代表设置失败
function lua_fun()
--Container0:表示container的路径
--index:表示container的工程名字
ssr.move_page_child("Container0",index)
end
移动Layer下的节点显示层级
nodeParentPath:字符串类型,父节点的路径
nodeSubPath:字符串类型,子节点的路径
index:int类型,节点的显示层级,0表示显示层级在最底层,-1表示显示层级在最顶层
int类型,返回0代表设置成功,返回-1代表设置失败
function lua_fun()
--layer0:表示父节点的路径
--Item0:表示子节点的路径
ssr.move_child("layer0","layer0/Item0",0)
end
获取主工程的根目录路径
NONE
字符串类型,返回主工程所在的路径
function lua_fun()
--path表示返回的当前工程所在的路径
local path = ssr.get_root_path()
ssr.print(path)
end
--获取外部Lua脚本路径
local newPath = ssr.get_root_path().."/resource/luaScript/?.lua;" .. package.path
package.path = newPath
--初始化外部脚本
require("LuaName")
强制关闭ssRender应用程序
NONE
NONE
function exit_fun()
--强制关闭当前的应用程序
ssr.exit()
end
设置当前的语言类型(前提条件:多语言表中需要存在对应的语种)
Index:int类型,多语言表中语种索引值
int类型,返回1代表设置成功,返回-1代表设置失败
function lua_fun()
ssr.language_set_type(0)
end
获取当前的语种类型
NONE
int类型,返回大于等于0的数字则代表获取成功,返回-1代表获取失败
function lua_fun()
--LanGetRet用于接收返回值
local LanGetRet = ssr.language_get_type()
ssr.print(LanGetRet)
end
触发状态机的状态
stateMachineName:字符串类型,状态机名称
stateName:字符串类型,状态名称
nameSpace:字符串类型,状态机路径(主工程则设置为空,Container工程则设置对应的Container名称)
int类型,返回0则代表设置成功,返回-1代表设置失败
function gotoState_fun()
--LanGetRet用于接收返回值
local StateRet = ssr.goto_state("stateMachineName","stateName","")
ssr.print(StateRet)
end
获取代码运行的操作系统
NONE
字符串类型
返回字符串则代表获取成功
"OS_Windows" -- 代码运行在Windows操作系统上
"__ANDROID__" -- 代码运行在Android操作系统上
"__QNX__" -- 代码运行在QNX操作系统上
"__linux__" -- 代码运行在Linux操作系统上
"OS_Other" -- 代码运行在其他操作系统上
返回-1代表获取失败
function getOS_fun()
--retOS用于接收返回值
local retOS = ssr.get_os_type()
ssr.print("Path: " ..retOS)
end
读取歌词文件和播放音乐文件并将其设置到对应的显示节点,再通过对节点的动态UI效果设置,实现音乐播放和歌词实时同步的效果
具体实现功能 1:音乐播放功能 2:歌词同步功能 3:歌词动态动画 4:动态背景颜色 5.其它动态UI效果(歌词旋转动画、音乐符号浮动效果)
local g_textRx = 0.00 -- 歌词旋转角度(X轴)
local g_textOp = 0.00 -- 歌词透明度
-- 音乐背景函数依赖
local g_count = 0 -- 音乐符号浮动计数器
-- 音乐背景图标ID
local g_musicPngTimerId = -1 -- 音乐符号计时器ID
-- 音乐背景颜色数组
local g_bgColorArray = {"#0026FFFF", "#FF0000FF", "#00FF21FF", "#FF00DCFF", "#FF6A00FF", "#00FFFFFF", "#0094FF80"}
-- 当前背景颜色索引
local g_bgColorIndex = 1
-- 全局计时器ID
local g_timerId
-- 初始化属性
function initProperty_fun()
ssr.set_property("Layer0/BgBlack/Scale", "1")
ssr.set_property("Layer0/BgBlack/Rotation", "-4")
ssr.set_property("Layer0/BgBlack/MusicLogo/Y", "120")
ssr.set_property("Layer0/Scene0/Node0/P_Z", "-53.00")
end
-- 绑定事件
function bindEvents_fun()
ssr.event_binding("Layer0/Touch0", "Clicked", "checkAndExecuteEvent_fun")
ssr.event_binding("Layer0/Touch3", "Clicked", "playMusicAndLyrics_fun")
ssr.print("--1.事件绑定成功!")
end
-- 事件执行
function checkAndExecuteEvent_fun()
createRotationTimer_fun()
ssr.print("--2.功能调用成功!")
end
-- 创建旋转计时器
function createRotationTimer_fun()
g_timerId = ssr.timer_create(2000, -1, "updateBackgroundColor_fun")
ssr.timer_start(g_timerId)
ssr.print("--3.计时器启动!")
end
-- 更新背景颜色
function updateBackgroundColor_fun()
processBackgroundColor_fun()
g_textRx = ssr.get_property("Layer0/Scene0/Node0/Music3D/R_X")
end
-- 歌词旋转状态1
function rotateTextState1_fun()
g_textRx = ssr.get_property("Layer0/Scene0/Node0/Music3D/R_X")
local nodeRy = ssr.get_property("Layer0/Scene0/Node0/Music3D/R_Y")
g_textOp = ssr.get_property("Layer0/Scene0/Node0/Music3D/Opacity")
local ry1AnimationId = ssr.animation_create()
local animationSteps = {
{node = "Layer0/Scene0/Node0/Music3D", propertyType = "R_X", from = tonumber(g_textRx), to = "-360.00", delay = 0, duration = 500, easingType = "None", cbkFunName = ""},
}
for _, step in pairs(animationSteps) do
ssr.animation_add_step(ry1AnimationId, step)
end
ssr.animation_trigger(ry1AnimationId)
end
-- 歌词旋转状态2
function rotateTextState2_fun()
g_textRx = ssr.get_property("Layer0/Scene0/Node0/Music3D/R_X")
g_textOp = ssr.get_property("Layer0/Scene0/Node0/Music3D/Opacity")
local ry2AnimationId = ssr.animation_create()
local animationSteps = {
{node = "Layer0/Scene0/Node0/Music3D", propertyType = "R_X", from = tonumber(g_textRx), to = "0.00", delay = 0, duration = 1000, easingType = "None", cbkFunName = ""},
}
for _, step in pairs(animationSteps) do
ssr.animation_add_step(ry2AnimationId, step)
end
ssr.animation_trigger(ry2AnimationId)
end
-- 设置音乐符号浮动计时器
function setMusicSymbolFloatTimer_fun()
if g_musicPngTimerId == -1 then
g_musicPngTimerId = ssr.timer_create(1000, -1, "updateMusicSymbolPosition_fun")
end
ssr.timer_start(g_musicPngTimerId)
end
-- 更新音乐符号位置
function updateMusicSymbolPosition_fun()
g_count = g_count + 1
local index = g_count % 2
if index == 0 then
ssr.set_property("Layer0/Item1/Y", "95")
else
ssr.set_property("Layer0/Item1/Y", "310")
end
end
-- 处理背景颜色
function processBackgroundColor_fun()
if g_bgColorIndex > #g_bgColorArray then
g_bgColorIndex = 1
ssr.print("--重置背景颜色数组索引!")
end
local value = g_bgColorArray[g_bgColorIndex]
ssr.print("--背景颜色值:" .. value)
ssr.set_property("Layer0/BgColor/Color", value)
ssr.print("--背景颜色设置完成!")
g_bgColorIndex = g_bgColorIndex + 1
end
-- 播放音乐和歌词
function playMusicAndLyrics_fun()
local g_rootPath = ssr.get_root_path()
local g_lyrics = loadLyrics_fun(g_rootPath .. "/resource/luaScript/song_lyrics.txt")
local g_musicFile = g_rootPath .. "/resource/luaScript/song.mp3"
ssr.print("成功加载 " .. #g_lyrics .. " 行歌词")
playMusic_fun(g_musicFile)
ssr.print("等待1.2秒,确保音乐播放器加载完成...")
waitForMusicPlayer_fun(1200, function()
playLyrics_fun(g_lyrics)
end)
end
-- 等待音乐播放器
function waitForMusicPlayer_fun(intervalMs, callback)
destroyActiveTimer_fun()
g_activeTimerId = ssr.timer_create(intervalMs, 1, "onMusicPlayerLoadComplete_fun")
ssr.timer_start(g_activeTimerId)
ssr.print("音乐播放器加载计时器启动: " .. tostring(g_activeTimerId))
g_onMusicPlayerLoadCompleteCallback = callback
end
-- 音乐播放器加载完成
function onMusicPlayerLoadComplete_fun()
destroyActiveTimer_fun()
ssr.print("音乐播放器加载完成")
if g_onMusicPlayerLoadCompleteCallback then
g_onMusicPlayerLoadCompleteCallback()
end
end
-- 播放音乐
function playMusic_fun(musicFile)
local command = 'start wmplayer "' .. musicFile .. '"'
os.execute(command)
end
-- 解析时间戳
function parseTimestamp_fun(timestamp)
local min, sec, ms = timestamp:match("(%d+):(%d+).(%d+)")
return tonumber(min) * 60 + tonumber(sec) + tonumber(ms) / 1000
end
-- 加载歌词
function loadLyrics_fun(filename)
local g_lyrics = {}
local g_file = io.open(filename, "r")
if g_file then
for line in g_file:lines() do
local timestamp, lyric = line:match("%[([%d:%d%.]+)%](.*)")
if timestamp and lyric then
local timeInSeconds = parseTimestamp_fun(timestamp)
table.insert(g_lyrics, { time = timeInSeconds, text = lyric })
ssr.print("加载歌词: " .. timestamp .. " - " .. lyric)
end
end
g_file:close()
else
ssr.print("无法打开文件: " .. filename)
end
return g_lyrics
end
-- 播放歌词
function playLyrics_fun(lyrics)
if #lyrics == 0 then
ssr.print("没有歌词可以播放")
return
end
g_playLyricsState = { lyrics = lyrics, currentLine = 1, totalLines = #lyrics }
ssr.print("开始播放歌词...")
scheduleNextLyric_fun()
end
-- 安排下一行歌词
function scheduleNextLyric_fun()
local state = g_playLyricsState
if state.currentLine > state.totalLines then
destroyActiveTimer_fun()
return
end
local currentLyric = state.lyrics[state.currentLine]
ssr.set_property("Layer0/Scene0/Node0/Music3D/Text3D", currentLyric.text)
if state.currentLine + 1 <= state.totalLines then
g_textRx = ssr.get_property("Layer0/Scene0/Node0/Music3D/R_X")
if g_textRx == "0.00" then
rotateTextState1_fun()
elseif g_textRx == "-360.00" then
rotateTextState2_fun()
end
end
state.currentLine = state.currentLine + 1
if state.currentLine <= state.totalLines then
local nextLyric = state.lyrics[state.currentLine]
local delay = (nextLyric.time - currentLyric.time) * 1000
destroyActiveTimer_fun()
g_activeTimerId = ssr.timer_create(delay, 1, "updateLyric_fun")
ssr.timer_start(g_activeTimerId)
else
destroyActiveTimer_fun()
end
end
-- 更新歌词回调
function updateLyric_fun()
destroyActiveTimer_fun()
scheduleNextLyric_fun()
end
-- 销毁当前计时器
function destroyActiveTimer_fun()
if g_activeTimerId then
ssr.print("销毁当前计时器,ID: " .. tostring(g_activeTimerId))
ssr.timer_stop(g_activeTimerId)
ssr.timer_destory(g_activeTimerId)
g_activeTimerId = nil
end
end
-- 程序入口
function OnCreateUIEnd_Fun()
createRotationTimer_fun()
playMusicAndLyrics_fun()
initProperty_fun()
bindEvents_fun()
end
-- 启动程序
OnCreateUIEnd_Fun()
版本1.0.0:[20240913] - 初始版本,包含Animation函数、Timer函数和get函数等
版本1.0.1:[20241115] -
更新版本,更新多语言接口描述、修正部分接口参数及描述信息错误问题