Dialog boxes

Tech Diver wrote on 5/9/2006, 9:46 AM
Is there a way of creating dialog boxes for inputting information using Vegas scripting without going to a compiled language? I am aware of the file open/save functions, but I need something other than that. I need to gather parameters for various scripts that I have written to make them more flexible. If dialog boxes can be created, it would be great if someone could please point me to an example and/or documentation.

Comments

johnmeyer wrote on 5/9/2006, 10:15 AM
Yup. Done all the time. Here's one I just picked at random:
/** 
* PURPOSE OF THIS SCRIPT:
*
* This script creates a region for every event on the selected track.
* As it creates each region, it lets you name that region using a dialog box.
* Each event is optionally previewed (a sample of ten frames across
* the entire event) for five seconds prior to naming.
* You can re-preview the event as often as you like.
*
* Because of the limitations of enumeration, there is no easy way to
* go back to a previous event. However, if you cancel the script,
* it will skip over any existing regions that you already created so
* you can continue where you left off.
*
* Region naming is done with ListBoxes that contain fixed text strings.
* You can also enter additional text using the text box.
* A region number is appended to each region name to ensure unique names.
*
* The "Enter" key "click" the next button no matter where you are in the
* dialog box.
*
* Written By: John Meyer
* November 6, 2003
* Tested on Windows XP Pro, SP1, with Vegas 4.0d
*
**/

import System;
import System.IO;
import System.Collections;
import System.Text;
import System.Windows.Forms;
import Sony.Vegas;
import System.Threading;

var evnt : TrackEvent;
var myRegion : Region;
var i : Double;
var dLength : Double;
var dStart : Double;

var RegionName : String;
var RegionNumber;
var PreviewVideo;


try {

RegionNumber = 1;
PreviewVideo = 0;

//Find the selected event
var track = FindSelectedTrack();
if (null == track)
throw "no selected track";

var eventEnum = new Enumerator(track.Events);

// Go through each event on the selected track.
while (!eventEnum.atEnd()) {

evnt = TrackEvent(eventEnum.item());
dLength = evnt.Length.ToMilliseconds();
dStart = evnt.Start.ToMilliseconds();

if (!RegionExist (dStart,dLength) ) { // Only proceed if no region

// Preview the video, but only if preview is enabled in the dialog check box.
if (PreviewVideo) {

for (i = 0; i < 20; i++) {
Vegas.Cursor = evnt.Start + new Timecode(0.05 * i * dLength);
Thread.Sleep(250); // Change this to change playback speed
Vegas.UpdateUI();
}
}

// Get region information through the dialog box defined below.
var dialog = new MainDialog("", 0, RegionNumber, PreviewVideo);
var dialogResult = dialog.ShowDialog();

// Proceed if the "Next" button was pressed.
if (System.Windows.Forms.DialogResult.OK == dialogResult) {

// Get the region name from the dialog box values.
RegionName = dialog.regionBox1.Text+", "+dialog.regionBox2.Text;

// Create the region name from the Listbox, textbox, and region number
if (dialog.RegionNameBox.Text != " ") {
RegionName = RegionName + " " + dialog.RegionNameBox.Text + " " + RegionNumber.ToString();
}

else {
RegionName = " " + RegionName+RegionNumber.ToString();
}

// Insert a region "over" this event.
myRegion = new Region(evnt.Start,evnt.Length,RegionName);
Vegas.Project.Regions.Add(myRegion);

// Move to the next event, and increment the region number
eventEnum.moveNext();
RegionNumber++;
}

else {

if (System.Windows.Forms.DialogResult.Cancel == dialogResult) {
evnt.Selected = true;
Vegas.Cursor = evnt.Start;
Vegas.UpdateUI();
throw "Operation cancelled";
}

}

PreviewVideo = dialog.PreviewBox.Checked ? 1 : 0;
}
else { // Else if !RegionExist
eventEnum.moveNext(); // If region already existed, simply go to the next event
evnt.Selected = false;
RegionNumber++;
}

}

}

catch (e) {
// MessageBox.Show(e);
}
// End Main Program

//
// ---------------------------------
// Begin functions


function FindSelectedTrack() : Track {
var trackEnum = new Enumerator(Vegas.Project.Tracks);
while (!trackEnum.atEnd()) {
var track : Track = Track(trackEnum.item());
if (track.Selected) {
return track;
}
trackEnum.moveNext();
}
return null;
}


