Cycle keys, showing orbits and constellation

All about writing scripts for Celestia in Lua and the .cel system
Topic author
Imy
Posts: 109
Joined: 13.10.2006
With us: 18 years 3 months

Cycle keys, showing orbits and constellation

Post #1by Imy » 07.05.2008, 13:51

Hi,

I post a final test script that I wrote to explain myself about what I called cycle keys to show orbits, object names, and constellation using only one key. This was an easy way for me to show these elements quickly , try it! All comments are inside the file. I've already posted it in a losted thread of this forum that's why i'm posting it in this special thread for scripts.

The script may interest someone :wink: :

Code: Select all

--[Script For Celestia (work base version : 150)]
--
-- Script to use cycle keys for showing constellations, constellation borders, orbits and labels.
--
-- Author: Imy, 02/2008 1.1
--
-- You are free to use, copy, modify, redistribute or do whatever you want with this script,
-- but a credit to the original author would be nice.
--
--
-- Shows constellation name and scheme using the same key (/). This runs also with orbit and 
-- name of different object classes like planet (1 or p keys), moon (2),spacecraft (3), asteroid(4),
-- comet (5).
-- Using Shift key with a normal key allows others linked features: for constellations, shift
-- button draws or erases constellation boundaries.
-- General orbit and label management is kept: hide or show last orbits drawn (0=zero), hide or
-- show last labels drawn (SHIFT+0).
-- Can force selected orbit (o) to be shown when it’s not necessary (otherwise orbit is
-- automatically drawn).
--
--
-- Specific Keys (base on Standard US key map):
-- about constellations :
-- > / : cycle for showing constellation names and schemes
-- > SHIFT + / (US key is '?'): show or hide constellation boundaries
-- about classes with orbits and labels
-- > 1 or p : cycle for showing planet names and orbits
-- > 2 : cycle for showing moon names and orbits
-- > 3 : cycle for showing spacecraft names and orbits
-- > 4 : cycle for showing asteroid names and orbits
-- > 5 : cycle for showing comet names and orbits
-- about general control for showing orbits and labels
-- > 0 (zero): Hide or show last orbit drawn if there are some
-- > SHIFT + 0 (zero) (US key is ')') : Hide or show last labels drawn if there are some
-- about parameters :
-- > o : Commonly, the orbit of a selected object is drawn only when others orbits are visible,
--       this key allows to show selected orbit whenever.
-- WARNING: Class buttons are above letters on keyboard. No shift is needed on US keyboard to
--           get numbers. SHIFT + O (from classic key map) is deactivated. (see known limitations)
--
--
-- How to test this script?
-- Paste the file in main Celestia directory in /script;
-- Launch Celestia program;
-- Load the script: {Celestia Menu} File>Scripts> (Name of this file);
-- -
-- Wait for the script is loaded (string must appear);
--    First about constellations:
--       Click several times with / key: first constellation names appear, then diagrams and
--       then only diagrams, and finally nothing. It restarts after. You have performed a cycle.
--      Usually, Shift button allows linked features: Do SHIFT + / and constellation borders
--          appear. A second time, they will disappear.
--    In a second time, object classes:
--      Cycles are similar with those of constellations except that instead of diagrams, it's
--          orbit lines. Classes defined are planets (with 1 or p keys), moons, spacecraft, asteroids
--          and comets. All classes have its own colour.
--   In a third time, general level management for orbits and labels:
--          Restart the script clicking again its filename in menu. All has been initialized. Show
--          planet orbits and labels and asteroid (key 4) orbits only. Get all the view in Celestia
--          Window.
--          Then press 0 (zero), and all orbits hide. Press SHIFT + 0 (zero) and all visible labels
--          will be masked. If you press again each of them, you get back keeping exactly what
--          you have just before.
--    Finally, just to finish my script tutorial, secondary parameter:
--          Restart again the script to initialize it (it's easier to explain then). Show
--      planet orbits and their labels. Select Earth and get all orbits in your window. Earth
--      orbit is red because this is the selected orbit. If you continue the planet cycle, you
--          have (pressing p key...) only orbits, and then nothing. Selected orbit is not still
--      visible. This is the program that manages when selected orbit must be drawn : when there
--      are other orbits drawn. But you can tell to Celestia to show the selected orbit whenever
--          pressing o key and so selected orbit will be always visible.
-- All stuffs for what the script was designed are there.
--
--
-- Code notes:
--   Parts :
--      Tool part ;
--      Main part ;
--    Specific functions:
--         SetLabelRenderFlags ;
--       SetOrbitRenderFlags ;
--      EquivalentClass ;
--      GetCycleIndex ;
--      DetectOrbit ;
--      DetectLabel ;
--      ManageCycle ;
--      GeneralLevelOrbit ;
--      GeneralLevelLabel ;
--      SetToMemory ;
--      GetFromMemory ;
--      celestia_keyboard_callback ;
--
-- "Cycle" is considered as all different steps that a cycled key has.
-- Cycle system is built on Celestia basic management of orbits and labels.
-- Constellation cycle is managed independently from others classes.
-- When target box appears, the script stops listenning to keys.
-- The script deactivates SHIFT+ O which, in classic key map, shows or hides orbits. 
--
-- Version :
-- 1.0 Initial
-- 1.1 Correct Enter key value
--
-- Known limitations:
-- *Some keys in this script can interfere with normal Celestia key map.
-- *Keys are defined for US standard keyboard: classes are identified with buttons above letters
-- without any shift! But other keyboards can have a different configuration where shift may be
-- used. According to your key map and your keyboard, changing script key map may be useful and
-- easier. I defined my key map with all numbers for each classes and zero for general level.
-- *This script detects when destination command line is activated. But changing related
-- things in option panel wasn't considered...
-- *When Escape is pressed, script is unloaded even when target command line hides...
-- *(it's only a test script, be indulgent please ;)!)
-------------------------------------------------------------------------------------------------

----------------
--TOOLS PART
----------------

--Shows/Hides all labels
function SetLabelRenderFlags (b)
   t=celestia:getlabelflags()   
   for class, flag in pairs(t) do
      t[class]=b
      celestia:setlabelflags(t)
   end
end


--Shows/Hides all orbits
function SetOrbitRenderFlags(b)
   t=celestia:getorbitflags()   
   for class, flag in pairs(t) do
      t[class]=b
      celestia:setorbitflags(t)
   end
end



--Determines equivalent class
function EquivalentClass (Class)
   ClassName=string.lower(Class)
   if ClassName=="planet" then
      return "planets"
   elseif ClassName=="asteroid" then
      return "asteroids"
   elseif ClassName=="moon" then
      return "moons"
   elseif ClassName=="spacecraft" then
      return "spacecraft"
   elseif ClassName=="comet" then
      return "comets"
   end
end

--Determines which cycle index it is
function GetCycleIndex (Class)
   t=celestia:getorbitflags()
   u=celestia:getlabelflags()
   
   if t[Class]==false and u[EquivalentClass(Class)]==true then
      return 1
   elseif t[Class]==true and u[EquivalentClass(Class)]==true then
      return 2
   elseif t[Class]==true and u[EquivalentClass(Class)]==false then
      return 3
   elseif t[Class]==false and u[EquivalentClass(Class)]==false then
      return 4
   else
      return -1
   end
end

--Detects if an orbit is currently drawn
function DetectOrbit(Class)
   Detected= false
   t=celestia:getorbitflags()   
   for class, flag in pairs(t) do
      if t[class]==true then Detected=true end
   end
   
   return Detected
end

--Detects if a label is currently drawn
function DetectLabel(Class)
   Detected=false
   t=celestia:getlabelflags()   
   for class, flag in pairs(t) do
      if t[class]==true then Detected=true end
   end
   
   return Detected
end

--Sets and manages cycles
function ManageCycle(Class)   
   t=celestia:getorbitflags()
   u=celestia:getlabelflags()
   CycleId=GetCycleIndex(Class)
   b=Class
   c=EquivalentClass(b)
   


   CycleId=CycleId+1
   
   if CycleId>4 then CycleId=1 end
   
   
   if CycleId==1 then
      t[b]=false      
      u[c]=true
      celestia:setorbitflags (t)
      celestia:setlabelflags(u)
      SetToMemory("Label")
   elseif CycleId==2 then      
      celestia:setrenderflags {orbits=true}      
      t[b]=true
      u[c]=true
      celestia:setorbitflags (t)
      celestia:setlabelflags(u)
      SetToMemory("Orbit")
      elseif CycleId==3 then      
      celestia:setrenderflags {orbits=true}
      t[b]=true
      u[c]=false      
      celestia:setorbitflags (t)
      celestia:setlabelflags(u)      
      SetToMemory("Label")
   elseif CycleId==4 then      
      t[b]=false
      u[c]=false
      celestia:setorbitflags (t)
      celestia:setlabelflags(u)
      SetToMemory("Orbit")
      if DetectOrbit()==false then         
         if ForceSelectedOrbit==false then
            celestia:setrenderflags{orbits=false}
            else
            celestia:setrenderflags{orbits=true}
         end
      else
         celestia:setrenderflags{orbits=true}
      end
   end

   
end


--General level management for orbits and labels
function GeneralLevelOrbit()
   if DetectOrbit()==true then
      SetOrbitRenderFlags (false)
      celestia:print("Orbits hidden")
   else
      GetFromMemory("Orbit")
      celestia:print("Last orbits shown")      
   end

   if DetectOrbit()==false then         
      if ForceSelectedOrbit==false then
         celestia:setrenderflags{orbits=false}
         else
         celestia:setrenderflags{orbits=true}
      end
   else
      celestia:setrenderflags{orbits=true}
   end

end

function GeneralLevelLabel()
   if DetectLabel()==true then
      SetLabelRenderFlags (false)
      celestia:print("Labels hidden")
   else
      GetFromMemory("Label")
      celestia:print("Last Labels shown")
   end

end



--Set in memory orbit/label flag table
function SetToMemory (LabelOrOrbit)
   Element=string.lower(LabelOrOrbit)
   if Element=="orbit" then
      MemOrbit=celestia:getorbitflags()
   elseif Element=="label" then
      MemLabel=celestia:getlabelflags()
   else
      return -1
   end
end

--Get from memory orbit/label flag table
function GetFromMemory (LabelOrOrbit)
   Element=string.lower(LabelOrOrbit)   
   if Element=="orbit" then
      celestia:print ("eee")
      celestia:setorbitflags(MemOrbit)
   elseif Element=="label" then
      celestia:setlabelflags(MemLabel)
   else
      return -1
   end
end


----------------
--MAIN PART
----------------

--Begins
CommandCycleIndexConst=4
ForceSelectedOrbit=false
MemOrbit=celestia:getorbitflags()
MemLabel=celestia:getlabelflags()


celestia:setrenderflags {orbits =false, constellations=false, boundaries=false}

SetLabelRenderFlags(false)
SetOrbitRenderFlags(false)

DestinationCommandLine=false

--Callback for keypresses (name is hardcoded in celestia)
function celestia_keyboard_callback(str)

   --Destination Command line (when you press enter)
   if str=="\r" then
      if DestinationCommandLine==true then
         DestinationCommandLine=false
      else
         DestinationCommandLine=true
      end
   end


   --Active keys
   if DestinationCommandLine==false then
      
      --Desactivates classic SHIFT+O
      if str=="\079" then
         return true
      end

      --Show or hide orbit general level(Key 0 (zero))
      if str=="\048" then
         GeneralLevelOrbit()
         return true
      end
      
      --Show or hide label general level(Keys SHIFT + 0 (zero)(US key map =))))
      if str=="\041" then
         GeneralLevelLabel()
         return true
      end

      --Force selected orbit to be shown (Keys o)
      if str == "\111" then
         if DetectOrbit()==false then
            orbitflags=celestia:getrenderflags()
            if orbitflags.orbits==true then
               ForceSelectedOrbit=false
            else
               ForceSelectedOrbit=true
            end
            celestia:setrenderflags {orbits=ForceSelectedOrbit}
         else
            ForceSelectedOrbit=not ForceSelectedOrbit
         end
         
         if ForceSelectedOrbit==true then
            celestia:print ("Force selected orbit to be shown")
         else
            celestia:print ("Manage automatically selected orbit")
         end

         return true
      end


      --About constellation borders (Keys SHIFT + / (US key map =?))
      if str=="\063" then
         boundariesflags=celestia:getrenderflags()
                  if boundariesflags.boundaries==true then
            celestia:hide("boundaries")
         else
            celestia:show("boundaries")
         end
         
         return true
      end
 

      --About constellation diagrams (Keys /)
      if str == "\047" then
   
            CommandCycleIndexConst=CommandCycleIndexConst+1
                 
   
            if CommandCycleIndexConst>4 then CommandCycleIndexConst=1 end
         
 
            if CommandCycleIndexConst==1 then
            celestia:hide("constellations")
               celestia:showlabel("constellations")
            elseif CommandCycleIndexConst==2 then
            celestia:show("constellations")
               celestia:showlabel("constellations")
            elseif CommandCycleIndexConst==3 then
            celestia:show("constellations")
               celestia:hidelabel("constellations")
            elseif CommandCycleIndexConst==4 then
            celestia:hide("constellations")
               celestia:hidelabel("constellations")
            end
         
         return true


      --About planets (Key p or 1)   
      elseif str == "\112" or str=="\049" then
            ManageCycle ("Planet")
         return true

      
      --About moons (Key 2)
      elseif str == "\050" then
            ManageCycle ("Moon")
         return true

      
      --About spacecrafts (Key 3)
      elseif str == "\051" then
            ManageCycle ("Spacecraft")
         return true


      --About asteroids   (Key 4)
         elseif str == "\052" then
            ManageCycle ("Asteroid")
         return true


      --About comets (Key 5)
      elseif str == "\053" then
            ManageCycle ("Comet")
         return true

         end 
   end

return false

end

-- enable keyboard-callback:
celestia:requestkeyboard(true)

celestia:flash("Cycle key example has started...")

-- and do nothing...
while true do
  wait(0)
end

----------------
--END OF SCRIPT
----------------




Return to “Scripting”