跳轉到內容

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
華夏公益教科書