Lua Edu Tools - How to add your own feature [Tutorial]
Posted: 09.03.2008, 13:18
The cockpit overlay feature was not added to the Lua Edu Tools because it is not directly related to an educational purpose. However, I think that providing a short tutorial that describes how to add an overlay texture (cockpit, keychart, worksheet, ...) might be quite interesting for users who want to learn how to customize the Lua Tools.
So here's a first example that explains how to add the cockpit overlay feature. The changes should be trivial if you wish to add a keychart overlay texture instead...
1- First, we build a new 'cockpitBox.lua' file: open a new text file and save it as 'cockpitBox.lua' in the 'tools' folder.
Here's how we build the code in the cockpitBox.lua file:
- The following line defines the required file for translations:
- Then, we add a cockpitBox to the toolBox:
- Now, let's add the cockpit checkBox to the cockpitBox:
- We finally set up the cockpit Frame that holds the cockpit texture:
NB: The attach field takes 5 arguments: attach(parent, lb, bb rb, tb) where parent is the parent box and lb, bb, rb, tb are the distances in pixels respectively to the left, bottom, right, and top borders of the parent box.
2- Then, we must precise the height of the cockpitBox in the toolbox definition:
- Open the 'utils/toolBox.lua' file
- Add the following line to the boxheight definition:
3- We must also edit the config.lua file:
- Add the cockpitBox to the toolset:
- Add the following entry that defines the texture to use for the cockpit overlay:
4- Finally, we just place our cockpit texture in the 'images' folder.
Here's an example of cockpit texture:
Enjoy!
PS: I voluntarily kept this first example quite simple. However, some of you might be interested in getting further information about some specific parts of the code I've provided. If this is the case, well, just ask, and I'll be happy to provide some further explanations...
So here's a first example that explains how to add the cockpit overlay feature. The changes should be trivial if you wish to add a keychart overlay texture instead...
1- First, we build a new 'cockpitBox.lua' file: open a new text file and save it as 'cockpitBox.lua' in the 'tools' folder.
Here's how we build the code in the cockpitBox.lua file:
- The following line defines the required file for translations:
Code: Select all
require "locale";
- Then, we add a cockpitBox to the toolBox:
Code: Select all
----------------------------------------------
-- Set up and Draw the boxes
----------------------------------------------
cockpitBox = CXBox:new()
:init(0, 0, 0, 0)
:movable(false)
:attach(screenBox, width, height, 0, 0);
cockpitBox.Customdraw =
function(this)
textlayout:setfont(normalfont);
textlayout:setfontcolor(ctext);
textlayout:setpos(this.lb+25, this.tb-14);
textlayout:println(_("Cockpit"));
end;
- Now, let's add the cockpit checkBox to the cockpitBox:
Code: Select all
cockpitCheck = CXBox:new()
:init(0, 0, 0, 0)
:bordercolor(cbubordoff)
:textfont(normalfont)
:textcolor(cbutextoff)
:textpos("center")
:movetext(0, 9)
:text("")
:movable(false)
:active(true)
:attach(cockpitBox, 100, 4, 35, 5)
cockpitCheck.Action = (function()
return
function()
cockpitFrame.Visible = not(cockpitFrame.Visible);
if cockpitFrame.Visible then
cockpitCheck.Text = "x";
else
cockpitCheck.Text = "";
end
end
end) ();
- We finally set up the cockpit Frame that holds the cockpit texture:
Code: Select all
cockpitFrame = CXBox:new()
:init(0, 0, 0, 0)
:fillimage(celestia:loadtexture(cockpitTexture))
:movable(false)
:active(false)
:visible(false)
:clickable(true)
:attach(screenBox, 0, 0, 0, 0)
NB: The attach field takes 5 arguments: attach(parent, lb, bb rb, tb) where parent is the parent box and lb, bb, rb, tb are the distances in pixels respectively to the left, bottom, right, and top borders of the parent box.
2- Then, we must precise the height of the cockpitBox in the toolbox definition:
- Open the 'utils/toolBox.lua' file
- Add the following line to the boxheight definition:
Code: Select all
boxheight =
{
timeBoxH = 98,
lightBoxH = 43,
magnitudeBoxH = 43,
galaxyLightBoxH = 43,
renderBoxH = 22,
obsModeBoxH = 135,
solarSystemBoxH = 22,
fovBoxH = 43,
asteroidBeltBoxH = 20,
infoBoxH = 20,
measureBoxH = 20,
distanceBoxH = 20,
magnificationBoxH = 20,
virtualPadBoxH = 20,
cockpitBoxH = 20, -- <== Line to add
compassBoxH = 20
}
3- We must also edit the config.lua file:
- Add the cockpitBox to the toolset:
Code: Select all
toolset =
{
"timeBox",
"lightBox",
"magnitudeBox",
"galaxyLightBox",
"renderBox",
"obsModeBox",
"solarSystemBox",
"fovBox",
"asteroidBeltBox",
"infoBox",
--"measureBox",
"distanceBox",
"magnificationBox",
"virtualPadBox",
"cockpitBox", -- <== Line to add
"compassBox",
}
- Add the following entry that defines the texture to use for the cockpit overlay:
Code: Select all
-------------------------------------------------------------------------------------------------------
-- Define the texture to use for the cockpit overlay.
-------------------------------------------------------------------------------------------------------
cockpitTexture = "../images/cockpit.png"
4- Finally, we just place our cockpit texture in the 'images' folder.
Here's an example of cockpit texture:
Enjoy!
PS: I voluntarily kept this first example quite simple. However, some of you might be interested in getting further information about some specific parts of the code I've provided. If this is the case, well, just ask, and I'll be happy to provide some further explanations...