//-------------------------------------------------------
// Checks if a region already exists for this event

function RegionExist (dStart,dLength) : boolean {

var markerEnum = new Enumerator(Vegas.Project.Regions);

while (!markerEnum.atEnd()) {
var Region = markerEnum.item();
var RegionLength = Region.Length.ToMilliseconds();
var RegionStart = Region.Position.ToMilliseconds();

if ( (dLength == RegionLength) && (dStart == RegionStart) ) {
return 1;
}
markerEnum.moveNext();
}
return 0;
}


//-------------------------------------------------------
// Form subclass that is the dialog box for this script
class MainDialog extends Form {
var RegionNameBox;
var regionBox1;
var regionBox2;
var PreviewBox;

function MainDialog(RegionName, region, RegionNumber, PreviewVideo) {

this.Text = "Create Regions";
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.StartPosition = FormStartPosition.CenterScreen;
this.Width = 500;

var buttonWidth = 80;

var label = new Label();
label.AutoSize = true;
label.Text = "Region Name 1:";
label.Left = 10;
label.Top = 15;
Controls.Add(label);
regionBox1 = new ListBox(); // Change to ComboBox() to permit random text entry
regionBox1.Left = label.Right;
regionBox1.Top = label.Top-5; // Note how positions are relative to other objects
regionBox1.Width = 200;
regionBox1.Height = 210;
regionBox1.DropDownWidth = 200;
regionBox1.Items.Add(""); // *********************************** //
regionBox1.Items.Add("Camille"); // //
regionBox1.Items.Add("Elise"); // //
regionBox1.Items.Add("Group"); // Change the names to the ones //
regionBox1.Items.Add("Jackie"); // you want. You can also simply //
regionBox1.Items.Add("Jamie1"); // add or delete the lines here //
regionBox1.Items.Add("Jamie2"); // to get the number of entries //
regionBox1.Items.Add("Katelyn"); // you want. //
regionBox1.Items.Add("Larissa"); // //
regionBox1.Items.Add("Loren"); // //
regionBox1.Items.Add("Michelle"); // //
regionBox1.Items.Add("Quinnie"); // //
regionBox1.Items.Add("Sierra"); // //
regionBox1.SelectedIndex = region; // *********************************** //
Controls.Add(regionBox1);

var label = new Label();
label.AutoSize = true;
label.Text = "Region Name 2:";
label.Left = 10;
label.Top = regionBox1.Bottom + 20;
Controls.Add(label);
regionBox2 = new ListBox(); // Change to ComboBox() to permit random text entry
regionBox2.Width = 200;
regionBox2.Height = 160;
regionBox2.DropDownWidth = 200;
regionBox2.Left = label.Right;
regionBox2.Top = label.Top-5;
regionBox2.Items.Add("");
regionBox2.Items.Add("Block"); // Add or delete these lines (same as above)
regionBox2.Items.Add("Close-up");
regionBox2.Items.Add("Dig");
regionBox2.Items.Add("Fun");
regionBox2.Items.Add("Hit");
regionBox2.Items.Add("Run");
regionBox2.Items.Add("Serve");
regionBox2.Items.Add("Set");
regionBox2.Items.Add("Unusual");
regionBox2.SelectedIndex = region;
Controls.Add(regionBox2);

RegionNameBox = addTextControl("Region Suffix: ", 10, 300, regionBox2.Bottom + 16, "");

var label = new Label();
label.AutoSize = true;
label.Text = "Region Number: " + RegionNumber.ToString();
label.Left = 10;
label.Top = RegionNameBox.Bottom + 20;
Controls.Add(label);

PreviewBox = new CheckBox();
PreviewBox.Left = 10;
PreviewBox.Width = 160;
PreviewBox.Top = label.Bottom + 15;
PreviewBox.Text = "Preview Video";
PreviewBox.Checked = PreviewVideo != 0;
Controls.Add(PreviewBox);

var buttonTop = PreviewBox.Top;

var cancelButton = new Button();
cancelButton.Text = "Cancel";
cancelButton.Left = this.Width - (1*(buttonWidth+10));
cancelButton.Top = regionBox1.Top;
cancelButton.Height = cancelButton.Font.Height + 12;
cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
CancelButton = cancelButton;
Controls.Add(cancelButton);

var NextButton = new Button();
NextButton.Text = "Next";
NextButton.Left = this.Width - (1*(buttonWidth+10));
NextButton.Top = buttonTop;
NextButton.Width = buttonWidth;
NextButton.Height = NextButton.Font.Height + 12;
NextButton.DialogResult = System.Windows.Forms.DialogResult.OK;
AcceptButton = NextButton; // The Enter key will automatically activate this button.
Controls.Add(NextButton);

var ReplayButton = new Button();
ReplayButton.Text = "Replay";
ReplayButton.Left = this.Width - (2*(buttonWidth+10));
ReplayButton.Top = buttonTop;
ReplayButton.Width = buttonWidth;
ReplayButton.Height = ReplayButton.Font.Height + 12;
ReplayButton.DialogResult = System.Windows.Forms.DialogResult.Retry;
Controls.Add(ReplayButton);


var titleHeight = this.Height - this.ClientSize.Height;
this.Height = titleHeight + NextButton.Bottom + 20;

RegionNameBox.Focus;

} // End function MainDialog
//
// -------------------------------------
function addTextControl(labelName, left, width, top, defaultValue) {
var label = new Label();
label.AutoSize = true;
label.Text = labelName + ":";
label.Left = left;
label.Top = top + 4;
Controls.Add(label);

var textbox = new TextBox();
textbox.Multiline = false;
textbox.Left = label.Right;
textbox.Top = top;
textbox.Width = width - (label.Width);
textbox.Text = defaultValue;
Controls.Add(textbox);

return textbox;
}

} // End class MainDialog

