XI) Installation, Customization, and other considerationsTo install a new tool you have to write its name in the file "
config.lua"
There is a file "
config.lua" for Lua Plug-ins and another file for Lua Edu Tools which is different but whose name is also "config.lua"
I don't like this very much because these files are very sensitive and the install procedure is different for each one.
But unfortunately there is no way to do otherwise.
We are in the same situation when it comes with personalization or localization.
But in this case we can avoid the pitfall of touching sensitive files and the personalization procedure can be the same, whatever the environment.
What I propose is to create a dedicated config file for the pluging.
This way we can define how to handle the personalizations without worrying about managing the specifics of Lua Lua Edu Tools and Plug-ins.
The installation (and above all, uninstallation) becomes much simpler: we can consider the new tool as a simple addon rather than a part of Lua Plug-ins or Lua Edu Tools. Its place comes naturally in the "
extras" directory of Celestia.
If you have already custumized the Lua Plug-ins version by modifying the "enable_plugin" and the "key_shortcut" sections of the file "config.lua",
undo these modifications.
And forget this method
In my opinion this should be reserved for tools that are part of Lua Plug-ins and are installed with it.
If you convince Vincent that your tools is essential and must be integrated into the next version of Lua Plug-ins, you can return to the previous method.
Because we are about to use several files for the addon,
create a new sub-directory in the celestia/extras directory. Name it
separation_angles or any name you prefer. The name of the directory doesn't matter: Lua Plug-ins and Lua Edu Tools are able find your script anywhere in the "extras" tree directory.
Put the fle
sepAnglesBox.lua in the newly created directory.
We want the same customizations as before:
- The activation key (for the Lua Plug-ins version only)
- whether the tool is visible or not at startup (for Lua Edu and Lua Plug-ins)
As it is your own config file, you can add any customization you want.
For example let's add the custimization of the text color for the resulting display.
Code: Select all
-------------------------------------------------------------
-- config file for the separation angles addon
-- change the values to customize the tool
-- if an item is not defined, a default value will be used
-------------------------------------------------------------
-- activating key for Lua Plug-ins (no effect with Lua Edu Tools)
sepAngles_shortcut = "A"
-- indicates if the tool is visible at startup
sepAngles_visible = false
-- angle display color (red, green, blue, transparency)
sepAngles_color = {0.7, 0.7, 1, 0.9}
Save this code as "
sepAngles_config.lua" in the directory of your addon.
Note that the variables used here must be
global variables.
This is why they are prefixed with the name of the addon.
In this way there is less chance that the name is already in use and causes problems with other variables defined elsewhere.
Back to sepAnglesBox.lua
In order to use the variables defined in
sepAngles_config.lua, you must add the following declaration at the very beginning of
sepAnglesBox.luaAdd this code at the end of the initializations:
Code: Select all
-- personalization
local display_angle = sepAngles_visible or false
local textcolor = sepAngles_color or {0.7, 0.7, 1, 0.9}
local shortcut = sepAngles_shortcut or "A"
and update the existing code to take this into account
Update of sepAnglesCheck:
Code: Select all
-- determines if checked at startup
local checktext = ""
if display_angle then checktext = "x" end
sepAnglesCheck = CXBox:new()
:init(0, 0, 0, 0)
:bordercolor(cbubordoff)
:textfont(smallfont)
:textcolor(ctext)
:textpos("center")
:movetext(0, 10)
:text(checktext) -- modif here
:movable(false)
:active(true)
:attach(sepAnglesBox, boxWidth - 40, 4, 29, 5)
In the sepAnglesFrame definition, update the color of the text and the visibility at startup
you must also update the color in sepAnglesFrame.Customdraw
Code: Select all
sepAnglesFrame = CXBox:new()
:init(0, 0, 0, 0)
:textfont(normalfont)
:textcolor(textcolor)
:text("Angular separation")
:movetext(-5,4)
:movable(false)
:clickable(true)
:visible(display_angle)
:attach(screenBox, posX, sy-posT-boxH, sx-boxW-posX, posT)
sepAnglesFrame.Customdraw = function(this)
getnewselection()
textlayout:setfont(normalfont)
textlayout:setfontcolor(textcolor)
textlayout:setlinespacing(lspace)
textlayout:setpos(this.lb,this.tb-lspace*2+2) -- second line of the box
textlayout:println(angle())
sx, sy = celestia:getscreendimension()
sepAnglesFrame:attach(screenBox, posX, sy-posT-boxH, sx-boxW-posX, posT)
end
And lastly, at the end of the script,
replace withCode: Select all
keymap[shortcut] = toggleAnglesDisplay
to acknowledge the customized key to activate the tool in Lua Plug-ins.
At this time the full code of
sepAnglesBox.lua is:
Code: Select all
require "sepAngles_config"
----------------------------------------------
-- Set initial values
----------------------------------------------
local sx, sy = celestia:getscreendimension()
local lspace = normalfont:getheight() + 1
local boxW, boxH = 120,lspace * 2 -- display width = 120 pixels, display height = 2 lines
local posX, posT = 0, 170 -- near the left margin, 170 pixels from the top of the screen.
local sel1, sel2 = nil, nil -- no object selected
local renderflags = celestia:getrenderflags()
renderflags["markers"] = true
celestia:setrenderflags(renderflags)
-- personalization
local display_angle = sepAngles_visible or false
local textcolor = sepAngles_color or {0.7, 0.7, 1, 0.9}
local shortcut = sepAngles_shortcut or "A"
----------------------------------------------
-- local functions
----------------------------------------------
local getnewselection = function()
local obj = celestia:getselection()
-- test if there is a new selection
local sel = true
if obj:type() == "null" then
sel = false
elseif sel2 and sel2:name() == obj:name() then
sel = false
elseif sel1 and sel1:name() == obj:name() then
sel = false
end
-- record and mark selection
if sel then
if sel2 then
sel1:unmark(); sel2:unmark()
sel1=obj; sel2 = nil
sel1:mark()
elseif sel1 then
sel2=obj; sel2:mark()
else
sel1=obj; sel1:mark()
end
end
end
local angle = function()
if sel1 and sel2 then
local obs = celestia:getobserver()
local a= sel1:getposition() - obs:getposition()
local b= sel2:getposition() - obs:getposition()
local sepangle = math.acos(a:normalize() * b:normalize())*180/math.pi
return string.format(" %0.2f°",sepangle)
else
return "- - - - - -"
end
end
----------------------------------------------
-- Set up and Draw the boxes
----------------------------------------------
if lua_edu_tools then
sepAnglesBox = CXBox:new()
:init(0, 0, 0, 0)
:movable(false)
:textfont(normalfont)
:textcolor(ctext)
:movetext(20, 4)
:text("Sep. angles")
:attach(screenBox, sx, sy, 0, 0)
-- defines if checked at startup
local checktext = ""
if display_angle then checktext = "x" end
sepAnglesCheck = CXBox:new()
:init(0, 0, 0, 0)
:bordercolor(cbubordoff)
:textfont(smallfont)
:textcolor(ctext)
:textpos("center")
:movetext(0, 10)
:text(checktext) -- modif here
:movable(false)
:active(true)
:attach(sepAnglesBox, boxWidth - 40, 4, 29, 5)
else -- Lua Plug-ins: simple "dummy" box
sepAnglesBox = CXBox:new()
:init(0, 0, 0, 0)
:movable(false)
:attach(screenBox, sx, sy, 0, 0)
end
sepAnglesFrame = CXBox:new()
:init(0, 0, 0, 0)
:textfont(normalfont)
:textcolor(textcolor)
:text("Angular separation")
:movetext(-5,4)
:movable(false)
:clickable(true)
:visible(display_angle)
:attach(screenBox, posX, sy-posT-boxH, sx-boxW-posX, posT)
sepAnglesFrame.Customdraw = function(this)
getnewselection()
textlayout:setfont(normalfont)
textlayout:setfontcolor(textcolor)
textlayout:setlinespacing(lspace)
textlayout:setpos(this.lb,this.tb-lspace*2+2) -- second line of the box
textlayout:println(angle())
sx, sy = celestia:getscreendimension()
sepAnglesFrame:attach(screenBox, posX, sy-posT-boxH, sx-boxW-posX, posT)
end
local toggleAnglesDisplay = function()
sepAnglesFrame.Visible = not sepAnglesFrame.Visible
celestia:unmarkall()
sel1=nil
sel2=nil
end
-- activation
if lua_edu_tools then -- activate with mouse
sepAnglesCheck.Action = function(this)
toggleAnglesDisplay()
if sepAnglesFrame.Visible then
this:text("x")
else
this:text("")
end
end
else -- Lua Plug-ins: activate with keyboard
keymap[shortcut] = toggleAnglesDisplay
end
It lays in the
celestia/extras/separation_angles directory with its configuration file (
sepAngles_config.lua)
Try it