跳转到内容

Celestia/Celx 脚本/CELX Lua 方法/回调

来自维基教科书,自由的教科书

boolean celestia_keyboard_callback(string:char)

键盘输入的回调函数必须命名为“celestia_keyboard_callback”。当脚本通过调用 celestia:requestkeyboard(true)(参见 celestia:requestkeyboard())激活键盘输入处理后,任何按键都会导致对该名称的函数调用。

回调函数可以返回 true 或 false,分别表示它处理了按键或 Celestia 应该继续处理该按键。不返回值与返回 true 相同。

参数

char
该函数应该接受一个参数,一个字符串,其中包含被按下的键的字符。
  • 带重音的字符(如 ä 或 é)将作为 UTF-8 序列传递,即作为多字节序列 - 如果你只想要处理 ASCII 字符,可以安全地忽略这一点。
  • 像 CTRL-A 这样的键作为包含 ASCII 码小于 32 的字符的字符串传递,Enter (13) 或 Backspace (8) 也是如此。

备注

  1. 一些特殊按键,如光标键或 [Esc] 键,不会触发回调。
  2. 如果在处理回调时发生错误,错误消息将被写入控制台日志(通过键盘上的波浪号 [~] 键显示,或者在某些系统上使用 [~] + [空格键] 显示),并且隐式返回 false,即 Celestia 将继续处理该按键。

示例
以下示例代码可以用于在用户按下键盘上的 [1] 或 [2] 键时运行一些特定的 CELX 代码。

-- Initialize variable to have no content:
last_pressed_key = nil

-- Define functions section:
function celestia_keyboard_callback(key)
   last_pressed_key = key
   return true
end

function get_pressed_key()
   last_pressed_key = nil
   celestia:requestkeyboard(true)
   while true do
      if last_pressed_key ~= nil then
         key = last_pressed_key
         last_pressed_key = nil
         celestia:requestkeyboard(false)
         return key
      end
      wait(0.1)
   end
end

-- Main CELX script section:

-- Section within your script, where you want to handle keyboard input:
key = get_pressed_key()
if key == "1" then
   valid_key = true
   celestia:print("Pressed the [1] key", 5.0, -1, -1, 2, 4)
   wait(5.0)
   -- You can add more CELX code here, specific to the [1] key
elseif key == "2" then
   valid_key = true
   celestia:print("Pressed the [2] key", 5.0, -1, -1, 2, 4)
   wait(5.0)
   -- You can add more CELX code here, specific to the [2] key
else
   valid_key = false
   celestia:print("Pressed invalid key", 5.0, -1, -1, 2, 4)
   wait(0.1)
end

if valid_key then
   celestia:print("The [1] and [2] keys are valid keys", 5.0, -1, -1, 2, 4)
   wait(5.0)
   -- You can add more CELX code here, specific to both the [1] and [2] keys
end

function celestia_cleanup_callback()

每当 Lua 脚本终止(因为脚本结束,用户通过按下 [Esc] 键终止脚本,或者发生错误),Celestia 将尝试执行名为“celestia_cleanup_callback”的函数。

这个函数可以用来执行任何清理操作,特别是将设置恢复到脚本启动之前的使用值。在一个 CELX 脚本中,许多 Celestia 选项可能设置得与用户的标准设置不同。通过使用这个清理回调函数,可以将设置重置到运行脚本之前的状态。

使用这个清理函数对编写用户友好的 CELX 脚本至关重要!

备注

  1. 错误只会报告到控制台日志中(通过键盘上的波浪号 [~] 键显示,或者在某些系统上使用 [~] + [空格键] 显示)。

示例
以下示例代码可以标准地插入到任何 CELX 脚本的开头(使用 Celestia 版本 1.6.1 或更高版本),以将 Celestia 设置恢复到运行脚本之前的状态,并在 CELX 脚本中将这些设置初始化为你喜欢的状态。

注意:当使用旧版本的 Celestia 时,请注意示例中的注释,以删除一些代码,这样示例也能在 Celestia 版本 1.5.0、版本 1.5.1 和版本 1.6.0 中使用。

-- Define Cleanup function to restore the settings
-- to the way they before running the script.

