Comments

Cheesehole wrote on 8/23/2008, 2:12 PM
This is what I came up with. It works like a normal event. To use it, you do this (onLoopRegionChanged is the function that gets triggered when the user makes/changes the loop region - you can of course attach as many methods as you want to the event):


LoopRegionChangedEventHandler myLoopRegionHandler = new LoopRegionChangedEventHandler(myVegas);
myLoopRegionHandler.LoopRegionChanged +=
new LoopRegionChangedEventHandler.LoopRegionChangedHandler(onLoopRegionChanged);


And here is the class:


class LoopRegionChangedEventHandler
{
public delegate void LoopRegionChangedHandler();
public event LoopRegionChangedHandler LoopRegionChanged;
Timer myTimer;
internal Vegas myVegas;
Timecode curLoopStart = new Timecode();
Timecode curLoopLength = new Timecode();
internal LoopRegionChangedEventHandler(Vegas vegas)
{
myVegas = vegas;
curLoopLength.Nanos = myVegas.Transport.LoopRegionLength.Nanos;
curLoopStart.Nanos = myVegas.Transport.LoopRegionStart.Nanos;
myTimer = new Timer();
myTimer.Interval = 200;
myTimer.Tick += new EventHandler(myTimer_Tick);
myTimer.Start();
}

void myTimer_Tick(object sender, EventArgs e)
{
onTick();
}
private void onTick()
{
if (isLoopRegionDifferent())
{
OnLoopRegionChanged();
}
}
private bool isLoopRegionDifferent()
{

Timecode newLoopStart = new Timecode();
newLoopStart.Nanos = myVegas.Transport.LoopRegionStart.Nanos;
Timecode newLoopLength = new Timecode();
newLoopLength.Nanos = myVegas.Transport.LoopRegionLength.Nanos;
if ((curLoopLength.Nanos!=newLoopLength.Nanos) || (curLoopStart.Nanos!=newLoopStart.Nanos))
{
curLoopStart.Nanos = newLoopStart.Nanos;
curLoopLength.Nanos = newLoopLength.Nanos;
return true;
}
else return false;
}
protected void OnLoopRegionChanged()
{
if (LoopRegionChanged != null)
{

LoopRegionChanged();
}
}
}


Maybe a nice addition would be to include the newLoopStart and newLoopLength in event arguments but I didn't need it so I left it out.

Hope it's useful! Let me know how to improve it...

Ben
Rosebud wrote on 9/1/2008, 2:24 PM
Hi,
I’m very interested about your code but unfortunately I can’t get it to work.
1/ there is a syntax error (onLoopRegionChanged > OnLoopRegionChanged)... not a big deal ;)))
2/After correcting the first error, I get this compilation error:
The name 'onLoopRegionChanged' does not exist in the current context

Any idea about what I’m doing wrong ?
TIA

Gilles
Cheesehole wrote on 9/3/2008, 10:54 AM
It's not a syntax error but it is definitely confusing because of all the similar names. I didn't know a way to write it that would be more clear though without the var names getting even longer.

It sounds like you are running the code without creating a method to be triggered. The method triggered is defined here:

myLoopRegionHandler.LoopRegionChanged +=
new LoopRegionChangedEventHandler.LoopRegionChangedHandler(onLoopRegionChanged);

The way the code above is written you need to make sure you have a method called onLoopRegionChanged declared somewhere.

private void onLoopRegionChanged()
{
/// code to trigger
MessageBox.Show("Loop region changed!");
}


Then you could add more methods just like any other event:
myLoopRegionHandler.LoopRegionChanged +=
new LoopRegionChangedEventHandler.LoopRegionChangedHandler(anotherActionToTakeWhenLoopRegionChanged);

I've posted a downloadable DLL of a compiled script demonstrating its use and the source project files for Visual C# 2008 here:
http://www.benorona.com/video/OnLoopRegionChange/

When you run the extension, it looks like this:


Then when you adjust the loop region:


As you adjust the start or end point of the loop region the time display will update to reflect the changes. The event handler looks like this:

private void onLoopRegionChanged()
{
/// update interface with loop region properties
labelStartTime.Text = myVegas.Transport.LoopRegionStart.ToPositionString();
labelLengthTime.Text = myVegas.Transport.LoopRegionLength.ToPositionString();
}


Questions? Post here but also email me.
Thanks!
Rosebud wrote on 9/3/2008, 1:25 PM
Many thx Ben !
All works fine now (you‘re right, I did not declared the method “onLoopRegionChanged”).

Nice piece of code !
Rosebud wrote on 9/7/2008, 3:48 AM
Ben,
This is certainly a dumb question, but how to remove this event when the custom command is closed ?
Thx
Gilles
JohnnyRoy wrote on 9/7/2008, 4:54 AM
Hi Gilles,

If you registered the event handler in your DockableControl, you can remove it using the -= operator in the OnClosed() method. Add something like this to your DockableControl:

protected override void OnClosed(EventArgs args)
{
// unregister event handlers
myLoopRegionHandler.LoopRegionChanged -= new LoopRegionChangedEventHandler.LoopRegionChangedHandler(onLoopRegionChanged);
base.OnClosed(args);
}

I usually register my event handlers during OnLoad() and unregister them in OnClose(). It makes it very easy to keep track of everything that way. I cut and aste from one to the other and change += to -= and I'm done. Hope this helps.

BTW, ScriptAddict nice piece of code. Thanks for sharing.

~jr
Rosebud wrote on 9/7/2008, 9:26 AM
Thx JohnnyRoy,
This is what I’m trying to do but with no result… :((

Here part of my code:

public void HandleInvoked(Object sender, EventArgs args)
{
if (!myVegas.ActivateDockView("TL Cursor Position change monitor"))
{
DockableControl dockView = new DockableControl("TL Cursor Position change monitor");
dockView.DefaultFloatingSize = new Size(640, 480);
dockView.DefaultDockWindowStyle = DockWindowStyle.Docked;
CursorPositionMonitorForm = new Extension_Vegas1.UserControl1(myVegas);
dockView.Controls.Add(CursorPositionMonitorForm);
dockView.Closed += new EventHandler(dockView_Closed);
myVegas.LoadDockView(dockView);
cursorPositionChangedEventHandler myCursorPositionHandler = new cursorPositionChangedEventHandler(myVegas);
myCursorPositionHandler.cursorPositionChanged += new cursorPositionChangedEventHandler.cursorPositionChangedHandler(CursorPositionMonitorForm.onCursorPositionChanged);

}
}

void dockView_Closed(Object sender, EventArgs args)
{
// HERE THE CODE I'M LOOKING FOR
}



Rosebud wrote on 9/7/2008, 11:14 PM
Ok, problem solved.

I declared the “cursorPositionChangedEventHandler myCursorPositionHandler;” in the Class. So now I have access to it in the dockView_Closed handler.


void dockView_Closed(Object sender, EventArgs args)
{
myCursorPositionHandler.cursorPositionChanged -= new cursorPositionChangedEventHandler.cursorPositionChangedHandler(CursorPositionMonitorForm.onCursorPositionChanged);
}

JohnnyRoy wrote on 9/8/2008, 4:29 AM
Glad you figured it out. I would have never seen that because I was thrown by your naming convention. I see that "cursorPositionChangedEventHandler" is a class. Generally it has been established that all class names should start with a capital letter so I immediately see lower case as a variable when I read OO code. You might want to change that to make it easier for others to read your code.

~jr
Rosebud wrote on 9/8/2008, 5:40 AM
Many thx for the advice Johnny.
I write code for fun and I’m aware that I make many error.