Improve the code in LUT5

All about writing scripts for Celestia in Lua and the .cel system
Avatar
Topic author
gironde M
Posts: 464
Joined: 16.12.2016
Age: 68
With us: 3 years 10 months
Location: Montigny-Les-Metz, Grand Est, Lorraine, Moselle, France

Improve the code in LUT5

Post #1by gironde » 28.06.2020, 06:41

Very often and as and when versions the code of LUT5 has been complicated for nothing.
I will put here what I find contrary to programming in LUA.

- first :
-----------------------------------------------------------------------
FNeb003Box.Action = (function()
return

function()
local obj = celestia:find("Frosty Leo Nebula")
celestia:select(obj)
local sel = celestia:getselection()
if not(empty(sel)) then
obs:follow(sel)
obs:goto(sel)
end
end
end) ()
-----------------------------------------------------------------------

the code should simply be:

Code: Select all

FNeb003Box.Action = function()
   local obj = celestia:find("Frosty Leo Nebula")
   celestia:select(obj)
   local sel = celestia:getselection()
   if not(empty(sel)) then
      obs:follow(sel)
      obs:goto(sel)
   end
end


all the LUT5 lua scripts were written this way (note that they still work)


- second :

:text(_("Frosty Leo"))
can be written like this :

Code: Select all

:text("Frosty Leo")

simpler !

:smile: :hi:

Added after 29 minutes 55 seconds:
a table in Lua is a list of data which are used by a program responsible for retrieving them for use in a part of the program.

generally they are written this way:
name_of_table = {n,n+1,n+2,n+3}
or
name_of_table =
{
n,
n+1,
n+2,
n+3
}

the comma after each element is required. (it is often a mistake that we look for a long time)
note that your language file is a Lua table.
a comma error will block the entire program.

-------------------------------------------------------------------

the semicolon (;) at the end of the instruction line is not necessary and does not replace the comma (,) in a table.

:hi: :smile:

Added after 4 minutes 34 seconds:
Caution: when you modify a code in a Lua module, always make a backup copy of your file and test your modifications.
:hi:

Added after 29 minutes 31 seconds:
This is what we find throughout the code in LUT5
name_of_box.Customdraw = function
name_of_button.Action = function
???

An Box, an frame, an button is the same for Lua. This is an object.
They all look like this :
FNeb001Box = CXBox:new()
:init(0, 0, 0, 0)
:bordercolor(cbubordoff)
:textfont(smallfont)
:textcolor({0,0.69,0.94,1})
:textpos("center")
:movetext(-3, 10)
:text("Name")
:movable(false)
:active(false)
:attach(FNebFrame, 2, 2+14*(gg-1), 190, 5+14*1)

CXBox:new() is the subroutine that creates the box.
all the lines that follow and starting with (:) are properties of the box (dimensions, color, displayed text, visibility, activity, ...)

When an box must be active, you must put your property: Active (true).
A click on the box will activate the corresponding code. Active = function (the function must have the same name as the box)

the .Customdraw functions are in fact the body of your LUT5 program and their instructions are executed constantly without intervention.

It is important to remember that the code begins to be executed as soon as a module (.lua file) is loaded.
So you have to pay careful attention to the loading order.
A module, in its Customdraw, must not refer to an object which is not yet loaded.

:hi:

Added after 1 hour 38 minutes:
to be continued :hi:

Avatar
Topic author
gironde M
Posts: 464
Joined: 16.12.2016
Age: 68
With us: 3 years 10 months
Location: Montigny-Les-Metz, Grand Est, Lorraine, Moselle, France

Post #2by gironde » 03.07.2020, 07:44

note: a box, a frame, a button, a check-box, a scrollbar are all identical objects. The difference is in our head and in the way of implementing them.


Box positioning system in Lua_applications and Lut5:

The main box is the screenBox. All the windows we open with Lut5 are contained in screenBox.
Its dimensions are provided by celestia and correspond to the dimensions of your Celestia screen.
For windows that you open with the Lut5 menu, screenBox is their container.

To position a box, we use the property inherent in each box:
: attach (name_container, X1, Y1, X2, Y2)

A box has 2 origins, (X1 Y1) located at the bottom left operating as in geometry,
and (X2 Y2) located at the top right operating at the opposite.