Tech Diver wrote on 5/9/2006, 11:04 AM
Thanks John, your example is very helpful. This is clearly not part of the Vegas script documentation. Do I assume correctly that this part the fundamental Javascript language? Do you have a pointer to some good documentation on these commands (I looked around but could not find anything).
jetdv wrote on 5/9/2006, 12:32 PM
All the dialog parts are not in the Vegas API. Instead, they are part of .NET and JScript. Here's a link for you:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/jscript7/html/jsoriJScript.asp

It can be done manually. Excalibur 1, 2, and 3, Tsunami, and Neon were all done this way. However, it is MUCH easier using a GUI to design the screens which is how Excalibur 4 and 5 were done.

So, yes, it is certainly possible. It just takes a lot of work and even more tweaking to get everything positioned correctly.
johnmeyer wrote on 5/9/2006, 1:26 PM
Here's a link to the page at Microsoft that I go to. This may lead you to the same place Ed linked to:

Microsoft jscript support
Tech Diver wrote on 5/9/2006, 3:31 PM
Jetdv and John, thanks for the links.

Jetdv, what GUI-based tool do you know of that I could use to build JScript dialog boxes? I really don't want to get an entire development platform like VB or Visual C++ (although I used to be a C++ programmer) just for these small tasks.
jetdv wrote on 5/10/2006, 6:43 AM
I use Visual Studio 2003 and C#. I don't know of one that will build JScript dialogs (that's not to say there isn't one out there).
jetdv wrote on 5/10/2006, 10:04 AM
Tech, I've just released a new newsletter (Vol 4 #3) which has an article on scripting in C# and using VS2003 to design a screen and compile to a DLL. The source can also be downloaded.
JohnnyRoy wrote on 5/10/2006, 6:50 PM
> (that's not to say there isn't one out there).

Trust me on this one... there isn’t one out there. I looked, and looked, and looked, and found nothing. JavaScript is primarily a web browser language so there is practically no market for a Windows dialog tool.

Do yourself a BIG favor and do what Edward (JetDV) has suggested. Learn C# and use Visual Studio 2003. You don’t have to compile your scripts anymore. Vegas 6 and Sound Forge 8 will process .CS files (C# source) just like .JS files. They will load faster if you compile them but it’s no longer required.

~jr
Tech Diver wrote on 5/11/2006, 6:31 AM
I found that scripting the dialog boxes manually is not as big a deal as I originally thought (having been a C++ developer certainly helped). I might eventually pick up a copy of Visual Studion 2005 since I am entitled to accademic pricing and can get the non-pro version for less than $100.

By the way, are there any JScript object classes for a slider? I want to enable variable input without forcing the user enter a value. In the worst case, I'll use a combo-box with a range of entries, which still allows the user to enter a specific value that I can validate.