Download RV Manual - Tweak Software
Transcript
CHAPTER 7. THE CUSTOM MATTE PACKAGE
At this point we don’t need the file any more so its closed on line 11.
Line 13 creates an empty dynamic array of MatteDescription objects to be filled in.
The for_each loop iterates over the lines variable. Each time through the loop, the next element in
lines is assigned to the variable line. The line is split over commas since that’s how defined the fields of
each line.
If there are no tokens after splitting the line, that means the line is empty and we ignore it. Otherwise,
a new MatteDescription is created using the split line and appended to the end of the mattes array.
Finally, the function returns whatever MatteDesriptions it managed to parse.
But wait, we’ll need something to read our environment variable. The easiest thing to do is make a
method that gets the environment variable and calls our new parse function on the file it finds. We’ll be
calling this instead of our parseMatteFile() function. If we want to change how it looks for the file later,
we can change it without touching our parsing function:
method: findAndParseMatteFile (MenuDescription[];)
{
use system;
return parseMatteFile( getenv("RV_CUSTOM_MATTE_DEFINITIONS") );
}
The method findAndParseMatteFile() is a one liner: just call our other function with the result of
the environment variable look up. The getenv() function is in the system module so we had to use it.
We’ll need two “use” lines first thing at the top of the module definition. These lines import the system
and io modules and their namespaces. These imports are available to all functions in the module. So we
no longer need to have them inside each function. To use this module in some other code you can either use
require or use. The only difference is how you call the functions:
require customMattes;
let mattes = customMattes.findAndParseMatteFile();
// --or-use customMattes;
let mattes = findAndParseMatteFile();
Note that use actually does two things: first it loads the module if its not already loaded (just like
require does) and then it uses the module’s namespace. The use of the namespace lasts through the end
of the current scope (the next end curly brace). So if we wanted to, we could put the use line at the top of
the file and it would be in effect for everything in the file.
Finally, we’ll add some member variables to our mode to hold the MatteDescription objects we parsed.
At this point the custom_mattes.mu file looks like this:
module: custom_mattes {
use rvtypes;
use io;
use commands;
use extra_commands;
use system;
class: CustomMatteMinorMode : MinorMode
{
class: MatteDescription
{
string name;
float left;
float right;
float bottom;
float top;
string text;
}
40