:attach
name_container : the box containing your box (parent box), all the following sizes are related to the container.
X1 is the abscissa of the bottom left corner of your box (the value increases to the right)
Y1 is the ordinate of this angle (the value increases upwards)
X2 is the abscissa of the top right corner of your box (the value increases to the left)
Y2 is the ordinate of this angle (the value increases downwards)

these 4 values determine a rectangle (your box)



function: name_box:bounds ()
this function returns 4 values: lb, bb, rb, tb
lb = number of pixels from the left edge of the box relative to the left edge of the container
bb = pixel number of the bottom edge of the box relative to the bottom edge of the container
rb = number of pixels from the right edge of the box relative to the left edge of the container
tb = number of pixels from the top edge of the box relative to the bottom edge of the container

lb = X1
bb = Y1
rb = lb + box_width = X1 + box_width
tb = bb + box_height = Y1 + box_height

or

X1 = lb
Y1 = bb
X2 = container_width - rb
Y2 = container_heitht - tb

with bounds () everything is expressed relative to the bottom left

Sans titre 2.jpg



to be continued ...

Added after 14 minutes 38 seconds:
other properties for a box

Code: Select all

new_name_Frame = CXBox:new()
   :init(0, 0, 0, 0)
   :fillcolor ({r,g,b,alpha})
      -- or :fillimage ("file of image of back")
   :bordercolor({r,g,b,alpha})
   :textfont(smallfont)
   :textcolor({r,g,b,alpha})
   :textpos("center")
   :movetext(x,y)
   :text("")
   :clickable (false)
   :movable(true)
   :visible(false)
   :active(false)
   :attach(parent,x1,y1,x2,y2)


init : initialize the new box
fillcolor ({r,g,b,alpha}): fill color of the box, if absent, the box is completely transparent
(this does not work my graphics card)
fillimage (""): by loading a png image, we can replace fillcolor (you must master the path)
bordercolor ({r,g,b,alpha}): color of border
textfont() : smallfont or normalfont
textcolor({r,g,b,alpha}) : color of text
textpos("center") : 'center' is the basic position. In some modules, we find 'left' but I did not find this option in CXBox.
movetext(x,y) : move text relative to textpos (in pixel)
text("") : string of characters
clickable (false) : we can click on the box, or not
movable(true) : the box can be moved with the mouse, or not
visible(false) : the box is visible or not
active(true) : the box is an active box or not. Associated with an .Action () script, the box becomes a button for example or a check-box or ...
attach : placement details in the parent box

the properties can be changed by code
example
name_box.Text = "what_I_want" (note that the property is written here with a capital letter)
name_box.Visible = true
name_box.Movable = false
...

to be continued

onetwothree
Site Admin
Posts: 510
Joined: 22.09.2018
With us: 2 years 1 month

Post #3by onetwothree » 03.07.2020, 09:04

the biggest problem with LUT is that it uses hardcoded values for dimensions. this works more or less good with default txf fonts, but breaks everything with scalable ttf fonts.

Avatar
Topic author
gironde M
Posts: 464
Joined: 16.12.2016
Age: 68
With us: 3 years 10 months
Location: Montigny-Les-Metz, Grand Est, Lorraine, Moselle, France

Post #4by gironde » 07.07.2020, 05:23

nothing prevents to continue using TXF fonts in LUT5 even if Celestia 1.7 will use TTF fonts.
As a reminder, the fonts are loaded into lut5 by the module 'textlayout.lua'

onetwothree
Site Admin
Posts: 510
Joined: 22.09.2018
With us: 2 years 1 month

Post #5by onetwothree » 07.07.2020, 06:38

first it never was possible to use both ttf and txf. second, txf fonts are already disabled, and will be removed later.

Avatar
Topic author
gironde M
Posts: 464
Joined: 16.12.2016
Age: 68
With us: 3 years 10 months
Location: Montigny-Les-Metz, Grand Est, Lorraine, Moselle, France

Post #6by gironde » 09.07.2020, 09:18

So here you are announcing the end of lua_applications and lua_universal_tools from future celestia 1.7 versions
I am not sure that this decision is unanimous.


Return to “Scripting”

Who is online