One button for showing constellation scheme and name

The place to discuss creating, porting and modifying Celestia's source code.
Avatar
selden
Developer
Posts: 10192
Joined: 04.09.2002
With us: 22 years 2 months
Location: NY, USA

Post #21by selden » 26.01.2008, 13:36

Imy wrote:I've still problems please:? :
[...]
-I'm often coding with VB language: in this you have lcase/ucase function, i suppose these kind of functions exists also in Lua. These functions change case of a string : you have "HeLLo", and then this string with lcase become "hello". What is the equivalent in lua language please?

According to the Lua manual at
http://www.lua.org/manual/5.1/index.html#contents
in the chapter on string manipulation at
http://www.lua.org/manual/5.1/manual.html#5.4
string.lower (s)
Receives a string and returns a copy of this string with all uppercase letters changed to lowercase. All other characters are left unchanged. The definition of what an uppercase letter is depends on the current locale.
and
string.upper (s)
Receives a string and returns a copy of this string with all lowercase letters changed to uppercase. All other characters are left unchanged. The definition of what a lowercase letter is depends on the current locale.

Hopefully someone else will answer your other questions.
Selden

Vincent
Developer
Posts: 1356
Joined: 07.01.2005
With us: 19 years 10 months
Location: Nancy, France

Post #22by Vincent » 26.01.2008, 15:26

Imy wrote:-How to desactivate keypress callback when enter panel is shown (where you tape your destination)
You can add a test to know when [Return] is pressed, and set requestkeyboard to false then. However, you'll have to find a way to set it back to true when [Return] is pressed again. But that might be quite tricky since keypress callback will be disabled then.

Code: Select all

--About Text Enter Mode
elseif str == "\r" then
  celestia:requestkeyboard(false)


Imy wrote:-What about setlabelflags? I've changed hidelabel and showlabel functions with setlabelflags as suggested so that I can use table instead of strings in its parameters. But when I've written celestia:setlabelflags {Planet=true}, well nothing, no planet labels although these ones can be shown with classic showlabel...
Labelflags should be uncapitalized and plural:

Code: Select all

celestia:setlabelflags {planets=true}
@+
Vincent

Celestia Qt4 SVN / Celestia 1.6.1 + Lua Edu Tools v1.2
GeForce 8600 GT 1024MB / AMD Athlon 64 Dual Core / 4Go DDR2 / XP SP3

Topic author
Imy
Posts: 109
Joined: 13.10.2006
With us: 18 years 1 month

Post #23by Imy » 28.01.2008, 14:14

Setorbitflags is singular and capitalized and setlabelflags is
plural and not capitalized?Why? Could we use numbers instead of these strings so that we can change the same class in each function?

Topic author
Imy
Posts: 109
Joined: 13.10.2006
With us: 18 years 1 month

Post #24by Imy » 30.01.2008, 10:43

When the destination command line is shown and you press escape to hide it (and cancel the line), you also desactivate your script. Does exist a solution to avoid desactivating my script in this case? Could we intercept key escape message? What is the keycode for escape (it seems that 027 doesn't run...)?

Topic author
Imy
Posts: 109
Joined: 13.10.2006
With us: 18 years 1 month

Post #25by Imy » 31.01.2008, 09:13

We have already talk about using SHIFT on letters, but what about other keys, for instance /? How to use CTRL button? I've read that you have to use numbers less 32 but I don't know more. Could you help me?

Topic author
Imy
Posts: 109
Joined: 13.10.2006
With us: 18 years 1 month

Post #26by Imy » 01.02.2008, 09:59

:?: Can Celestia script really detect when shift button is pressed? Or keypress callback function can only detects only characters ?

Despite that, I decided to post my final and last cyclekey script to show how cycles are interesting. I changed it a lot, considering functions, correcting bugs and commenting it. I added a new class (comets), and a general level management for orbits and labels. I also made a bit of code for managing selected orbit. Constellation borders are also now managed. The script key map has changed and i think it's easier to use now. Key map is also opened for new classes. I've selected keys considering that shift button as a way to get linked feature with the function of the normal key : i think it's easier to remember these shortcuts. I internationalized this script, using US keyboard : you may have to adapt key map script to your keyboard... :( (Read script comments for more information about keyboard)

What can add else? I don't know if this script will be useful for someone but at least for me, it was a good exercise to learn lua language with celestia librairy ! I use it and it's difficult to get back on classic management of orbits and labels ! :wink: Test yourself !
See my tutorial in comments to get an overview of script functions.

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.0
--
-- 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 other 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 are drawn only when others orbits are visible,
--       this key allows to show selected orbits 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.
--
--
-- 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=="\013" 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 orbits 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 “Development”