function celestia_cleanup_callback()
   celestia:setrenderflags(orig_renderflags)
   celestia:setlabelflags(orig_labelflags)
   celestia:setorbitflags(orig_orbitflags)
   celestia:setambient(orig_amb)
   celestia:setfaintestvisible(orig_faintest)
   celestia:setminorbitsize(orig_minorbit)
   celestia:setstardistancelimit(orig_stardistance)
   celestia:setminfeaturesize(orig_minfeature)
   celestia:setstarstyle(orig_starstyle)
   celestia:settime(orig_time + (celestia:getscripttime() / 24 / 3600) )
   celestia:settimescale(orig_timescale)
   celestia:setgalaxylightgain(orig_galaxylight)
   celestia:getobserver():setfov(orig_fov)
   celestia:getobserver():setlocationflags(orig_locationflags)
   celestia:getobserver():singleview()
   celestia:setoverlayelements(orig_overlay)
   celestia:setaltazimuthmode(orig_altazimuthmode)
   -- Comment next line if NOT using Celestia v1.6.0 or later
   celestia:settextureresolution(orig_textres)
   -- Comment next line if NOT using Celestia v1.6.1 or later
   celestia:settextcolor(orig_txt_r,orig_txt_g,orig_txt_b)
end

-- immidiatily followed by the following code to obtain the
-- current user settings and save them during the script.

orig_renderflags                 = celestia:getrenderflags()
orig_labelflags                  = celestia:getlabelflags()
orig_orbitflags                  = celestia:getorbitflags()
orig_amb                         = celestia:getambient()
orig_faintest                    = celestia:getfaintestvisible()
orig_minorbit                    = celestia:getminorbitsize()
orig_stardistance                = celestia:getstardistancelimit()
orig_minfeature                  = celestia:getminfeaturesize()
orig_starstyle                   = celestia:getstarstyle()
orig_time                        = celestia:gettime()
orig_timescale                   = celestia:gettimescale()
orig_galaxylight                 = celestia:getgalaxylightgain()
orig_fov                         = celestia:getobserver():getfov() 
orig_locationflags               = celestia:getobserver():getlocationflags()
orig_overlay                     = celestia:getoverlayelements()
orig_altazimuthmode              = celestia:getaltazimuthmode()
-- Comment next line if NOT using Celestia v1.6.0 or later
orig_textres                     = celestia:gettextureresolution()
-- Comment next line if NOT using Celestia v1.6.1 or later
orig_txt_r,orig_txt_g,orig_txt_b = celestia:gettextcolor()

-- The following initialization part can be customized
-- by the wishes of the CELX writer to the way he/she
-- wants the settings to be during the script.

celestia:setorbitflags{Planet = true,
                       Moon = false,
                       Asteroid = false,
                       Comet = false,
                       Spacecraft = false,
                       Invisible = false,
                       Unknown = false,
                       DwarfPlanet = false,     -- Comment line if NOT using Celestia v1.6.0 or later
                       MinorMoon = false,       -- Comment line if NOT using Celestia v1.6.0 or later
                       Star = false             -- Comment line if NOT using Celestia v1.6.0 or later
                      }

celestia:setrenderflags{atmospheres = true,
                        automag = true,
                        boundaries = false,
                        cloudmaps = true,
                        cloudshadows = true,
                        comettails = true,
                        constellations = false,
                        eclipseshadows = true,
                        galaxies = true,
                        grid = false,
                        markers = false,
                        nebulae = false,
                        nightmaps = true,
                        orbits = false,
                        planets = true,
                        ringshadows = true,
                        stars = true,
                        smoothlines = true,
                        lightdelay = false,
                        partialtrajectories = false,
                        ecliptic = false,       -- Comment line if NOT using Celestia v1.6.0 or later
                        equatorialgrid = false, -- Comment line if NOT using Celestia v1.6.0 or later
                        galacticgrid = false,   -- Comment line if NOT using Celestia v1.6.0 or later
                        eclipticgrid = false,   -- Comment line if NOT using Celestia v1.6.0 or later
                        horizontalgrid = false  -- Comment line if NOT using Celestia v1.6.0 or later
                       }

celestia:setlabelflags{planets = false,
                       moons = false,
                       spacecraft = false,
                       asteroids = false,
                       comets = false,
                       stars = false,
                       galaxies = false,
                       locations = false,
                       constellations = false,
                       i18nconstellations = false,
                       openclusters = false,
                       nebulae = false,
                       dwarfplanets = false,    -- Comment line if NOT using Celestia v1.6.0 or later
                       minormoons = false,      -- Comment line if NOT using Celestia v1.6.0 or later
                       globulars = false        -- Comment line if NOT using Celestia v1.6.0 or later
                      }

celestia:setambient(0.05)
celestia:setfaintestvisible(7)

-- Make your standard Star Style choice below by 
-- commenting/uncommenting one of the following lines:
celestia:setstarstyle("point")
-- celestia:setstarstyle("fuzzy")
-- celestia:setstarstyle("disc")

-- End of initialization part of the script
华夏公益教科书